aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-03-28 20:19:52 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-03-28 20:19:52 +0300
commitfc29c9bf10ebf471708caecdfed030ec78c915cb (patch)
tree9051000266f524c07f3ee97f3ea943130fc2cf16
parent1f0dd604952e39d030367b2bbf45b69f8c63cc5b (diff)
downloadastar-fc29c9bf10ebf471708caecdfed030ec78c915cb.tar.xz
Add basic BMP output, not sure why
-rw-r--r--.gitignore1
-rw-r--r--Makefile4
-rw-r--r--bmp.c101
-rw-r--r--bmp.h11
-rw-r--r--main.c10
5 files changed, 123 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index bc5c5f9..b0df1f2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
astar
tags
.*.swp
+*.bmp
diff --git a/Makefile b/Makefile
index ae80767..218e81f 100644
--- a/Makefile
+++ b/Makefile
@@ -2,10 +2,10 @@ CC=gcc
EXECUTABLE=astar
astar: *.c *.h
- $(CC) -Wall -Wpedantic -Wextra -Werror -O3 -o $(EXECUTABLE) priority_queue.c map.c stack.c path.c main.c -lncurses
+ $(CC) -Wall -Wpedantic -Wextra -Werror -O3 -o $(EXECUTABLE) priority_queue.c map.c stack.c path.c bmp.c main.c -lncurses
debug: *.c *.h
- $(CC) -Wall -g -o $(EXECUTABLE) priority_queue.c map.c stack.c path.c main.c -lncurses
+ $(CC) -Wall -g -o $(EXECUTABLE) priority_queue.c map.c stack.c path.c bmp.c main.c -lncurses
clean:
rm ./$(EXECUTABLE)
diff --git a/bmp.c b/bmp.c
new file mode 100644
index 0000000..14a8d61
--- /dev/null
+++ b/bmp.c
@@ -0,0 +1,101 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "map.h"
+#include "path.h"
+#include "structs.h"
+#include "priority_queue.h"
+
+/* TODO: clean up, do defines or whatever */
+void map_to_bmp(Map map, size_t map_width, size_t map_height, Position start, Position goal, Path path, char **visited, char *filename) {
+ /* Dimensions of the resulting image */
+ int32_t width = map_width;
+ int32_t height = map_height;
+ uint16_t bitcount = 24;
+
+ /* Width of a single row in bytes */
+ int width_in_bytes = ((width * bitcount + 31) / 32) * 4;
+
+ /* Size in bytes of the image, without the header */
+ uint32_t imagesize = width_in_bytes * height;
+
+ /* Size of the info header */
+ const uint32_t biSize = 40;
+
+ /* Bitmap bit offset */
+ const uint32_t bfOffBits = 54;
+
+ /* Size in bytes of resulting BMP */
+ uint32_t filesize = 54 + imagesize;
+
+ /* No. of planes */
+ const uint16_t biPlanes = 1;
+
+ /* The header */
+ unsigned char header[54] = { 0 };
+ memcpy(header, "BM", 2);
+ memcpy(header + 2 , &filesize, 4);
+ memcpy(header + 10, &bfOffBits, 4);
+ memcpy(header + 14, &biSize, 4);
+ memcpy(header + 18, &width, 4);
+ memcpy(header + 22, &height, 4);
+ memcpy(header + 26, &biPlanes, 2);
+ memcpy(header + 28, &bitcount, 2);
+ memcpy(header + 34, &imagesize, 4);
+
+ /* The buffer for image data */
+ unsigned char* buf = malloc(imagesize);
+
+ /* Draw the walls */
+ for(int row = height - 1; row >= 0; row--)
+ {
+ for(int col = 0; col < width; col++)
+ {
+ if (map[height - 1 - row][col] == WALL) {
+ buf[row * width_in_bytes + col * 3 + 0] = 255; /* b */
+ buf[row * width_in_bytes + col * 3 + 1] = 255; /* g */
+ buf[row * width_in_bytes + col * 3 + 2] = 255; /* r */
+ } else if (visited != NULL && visited[height - 1 - row][col]) {
+ buf[row * width_in_bytes + col * 3 + 0] = 0; /* b */
+ buf[row * width_in_bytes + col * 3 + 1] = 255; /* g */
+ buf[row * width_in_bytes + col * 3 + 2] = 0; /* r */
+ } else {
+ buf[row * width_in_bytes + col * 3 + 0] = 0; /* b */
+ buf[row * width_in_bytes + col * 3 + 1] = 0; /* g */
+ buf[row * width_in_bytes + col * 3 + 2] = 0; /* r */
+ }
+ }
+ }
+
+ /* Draw the path */
+ if (path != NULL) {
+ Position cur = goal;
+ while (cur.x != start.x || cur.y != start.y) {
+ buf[(height - cur.y - 1) * width_in_bytes + cur.x * 3 + 0] = 0; /* b */
+ buf[(height - cur.y - 1) * width_in_bytes + cur.x * 3 + 1] = 0; /* g */
+ buf[(height - cur.y - 1) * width_in_bytes + cur.x * 3 + 2] = 255; /* r */
+ cur = path[cur.y][cur.x].parent;
+ }
+ }
+
+ /* Start */
+ buf[(height - start.y - 1) * width_in_bytes + start.x * 3 + 0] = 255; /* b */
+ buf[(height - start.y - 1) * width_in_bytes + start.x * 3 + 1] = 0; /* g */
+ buf[(height - start.y - 1) * width_in_bytes + start.x * 3 + 2] = 255; /* r */
+
+ /* Goal */
+ buf[(height - goal.y - 1) * width_in_bytes + goal.x * 3 + 0] = 0; /* b */
+ buf[(height - goal.y - 1) * width_in_bytes + goal.x * 3 + 1] = 255; /* g */
+ buf[(height - goal.y - 1) * width_in_bytes + goal.x * 3 + 2] = 255; /* r */
+
+
+ FILE *fout = fopen(filename, "wb");
+ fwrite(header, 1, 54, fout);
+ fwrite((char*)buf, 1, imagesize, fout);
+ fclose(fout);
+ free(buf);
+
+ return;
+}
diff --git a/bmp.h b/bmp.h
new file mode 100644
index 0000000..1f28a0e
--- /dev/null
+++ b/bmp.h
@@ -0,0 +1,11 @@
+#ifndef BMP_H_
+#define BMP_H_
+
+#include "map.h"
+#include "path.h"
+#include "structs.h"
+#include "priority_queue.h"
+
+void map_to_bmp(Map map, size_t map_width, size_t map_height, Position start, Position goal, Path path, char **visited, char *filename);
+
+#endif /* BMP_H_ */
diff --git a/main.c b/main.c
index 442e6eb..5684e14 100644
--- a/main.c
+++ b/main.c
@@ -12,9 +12,9 @@
#include "error.h"
#include "priority_queue.h"
#include "path.h"
+#include "bmp.h"
/* So, TODO for now:
- - Generate an image??
- Allow maps to have costs
- Implement greedy-best-first algorithm (4 and 8 dir)
- Implement Dijkstra algorithm (4 and 8 dir)
@@ -120,7 +120,7 @@ int main(int argc, char **argv) {
int offset_x = 0, offset_y = 0;
- draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL, NULL, NULL, NULL);
+ //draw_map(map, width, height, offset_x, offset_y, start_pos, end_pos, NULL, NULL, NULL, NULL);
//print_map_out(map, width, height);
char **visited = visited_new(width, height);
@@ -162,6 +162,12 @@ int main(int argc, char **argv) {
path = breadth_first_search_path(dirs, map, width, height, start_pos, end_pos, visited, anim);
}
break;
+ case 's':
+ /* TODO: prompt for filename? or do a define idk */
+ map_to_bmp(map, width, height, start_pos, end_pos, path, visited, "out.bmp");
+ mvprintw(height + offset_y + DRAW_MAP_OFFSET_Y + 1, offset_x, "Saved to out.bmp");
+ getch();
+ break;
case 'n':
if (is_maze) {
map_free(map, height);