Shamelessly stole a write_to_png routine from abelian-sandpile

Found and fixed a bug I would've never noticed in abelian sandpile:
the png is technically rotated by 90 using the standard WIDTH then
HEIGHT for loop, so I had to switch them.  As the abelian sandpile
fractal is 4 way symmetric this wasn't noticeable.
This commit is contained in:
2023-09-03 20:00:10 +01:00
parent ab7db03aaa
commit c8ecce9537

21
main.c
View File

@@ -16,6 +16,8 @@
#include <raylib.h>
#include <raymath.h>
#include <stb/stb_image_write.h>
#define WIDTH 1024
#define HEIGHT 1024
#define MAX_THREADS 4
@@ -90,6 +92,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)