feat: implemented automatic stepping

This commit is contained in:
Sara Gerretsen 2025-09-22 00:43:15 +02:00
parent 266ff5de0b
commit 8b2b0ac34e

View file

@ -6,13 +6,11 @@
#include <clay/clay.h> #include <clay/clay.h>
namespace application { namespace application {
namespace internal { static bool isSimulating{ false };
bool isSimulating{ false };
}
static void SetSimulatingButton(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) { static void SetSimulatingButton(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
internal::isSimulating = data == 1; isSimulating = data == 1;
} }
} }
@ -29,12 +27,19 @@ static void StepSimulationButton(Clay_ElementId element, Clay_PointerData pointe
} }
static void StepControl() { static void StepControl() {
static uint64_t lastStep = 0;
if (isSimulating) {
double deltaTime{ (double)(SDL_GetTicks() - lastStep) * 0.001 };
if (deltaTime > 0.16) {
simulation::Step();
}
}
CLAY_AUTO_ID({ CLAY_AUTO_ID({
.layout = { .layout = {
.childGap = 16 .childGap = 16
} }
}) { }) {
if (internal::isSimulating) { if (isSimulating) {
elements::TextButton(CLAY_STRING("Pause"), style::warningButton, &SetSimulatingButton, false); elements::TextButton(CLAY_STRING("Pause"), style::warningButton, &SetSimulatingButton, false);
} else { } else {
elements::TextButton(CLAY_STRING("Start"), style::proceedButton, &SetSimulatingButton, true); elements::TextButton(CLAY_STRING("Start"), style::proceedButton, &SetSimulatingButton, true);
@ -61,10 +66,12 @@ static void PrimaryControls() {
} }
}) { }) {
StepControl(); StepControl();
if (!isSimulating) {
elements::TextButton(CLAY_STRING("Randomize"), style::actionButton, &RandomizeFieldButton); elements::TextButton(CLAY_STRING("Randomize"), style::actionButton, &RandomizeFieldButton);
} }
} }
} }
}
} }
Clay_RenderCommandArray RenderApplication() { Clay_RenderCommandArray RenderApplication() {