diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | bmp.c | 101 | ||||
| -rw-r--r-- | bmp.h | 11 | ||||
| -rw-r--r-- | main.c | 10 |
5 files changed, 123 insertions, 4 deletions
@@ -1,3 +1,4 @@ astar tags .*.swp +*.bmp @@ -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) @@ -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; +} @@ -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_ */ @@ -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); |
