diff options
-rw-r--r-- | main.c | 24 |
1 files changed, 23 insertions, 1 deletions
@@ -16,6 +16,9 @@ #include <raylib.h> #include <raymath.h> +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include <stb/stb_image_write.h> + #define WIDTH 1024 #define HEIGHT 1024 #define MAX_THREADS 4 @@ -90,6 +93,25 @@ void threads_cancel_render(void) pthread_join(threads[i], NULL); } +bool write_to_png(const char *filepath) +{ + unsigned char *image_data = calloc(3 * WIDTH * HEIGHT, sizeof(*image_data)); + + size_t image_ptr = 0; + + for (size_t i = 0; i < HEIGHT; ++i) + for (size_t j = 0; j < WIDTH; ++j, image_ptr += 3) + { + Color c = cells[(j * WIDTH) + i]; + unsigned char colour[3] = {c.r, c.g, c.b}; + memcpy(image_data + image_ptr, colour, sizeof(*colour) * 3); + } + + stbi_write_png(filepath, WIDTH, HEIGHT, 3, image_data, 3 * WIDTH); + free(image_data); + return true; +} + #define ZOOM_INC 0.3f #define MOVE_INC WIDTH / 50 int main(void) @@ -141,7 +163,7 @@ int main(void) else if (IsKeyPressed(KEY_SPACE)) { threads_cancel_render(); - memset(cells, 0, WIDTH * HEIGHT); + memset(cells, 0, sizeof(cells[0]) * WIDTH * HEIGHT); MAX_ITER *= 2; rendered = false; threads_start_render(args); |