diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | file-handler.c | 112 | ||||
-rw-r--r-- | files.c | 50 | ||||
-rw-r--r-- | lib.h | 3 | ||||
-rw-r--r-- | main.c | 31 |
5 files changed, 53 insertions, 145 deletions
@@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall -Wextra -pedantic -ggdb -fsanitize=address LIBS=-lm -lraylib -OBJECTS=file-handler.o main.o +OBJECTS=files.o main.o OUT=sandpile.out ARGS= diff --git a/file-handler.c b/file-handler.c deleted file mode 100644 index 760595c..0000000 --- a/file-handler.c +++ /dev/null @@ -1,112 +0,0 @@ -/* file-handler.c - * Created: 2023-08-25 - * Author: Aryadev Chavali - * Description: Implementations of writing and loading state from files - */ - -#include <stdio.h> -#include <string.h> - -#include "./lib.h" - -#define CHUNK_SIZE 1024 - -typedef struct Buffer -{ - char *data; - size_t used, available; -} buffer_t; - -void buffer_init(buffer_t *buffer) -{ - *buffer = - (buffer_t){calloc(CHUNK_SIZE, sizeof(*buffer->data)), 0, CHUNK_SIZE}; -} - -void buffer_realloc(buffer_t *buffer, size_t new_size) -{ - buffer->data = realloc(buffer->data, new_size); - buffer->available = new_size; -} - -void buffer_tighten(buffer_t *buffer) -{ - buffer->data = realloc(buffer->data, buffer->used + 1); - buffer->data[buffer->used] = '\0'; - buffer->available = buffer->used; -} - -#define IS_DIGIT(c) (c <= '9' && c >= '0') - -bool load_from_file(state_t *state, const char *filepath) -{ - // Read file completely - FILE *fp = fopen(filepath, "r"); - if (!fp) - return false; - - buffer_t buffer; - buffer_init(&buffer); - size_t acc = 0; - size_t bytes_read = 0; - while ((bytes_read = fread(buffer.data + acc, sizeof(*buffer.data), - CHUNK_SIZE, fp)) != 0) - { - buffer.used += bytes_read; - acc += bytes_read; - buffer_realloc(&buffer, buffer.available + CHUNK_SIZE); - } - fclose(fp); - buffer_tighten(&buffer); - - // first line is width - state->dwidth = atoi(buffer.data); - state->data = calloc(state->dwidth * state->dwidth, sizeof(*state->data)); - state->multiplier = state->window_len / state->dwidth; - - size_t line_size = strcspn(buffer.data, "\n"); - char *ptr = buffer.data + line_size + 1; - size_t i = 0; - for (ptr = strpbrk(ptr, "\n"); ptr && i < state->dwidth; - ptr = strpbrk(ptr, "\n"), ++i) - { - if (ptr[1] == '\0') - break; - ++ptr; - size_t j = 0; - size_t line_size = strcspn(ptr, "\n"); - for (size_t lptr = 0; lptr < line_size;) - { - size_t end_of_digit = lptr; - for (; IS_DIGIT(ptr[end_of_digit]); ++end_of_digit) - continue; - - state->data[(i * state->dwidth) + j] = atoi(ptr + lptr); - - lptr = end_of_digit + 1; - ++j; - } - ++ptr; - } - - free(buffer.data); - return true; -} - -bool write_to_file(state_t *state, const char *filepath) -{ - FILE *fp = fopen(filepath, "w"); - fprintf(fp, "%lu\n", state->dwidth); - for (size_t i = 0; i < state->dwidth; ++i) - { - for (size_t j = 0; j < state->dwidth; ++j) - { - fprintf(fp, "%d", state->data[(i * state->dwidth) + j]); - if (j != state->dwidth - 1) - fprintf(fp, ","); - } - fprintf(fp, "\n"); - } - fclose(fp); - return true; -} @@ -0,0 +1,50 @@ +/* file-handler.c + * Created: 2023-08-25 + * Author: Aryadev Chavali + * Description: Implementations of writing and loading state->from files + */ + +#include <stdio.h> +#include <string.h> + +#include <stb/stb_image_write.h> + +#include "./lib.h" + +#define CHUNK_SIZE 1024 + +bool write_to_png(state_t *state, const char *filepath) +{ + 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) + { + unsigned char colour[3] = {0}; + uint64_t sandpile = state->data[(i * state->dwidth) + j]; + if (sandpile == 0) + colour[0] = colour[1] = colour[2] = 0; + else if (sandpile == 1) + colour[0] = colour[2] = 255; + else if (sandpile == 2) + { + colour[0] = 255; + colour[1] = colour[2] = 20; + } + else if (sandpile == 3) + { + colour[2] = 255; + colour[0] = colour[1] = 20; + } + + memcpy(image_data + image_ptr, colour, sizeof(*colour) * 3); + } + + stbi_write_png(filepath, state->dwidth, state->dwidth, 3, image_data, + 3 * state->dwidth); + free(image_data); + return true; +} @@ -18,7 +18,6 @@ typedef struct State uint64_t payload; } state_t; -bool load_from_file(state_t *state, const char *filepath); -bool write_to_file(state_t *state, const char *filepath); +bool write_to_png(state_t *state, const char *filepath); #endif @@ -13,8 +13,6 @@ #include <raylib.h> #include <raymath.h> -#include <stb/stb_image_write.h> - #include "./lib.h" struct StepArg @@ -150,34 +148,7 @@ 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); + write_to_png(&state, "data.png"); free(state.data); return 0; } |