Browse Source

core: disable our rare uses of realloc() with a proper Array<> object.

legacy
Sam Hocevar sam 13 years ago
parent
commit
96342ddb0c
2 changed files with 22 additions and 33 deletions
  1. +16
    -20
      src/dict.cpp
  2. +6
    -13
      src/map.cpp

+ 16
- 20
src/dict.cpp View File

@@ -36,8 +36,6 @@ class DictData


public: public:
DictData() : DictData() :
entities(0),
maxid(0),
nentities(0) nentities(0)
{ {
/* Nothing to do */ /* Nothing to do */
@@ -49,12 +47,11 @@ public:
if (nentities) if (nentities)
Log::Error("still %i entities in dict\n", nentities); Log::Error("still %i entities in dict\n", nentities);
#endif #endif
free(entities);
} }


private: private:
Entity **entities;
int maxid, nentities;
Array<Entity *> m_entities;
int nentities;
}; };


/* /*
@@ -77,9 +74,9 @@ int Dict::MakeSlot(char const *name)


/* If the entry is already registered, remember its ID. Look for an /* If the entry is already registered, remember its ID. Look for an
* empty slot at the same time. */ * empty slot at the same time. */
for (slotid = 0; slotid < data->maxid; slotid++)
for (slotid = 0; slotid < data->m_entities.Count(); slotid++)
{ {
Entity *e = data->entities[slotid];
Entity *e = data->m_entities[slotid];
if (!e) if (!e)
{ {
empty = slotid; empty = slotid;
@@ -104,22 +101,21 @@ int Dict::MakeSlot(char const *name)
} }


/* If this is a new entry, create a new slot for it. */ /* If this is a new entry, create a new slot for it. */
if (slotid == data->maxid || !data->entities[slotid])
if (slotid == data->m_entities.Count() || !data->m_entities[slotid])
{ {
if (slotid == data->maxid)
if (slotid == data->m_entities.Count())
{ {
empty = data->maxid++;
data->entities = (Entity **)realloc(data->entities,
data->maxid * sizeof(Entity *));
empty = data->m_entities.Count();
data->m_entities.Push(NULL);
} }


data->entities[empty] = NULL;
data->m_entities[empty] = NULL;
slotid = empty; slotid = empty;
data->nentities++; data->nentities++;
} }
else else
{ {
Ticker::Ref(data->entities[slotid]);
Ticker::Ref(data->m_entities[slotid]);
} }


return slotid; return slotid;
@@ -127,17 +123,17 @@ int Dict::MakeSlot(char const *name)


void Dict::RemoveSlot(int slotid) void Dict::RemoveSlot(int slotid)
{ {
if (Ticker::Unref(data->entities[slotid]) == 0)
if (Ticker::Unref(data->m_entities[slotid]) == 0)
{ {
data->entities[slotid] = NULL;
data->m_entities[slotid] = NULL;
data->nentities--; data->nentities--;
} }
} }


void Dict::RemoveSlot(Entity *entity) void Dict::RemoveSlot(Entity *entity)
{ {
for (int slotid = 0; slotid < data->maxid; slotid++)
if (data->entities[slotid] == entity)
for (int slotid = 0; slotid < data->m_entities.Count(); slotid++)
if (data->m_entities[slotid] == entity)
{ {
RemoveSlot(slotid); RemoveSlot(slotid);
return; return;
@@ -152,12 +148,12 @@ void Dict::RemoveSlot(Entity *entity)
void Dict::SetEntity(int slotid, Entity *entity) void Dict::SetEntity(int slotid, Entity *entity)
{ {
Ticker::Ref(entity); Ticker::Ref(entity);
data->entities[slotid] = entity;
data->m_entities[slotid] = entity;
} }


Entity *Dict::GetEntity(int slotid) Entity *Dict::GetEntity(int slotid)
{ {
return data->entities[slotid];
return data->m_entities[slotid];
} }


} /* namespace lol */ } /* namespace lol */


+ 6
- 13
src/map.cpp View File

@@ -38,8 +38,7 @@ private:
TileSet *tilesets[MAX_TILESETS]; TileSet *tilesets[MAX_TILESETS];
int ntilers; int ntilers;


Layer **layers;
int nlayers;
Array<Layer *> m_layers;


int width, height; int width, height;
}; };
@@ -52,8 +51,6 @@ Map::Map(char const *path)
: data(new MapData()) : data(new MapData())
{ {
data->ntilers = 0; data->ntilers = 0;
data->layers = NULL;
data->nlayers = 0;
data->width = 0; data->width = 0;
data->height = 0; data->height = 0;


@@ -120,8 +117,7 @@ Map::Map(char const *path)
{ {
Layer *l = new Layer(data->width, data->height, Layer *l = new Layer(data->width, data->height,
level, orientation, tiles); level, orientation, tiles);
data->layers[data->nlayers] = l;
data->nlayers++;
data->m_layers.Push(l);
tiles = NULL; tiles = NULL;
//Log::Debug("new layer %ix%i\n", data->width, data->height); //Log::Debug("new layer %ix%i\n", data->width, data->height);
} }
@@ -143,8 +139,6 @@ Map::Map(char const *path)
"width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5) "width=\"%i\" height=\"%i\"", &a, &i, &b, &j, &k) == 5)
{ {
/* This is a layer description. Prepare to read the data. */ /* This is a layer description. Prepare to read the data. */
data->layers = (Layer **)realloc(data->layers,
sizeof(Layer **) * (data->nlayers + 1));
orientation = toupper(a) == 'V' ? 1 : 0; orientation = toupper(a) == 'V' ? 1 : 0;
level = i * 32; level = i * 32;
data->width = j; data->width = j;
@@ -161,16 +155,15 @@ Map::~Map()
{ {
for (int i = 0; i < data->ntilers; i++) for (int i = 0; i < data->ntilers; i++)
Tiler::Deregister(data->tilesets[i]); Tiler::Deregister(data->tilesets[i]);
for (int i = 0; i < data->nlayers; i++)
delete data->layers[i];
free(data->layers);
for (int i = 0; i < data->m_layers.Count(); i++)
delete data->m_layers[i];
delete data; delete data;
} }


void Map::Render(int x, int y, int z) void Map::Render(int x, int y, int z)
{ {
for (int i = 0; i < data->nlayers; i++)
data->layers[i]->Render(x, y, z);
for (int i = 0; i < data->m_layers.Count(); i++)
data->m_layers[i]->Render(x, y, z);
} }


int Map::GetWidth() int Map::GetWidth()


Loading…
Cancel
Save