summaryrefslogtreecommitdiffstats
path: root/src/tmx.c
blob: 0e401b38fdd2c20b7330c89a764ecc609992168e (plain)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include <expat.h>
#include <SDL.h>
#include "logging.h"
#include "base64.h"
#include "compression.h"
#include "map.h"
#include "image.h"

struct tmx {
	char *dirname;
	struct map *map;
	enum {
		TMX_PARSING_NONE = 0,
		TMX_PARSING_MAP,
		TMX_PARSING_TILESET,
		TMX_PARSING_IMAGE,
		TMX_PARSING_LAYER,
		TMX_PARSING_DATA
	} parsing;
	struct tileset *cur_tileset;
	struct layer *cur_layer;
	enum {
		TMX_DATA_ENC_NONE = 0,
		TMX_DATA_ENC_BASE64,
		TMX_DATA_ENC_CSV
	} data_encoding;
	enum {
		TMX_DATA_COMP_NONE = 0,
		TMX_DATA_COMP_GZIP,
		TMX_DATA_COMP_ZLIB
	} data_compression;
	char *layer_data;
};

#define foreach_tmx_attr(attr)                                                \
	for (; (attr)[0] != NULL; (attr) += 2)

#define tmx_get_int_attr(attr, name, p)                                       \
	do {                                                                  \
		if (strcmp((attr)[0], (name)) == 0) {                         \
			if (sscanf((attr)[1], "%d", (p)) != 1) {              \
				err(1, "Invalid \"%s\" attribute", (name));   \
			}                                                     \
		}                                                             \
	} while (0);

#define tmx_get_string_attr(attr, name, p)                                    \
	do {                                                                  \
		if (strcmp((attr)[0], (name)) == 0) {                         \
			(p) = strdup((attr)[1]);                              \
		}                                                             \
	} while (0);

static void
tmx_start_map(struct tmx *cur_tmx, const char **attr)
{
	if (cur_tmx->map != NULL) {
		err(1, "Found multiple maps in TMX file");
	}

	cur_tmx->map = malloc(sizeof(*cur_tmx->map));
	if (cur_tmx->map == NULL) {
		err(1, "Failed to allocate map");
	}
	memset(cur_tmx->map, 0, sizeof(*cur_tmx->map));

	foreach_tmx_attr (attr) {
		tmx_get_int_attr(attr, "width", &cur_tmx->map->width);
		tmx_get_int_attr(attr, "height", &cur_tmx->map->height);
		tmx_get_int_attr(attr, "tilewidth", &cur_tmx->map->tilewidth);
		tmx_get_int_attr(attr, "tileheight", &cur_tmx->map->tileheight);
	}
}

static void
tmx_start_tileset(struct tmx *cur_tmx, const char **attr)
{
	struct tileset *new_tileset;

	if (cur_tmx->map == NULL) {
		err(1, "Malformed TMX file");
	}

	new_tileset = malloc(sizeof(*new_tileset));
	if (new_tileset == NULL) {
		err(1, "Failed to allocate tileset");
	}
	memset(new_tileset, 0, sizeof(*new_tileset));

	if (cur_tmx->cur_tileset == NULL) {
		cur_tmx->map->tilesets = new_tileset;
	} else {
		cur_tmx->cur_tileset->next = new_tileset;
	}
	cur_tmx->cur_tileset = new_tileset;

	foreach_tmx_attr (attr) {
		tmx_get_int_attr(attr, "firstgid",
				&cur_tmx->cur_tileset->firstgid);
		tmx_get_int_attr(attr, "tilewidth",
				&cur_tmx->cur_tileset->tilewidth);
		tmx_get_int_attr(attr, "tileheight",
				&cur_tmx->cur_tileset->tileheight);
	}

	/* TODO: Parse tilesets referenced in "source" attributes. */
}

static void
tmx_start_image(struct tmx *cur_tmx, const char **attr)
{
	char *source;
	char *path;

	if (cur_tmx->cur_tileset == NULL) {
		err(1, "Malformed TMX file");
	}

	foreach_tmx_attr (attr) {
		tmx_get_string_attr(attr, "source", source);
	}

	/* TODO: Move to end tag handler and check for "loadimage" property. */
	path = malloc(strlen(cur_tmx->dirname) + strlen(source) + 2);
	if (path == NULL) {
		err(1, "Failed to allocate resource path string");
	}
	sprintf(path, "%s/%s", cur_tmx->dirname, source);
	cur_tmx->cur_tileset->image = load_png(path);
	free(path);

	debug("  Found tileset with firstgid %d and source \"%s\"",
			cur_tmx->cur_tileset->firstgid, source);

	free(source);
}

static void
tmx_start_layer(struct tmx *cur_tmx, const char **attr)
{
	struct layer *new_layer;

	if (cur_tmx->map == NULL) {
		err(1, "Malformed TMX file");
	}

	new_layer = malloc(sizeof(*new_layer));
	if (new_layer == NULL) {
		err(1, "Failed to allocate layer");
	}
	memset(new_layer, 0, sizeof(*new_layer));

	if (cur_tmx->cur_layer == NULL) {
		cur_tmx->map->layers = new_layer;
	} else {
		cur_tmx->cur_layer->next = new_layer;
	}
	cur_tmx->cur_layer = new_layer;

	foreach_tmx_attr (attr) {
		tmx_get_string_attr(attr, "name", cur_tmx->cur_layer->name);
	}

	debug("  Found layer with name \"%s\"", cur_tmx->cur_layer->name);
}

static void
tmx_start_data(struct tmx *cur_tmx, const char **attr)
{
	if (cur_tmx->cur_layer == NULL) {
		err(1, "Malformed TMX file");
	}

	foreach_tmx_attr (attr) {
		if (strcmp(attr[0], "encoding") == 0) {
			if (strcmp(attr[1], "base64") == 0) {
				cur_tmx->data_encoding = TMX_DATA_ENC_BASE64;
			} else if (strcmp(attr[1], "csv") == 0) {
				cur_tmx->data_encoding = TMX_DATA_ENC_CSV;
			} else {
				err(1, "Unsupported data encoding in TMX file");
			}
		} else if (strcmp(attr[0], "compression") == 0) {
			if (strcmp(attr[1], "gzip") == 0) {
				cur_tmx->data_compression = TMX_DATA_COMP_GZIP;
			} else if (strcmp(attr[1], "zlib") == 0) {
				cur_tmx->data_compression = TMX_DATA_COMP_ZLIB;
			} else {
				err(1, "Unsupported compression in TMX file");
			}
		}
	}
}

static void XMLCALL
tmx_start(void *data, const char *el, const char **attr)
{
	struct tmx *cur_tmx;

	cur_tmx = (struct tmx *) data;

	if (strcmp(el, "map") == 0) {
		tmx_start_map(cur_tmx, attr);
		cur_tmx->parsing = TMX_PARSING_MAP;
	} else if (strcmp(el, "tileset") == 0) {
		tmx_start_tileset(cur_tmx, attr);
		cur_tmx->parsing = TMX_PARSING_TILESET;
	} else if (strcmp(el, "image") == 0) {
		tmx_start_image(cur_tmx, attr);
		cur_tmx->parsing = TMX_PARSING_IMAGE;
	} else if (strcmp(el, "layer") == 0) {
		tmx_start_layer(cur_tmx, attr);
		cur_tmx->parsing = TMX_PARSING_LAYER;
	} else if (strcmp(el, "data") == 0) {
		tmx_start_data(cur_tmx, attr);
		cur_tmx->parsing = TMX_PARSING_DATA;
	}

	/* TODO: Handle objectgroup, object, properties, and property tags. */
}

static void
tmx_end_data(struct tmx *cur_tmx)
{
	size_t decoded_len;
	size_t decomp_len;
	char *decoded_buf;
	char *decomp_buf;

	decoded_len = strlen(cur_tmx->layer_data);

	decomp_len = 4 * cur_tmx->map->width * cur_tmx->map->height;
	debug("  Expected map data size: %d", decomp_len);

	decoded_buf = malloc(decoded_len + 1);
	if (decoded_buf == NULL) {
		err(1, "Failed to allocate map layer data buffer");
	}

	if (cur_tmx->data_encoding == TMX_DATA_ENC_BASE64) {
		debug("  Decoding base 64 layer data...");
		base64_decode(cur_tmx->layer_data,
				decoded_buf, decoded_len + 1);
	}

	if (cur_tmx->data_compression == TMX_DATA_COMP_NONE) {
		debug("  Layer data already decompressed");
		decomp_buf = decoded_buf;
	} else if (cur_tmx->data_compression == TMX_DATA_COMP_ZLIB) {
		debug("  Decompressing layer data with zlib...");
		decomp_buf = malloc(decomp_len);
		if (decomp_buf == NULL) {
			err(1, "Failed to allocate map layer data buffer");
		}
		decompress(decoded_buf, decoded_len, decomp_buf, decomp_len);
		free(decoded_buf);
	} else if (cur_tmx->data_compression == TMX_DATA_COMP_GZIP) {
		debug("  Decompressing layer data with gzip...");
		decomp_buf = malloc(decomp_len);
		if (decomp_buf == NULL) {
			err(1, "Failed to allocate map layer data buffer");
		}
		decompress(decoded_buf, decoded_len, decomp_buf, decomp_len);
		free(decoded_buf);
	}

	free(cur_tmx->layer_data);

	cur_tmx->cur_layer->tiles = (Uint32 *) decomp_buf;
}

static void XMLCALL
tmx_end(void *data, const char *el)
{
	struct tmx *cur_tmx;

	cur_tmx = (struct tmx *) data;

	if (strcmp(el, "data") == 0) {
		tmx_end_data(cur_tmx);
	}

	cur_tmx->parsing = TMX_PARSING_NONE;
}

static void XMLCALL
tmx_data(void *data, const char *s, int len)
{
	struct tmx *cur_tmx;
	char *s_z;
	char *s_z_trimmed, *s_z_trimmed_end;

	cur_tmx = (struct tmx *) data;

	if (cur_tmx->parsing != TMX_PARSING_DATA) {
		return;
	}

	s_z = s_z_trimmed = strndup(s, len);

	while(isspace(*s_z_trimmed)) {
		++s_z_trimmed;
		--len;
	}
	if (*s_z_trimmed == '\0') {
		return;
	}
	s_z_trimmed_end = s_z_trimmed + len - 1;
	while (s_z_trimmed_end > s_z_trimmed && isspace(*s_z_trimmed_end)) {
		--s_z_trimmed_end;
	}
	*(s_z_trimmed_end + 1) = '\0';

	cur_tmx->layer_data = strdup(s_z_trimmed);

	free(s_z);
}

struct map *
tmx_load(const char *path)
{
	XML_Parser p;
	struct tmx cur_tmx;
	char *path_dup;
	FILE *tmx_fp;
	void *tmx_buf;
	size_t len;

	debug("Parsing TMX file \"%s\"...", path);

	p = XML_ParserCreate(NULL);
	if (p == NULL) {
		err(1, "Failed to create TMX parser");
	}

	XML_SetElementHandler(p, tmx_start, tmx_end);
	XML_SetCharacterDataHandler(p, tmx_data);

	memset(&cur_tmx, 0, sizeof(cur_tmx));
	path_dup = strdup(path);
	cur_tmx.dirname = dirname(path_dup);
	XML_SetUserData(p, &cur_tmx);

	tmx_fp = fopen(path, "rb");
	if (tmx_fp == NULL) {
		err(1, "Failed to open TMX file");
	}

	tmx_buf = XML_GetBuffer(p, 8192);
	if (tmx_buf == NULL) {
		err(1, "Failed to create TMX parse buffer");
	}

	while (!feof(tmx_fp)) {
		len = fread(tmx_buf, 1, 8192, tmx_fp);
		if (XML_ParseBuffer(p, len, feof(tmx_fp)) == XML_STATUS_ERROR) {
			err(1, "Failed to parse TMX file (%s)",
					XML_ErrorString(XML_GetErrorCode(p)));
		}
	}

	fclose(tmx_fp);

	free(path_dup);

	return cur_tmx.map;
}