diff options
| author | Kirill Petrashin <kirill8201@yandex.ru> | 2026-03-16 19:48:24 +0300 |
|---|---|---|
| committer | Kirill Petrashin <kirill8201@yandex.ru> | 2026-03-16 19:48:24 +0300 |
| commit | 5e07b322725c603038647aad873c4da480c7c520 (patch) | |
| tree | 670b46c7365d43c76376893cf87940f8540d47d8 | |
| parent | b1337798203e1ea4ddb7c76e6ca343e904fb19aa (diff) | |
Add ability to move the map around
| -rw-r--r-- | main.c | 14 | ||||
| -rw-r--r-- | map.c | 51 | ||||
| -rw-r--r-- | map.h | 2 |
3 files changed, 48 insertions, 19 deletions
@@ -45,8 +45,18 @@ int main(void) { Map map = rbt_maze_map(width, height, (unsigned int) time(NULL)); Position fake_start_position_to_pass_into_draw_map = {0, 0}; Position fake_goal_position_to_pass_into_draw_map = {width*2-2, height*2-2}; - draw_map(map, width*2-1, height*2-1, fake_start_position_to_pass_into_draw_map, fake_goal_position_to_pass_into_draw_map); - getch(); + int offset_x = 0, offset_y = 0; + while (1) { + draw_map(map, width*2-1, height*2-1, offset_x, offset_y, fake_start_position_to_pass_into_draw_map, fake_goal_position_to_pass_into_draw_map); + char c = getch(); + switch (c) { + case 'h': offset_x -= 2; break; + case 'l': offset_x += 2; break; + case 'j': offset_y += 1; break; + case 'k': offset_y -= 1; break; + case 'q': endwin(); return 0; + } + } endwin(); return 0; @@ -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(); } @@ -35,6 +35,6 @@ unsigned int neighbours(Position neighbour_array[], Position pos, size_t width, Map rbt_maze_map(size_t width, size_t height, unsigned int seed); /* Draw the map. Bet you didn't expect that. */ -void draw_map(Map map, size_t width, size_t height, Position start, Position goal); +void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal); #endif /*MAP_H_ */ |
