(main)+threading for steps

Doing this experiment with threads shows a strong improvement in
render speed by separating "steps" from the direct render.
This commit is contained in:
2023-08-25 18:59:03 +01:00
parent 985a883fda
commit 62647556fa

31
main.c
View File

@@ -4,8 +4,12 @@
* Description: Entry point of program
*/
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <raylib.h>
#include <raymath.h>
@@ -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;
}