diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-03-25 15:05:37 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-03-25 15:05:37 (EDT) |
commit | 514790f9d67e6dfffb7f6ed338a53be3eb84faeb (patch) | |
tree | c1a66e6d4ca47b6556d64f2eb8589c649434c004 | |
parent | 34ccab84eee6406833b3bbc556301e5f070be2e8 (diff) | |
download | dodge-balls-514790f9d67e6dfffb7f6ed338a53be3eb84faeb.zip dodge-balls-514790f9d67e6dfffb7f6ed338a53be3eb84faeb.tar.gz dodge-balls-514790f9d67e6dfffb7f6ed338a53be3eb84faeb.tar.bz2 |
tileset: Ensure tile size matches map's
-rw-r--r-- | src/map.c | 16 | ||||
-rw-r--r-- | src/tileset.c | 38 | ||||
-rw-r--r-- | src/tileset.h | 5 |
3 files changed, 26 insertions, 33 deletions
@@ -396,7 +396,7 @@ _db_tmx_map_el_start(void *pv, const char *name, const char **attr) 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); + firstgid, map->tw, map->th, map->tileset_tail); if (map->tileset_head == NULL) { map->tileset_head = map->tileset_tail; } @@ -516,8 +516,6 @@ _db_map_render_layer(struct db_map *map, struct db_map_layer *layer, int firstgid; int lastgid; int columns; - int tilewidth; - int tileheight; SDL_Rect tile_rect; SDL_Rect layer_rect; @@ -532,20 +530,18 @@ _db_map_render_layer(struct db_map *map, struct db_map_layer *layer, firstgid = db_tileset_firstgid(tileset); lastgid = firstgid + db_tileset_tilecount(tileset); if (gid >= firstgid && gid < lastgid) { - columns = db_tileset_columns (tileset); - tilewidth = db_tileset_tilewidth (tileset); - tileheight = db_tileset_tileheight(tileset); + columns = db_tileset_columns(tileset); gid -= firstgid; tile_rect.x = gid % columns; tile_rect.y = gid / columns; - tile_rect.w = tilewidth; - tile_rect.h = tileheight; + tile_rect.w = map->tw; + tile_rect.h = map->th; tile_rect.x *= tile_rect.w; tile_rect.y *= tile_rect.h; layer_rect.x = i % map->w; layer_rect.y = i / map->w; - layer_rect.w = tilewidth; - layer_rect.h = tileheight; + layer_rect.w = map->tw; + layer_rect.h = map->th; layer_rect.x *= layer_rect.w; layer_rect.y *= layer_rect.h; db_dbg("Blitting %dx%d tile at (%d,%d) " diff --git a/src/tileset.c b/src/tileset.c index 366c925..f63fa0a 100644 --- a/src/tileset.c +++ b/src/tileset.c @@ -293,6 +293,8 @@ _db_tsx_tileset_start(void *pv, const char *name, const char **attr) { XML_Parser p; struct db_tileset *tileset; + int tw; + int th; db_dbg(" <%s> (tileset)", name); @@ -300,8 +302,14 @@ _db_tsx_tileset_start(void *pv, const char *name, const char **attr) tileset = db_xml_node_peek(p); if (db_xml_check_tag(name, "tileset")) { - db_xml_get_int_attr(p, attr, "tilewidth", &tileset->tw, 1); - db_xml_get_int_attr(p, attr, "tileheight", &tileset->th, 1); + db_xml_get_int_attr(p, attr, "tilewidth", &tw, 1); + db_xml_get_int_attr(p, attr, "tileheight", &th, 1); + if (tw != tileset->tw || th != tileset->th) { + db_err("Tileset has %dx%d tiles, not %dx%d like map", + tw, th, tileset->tw, tileset->th); + XML_StopParser(p, XML_FALSE); + return; + } db_xml_get_int_attr(p, attr, "tilecount", &tileset->tc, 1); db_xml_get_int_attr(p, attr, "columns", &tileset->cols, 1); db_dbg(" Tile size: %dx%d px", @@ -315,7 +323,7 @@ _db_tsx_tileset_start(void *pv, const char *name, const char **attr) struct db_tileset * db_tileset_new(const char *game_id, const char *file, int firstgid, - struct db_tileset *prev) + int tilewidth, int tileheight, struct db_tileset *prev) { struct db_tileset *tileset; XML_Parser p; @@ -331,6 +339,13 @@ db_tileset_new(const char *game_id, const char *file, int firstgid, return NULL; } + tileset->firstgid = firstgid; + tileset->tw = tilewidth; + tileset->th = tileheight; + if (prev != NULL) { + prev->next = tileset; + } + tileset->game_id = strdup(game_id); if (tileset->game_id == NULL) { db_err("Failed to allocate memory"); @@ -404,27 +419,10 @@ db_tileset_new(const char *game_id, const char *file, int firstgid, XML_ParserFree(p); fclose(fp); - tileset->firstgid = firstgid; - if (prev != NULL) { - prev->next = tileset; - } - return tileset; } int -db_tileset_tilewidth(struct db_tileset *tileset) -{ - return tileset->tw; -} - -int -db_tileset_tileheight(struct db_tileset *tileset) -{ - return tileset->th; -} - -int db_tileset_tilecount(struct db_tileset *tileset) { return tileset->tc; diff --git a/src/tileset.h b/src/tileset.h index d350765..79b8bc7 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -25,9 +25,8 @@ struct db_tileset; struct db_tileset *db_tileset_new(const char *game_id, const char *file, - int firstgid, struct db_tileset *prev); -int db_tileset_tilewidth (struct db_tileset *tileset) __attribute__((__pure__)); -int db_tileset_tileheight(struct db_tileset *tileset) __attribute__((__pure__)); + int firstgid, int tilewidth, int tileheight, + struct db_tileset *prev); int db_tileset_tilecount (struct db_tileset *tileset) __attribute__((__pure__)); int db_tileset_columns (struct db_tileset *tileset) __attribute__((__pure__)); int db_tileset_firstgid (struct db_tileset *tileset) __attribute__((__pure__)); |