From 693d232ab6d8180292c776347a33b6c9c5f0f997 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 26 Mar 2021 09:45:52 -0400 Subject: map: Set collision tile bit fields --- 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; } -- cgit v0.9.1