From 5e07b322725c603038647aad873c4da480c7c520 Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Mon, 16 Mar 2026 19:48:24 +0300 Subject: Add ability to move the map around --- map.c | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) (limited to 'map.c') diff --git a/map.c b/map.c index 90ca91c..fbe2577 100644 --- a/map.c +++ b/map.c @@ -93,18 +93,35 @@ Map rbt_maze_map(size_t width, size_t height, unsigned int seed) { return map; } -void draw_map(Map map, size_t width, size_t height, Position start, Position goal) { - /* Draw the walls around the map (they are not in map) */ +/* FIXME: I don't think we need DRAW_MAP_OFFSET_{X,Y} anymore + * Although, at the same time, it does make less magic values, so I guess it's fine */ +void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal) { + /* I think it flickers less when we do that */ + wnoutrefresh(stdscr); + + /* Clean up the area around the map */ + for (ssize_t i = -1; i <= (ssize_t)(width*2 + 4); i++) { /* Horizontal */ + mvaddch(DRAW_MAP_OFFSET_Y - 1 + offset_y - 1, i + offset_x, ' '); + mvaddch(DRAW_MAP_OFFSET_Y + height + offset_y + 1, i + offset_x, ' '); + } + for (size_t i = 0; i <= height + 2; i++) { /* Horizontal */ + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 2 + offset_x - 2, ' '); + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 1 + offset_x - 2, ' '); + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + offset_x + 2, ' '); + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + 1 + offset_x + 2, ' '); + } + + /* Draw the borders*/ attron(COLOR_PAIR(WALL_COLOR)); - for (size_t i = 0; i <= width*2 + 3; i++) { - mvaddch(DRAW_MAP_OFFSET_X - 2, i, WALL_CHAR); - mvaddch(DRAW_MAP_OFFSET_Y + height, i, WALL_CHAR); + for (size_t i = 0; i <= width*2 + 3; i++) { /* Horizontal */ + mvaddch(DRAW_MAP_OFFSET_Y - 1 + offset_y, i + offset_x, WALL_CHAR); + mvaddch(DRAW_MAP_OFFSET_Y + height + offset_y, i + offset_x, WALL_CHAR); } - for (size_t i = 1; i <= height; i++) { - mvaddch(i, DRAW_MAP_OFFSET_X - 2, WALL_CHAR); - mvaddch(i, DRAW_MAP_OFFSET_X - 1, WALL_CHAR); - mvaddch(i, DRAW_MAP_OFFSET_X + width * 2, WALL_CHAR); - mvaddch(i, DRAW_MAP_OFFSET_X + width * 2 + 1, WALL_CHAR); + for (size_t i = 1; i <= height; i++) { /* Vertical */ + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 2 + offset_x, WALL_CHAR); + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X - 1 + offset_x, WALL_CHAR); + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + offset_x, WALL_CHAR); + mvaddch(i + offset_y, DRAW_MAP_OFFSET_X + width * 2 + 1 + offset_x, WALL_CHAR); } attroff(COLOR_PAIR(WALL_COLOR)); @@ -127,8 +144,8 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa attron(color_pair); /* We draw two characters because they roughly make a square together. * It looks WAY better if we do this. */ - mvaddch(i + DRAW_MAP_OFFSET_Y, j*2 + DRAW_MAP_OFFSET_X, c); - mvaddch(i + DRAW_MAP_OFFSET_Y, j*2 + DRAW_MAP_OFFSET_X + 1, c); + mvaddch(i + DRAW_MAP_OFFSET_Y + offset_y, j*2 + DRAW_MAP_OFFSET_X + offset_x, c); + mvaddch(i + DRAW_MAP_OFFSET_Y + offset_y, j*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, c); attroff(color_pair); } } @@ -136,15 +153,17 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa /* Draw the start */ attron(A_BOLD); attron(COLOR_PAIR(START_COLOR)); - mvaddch(start.y + DRAW_MAP_OFFSET_Y, start.x*2 + DRAW_MAP_OFFSET_X, START_CHAR_1); - mvaddch(start.y + DRAW_MAP_OFFSET_Y, start.x*2 + DRAW_MAP_OFFSET_X + 1, START_CHAR_2); + mvaddch(start.y + DRAW_MAP_OFFSET_Y + offset_y, start.x*2 + DRAW_MAP_OFFSET_X + offset_x, START_CHAR_1); + mvaddch(start.y + DRAW_MAP_OFFSET_Y + offset_y, start.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, START_CHAR_2); attroff(COLOR_PAIR(START_COLOR)); /* Draw the goal */ attron(COLOR_PAIR(GOAL_COLOR)); - mvaddch(goal.y + DRAW_MAP_OFFSET_Y, goal.x*2 + DRAW_MAP_OFFSET_X, GOAL_CHAR_1); - mvaddch(goal.y + DRAW_MAP_OFFSET_Y, goal.x*2 + DRAW_MAP_OFFSET_X + 1, GOAL_CHAR_2); + mvaddch(goal.y + DRAW_MAP_OFFSET_Y + offset_y, goal.x*2 + DRAW_MAP_OFFSET_X + offset_x, GOAL_CHAR_1); + mvaddch(goal.y + DRAW_MAP_OFFSET_Y + offset_y, goal.x*2 + DRAW_MAP_OFFSET_X + 1 + offset_x, GOAL_CHAR_2); attroff(COLOR_PAIR(GOAL_COLOR)); attroff(A_BOLD); + + doupdate(); } -- cgit v1.2.3