source: trunk/src/dict.cpp @ 1942

Last change on this file since 1942 was 1942, checked in by sam, 7 years ago

core: do not deregister Tilers from Sprites, it’s asymmetrical.

  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstring>
16#include <cstdlib>
17
18#include "core.h"
19
20#if defined _WIN32 || defined _XBOX
21#   define strcasecmp _stricmp
22#endif
23
24using namespace std;
25
26namespace lol
27{
28
29/*
30 * Dict implementation class
31 */
32
33class DictData
34{
35    friend class Dict;
36
37public:
38    DictData() :
39        entities(0),
40        maxid(0),
41        nentities(0)
42    {
43        /* Nothing to do */
44    }
45
46    ~DictData()
47    {
48#if !LOL_RELEASE
49        if (nentities)
50            Log::Error("still %i entities in dict\n", nentities);
51#endif
52        free(entities);
53    }
54
55private:
56    Entity **entities;
57    int maxid, nentities;
58};
59
60/*
61 * Public Dict class
62 */
63
64Dict::Dict()
65  : data(new DictData())
66{
67}
68
69Dict::~Dict()
70{
71    delete data;
72}
73
74int Dict::MakeSlot(char const *name)
75{
76    int slotid, empty = -1;
77
78    /* If the entry is already registered, remember its ID. Look for an
79     * empty slot at the same time. */
80    for (slotid = 0; slotid < data->maxid; slotid++)
81    {
82        Entity *e = data->entities[slotid];
83        if (!e)
84        {
85            empty = slotid;
86            break;
87        }
88        else
89        {
90            char const *oldname = e->GetName();
91            if (*oldname == '<')
92            {
93                while (*oldname && *oldname != '>')
94                    oldname++;
95                while (*oldname == '>')
96                    oldname++;
97                while (*oldname == ' ')
98                    oldname++;
99            }
100
101            if (!strcasecmp(name, oldname))
102                break;
103        }
104    }
105
106    /* If this is a new entry, create a new slot for it. */
107    if (slotid == data->maxid || !data->entities[slotid])
108    {
109        if (slotid == data->maxid)
110        {
111            empty = data->maxid++;
112            data->entities = (Entity **)realloc(data->entities,
113                                                data->maxid * sizeof(Entity *));
114        }
115
116        data->entities[empty] = NULL;
117        slotid = empty;
118        data->nentities++;
119    }
120    else
121    {
122        Ticker::Ref(data->entities[slotid]);
123    }
124
125    return slotid;
126}
127
128void Dict::RemoveSlot(int slotid)
129{
130    if (Ticker::Unref(data->entities[slotid]) == 0)
131    {
132        data->entities[slotid] = NULL;
133        data->nentities--;
134    }
135}
136
137void Dict::RemoveSlot(Entity *entity)
138{
139    for (int slotid = 0; slotid < data->maxid; slotid++)
140        if (data->entities[slotid] == entity)
141        {
142            RemoveSlot(slotid);
143            return;
144        }
145
146#if !LOL_RELEASE
147    Log::Error("removing unregistered entity %p (%s)\n",
148               entity, entity->GetName());
149#endif
150}
151
152void Dict::SetEntity(int slotid, Entity *entity)
153{
154    Ticker::Ref(entity);
155    data->entities[slotid] = entity;
156}
157
158Entity *Dict::GetEntity(int slotid)
159{
160    return data->entities[slotid];
161}
162
163} /* namespace lol */
164
Note: See TracBrowser for help on using the repository browser.