From 2089d501882f927f8d8dd932dd1b167f722ae1cd Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 28 Mar 2021 21:43:24 -0400 Subject: [WIP] map: Merge wall tiles into hor/vert lines --- diff --git a/src/map.c b/src/map.c index f33782f..0a093ee 100644 --- a/src/map.c +++ b/src/map.c @@ -32,6 +32,11 @@ #include "output.h" #include "xml.h" +struct db_wall { + SDL_Rect rect; + struct db_wall *next; +}; + struct db_map_layer { Uint32 *tiles; char *encoding; @@ -74,7 +79,8 @@ struct db_map { struct db_ball *ball_head; struct db_ball *ball_tail; Uint8 *p_col; - Uint8 *b_col; + struct db_wall *wall_head; + struct db_wall *wall_tail; }; static char * @@ -771,6 +777,9 @@ _db_map_set_collision_tiles(struct db_map *map) struct db_map_layer *layer; int i; int gid; + Uint8 *b_col; + int x; + int y; 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)); @@ -778,8 +787,8 @@ _db_map_set_collision_tiles(struct db_map *map) 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) { + b_col = calloc((map->w * map->h + 7) / 8, sizeof(*map->p_col)); + if (b_col == NULL) { db_err("Failed to allocate memory"); free(map->p_col); return; @@ -798,7 +807,31 @@ _db_map_set_collision_tiles(struct db_map *map) 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); + b_col[i / 8] |= 1 << (i % 8); + } + } + } + + for (y = 0; y < map->h; ++y) { + for (x = 0; x < map->w; ++x) { + i = y * map->w + x; + if ((b_col[i / 8] & (1 << (i % 8))) != 0) { + if (x > 0 && map->walls[i] != NULL) { + ++map->walls[i - 1].rect.w; + map->walls[i] = map->walls[i - 1]; + continue; + } + map->wall_tail = calloc(1, + sizeof(*map->wall_tail)); + if (map->wall_tail == NULL) { + db_err("Failed to allocate memory"); + free(map->p_col); + free(b_col); + return; + } + if (map->wall_head == NULL) { + map->wall_head = map->wall_tail; + } } } } -- cgit v0.9.1