From f0b6bb6974c823c4b5a7deffce0748f2ff6370de Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Tue, 14 Apr 2026 18:25:30 +0300 Subject: Change how messages are written, clean up differently --- bmp.c | 6 ++++-- config.h | 2 ++ main.c | 7 ++++--- map.c | 42 ++++++++++++++++++------------------------ map.h | 12 +++++++----- path.c | 4 ++-- 6 files changed, 37 insertions(+), 36 deletions(-) diff --git a/bmp.c b/bmp.c index 33dd7dc..739c2ad 100644 --- a/bmp.c +++ b/bmp.c @@ -94,7 +94,8 @@ void map_to_bmp(Map map, size_t map_width, size_t map_height, Position start, Po FILE *fout = fopen(filename, "wb"); if (fout == NULL) { - message(height, "Couldn't open %s for writing", filename); + set_message("Couldn't open %s for writing", filename); + print_message(height); return; } fwrite(header, 1, 54, fout); @@ -102,6 +103,7 @@ void map_to_bmp(Map map, size_t map_width, size_t map_height, Position start, Po fclose(fout); free(buf); - message(height, "Saved to %s", filename); + set_message("Saved to %s", filename); + print_message(height); return; } diff --git a/config.h b/config.h index f435d7d..391be5f 100644 --- a/config.h +++ b/config.h @@ -18,6 +18,8 @@ #define CURSOR_CHAR_1 '<' #define CURSOR_CHAR_2 '>' +#define MESSAGE_MAX_SIZE 128 + #define FILENAME_BUF_SIZE 128 #define FILENAME_PROMPT "Filename:" diff --git a/main.c b/main.c index 3ebc655..cac6a85 100644 --- a/main.c +++ b/main.c @@ -192,10 +192,10 @@ int main(int argc, char **argv) { case 'i': if(is_maze) { switch (c) { - case 'y': if (mwidth > 1) mwidth -= 1; break; + case 'y': if (mwidth > 2) mwidth -= 1; break; case 'o': mwidth += 1; break; case 'u': mheight += 1; break; - case 'i': if (mheight > 1) mheight -= 1; break; + case 'i': if (mheight > 2) mheight -= 1; break; } map_free(map, height); visited_free(visited, height); @@ -216,7 +216,8 @@ int main(int argc, char **argv) { curs_set(2); /* Show the cursor */ echo(); /* Echo characters */ - message(height, FILENAME_PROMPT); + set_message(FILENAME_PROMPT); + print_message(height); char filename[FILENAME_BUF_SIZE] = "out.bmp"; mvgetnstr(height + map_offset_y + 1, map_offset_x - 2 + sizeof(FILENAME_PROMPT), filename, FILENAME_BUF_SIZE - 1); diff --git a/map.c b/map.c index ce8f949..035623a 100644 --- a/map.c +++ b/map.c @@ -12,6 +12,7 @@ int map_offset_x = 2; int map_offset_y = 1; +char message[MESSAGE_MAX_SIZE] = ""; Map empty_map(size_t width, size_t height) { Map map = malloc(sizeof(MapTile*) * height); @@ -214,7 +215,8 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position * void map_to_file_plaintext(char *filename, Map map, size_t width, size_t height, Position start, Position end) { FILE *file = fopen(filename, "w"); if (file == NULL) { - message(height, "Couldn't open %s for writing", filename); + set_message("Couldn't open %s for writing", filename); + print_message(height); return; } @@ -232,7 +234,8 @@ void map_to_file_plaintext(char *filename, Map map, size_t width, size_t height, putc('\n', file); } - message(height, "Saved the map to %s", filename); + set_message("Saved the map to %s", filename); + print_message(height); return; } @@ -245,8 +248,7 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa * UPD: it was causing a bug, so I commented it out. I don't know why either */ //wnoutrefresh(stdscr); - /* Clear the message line */ - if (move(height + map_offset_y + 1, map_offset_x - 2) != ERR) { clrtoeol(); }; + erase(); /* Rendering boundaries, used to not render stuff that's outside the screen for performance */ size_t top_boundary = 0, @@ -258,22 +260,6 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa if (top_boundary + LINES < height) bottom_boundary = top_boundary + LINES; if (left_boundary + COLS/2 < height) right_boundary = left_boundary + COLS/2 + 1; - /* Clean up the area around the map */ - for (ssize_t i = -1; i <= (ssize_t)(width*2 + 4); i++) { /* Horizontal */ - mvaddch(map_offset_y - 2, i + map_offset_x - 2, ' '); - mvaddch(height + map_offset_y + 1, i + map_offset_x - 2, ' '); - mvaddch(height + map_offset_y + 2, i + map_offset_x - 2, ' '); - mvaddch(height + map_offset_y + 3, i + map_offset_x - 2, ' '); - } - for (size_t i = 0; i <= height + 2; i++) { /* Vertical */ - mvaddch(i + map_offset_y - 1, map_offset_x - 4, ' '); - mvaddch(i + map_offset_y - 1, map_offset_x - 3, ' '); - mvaddch(i + map_offset_y - 1, width * 2 + 0 + map_offset_x + 2, ' '); - mvaddch(i + map_offset_y - 1, width * 2 + 1 + map_offset_x + 2, ' '); - mvaddch(i + map_offset_y - 1, width * 2 + 2 + map_offset_x + 2, ' '); - mvaddch(i + map_offset_y - 1, width * 2 + 3 + map_offset_x + 2, ' '); - } - /* Draw the borders */ attron(COLOR_PAIR(WALL_COLOR)); for (size_t i = 0; i <= width*2 + 3; i++) { /* Horizontal */ @@ -387,7 +373,7 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa cur = path[cur.y][cur.x].parent; } attroff(COLOR_PAIR(PATH_COLOR)); - message(height, "Path cost: %zu", length); + /* FIXME: print this as part of bottom wall? set_message("Path cost: %zu", length); */ } /* Draw the start */ @@ -412,10 +398,16 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa } attroff(A_BOLD); + print_message(height); + /* Read the comment at the start of this function */ //doupdate(); } +void print_message(size_t height) { + mvaddnstr(height + map_offset_y + 1, map_offset_x - 2, message, MESSAGE_MAX_SIZE); +} + void map_free(Map map, size_t height) { if (map == NULL) return; for (size_t i = 0; i < height; i++) { @@ -439,7 +431,8 @@ void print_map_out(Map map, size_t width, size_t height) { void map_editor(Map *map, size_t *width, size_t *height, Position *start, Position *goal) { clear(); draw_map(*map, *width, *height, *start, *goal, NULL, NULL, NULL, NULL); - message(*height, "You've entered the map editor. 'q' to quit"); + set_message("You've entered the map editor. 'q' to quit"); + print_message(*height); MEVENT event; mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL); @@ -451,7 +444,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi while (1) { int ch = getch(); - if (ch == 'q') break; + if (ch == 'q') {clear_message(); break; } /* Handle keyboard keys */ switch (ch) { case 'h': map_offset_x += 2; break; @@ -485,7 +478,8 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi curs_set(2); /* Show the cursor */ echo(); /* Echo characters */ - message(*height, FILENAME_PROMPT); + set_message(FILENAME_PROMPT); + print_message(*height); char filename[FILENAME_BUF_SIZE] = "map"; mvgetnstr(*height + map_offset_y + 1, map_offset_x - 2 + sizeof(FILENAME_PROMPT), filename, FILENAME_BUF_SIZE - 1); diff --git a/map.h b/map.h index b2b3ff4..23a8773 100644 --- a/map.h +++ b/map.h @@ -3,16 +3,16 @@ #include #include "structs.h" +#include "config.h" #include "path.h" #include "priority_queue.h" -/* Prints a message at the line right below the map. Multiline messages are undefined behaviour */ -#define message(height, ...) { \ - if (move((height) + map_offset_y + 1, map_offset_x - 2) != ERR) { clrtoeol(); /* Clear the line */\ - mvprintw((height) + map_offset_y + 1, map_offset_x - 2, __VA_ARGS__); }} - extern int map_offset_x; extern int map_offset_y; +extern char message[MESSAGE_MAX_SIZE]; + +#define set_message(...) snprintf(message, MESSAGE_MAX_SIZE, __VA_ARGS__); +#define clear_message() message[0] = 0; /* Returns an empty map of given size */ Map empty_map(size_t width, size_t height); @@ -53,6 +53,8 @@ void map_to_file_plaintext(char *filename, Map map, size_t width, size_t height, * path could be NULL to draw a map with no path. So can cursor, frontier and visited */ void draw_map(Map map, size_t width, size_t height, Position start, Position goal, Position *cursor, Path path, char **visited, PositionPQ *frontier); +void print_message(size_t height); + /* Frees all the memory reserved for the map */ void map_free(Map map, size_t height); diff --git a/path.c b/path.c index e216d8a..fd92908 100644 --- a/path.c +++ b/path.c @@ -18,7 +18,7 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos static char automatic = 0; while (1) { draw_map(map, width, height, start, end, cur, NULL, visited, frontier); - message(height, "cur: %zu %zu", cur->x, cur->y); + set_message("cur: %zu %zu", cur->x, cur->y); print_message(height); if (automatic) { wrefresh(stdscr); usleep(ANIM_DELAY_USEC); return 0; } switch (getch()) { case 'h': map_offset_x += 2; break; @@ -52,7 +52,7 @@ int anim(Map map, size_t width, size_t height, Position start, Position end, Pos /* TODO: Add a binding to do a bmp */ case 'a': automatic = 1; break; - case 'q': return -1; + case 'q': clear_message(); return -1; default: return 0; } } -- cgit v1.2.3