1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/*
* Copyright (C) 2013 Patrick "P. J." McDermott
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <expat.h>
#include <config.h>
#include "tileset.h"
#include "resource.h"
#include "tmx.h"
#include "image.h"
#include "../xml.h"
#include "../logging.h"
struct resource_table ts_res;
static void XMLCALL tmx_tileset_start(void *, const char *, const char **);
static void XMLCALL tmx_tileset_end(void *, const char *);
static void XMLCALL tmx_image_end(void *, const char *);
static void XMLCALL tmx_tile_end(void *, const char *);
static void XMLCALL tmx_tile_properties_start(void *, const char *,
const char **);
static void XMLCALL tmx_tile_property_start(void *, const char *,
const char **);
struct tileset *
tileset_get(const char *path, const char *basedir)
{
struct tileset *ts;
XML_Parser p;
FILE *tmx_fp;
void *tmx_buf;
size_t len;
enum XML_Status status;
ts = (struct tileset *) resource_get(&ts_res, path);
if (ts != NULL) {
resource_use((struct resource *) ts);
return ts;
}
ts = resource_alloc(path, sizeof(*ts));
if (ts == NULL) {
return NULL;
}
ts->dirname = strdup(basedir);
p = XML_ParserCreate(NULL);
if (p == NULL) {
warn("Failed to create TSX parser");
return NULL;
}
XML_UseParserAsHandlerArg(p);
xml_node_push(p, ts, tmx_tileset_start, tmx_invalid_end, NULL);
tmx_fp = fopen(path, "rb");
if (tmx_fp == NULL) {
warn("Failed to open TSX file");
xml_node_pop(p);
XML_ParserFree(p);
return NULL;
}
tmx_buf = XML_GetBuffer(p, 8192);
if (tmx_buf == NULL) {
warn("Failed to create TSX parse buffer");
xml_node_pop(p);
XML_ParserFree(p);
fclose(tmx_fp);
return NULL;
}
while (!feof(tmx_fp)) {
len = fread(tmx_buf, 1, 8192, tmx_fp);
status = XML_ParseBuffer(p, len, feof(tmx_fp));
if (status == XML_STATUS_OK) {
continue;
}
warn("Failed to parse TSX file (%s)",
XML_ErrorString(XML_GetErrorCode(p)));
xml_node_pop(p);
XML_ParserFree(p);
fclose(tmx_fp);
return NULL;
}
XML_ParserFree(p);
fclose(tmx_fp);
resource_use((struct resource *) ts);
resource_add(&ts_res, path, (struct resource *) ts);
return ts;
}
void XMLCALL
tmx_tileset_el_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;
struct tileset *ts;
char *dirname;
char *source;
char *path;
struct image *img;
#ifdef DEBUG_TMX
debug("<%s> (tileset child)", name);
#endif
ts = xml_node_peek(p);
if (xml_check_tag(name, "tileoffset")) {
/* Not used by engine. */
xml_node_push(p, NULL, tmx_unused_start, tmx_unused_end, NULL);
} else if (xml_check_tag(name, "properties")) {
/* Not used by engine. */
xml_node_push(p, NULL, tmx_unused_start, tmx_unused_end, NULL);
} else if (xml_check_tag(name, "image")) {
xml_get_int_attr(p, attr, "width", &ts->width, 1);
xml_get_int_attr(p, attr, "height", &ts->height, 1);
ts->width /= ts->tilewidth;
ts->height /= ts->tileheight;
img = NULL;
if (ts->type == TILESET_TYPE_IMAGE) {
dirname = ts->dirname;
xml_get_string_attr(p, attr, "source", &source, 0);
path = malloc(strlen(dirname) + strlen(source) + 2);
if (path == NULL) {
return;
}
sprintf(path, "%s/%s", dirname, source);
img = img_png_get(path);
/* TODO: Get the color key from the trans attribute. */
SDL_SetColorKey(img->image, SDL_SRCCOLORKEY,
SDL_MapRGB(img->image->format,
0xFC, 0x00, 0xFF));
free(source);
} else if (ts->type == TILESET_TYPE_COLLISION) {
ts->collision_tiles = malloc(ts->width * ts->height *
sizeof(*ts->collision_tiles));
if (ts->collision_tiles == NULL) {
return;
}
}
xml_node_push(p, img, tmx_invalid_start, tmx_image_end, NULL);
} else if (xml_check_tag(name, "tile")) {
xml_get_int_attr(p, attr, "id", &ts->cur_collision_tile, 1);
xml_node_push(p, ts, tmx_tile_properties_start, tmx_tile_end,
NULL);
} else {
xml_unexpected_start_tag(p, name, "tileoffset, image, tile");
}
}
static void XMLCALL
tmx_tileset_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;
struct tileset *ts;
#ifdef DEBUG_TMX
debug("<%s> (external tileset)", name);
#endif
if (xml_check_tag(name, "tileset")) {
ts = xml_node_peek(p);
xml_get_string_attr(p, attr, "name", &ts->name, 1);
xml_get_int_attr(p, attr, "tilewidth", &ts->tilewidth, 1);
xml_get_int_attr(p, attr, "tileheight", &ts->tileheight, 1);
if (strcmp(ts->name, "collision") == 0) {
ts->type = TILESET_TYPE_COLLISION;
} else {
ts->type = TILESET_TYPE_IMAGE;
}
xml_node_push(p, ts, tmx_tileset_el_start, tmx_tileset_end,
NULL);
} else {
xml_unexpected_start_tag(p, name, "tileset");
}
}
static void XMLCALL
tmx_tileset_end(void *pv, const char *name)
{
XML_Parser p = (XML_Parser) pv;
#ifdef DEBUG_TMX
debug("</%s> (external tileset)", name);
#endif
if (xml_check_tag(name, "tileset")) {
xml_node_pop(p);
} else {
xml_unexpected_end_tag(p, name, "tileset");
}
}
static void XMLCALL
tmx_image_end(void *pv, const char *name)
{
XML_Parser p = (XML_Parser) pv;
struct image *img;
struct tileset *ts;
#ifdef DEBUG_TMX
debug("</%s> (image)", name);
#endif
if (xml_check_tag(name, "image")) {
img = (struct image *) xml_node_pop(p);
ts = (struct tileset *) xml_node_peek(p);
ts->image = img;
} else {
xml_unexpected_end_tag(p, name, "image");
}
}
static void XMLCALL
tmx_tile_end(void *pv, const char *name)
{
XML_Parser p = (XML_Parser) pv;
#ifdef DEBUG_TMX
debug("</%s> (tile)", name);
#endif
xml_node_pop(p);
if (xml_check_tag(name, "tile")) {
} else {
xml_unexpected_end_tag(p, name, "tile");
}
}
static void XMLCALL
tmx_tile_properties_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;
struct tileset *ts;
#ifdef DEBUG_TMX
debug("<%s> (tile properties)", name);
#endif
ts = xml_node_peek(p);
if (xml_check_tag(name, "properties")) {
/* <properties> has no attributes, but GCC warns of an
* "unused parameter ‘attr’". */
for (; 0; ++attr);
xml_node_push(p, ts, tmx_tile_property_start, tmx_unused_end,
NULL);
} else {
xml_unexpected_start_tag(p, name, "properties");
}
}
static void XMLCALL
tmx_tile_property_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;
struct tileset *ts;
char *attr_name;
char *attr_value;
int coll_t, coll_r, coll_b, coll_l;
int n;
#ifdef DEBUG_TMX
debug("<%s> (tile property)", name);
#endif
ts = xml_node_peek(p);
if (xml_check_tag(name, "property")) {
xml_get_string_attr(p, attr, "name", &attr_name, 1);
xml_get_string_attr(p, attr, "value", &attr_value, 1);
if (strcmp(attr_name, "collision") ==0) {
n = sscanf(attr_value, "%d,%d,%d,%d",
&coll_t, &coll_r, &coll_b, &coll_l);
if (n != 4) {
return;
}
ts->collision_tiles[ts->cur_collision_tile] =
coll_t << 3 | coll_r << 2 |
coll_b << 1 | coll_l << 0;
}
free(attr_name);
free(attr_value);
for (; 0; ++attr, ++ts);
xml_node_push(p, NULL, tmx_invalid_start, tmx_unused_end, NULL);
} else {
xml_unexpected_start_tag(p, name, "property");
}
}
|