From 62647556fadb5407a8b144c5b3067772288b458f Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 25 Aug 2023 18:59:03 +0100 Subject: (main)+threading for steps Doing this experiment with threads shows a strong improvement in render speed by separating "steps" from the direct render. --- main.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 9edb8bf..c20662d 100644 --- a/main.c +++ b/main.c @@ -4,8 +4,12 @@ * Description: Entry point of program */ #include +#include #include +#include +#include + #include #include @@ -17,6 +21,8 @@ struct State size_t window_len; int multiplier; + + bool thread_alive; }; typedef struct State state_t; @@ -44,22 +50,32 @@ void step(state_t *state) } } +void *compute_thread(void *input) +{ + state_t *state = input; + while (state->thread_alive) + step(state); + return NULL; +} + int main(void) { - state_t state = {NULL, 256, 512, 0}; + state_t state = {NULL, 512, 512, 0, true}; state.data = calloc(state.dwidth * state.dwidth, sizeof(*state.data)); state.multiplier = state.window_len / state.dwidth; - Camera2D camera = {0}; - camera.zoom = 1.0f; + const float zoom = 0.125f; + Camera2D camera = {0}; + camera.zoom = 1.0f; + + pthread_t step_thread; + pthread_create(&step_thread, NULL, &compute_thread, &state); + InitWindow(state.window_len, state.window_len, "Abelian sand pile"); SetTargetFPS(60); - const float zoom = 0.125f; while (!WindowShouldClose()) { - step(&state); - if (IsKeyPressed(KEY_UP) || IsKeyDown(KEY_UP)) { Vector2 centre = {state.window_len / 2, state.window_len / 2}; @@ -110,6 +126,9 @@ int main(void) EndMode2D(); EndDrawing(); } + + if (state.thread_alive) + pthread_cancel(step_thread); CloseWindow(); return 0; } -- cgit v1.2.3-13-gbd6f