From 8a0e4c830fea4ffa3eaaed370be3b2256cfa925a Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Mon, 22 Mar 2021 17:07:46 -0400 Subject: db_tileset_new(): New function --- (limited to 'src/map.c') diff --git a/src/map.c b/src/map.c index 123491b..3e7c0e3 100644 --- a/src/map.c +++ b/src/map.c @@ -24,18 +24,22 @@ #include "defs.h" #include "dirs.h" #include "map.h" +#include "tileset.h" #include "output.h" #include "xml.h" struct db_map { - int w; - int h; - int tw; - int th; - Uint8 bg_r; - Uint8 bg_g; - Uint8 bg_b; - int fr; + char *game_id; + int w; + int h; + int tw; + int th; + Uint8 bg_r; + Uint8 bg_g; + Uint8 bg_b; + int fr; + struct db_tileset *tileset_head; + struct db_tileset *tileset_tail; }; static char * @@ -207,6 +211,8 @@ _db_tmx_map_el_start(void *pv, const char *name, const char **attr) { XML_Parser p; struct db_map *map; + int firstgid; + char *source; db_dbg(" <%s> (map child)", name); @@ -217,7 +223,19 @@ _db_tmx_map_el_start(void *pv, const char *name, const char **attr) db_xml_node_push(p, map, _db_tmx_property_start, _db_tmx_properties_end, NULL); } else if (db_xml_check_tag(name, "tileset")) { - /* TODO: attr */ + db_xml_get_int_attr(p, attr, "firstgid", &firstgid, 1); + db_xml_get_string_attr(p, attr, "source", &source, 1); + db_dbg(" Tileset: <%s>", source); + map->tileset_tail = db_tileset_new(map->game_id, source, + firstgid, map->tileset_tail); + if (map->tileset_head == NULL) { + map->tileset_head = map->tileset_tail; + } + free(source); + if (map->tileset_tail == NULL) { + XML_StopParser(p, XML_FALSE); + return; + } db_xml_node_push(p, map, _db_tmx_invalid_start, _db_tmx_tileset_end, NULL); } else if (db_xml_check_tag(name, "layer")) { @@ -336,8 +354,16 @@ db_map_new(const char *game_id, const char *level_id) return NULL; } + map->game_id = strdup(game_id); + if (map->game_id == NULL) { + db_err("Failed to allocate memory"); + free(map); + return NULL; + } + p = XML_ParserCreate(NULL); if (p == NULL) { + free(map->game_id); free(map); return NULL; } @@ -349,6 +375,7 @@ db_map_new(const char *game_id, const char *level_id) db_xml_node_pop(p); XML_ParserFree(p); free(path); + free(map->game_id); free(map); return NULL; } @@ -358,6 +385,7 @@ db_map_new(const char *game_id, const char *level_id) db_xml_node_pop(p); XML_ParserFree(p); free(path); + free(map->game_id); free(map); return NULL; } @@ -368,6 +396,7 @@ db_map_new(const char *game_id, const char *level_id) XML_ParserFree(p); free(path); fclose(fp); + free(map->game_id); free(map); return NULL; } @@ -386,6 +415,7 @@ db_map_new(const char *game_id, const char *level_id) db_xml_node_pop(p); XML_ParserFree(p); fclose(fp); + free(map->game_id); free(map); return NULL; } -- cgit v0.9.1