source: trunk/src/dict.cpp @ 221

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

Complete Lol Engine / Deus Hax / Monsterz split.

  • Property svn:keywords set to Id
File size: 2.2 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 (nentities)
44            fprintf(stderr, "ERROR: still %i entities in dict\n", nentities);
45        free(entities);
46    }
47
48private:
49    Entity **entities;
50    int nentities;
51};
52
53/*
54 * Public Dict class
55 */
56
57Dict::Dict()
58{
59    data = new DictData();
60}
61
62Dict::~Dict()
63{
64    delete data;
65}
66
67int Dict::MakeSlot(char const *name)
68{
69    int id, empty = -1;
70
71    /* If the entry is already registered, remember its ID. Look for an
72     * empty slot at the same time. */
73    for (id = 0; id < data->nentities; id++)
74    {
75        Entity *e = data->entities[id];
76        if (!e)
77            empty = id;
78        else if (!strcasecmp(name, e->GetName()))
79            break;
80    }
81
82    /* If this is a new entry, create a new slot for it. */
83    if (id == data->nentities)
84    {
85        if (empty == -1)
86        {
87            empty = data->nentities++;
88            data->entities = (Entity **)realloc(data->entities,
89                                           data->nentities * sizeof(Entity *));
90        }
91
92        data->entities[empty] = NULL;
93        id = empty;
94    }
95    else
96    {
97        Ticker::Ref(data->entities[id]);
98    }
99
100    return id;
101}
102
103void Dict::RemoveSlot(int id)
104{
105    if (Ticker::Unref(data->entities[id]) == 0)
106        data->entities[id] = NULL;
107    data->nentities--;
108}
109
110
111void Dict::SetEntity(int id, Entity *entity)
112{
113    Ticker::Ref(entity);
114    data->entities[id] = entity;
115}
116
117Entity *Dict::GetEntity(int id)
118{
119    return data->entities[id];
120}
121
Note: See TracBrowser for help on using the repository browser.