summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-21 12:29:47 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-21 14:58:57 (EDT)
commit6989f12a81529f617be367d2ec0b8ac11b90adee (patch)
tree35f2a56500f1b67eef5ec6a06bab21afe2ef2fd4
parenta4989fde9038180d179eb0a677781b6e40857214 (diff)
downloaddodge-balls-6989f12a81529f617be367d2ec0b8ac11b90adee.zip
dodge-balls-6989f12a81529f617be367d2ec0b8ac11b90adee.tar.gz
dodge-balls-6989f12a81529f617be367d2ec0b8ac11b90adee.tar.bz2
map: Parse <map> children
-rw-r--r--src/map.c152
1 files changed, 149 insertions, 3 deletions
diff --git a/src/map.c b/src/map.c
index 9028354..123491b 100644
--- a/src/map.c
+++ b/src/map.c
@@ -35,6 +35,7 @@ struct db_map {
Uint8 bg_r;
Uint8 bg_g;
Uint8 bg_b;
+ int fr;
};
static char *
@@ -84,8 +85,153 @@ _db_tmx_invalid_start(void *pv, const char *name,
}
static void XMLCALL
+_db_tmx_property_end(void *pv, const char *name)
+{
+ XML_Parser p;
+
+ db_dbg(" </%s> (property)", name);
+
+ p = (XML_Parser) pv;
+
+ if (db_xml_check_tag(name, "property")) {
+ db_xml_node_pop(p);
+ } else {
+ db_xml_unexpected_end_tag(p, name, "property");
+ }
+}
+
+static void XMLCALL
+_db_tmx_property_start(void *pv, const char *name, const char **attr)
+{
+ XML_Parser p;
+ struct db_map *map;
+ char *p_name;
+ char *p_type;
+
+ db_dbg(" <%s> (property)", name);
+
+ p = (XML_Parser) pv;
+ map = db_xml_node_peek(p);
+
+ if (db_xml_check_tag(name, "property")) {
+ db_xml_get_string_attr(p, attr, "name", &p_name, 1);
+ db_xml_get_string_attr(p, attr, "type", &p_type, 1);
+ if (strcmp(p_name, "framerate") == 0) {
+ if (strcmp(p_type, "int") != 0) {
+ db_err("Framerate must be an integer");
+ free(p_name);
+ free(p_type);
+ XML_StopParser(p, XML_FALSE);
+ return;
+ }
+ db_xml_get_int_attr(p, attr, "value", &map->fr, 1);
+ db_dbg(" Framerate: %d", map->fr);
+ } else {
+ db_dbg("Skipping unknown property \"%s\"", p_name);
+ }
+ free(p_name);
+ free(p_type);
+ db_xml_node_push(p, map, _db_tmx_invalid_start,
+ _db_tmx_property_end, NULL);
+ } else {
+ db_xml_unexpected_start_tag(p, name, "property");
+ }
+}
+
+static void XMLCALL
+_db_tmx_properties_end(void *pv, const char *name)
+{
+ XML_Parser p;
+
+ db_dbg(" </%s> (properties)", name);
+
+ p = (XML_Parser) pv;
+
+ if (db_xml_check_tag(name, "properties")) {
+ db_xml_node_pop(p);
+ } else {
+ db_xml_unexpected_end_tag(p, name, "properties");
+ }
+}
+
+static void XMLCALL
+_db_tmx_tileset_end(void *pv, const char *name)
+{
+ XML_Parser p;
+
+ db_dbg(" </%s> (tileset)", name);
+
+ p = (XML_Parser) pv;
+
+ if (db_xml_check_tag(name, "tileset")) {
+ db_xml_node_pop(p);
+ } else {
+ db_xml_unexpected_end_tag(p, name, "tileset");
+ }
+}
+
+static void XMLCALL
+_db_tmx_layer_end(void *pv, const char *name)
+{
+ XML_Parser p;
+
+ db_dbg(" </%s> (layer)", name);
+
+ p = (XML_Parser) pv;
+
+ if (db_xml_check_tag(name, "layer")) {
+ db_xml_node_pop(p);
+ } else {
+ db_xml_unexpected_end_tag(p, name, "layer");
+ }
+}
+
+static void XMLCALL
+_db_tmx_objectgroup_end(void *pv, const char *name)
+{
+ XML_Parser p;
+
+ db_dbg(" </%s> (objectgroup)", name);
+
+ p = (XML_Parser) pv;
+
+ if (db_xml_check_tag(name, "objectgroup")) {
+ db_xml_node_pop(p);
+ } else {
+ db_xml_unexpected_end_tag(p, name, "objectgroup");
+ }
+}
+
+static void XMLCALL
_db_tmx_map_el_start(void *pv, const char *name, const char **attr)
{
+ XML_Parser p;
+ struct db_map *map;
+
+ db_dbg(" <%s> (map child)", name);
+
+ p = (XML_Parser) pv;
+ map = db_xml_node_peek(p);
+
+ if (db_xml_check_tag(name, "properties")) {
+ 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_node_push(p, map, _db_tmx_invalid_start,
+ _db_tmx_tileset_end, NULL);
+ } else if (db_xml_check_tag(name, "layer")) {
+ /* TODO: data */
+ db_xml_node_push(p, map, _db_tmx_invalid_start,
+ _db_tmx_layer_end, NULL);
+ } else if (db_xml_check_tag(name, "objectgroup")) {
+ /* TODO: object */
+ db_xml_node_push(p, map, _db_tmx_invalid_start,
+ _db_tmx_objectgroup_end, NULL);
+ } else {
+ db_xml_unexpected_start_tag(p, name,
+ "properties, tileset, layer, or objectgroup");
+ }
}
static void XMLCALL
@@ -150,8 +296,8 @@ _db_tmx_map_start(void *pv, const char *name, const char **attr)
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);
+ 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);
@@ -164,7 +310,7 @@ _db_tmx_map_start(void *pv, const char *name, const char **attr)
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)",
+ db_dbg(" 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);