From b7bab0bc3c2c82d5f356ae525397396a63db6cdf Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Mon, 29 Jun 2026 18:40:49 +0300 Subject: Map creation --- map.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ map.h | 7 +++++++ 2 files changed, 69 insertions(+) create mode 100644 map.c diff --git a/map.c b/map.c new file mode 100644 index 0000000..3d81c00 --- /dev/null +++ b/map.c @@ -0,0 +1,62 @@ +#include "map.h" + +#include + +Map map_new(size_t width, size_t height) { + Map map; + map.width = 0; + map.height = 0; + + map.tiles = malloc(sizeof(Pixel *) * height); + if (map.tiles == NULL) return map; + for (size_t row = 0; row < height; row++) { + map.tiles[row] = malloc(sizeof(Pixel *) * width); + if (map.tiles[row] == NULL) { + /* FIXME: free prev rows */ + map.tiles = NULL; + return map; + } + for (size_t col = 0; col < width; col++) { + map.tiles[row][col] = P_EMPTY; + } + } + + map.width = width; + map.height = height; + + return map; +} + +void map_free(Map *map) { + if (map->tiles == NULL) goto null; + + for (size_t row = 0; row < map->height; row++) + free(map->tiles[row]); + +null: + map->height = 0; + map->width = 0; + return; +} + +Map map_default(void) { + /* haha map map map */ + Map map = map_new(20, 30); + + /* Boundaries */ + for (size_t x = 0; x < map.width; x++) { /* Horizontal */ + map.tiles[0][x] = P_MAGENTA; + map.tiles[map.height - 1][x] = P_RED; + } + for (size_t y = 0; y < map.height; y++) { /* Vertical */ + map.tiles[y][0] = P_MAGENTA; + map.tiles[y][map.width - 1] = P_RED; + } + + /* Some random pillars */ + map.tiles[5][8] = P_GREEN; + map.tiles[20][4] = P_GREEN; + map.tiles[9][11] = P_GREEN; + + return map; +} diff --git a/map.h b/map.h index b724c93..141f74c 100644 --- a/map.h +++ b/map.h @@ -3,10 +3,17 @@ #include "framebuffer.h" +/* Used to represent an empty tile */ +#define P_EMPTY (Pixel){C_BLACK, C_BLACK, '\0'} + /* A structure representing a map. */ typedef struct Map_s { size_t width, height; Pixel **tiles; /* used as tiles[row][col] or tiles[y][x] */ } Map; +Map map_new(size_t width, size_t height); +void map_free(Map *map); +Map map_default(void); + #endif /* MAP_H_ */ -- cgit v1.2.3