aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-13 14:27:09 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-13 14:27:09 +0300
commit5069c784436a667048672c8289caddb04242a29f (patch)
tree0a4fd82cf12f715ba7f5cea4cded279dfc0276ee
parent9a4c09683a385bf1ae2e9b67b674640f70fc73e0 (diff)
downloadastar-5069c784436a667048672c8289caddb04242a29f.tar.xz
Further optimize draw_map()
-rw-r--r--map.c71
1 files changed, 38 insertions, 33 deletions
diff --git a/map.c b/map.c
index 40c30b5..b2e6327 100644
--- a/map.c
+++ b/map.c
@@ -220,6 +220,16 @@ 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);
+ /* Rendering boundaries, used to not render stuff that's outside the screen for performance */
+ size_t top_boundary = 0,
+ bottom_boundary = height,
+ left_boundary = 0,
+ right_boundary = width;
+ if (map_offset_y < 0) top_boundary = -map_offset_y;
+ if (map_offset_x < 0) left_boundary = -(map_offset_x / 2);
+ 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, ' ');
@@ -252,15 +262,6 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa
/* Draw field */
if (map != NULL) {
char c = '\0'; /* The char for the current tile */
- /* Rendering boundaries, used to not render stuff that's outside the screen for performance */
- size_t top_boundary = 0,
- bottom_boundary = height,
- left_boundary = 0,
- right_boundary = width;
- if (map_offset_y < 0) top_boundary = -map_offset_y;
- if (map_offset_x < 0) left_boundary = -(map_offset_x / 2);
- if (top_boundary + LINES < height) bottom_boundary = top_boundary + LINES;
- if (left_boundary + COLS/2 < height) right_boundary = left_boundary + COLS/2 + 1;
for (size_t row = top_boundary; row < bottom_boundary; row++) {
for (size_t col = left_boundary; col < right_boundary; col++) {
int color_pair = 0; /* The color pair of the current char */
@@ -318,32 +319,36 @@ void draw_map(Map map, size_t width, size_t height, Position start, Position goa
Position prev = goal;
Position cur = goal;
while (cur.x != start.x || cur.y != start.y) {
- char c1 = ' ', c2 = ' ';
- if (cur.x + 1 - prev.x == 0) { /* To the right */
- if (cur.y == prev.y) {
- c1 = ' '; c2 = '>';
- } else if (cur.y + 1 - prev.y == 0) {
- c1 = ' '; c2 = 'J';
- } else {
- c1 = ' '; c2 = '*';
- }
- } else if (cur.x - prev.x == 1) { /* To the left */
- if (cur.y == prev.y) {
- c1 = '<'; c2 = ' ';
- } else if (cur.y + 1 - prev.y == 0) {
- c1 = 'L'; c2 = ' ';
- } else {
- c1 = 'F'; c2 = ' ';
- }
- } else { /* Purely vertical */
- if (cur.y + 1 - prev.y == 0) {
- c1 = '\\'; c2 = '/';
- } else if (cur.y - prev.y == 1) {
- c1 = '/'; c2 = '\\';
+ if (cur.x >= left_boundary && cur.x < right_boundary\
+ && cur.y >= top_boundary && cur.y < bottom_boundary) {
+ char c1 = ' ', c2 = ' ';
+ if (cur.x + 1 - prev.x == 0) { /* To the right */
+ if (cur.y == prev.y) {
+ c1 = ' '; c2 = '>';
+ } else if (cur.y + 1 - prev.y == 0) {
+ c1 = ' '; c2 = 'J';
+ } else {
+ c1 = ' '; c2 = '*';
+ }
+ } else if (cur.x - prev.x == 1) { /* To the left */
+ if (cur.y == prev.y) {
+ c1 = '<'; c2 = ' ';
+ } else if (cur.y + 1 - prev.y == 0) {
+ c1 = 'L'; c2 = ' ';
+ } else {
+ c1 = 'F'; c2 = ' ';
+ }
+ } else { /* Purely vertical */
+ if (cur.y + 1 - prev.y == 0) {
+ c1 = '\\'; c2 = '/';
+ } else if (cur.y - prev.y == 1) {
+ c1 = '/'; c2 = '\\';
+ }
}
+ mvaddch(cur.y + map_offset_y, cur.x*2 + map_offset_x, c1);
+ mvaddch(cur.y + map_offset_y, cur.x*2 + 1 + map_offset_x, c2);
}
- mvaddch(cur.y + map_offset_y, cur.x*2 + map_offset_x, c1);
- mvaddch(cur.y + map_offset_y, cur.x*2 + 1 + map_offset_x, c2);
+
if (cur.x - path[cur.y][cur.x].parent.x == 0 || cur.y - path[cur.y][cur.x].parent.y == 0) {
length += COST_ORTHOGONAL;
} else {