feat: added toggle for locked framerate

This commit is contained in:
Sara Gerretsen 2025-09-23 13:04:36 +02:00
parent 17f78ef57d
commit 622fc2cb40

View file

@ -7,19 +7,17 @@
namespace application { namespace application {
static bool isSimulating{ false }; static bool isSimulating{ false };
static bool lockFramerate{ true };
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) {
isSimulating = data == 1; isSimulating = data == 1;
if (isSimulating) {
simulation::drawDebugInfo = false;
}
} }
} }
static void RandomizeFieldButton(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) { static void RandomizeFieldButton(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) {
simulation::InitializeRandom(2, 1000); simulation::InitializeRandom(2, 30);
} }
} }
@ -50,6 +48,7 @@ static void StepControl() {
} }
} }
elements::Toggle(CLAY_STRING("Show Changes"), style::actionButton, simulation::drawDebugInfo); elements::Toggle(CLAY_STRING("Show Changes"), style::actionButton, simulation::drawDebugInfo);
elements::Toggle(CLAY_STRING("Lock Speed"), style::actionButton, lockFramerate);
} }
} }
@ -111,7 +110,7 @@ static void DebugInfoLegend() {
.textColor = style::TextColor(0) .textColor = style::TextColor(0)
}); });
} }
elements::Body(CLAY_STRING("Warning:\nDrawing simulation\nchanges greatly\nlowers performance."), { elements::Body(CLAY_STRING("Warning:\nDrawing simulation changes\ngreatly lowers performance."), {
.textColor = style::TextColor(2) .textColor = style::TextColor(2)
}); });
} }
@ -150,17 +149,20 @@ static void PrimaryControls() {
} }
} }
Clay_RenderCommandArray RenderApplication() { static void TryStep() {
static uint64_t lastStep = 0; static uint64_t lastStep = 0;
if (isSimulating) { if (isSimulating) {
double deltaTime{ (double)(SDL_GetTicks() - lastStep) * 0.001 }; double deltaTime{ (double)(SDL_GetTicks() - lastStep) * 0.001 };
constexpr double targetDeltaTime = 1.0/24.0; constexpr double targetDeltaTime = 1.0/24.0;
if (deltaTime > targetDeltaTime) { if (!lockFramerate || deltaTime > targetDeltaTime) {
lastStep = SDL_GetTicks(); lastStep = SDL_GetTicks();
simulation::Step(); simulation::Step();
} }
} }
}
Clay_RenderCommandArray RenderApplication() {
TryStep();
Clay_BeginLayout(); Clay_BeginLayout();
CLAY(CLAY_ID("OuterContainer"), style::Window()) { CLAY(CLAY_ID("OuterContainer"), style::Window()) {
simulation::SetSimulationHovered(Clay_Hovered()); simulation::SetSimulationHovered(Clay_Hovered());