diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-26 00:50:08 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-26 00:50:08 +0100 |
commit | 1bc283b195fb6f7152a5b98385f019056e95d637 (patch) | |
tree | 573cc21b903e0a9dd5ef0d30af893bf3be30f86b /main.c | |
parent | 0a5d5fc87cc452e9c82ff8ef91662686f9ffe283 (diff) | |
download | abelian-sandpile-1bc283b195fb6f7152a5b98385f019056e95d637.tar.gz abelian-sandpile-1bc283b195fb6f7152a5b98385f019056e95d637.tar.bz2 abelian-sandpile-1bc283b195fb6f7152a5b98385f019056e95d637.zip |
+write to image support
Writes output to "data.png"
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 35 |
1 files changed, 32 insertions, 3 deletions
@@ -13,6 +13,8 @@ #include <raylib.h> #include <raymath.h> +#include <stb/stb_image_write.h> + #include "./lib.h" void step(state_t *state) @@ -101,11 +103,11 @@ int main(void) if (sandpile == 0) c = BLACK; else if (sandpile == 1) - c = GREEN; + c = MAGENTA; else if (sandpile == 2) - c = PURPLE; + c = RED; else if (sandpile == 3) - c = YELLOW; + c = BLUE; DrawRectangle(i * state.multiplier, j * state.multiplier, state.multiplier, state.multiplier, c); @@ -121,6 +123,33 @@ int main(void) } CloseWindow(); + unsigned char *image_data = + calloc(3 * state.dwidth * state.dwidth, sizeof(*image_data)); + + size_t image_ptr = 0; + + for (size_t i = 0; i < state.dwidth; ++i) + for (size_t j = 0; j < state.dwidth; ++j, image_ptr += 3) + { + Color c = {0}; + uint64_t sandpile = state.data[(i * state.dwidth) + j]; + if (sandpile == 0) + c = BLACK; + else if (sandpile == 1) + c = MAGENTA; + else if (sandpile == 2) + c = RED; + else if (sandpile == 3) + c = BLUE; + + image_data[image_ptr] = c.r; + image_data[image_ptr + 1] = c.g; + image_data[image_ptr + 2] = c.b; + } + + stbi_write_png("data.png", state.dwidth, state.dwidth, 3, image_data, + 3 * state.dwidth); + free(image_data); free(state.data); return 0; } |