aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-03-14 21:00:51 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-03-14 21:00:51 +0300
commitd0b1a2ab1a6f5c004d675d700181b16385a30b04 (patch)
tree1da501dd9a52654e246cde8969e48b026a8adb47
parent8d165ccf784dd7a3afe35b68339f2b536591362c (diff)
Improve rendering
-rw-r--r--config.h14
-rw-r--r--main.c19
-rw-r--r--map.c38
-rw-r--r--map.h3
4 files changed, 50 insertions, 24 deletions
diff --git a/config.h b/config.h
index 3eb931d..096fd32 100644
--- a/config.h
+++ b/config.h
@@ -1,13 +1,17 @@
#ifndef CONFIG_H_
#define CONFIG_H_
-// The characters that represent different tiles
-#define EMPTY_CHAR '.'
-#define GOAL_CHAR 'Z'
+/* The characters that represent different tiles.
+ * Some have two characters -- that's because of the rendering trick where we
+ * use two characters back-to-back so they look like a square. */
+#define EMPTY_CHAR ' '
+#define GOAL_CHAR_1 'G'
+#define GOAL_CHAR_2 'L'
#define WALL_CHAR '#'
-#define START_CHAR 'A'
+#define START_CHAR_1 'S'
+#define START_CHAR_2 'T'
-#define DRAW_MAP_OFFSET_X 1
+#define DRAW_MAP_OFFSET_X 2
#define DRAW_MAP_OFFSET_Y 1
#endif //CONFIG_H_
diff --git a/main.c b/main.c
index bfa19f5..5348807 100644
--- a/main.c
+++ b/main.c
@@ -6,10 +6,11 @@
#include "map.h"
#include "structs.h"
-// So, TODO for now:
-// - Implement the A* algorithm
-// - Implement adding maps from files (with rle, preferably)
-// - Implement controls (to change maps, move start/goal, etc.)
+/* So, TODO for now:
+ - Implement the A* algorithm
+ - Implement adding maps from files (with rle, preferably)
+ - Implement controls (to change maps, move start/goal, etc.)
+ - Render goal and start in bold */
void sigint_handler(int sig) {
(void)sig; // We know it's a SIGINT
@@ -22,8 +23,9 @@ void initialize_colors(void) {
start_color();
use_default_colors();
init_pair(EMPTY_COLOR, COLOR_BLACK, -1);
- init_pair(GOAL_COLOR, COLOR_CYAN, -1);
- init_pair(WALL_COLOR, COLOR_WHITE, -1);
+ init_pair(GOAL_COLOR, COLOR_RED, -1);
+ init_pair(WALL_COLOR, COLOR_WHITE, COLOR_WHITE); /* Using white as bg makes
+ them seem solid */
init_pair(START_COLOR, COLOR_RED, -1);
}
@@ -37,8 +39,9 @@ int main(void) {
initialize_colors();
Map map = rbt_maze_map(20, 10, (unsigned int) time(NULL));
- Position fake_start_position_to_pass_into_draw_map = {11, 21};
- draw_map(map, 20*2-1, 10*2-1, fake_start_position_to_pass_into_draw_map);
+ Position fake_start_position_to_pass_into_draw_map = {0, 0};
+ Position fake_goal_position_to_pass_into_draw_map = {20*2-2, 10*2-2};
+ draw_map(map, 20*2-1, 10*2-1, fake_start_position_to_pass_into_draw_map, fake_goal_position_to_pass_into_draw_map);
getch();
endwin();
diff --git a/map.c b/map.c
index f83269f..b0dbb2a 100644
--- a/map.c
+++ b/map.c
@@ -94,21 +94,31 @@ Map rbt_maze_map(size_t width, size_t height, unsigned int seed) {
return map;
}
-void draw_map(Map map, int width, int height, Position start) {
+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)
+ 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 = 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);
+ }
+ attroff(COLOR_PAIR(WALL_COLOR));
+
// Draw field
char c; // The char for the current tile
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
+ for (size_t i = 0; i < height; i++) {
+ for (size_t 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;
@@ -116,14 +126,24 @@ void draw_map(Map map, int width, int height, Position start) {
}
attron(color_pair);
- mvaddch(i + DRAW_MAP_OFFSET_Y, j + DRAW_MAP_OFFSET_X, c);
+ /* 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);
attroff(color_pair);
}
}
// Draw the start
attron(COLOR_PAIR(START_COLOR));
- mvaddch(start.y + DRAW_MAP_OFFSET_Y, start.x + DRAW_MAP_OFFSET_X, START_CHAR);
+ 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);
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);
+ attroff(COLOR_PAIR(GOAL_COLOR));
}
diff --git a/map.h b/map.h
index b06da7c..7cea584 100644
--- a/map.h
+++ b/map.h
@@ -6,7 +6,6 @@
enum MapTile_e {
EMPTY = 0,
- GOAL,
WALL,
};
@@ -36,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, int width, int height, Position start);
+void draw_map(Map map, size_t width, size_t height, Position start, Position goal);
#endif //MAP_H_