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