(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:
31
main.c
31
main.c
@@ -4,8 +4,12 @@
|
|||||||
* Description: Entry point of program
|
* Description: Entry point of program
|
||||||
*/
|
*/
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
|
|
||||||
@@ -17,6 +21,8 @@ struct State
|
|||||||
|
|
||||||
size_t window_len;
|
size_t window_len;
|
||||||
int multiplier;
|
int multiplier;
|
||||||
|
|
||||||
|
bool thread_alive;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct State state_t;
|
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)
|
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.data = calloc(state.dwidth * state.dwidth, sizeof(*state.data));
|
||||||
state.multiplier = state.window_len / state.dwidth;
|
state.multiplier = state.window_len / state.dwidth;
|
||||||
|
|
||||||
Camera2D camera = {0};
|
const float zoom = 0.125f;
|
||||||
camera.zoom = 1.0f;
|
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");
|
InitWindow(state.window_len, state.window_len, "Abelian sand pile");
|
||||||
SetTargetFPS(60);
|
SetTargetFPS(60);
|
||||||
const float zoom = 0.125f;
|
|
||||||
|
|
||||||
while (!WindowShouldClose())
|
while (!WindowShouldClose())
|
||||||
{
|
{
|
||||||
step(&state);
|
|
||||||
|
|
||||||
if (IsKeyPressed(KEY_UP) || IsKeyDown(KEY_UP))
|
if (IsKeyPressed(KEY_UP) || IsKeyDown(KEY_UP))
|
||||||
{
|
{
|
||||||
Vector2 centre = {state.window_len / 2, state.window_len / 2};
|
Vector2 centre = {state.window_len / 2, state.window_len / 2};
|
||||||
@@ -110,6 +126,9 @@ int main(void)
|
|||||||
EndMode2D();
|
EndMode2D();
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (state.thread_alive)
|
||||||
|
pthread_cancel(step_thread);
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user