diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-25 18:42:49 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-25 18:42:49 +0100 |
commit | e6fc32ef086028b787d97797b464e172dfded76f (patch) | |
tree | a8c8593d88fb3060ac70bebcea81da703e602cf6 /main.c | |
parent | 0d25b2b78e213df5beabf429b3ab4081b0ad0751 (diff) | |
download | abelian-sandpile-e6fc32ef086028b787d97797b464e172dfded76f.tar.gz abelian-sandpile-e6fc32ef086028b787d97797b464e172dfded76f.tar.bz2 abelian-sandpile-e6fc32ef086028b787d97797b464e172dfded76f.zip |
(main)+state data structure and iteration logic
Per iteration, increment the centre of the grid, and perform any
collapsing necessary (if >= 4 then sand pile "collapses" onto its
cardinal directions).
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 51 |
1 files changed, 48 insertions, 3 deletions
@@ -3,22 +3,67 @@ * Author: Aryadev Chavali * Description: Entry point of program */ +#include <malloc.h> #include <stdio.h> #include <raylib.h> -#define WIDTH 512 -#define HEIGHT 512 +struct State +{ + // Sandpiles + unsigned char *data; + size_t dwidth; + + size_t window_len; + int multiplier; +}; + +typedef struct State state_t; + +void step(state_t *state) +{ + state->data[((state->dwidth / 2) * state->dwidth) + (state->dwidth / 2)]++; + for (size_t i = 0; i < state->dwidth; ++i) + for (size_t j = 0; j < state->dwidth; ++j) + if (state->data[(i * state->dwidth) + j] >= 4) + { + unsigned char *references[] = { + (j == 0) ? NULL : &state->data[((i)*state->dwidth) + j - 1], + (i == state->dwidth - 1) + ? NULL + : &state->data[((i + 1) * state->dwidth) + j], + (j == state->dwidth - 1) + ? NULL + : &state->data[(i * state->dwidth) + j + 1], + (i == 0) ? NULL : &state->data[((i - 1) * state->dwidth) + j]}; + for (size_t k = 0; k < 4; ++k) + if (references[k]) + *references[k] += 1; + state->data[(i * state->dwidth) + j] = 0; + } +} int main(void) { - InitWindow(WIDTH, HEIGHT, "Abelian sand pile"); + state_t state = {NULL, 256, 512, 0}; + state.data = calloc(state.dwidth * state.dwidth, sizeof(*state.data)); + state.multiplier = state.window_len / state.dwidth; + + InitWindow(state.window_len, state.window_len, "Abelian sand pile"); SetTargetFPS(60); while (!WindowShouldClose()) { + step(&state); BeginDrawing(); ClearBackground(BLACK); DrawText("Hello, world!", 100, 100, 25, RAYWHITE); + + for (size_t i = 0; i < state.dwidth; ++i) + for (size_t j = 0; j < state.dwidth; ++j) + { + DrawRectangle(i * state.multiplier, j * state.multiplier, + state.multiplier, state.multiplier, BLACK); + } EndDrawing(); } CloseWindow(); |