summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-26 09:45:52 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-26 09:56:30 (EDT)
commit693d232ab6d8180292c776347a33b6c9c5f0f997 (patch)
treea118bc0690f2f8ec73400c42ba26aa94e10e14aa
parent2ed1053fbb55cc7ebb83dce77b35d0fae3a43f15 (diff)
downloaddodge-balls-693d232ab6d8180292c776347a33b6c9c5f0f997.zip
dodge-balls-693d232ab6d8180292c776347a33b6c9c5f0f997.tar.gz
dodge-balls-693d232ab6d8180292c776347a33b6c9c5f0f997.tar.bz2
map: Set collision tile bit fields
-rw-r--r--src/map.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/map.c b/src/map.c
index c134e6f..0d57a09 100644
--- a/src/map.c
+++ b/src/map.c
@@ -73,6 +73,8 @@ struct db_map {
int target_gid;
struct db_ball *ball_head;
struct db_ball *ball_tail;
+ Uint8 *p_col;
+ Uint8 *b_col;
};
static char *
@@ -748,6 +750,45 @@ _db_tmx_map_start(void *pv, const char *name, const char **attr)
}
}
+static void
+_db_map_set_collision_tiles(struct db_map *map)
+{
+ struct db_map_layer *layer;
+ int i;
+ int gid;
+
+ db_dbg("Allocating %d bit field elements", (map->w * map->h + 7) / 8);
+ map->p_col = calloc((map->w * map->h + 7) / 8, sizeof(*map->p_col));
+ if (map->p_col == NULL) {
+ db_err("Failed to allocate memory");
+ return;
+ }
+ map->b_col = calloc((map->w * map->h + 7) / 8, sizeof(*map->p_col));
+ if (map->b_col == NULL) {
+ db_err("Failed to allocate memory");
+ free(map->p_col);
+ return;
+ }
+
+ for (layer = map->layer_head; layer != NULL; layer = layer->next) {
+ for (i = 0; i < map->w * map->h; ++i) {
+ gid = layer->tiles[i];
+ if (gid == 0) {
+ continue;
+ }
+ if (db_tile_player_collides(map->tileset_head, gid)) {
+ db_dbg("Tile %d collides: setting bit %d of "
+ "field element %d",
+ i, i % 8, i / 8);
+ map->p_col[i / 8] |= 1 << (i % 8);
+ }
+ if (db_tile_ball_collides(map->tileset_head, gid)) {
+ map->b_col[i / 8] |= 1 << (i % 8);
+ }
+ }
+ }
+}
+
struct db_map *
db_map_new(const char *game_id, const char *level_id)
{
@@ -837,6 +878,8 @@ db_map_new(const char *game_id, const char *level_id)
XML_ParserFree(p);
fclose(fp);
+ _db_map_set_collision_tiles(map);
+
return map;
}