diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-03-21 09:27:50 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-03-21 09:27:50 (EDT) |
commit | a4989fde9038180d179eb0a677781b6e40857214 (patch) | |
tree | 47e8cde8afaa0c0c6753fd227c6a3aec572eceb5 | |
parent | ec9fb981d8254227ed4089db8be71e6fca8697bd (diff) | |
download | dodge-balls-a4989fde9038180d179eb0a677781b6e40857214.zip dodge-balls-a4989fde9038180d179eb0a677781b6e40857214.tar.gz dodge-balls-a4989fde9038180d179eb0a677781b6e40857214.tar.bz2 |
map: Parse map attributes
-rw-r--r-- | src/map.c | 71 |
1 files changed, 67 insertions, 4 deletions
@@ -16,16 +16,25 @@ * You should have received a copy of the GNU General Public License * along with Dodge Balls. If not, see <http://www.gnu.org/licenses/>. */ + #include <expat.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include "defs.h" #include "dirs.h" #include "map.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; }; static char * @@ -75,6 +84,11 @@ _db_tmx_invalid_start(void *pv, const char *name, } static void XMLCALL +_db_tmx_map_el_start(void *pv, const char *name, const char **attr) +{ +} + +static void XMLCALL _db_tmx_map_end(void *pv, const char *name) { XML_Parser p; @@ -91,11 +105,16 @@ _db_tmx_map_end(void *pv, const char *name) } static void XMLCALL -_db_tmx_map_start(void *pv, const char *name, - const char **attr __attribute__((__unused__))) +_db_tmx_map_start(void *pv, const char *name, const char **attr) { - XML_Parser p; + XML_Parser p; struct db_map *map; + char *orientation; + char *renderorder; + char *backgroundcolor; + unsigned int bg_r; + unsigned int bg_g; + unsigned int bg_b; db_dbg(" <%s> (map)", name); @@ -103,7 +122,51 @@ _db_tmx_map_start(void *pv, const char *name, map = db_xml_node_peek(p); if (db_xml_check_tag(name, "map")) { - db_xml_node_push(p, map, _db_tmx_invalid_start, + /* Check orientation */ + db_xml_get_string_attr(p, attr, "orientation", &orientation, 1); + if (strcmp(orientation, "orthogonal") != 0) { + db_err("Map must be orthogonal"); + XML_StopParser(p, XML_FALSE); + return; + } + free(orientation); + /* Check render order */ + db_xml_get_string_attr(p, attr, "renderorder", &renderorder, 1); + if (strcmp(renderorder, "right-down") != 0) { + db_err("Map must be rendered right-down"); + XML_StopParser(p, XML_FALSE); + return; + } + free(renderorder); + /* Get and check size */ + db_xml_get_int_attr(p, attr, "width", &map->w, 1); + db_xml_get_int_attr(p, attr, "height", &map->h, 1); + db_xml_get_int_attr(p, attr, "tilewidth", &map->tw, 1); + db_xml_get_int_attr(p, attr, "tileheight", &map->th, 1); + if (map->w * map->tw != DB_WINDOW_W || + map->h * map->th != DB_WINDOW_H) { + db_err("Map size must be %dx%d px", + DB_WINDOW_W, DB_WINDOW_H); + XML_StopParser(p, XML_FALSE); + return; + } + db_dbg("Map size: %dx%d px", map->w, map->h); + db_dbg("Tile size: %dx%d px", map->tw, map->th); + /* Get and check background color */ + db_xml_get_string_attr(p, attr, "backgroundcolor", + &backgroundcolor, 1); + if (sscanf(backgroundcolor, "#%02x%02x%02x", + &bg_r, &bg_g, &bg_b) != 3) { + db_err("Background color must be \"#xxxxxx\""); + XML_StopParser(p, XML_FALSE); + return; + } + map->bg_r = bg_r; + map->bg_g = bg_g; + map->bg_b = bg_b; + db_dbg("Map background color: (0x%02x, 0x%02x, 0x%02x)", + map->bg_r, map->bg_g, map->bg_b); + db_xml_node_push(p, map, _db_tmx_map_el_start, _db_tmx_map_end, NULL); } else { db_xml_unexpected_start_tag(p, name, "map"); |