main: parallelise mutation and iteration

Construct threads which mutate and iterate the simulation.  This makes
them not dependent on the Target FPS of the main thread.
This commit is contained in:
2026-03-18 09:53:44 +00:00
parent deec79a3bf
commit bd2d51b57c
2 changed files with 65 additions and 9 deletions

View File

@@ -8,14 +8,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <threads.h>
#include <time.h> #include <time.h>
#include "base.h" #include "base.h"
#include "sv.h"
#include "vec.h"
#include "bf.h" #include "bf.h"
#include "simulation.h" #include "simulation.h"
#include "sv.h"
#include "vec.h"
void simulation_soup(simulation_t *sim) void simulation_soup(simulation_t *sim)
{ {
@@ -25,22 +25,78 @@ void simulation_soup(simulation_t *sim)
} }
} }
struct ThreadState
{
simulation_t *simulation;
bool paused, done;
};
int thread_simulation_iterate(void *ptr)
{
struct ThreadState *state = ptr;
while (!state->done)
{
while (state->paused && !state->done)
{
}
simulation_iterate(state->simulation);
}
return 0;
}
int thread_simulation_mutate(void *ptr)
{
struct ThreadState *state = ptr;
while (!state->done)
{
while (state->paused && !state->done)
{
}
simulation_mutate(state->simulation);
}
return 0;
}
int main(void) int main(void)
{ {
simulation_t sim = {0}; simulation_t sim = {0};
simulation_soup(&sim); simulation_soup(&sim);
srand(time(NULL)); srand(time(NULL));
thrd_t thread_iterator, thread_mutator;
struct ThreadState state_iterator = {
.simulation = &sim,
.paused = true,
.done = false,
};
struct ThreadState state_mutator = {
.simulation = &sim,
.paused = true,
.done = false,
};
thrd_create(&thread_iterator, thread_simulation_iterate, &state_iterator);
thrd_create(&thread_mutator, thread_simulation_mutate, &state_mutator);
InitWindow(WIDTH, HEIGHT, "CompLife"); InitWindow(WIDTH, HEIGHT, "CompLife");
SetTargetFPS(60); SetTargetFPS(60);
for (size_t ticks = 0; !WindowShouldClose(); ++ticks) for (size_t ticks = 0; !WindowShouldClose(); ++ticks)
{ {
simulation_iterate(&sim); if (IsKeyPressed(KEY_SPACE))
{
state_iterator.paused = !state_iterator.paused;
state_mutator.paused = !state_mutator.paused;
}
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
simulation_draw(&sim); simulation_draw(&sim);
DrawFPS(0, 0);
EndDrawing(); EndDrawing();
} }
state_iterator.done = true;
state_mutator.done = true;
thrd_join(thread_iterator, NULL);
thrd_join(thread_mutator, NULL);
CloseWindow(); CloseWindow();
return 0; return 0;
} }

View File

@@ -96,11 +96,11 @@ void simulation_draw(simulation_t *sim)
(Vector2){CELL_WIDTH, CELL_HEIGHT}, color); (Vector2){CELL_WIDTH, CELL_HEIGHT}, color);
} }
DrawRectangleLinesEx((Rectangle){.x = s_x * CELL_WIDTH, // DrawRectangleLinesEx((Rectangle){.x = s_x * CELL_WIDTH,
.y = s_y * CELL_HEIGHT, // .y = s_y * CELL_HEIGHT,
.width = CELL_WIDTH * SIMULATION_ROW, // .width = CELL_WIDTH * SIMULATION_ROW,
.height = CELL_HEIGHT * SIMULATION_ROW}, // .height = CELL_HEIGHT * SIMULATION_ROW},
1, WHITE); // 1, WHITE);
} }
} }