source: trunk/src/dict.cpp @ 419

Last change on this file since 419 was 419, checked in by sam, 9 years ago

Name tilers, samples and pieces for easier debugging.

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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 <cstdio>
17#include <cstdlib>
18
19#include "core.h"
20
21#if defined WIN32
22#   define strcasecmp _stricmp
23#endif
24
25/*
26 * Dict implementation class
27 */
28
29class DictData
30{
31    friend class Dict;
32
33public:
34    DictData() :
35        entities(0),
36        nentities(0)
37    {
38        /* Nothing to do */
39    }
40
41    ~DictData()
42    {
43#if !FINAL_RELEASE
44        if (nentities)
45            fprintf(stderr, "ERROR: still %i entities in dict\n", nentities);
46#endif
47        free(entities);
48    }
49
50private:
51    Entity **entities;
52    int nentities;
53};
54
55/*
56 * Public Dict class
57 */
58
59Dict::Dict()
60  : data(new DictData())
61{
62}
63
64Dict::~Dict()
65{
66    delete data;
67}
68
69int Dict::MakeSlot(char const *name)
70{
71    int id, empty = -1;
72
73    /* If the entry is already registered, remember its ID. Look for an
74     * empty slot at the same time. */
75    for (id = 0; id < data->nentities; id++)
76    {
77        Entity *e = data->entities[id];
78        if (!e)
79            empty = id;
80        else
81        {
82            char const *oldname = e->GetName();
83            if (*oldname == '<')
84            {
85                while (*oldname && *oldname != '>')
86                    oldname++;
87                while (*oldname == '>')
88                    oldname++;
89                while (*oldname == ' ')
90                    oldname++;
91            }
92
93            if (!strcasecmp(name, oldname))
94                break;
95        }
96    }
97
98    /* If this is a new entry, create a new slot for it. */
99    if (id == data->nentities)
100    {
101        if (empty == -1)
102        {
103            empty = data->nentities++;
104            data->entities = (Entity **)realloc(data->entities,
105                                           data->nentities * sizeof(Entity *));
106        }
107
108        data->entities[empty] = NULL;
109        id = empty;
110    }
111    else
112    {
113        Ticker::Ref(data->entities[id]);
114    }
115
116    return id;
117}
118
119void Dict::RemoveSlot(int id)
120{
121    if (Ticker::Unref(data->entities[id]) == 0)
122    {
123        data->entities[id] = NULL;
124        if (data->nentities)
125            data->nentities--;
126#if !FINAL_RELEASE
127        else
128            fprintf(stderr, "ERROR: removing entity from empty dict\n");
129#endif
130    }
131}
132
133
134void Dict::SetEntity(int id, Entity *entity)
135{
136    Ticker::Ref(entity);
137    data->entities[id] = entity;
138}
139
140Entity *Dict::GetEntity(int id)
141{
142    return data->entities[id];
143}
144
Note: See TracBrowser for help on using the repository browser.