From fe40ce26ec975c2e2f89409913321064a7876d5c Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Tue, 10 Mar 2026 18:59:23 +0300 Subject: Initial commit --- map.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 map.c (limited to 'map.c') diff --git a/map.c b/map.c new file mode 100644 index 0000000..21023f0 --- /dev/null +++ b/map.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include "map.h" +#include "config.h" +#include "stack.h" + +Map empty_map(size_t width, size_t height) { + Map map = malloc(sizeof(MapTile*) * height); + if (map == NULL) return NULL; + + for (size_t row = 0; row < height; row++) { + map[row] = malloc(sizeof(MapTile) * width); + if (map[row] == NULL) return NULL; + memset(map[row], 0, width*sizeof(MapTile)); + } + + return map; +} + +Map rbt_maze_map(size_t width, size_t height) { + size_t map_width = width * 2 - 1, + map_height = height * 2 - 1; + Map map = empty_map(map_width, map_height); + + // Vertical walls + for (size_t i = 1; i < map_width; i += 2) { + for (size_t j = 0; j < map_height; j += 1) { + map[j][i] = WALL; + } + } + // Horizontal walls + for (size_t i = 1; i < map_height; i += 2) { + for (size_t j = 0; j < map_width; j += 1) { + map[i][j] = WALL; + } + } + + //TODO: Draw the rest of the owl + + return map; +} + +void draw_map(Map map, int width, int height, Position player_pos) { + // Draw field + char c; // The char for the current tile + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + int color_pair = 0; // The color pair of the current char + switch (map[i][j]) { + case EMPTY: + color_pair = COLOR_PAIR(EMPTY_COLOR); + c = EMPTY_CHAR; + break; + case GOAL: + color_pair = COLOR_PAIR(GOAL_COLOR); + c = GOAL_CHAR; + break; + case WALL: + color_pair = COLOR_PAIR(WALL_COLOR); + c = WALL_CHAR; + break; + + } + attron(color_pair); + mvaddch(i + DRAW_MAP_OFFSET_Y, j + DRAW_MAP_OFFSET_X, c); + attroff(color_pair); + } + } + + // Draw the player + attron(COLOR_PAIR(PLAYER_COLOR)); + mvaddch(player_pos.y + DRAW_MAP_OFFSET_Y, player_pos.x + DRAW_MAP_OFFSET_X, PLAYER_CHAR); + attroff(COLOR_PAIR(PLAYER_COLOR)); +} + -- cgit v1.2.3