diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/map.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -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; } |