summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2013-02-17 01:56:08 (EST)
committer P. J. McDermott <pjm@nac.net>2013-02-17 01:56:08 (EST)
commit833b3d56704730776e528b89013e9419fd128eba (patch)
tree5d380665ce4f9d9147eeae164f2eec66b96960e4 /src
parent1a8f91467aad191721d57a9ad772f6700c029e1d (diff)
downloadoverworld-rpg-833b3d56704730776e528b89013e9419fd128eba.zip
overworld-rpg-833b3d56704730776e528b89013e9419fd128eba.tar.gz
overworld-rpg-833b3d56704730776e528b89013e9419fd128eba.tar.bz2
Allocate maps & tilesets before their start elems.
Diffstat (limited to 'src')
-rw-r--r--src/main.c6
-rw-r--r--src/resources/map.c61
2 files changed, 38 insertions, 29 deletions
diff --git a/src/main.c b/src/main.c
index c867f65..0727d5b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -9,12 +9,16 @@ main(void)
{
struct map *map;
struct image *img;
+ struct tileset *ts;
SDL_Rect imgrect, surfacerect;
init();
map = map_get("data/forest1.simple.tmx");
- printf("Map dimensions: %dx%d\n", map->width, map->height);
+ debug("Map dimensions: %dx%d", map->width, map->height);
+ for (ts = map->tilesets_head; ts != NULL; ts = ts->next) {
+ debug("Tilesheet name: %s", ts->name);
+ }
img = img_png_get("../forest-6-layer-test_ground.png");
img_png_free(img);
diff --git a/src/resources/map.c b/src/resources/map.c
index 6e5606b..e0c48c0 100644
--- a/src/resources/map.c
+++ b/src/resources/map.c
@@ -16,7 +16,7 @@ static void XMLCALL tmx_map_end(void *, const char *);
static void XMLCALL tmx_map_el_start(void *, const char *, const char **);
static void XMLCALL tmx_tileset_start(void *, const char *, const char **);
static void XMLCALL tmx_tileset_end(void *, const char *);
-static void XMLCALL tmx_ext_ts_end(void *, const char *);
+static void XMLCALL tmx_tilesheetemb_end(void *, const char *);
static void XMLCALL tmx_tileset_el_start(void *, const char *, const char **);
static void XMLCALL tmx_image_end(void *, const char *);
static void XMLCALL tmx_unused_start(void *, const char *, const char **);
@@ -40,6 +40,11 @@ map_get(const char *path)
return map;
}
+ map = resource_alloc(path, sizeof(*map));
+ if (map == NULL) {
+ return NULL;
+ }
+
p = XML_ParserCreate(NULL);
if (p == NULL) {
warn("Failed to create TMX parser");
@@ -80,7 +85,6 @@ map_get(const char *path)
return NULL;
}
- map = (struct map *) xml_node_pop(p);
XML_ParserFree(p);
fclose(tmx_fp);
@@ -128,6 +132,11 @@ tileset_get(const char *path)
return ts;
}
+ ts = resource_alloc(path, sizeof(*ts));
+ if (ts == NULL) {
+ return NULL;
+ }
+
p = XML_ParserCreate(NULL);
if (p == NULL) {
warn("Failed to create TSX parser");
@@ -168,7 +177,6 @@ tileset_get(const char *path)
return NULL;
}
- ts = (struct tileset *) xml_node_pop(p);
XML_ParserFree(p);
fclose(tmx_fp);
@@ -187,11 +195,7 @@ tmx_map_start(void *pv, const char *name, const char **attr)
debug("<%s> (map)", name);
if (xml_check_tag(name, "map")) {
- m = resource_alloc("", sizeof(*m));
- if (m == NULL) {
- XML_StopParser(p, XML_FALSE);
- return;
- }
+ m = xml_node_peek(p);
xml_get_int_attr(p, attr, "width", &m->width, 1);
xml_get_int_attr(p, attr, "height", &m->height, 1);
xml_get_int_attr(p, attr, "tilewidth", &m->tilewidth, 1);
@@ -207,14 +211,11 @@ static void XMLCALL
tmx_map_end(void *pv, const char *name)
{
XML_Parser p = (XML_Parser) pv;
- struct map *m;
debug("</%s> (map)", name);
if (xml_check_tag(name, "map")) {
- /* A hack to expose the map back to map_get(). */
- m = xml_node_pop(p);
- xml_node_push(p, m, tmx_invalid_start, tmx_invalid_end, NULL);
+ xml_node_pop(p);
} else {
xml_unexpected_end_tag(p, name, "map");
}
@@ -239,15 +240,23 @@ tmx_map_el_start(void *pv, const char *name, const char **attr)
/* External tilesheet. */
ts = tileset_get(source);
free(source);
- xml_node_push(p, ts, tmx_invalid_start, tmx_ext_ts_end,
- NULL);
+ xml_node_push(p, ts, tmx_invalid_start,
+ tmx_tilesheetemb_end, NULL);
} else {
- /* Pass through. */
- tmx_tileset_start(pv, name, attr);
- ts = (struct tileset *) xml_node_peek(p);
+ /* Embedded tilesheet. */
+ ts = resource_alloc("internal", sizeof(*ts));
+ if (ts == NULL) {
+ return;
+ }
+ 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);
+ xml_node_push(p, ts, tmx_tileset_el_start,
+ tmx_tilesheetemb_end, NULL);
}
xml_get_int_attr(p, attr, "firstgid", &ts->firstgid, 1);
- xml_get_string_attr(p, attr, "name", &ts->name, 1);
} else {
xml_unexpected_start_tag(p, name,
"properties, tileset, layer, or objectgroup");
@@ -263,7 +272,7 @@ tmx_tileset_start(void *pv, const char *name, const char **attr)
debug("<%s> (tileset)", name);
if (xml_check_tag(name, "tileset")) {
- ts = resource_alloc("", sizeof(*ts));
+ 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);
@@ -278,27 +287,23 @@ static void XMLCALL
tmx_tileset_end(void *pv, const char *name)
{
XML_Parser p = (XML_Parser) pv;
- struct tileset *ts;
- debug("</%s> (tileset)", name);
+ debug("</%s> (external tileset)", name);
if (xml_check_tag(name, "tileset")) {
- ts = (struct tileset *) xml_node_pop(p);
- /* XXX?? */
- resource_use((struct resource *) ts);
- resource_add(&ts_res, "", (struct resource *) ts);
+ xml_node_pop(p);
} else {
xml_unexpected_end_tag(p, name, "tileset");
}
}
static void XMLCALL
-tmx_ext_ts_end(void *pv, const char *name)
+tmx_tilesheetemb_end(void *pv, const char *name)
{
XML_Parser p = (XML_Parser) pv;
struct tileset *ts;
- debug("</%s> (tileset link)", name);
+ debug("</%s> (tileset)", name);
if (xml_check_tag(name, "tileset")) {
ts = (struct tileset *) xml_node_pop(p);
@@ -319,7 +324,7 @@ tmx_tileset_el_start(void *pv, const char *name, const char **attr)
if (xml_check_tag(name, "image")) {
xml_get_string_attr(p, attr, "source", &source, 0);
- img = NULL; /*img_png_get(source);*/
+ img = img_png_get(source);
free(source);
xml_node_push(p, img, tmx_invalid_start, tmx_image_end, NULL);
} else {