source: trunk/src/tiler.cpp @ 75

Last change on this file since 75 was 75, checked in by sam, 10 years ago

Much better resource allocation and release. Scene manager stub.

  • Property svn:keywords set to Id
File size: 1.5 KB
Line 
1
2#include <cstring>
3#include <cstdio>
4#include <stdlib.h>
5
6#include "tiler.h"
7#include "tileset.h"
8
9/*
10 * Tiler implementation class
11 */
12
13static class TilerData
14{
15    friend class Tiler;
16
17public:
18    TilerData() :
19        tilesets(0),
20        ntilesets(0)
21    {
22        /* Nothing to do */
23    }
24
25    ~TilerData()
26    {
27        free(tilesets);
28    }
29
30private:
31    TileSet **tilesets;
32    int ntilesets;
33}
34tilerdata;
35
36static TilerData * const data = &tilerdata;
37
38/*
39 * Public Tiler class
40 */
41
42int Tiler::Register(char const *path)
43{
44    int id, empty = -1;
45
46    /* If the tileset is already registered, remember its ID. Look for an
47     * empty slot at the same time. */
48    for (id = 0; id < data->ntilesets; id++)
49    {
50        TileSet *t = data->tilesets[id];
51        if (!t)
52            empty = id;
53        else if (!strcasecmp(path, t->GetName()))
54            break;
55    }
56
57    /* If this is a new tileset, create a new one. */
58    if (id == data->ntilesets)
59    {
60        if (empty == -1)
61        {
62            empty = data->ntilesets++;
63            data->tilesets = (TileSet **)realloc(data->tilesets,
64                                         data->ntilesets * sizeof(TileSet *));
65        }
66
67        data->tilesets[empty] = new TileSet(path);
68        id = empty;
69    }
70
71    data->tilesets[id]->Ref();
72    return id + 1; /* ID 0 is for the empty tileset */
73}
74
75void Tiler::Deregister(int id)
76{
77    --id; /* ID 0 is for the empty tileset */
78
79    if (data->tilesets[id]->Unref() == 0)
80    {
81        delete data->tilesets[id];
82        data->tilesets[id] = NULL;
83    }
84}
85
Note: See TracBrowser for help on using the repository browser.