emulator-template: Pace host at ~60 fps, convert sample demo to delta-time

The host busy-spun loop() with no frame limiting, so every frame-based
animation ran at desktop fps (hundreds+). Add SDL_Delay-based pacing to
the 60 Hz contract, and switch the bundled Demo to delta-time updates
(velocities in px/s scaled by measured dt) so speed is correct at any
frame rate.
This commit is contained in:
Dejvino
2026-11-16 20:47:11 +01:00
parent a243f303f6
commit 6fdfa8797e
2 changed files with 38 additions and 8 deletions
+18
View File
@@ -109,6 +109,12 @@ int main(int argc, char **argv)
// --- Main loop ---
bool running = true;
// Frame pacing: the sketch contract is "loop() called once per frame
// (~60 fps)". Without pacing this loop busy-spins at hundreds/thousands
// of fps on a desktop and every frame-based animation runs too fast,
// so we delay until the next 60 Hz tick before starting the next frame.
const Uint64 FRAME_MS = 1000 / 60; // ~16.7 ms per frame
Uint64 nextFrame = SDL_GetTicks() + FRAME_MS;
while (running && !emu::quitRequested) {
// --- Event pump ---
SDL_Event e;
@@ -192,6 +198,18 @@ int main(int argc, char **argv)
}
}
}
// --- Frame pacing: sleep until the next 60 Hz tick ---
Uint64 nowMs = SDL_GetTicks();
if (nowMs >= nextFrame) {
// Behind schedule (a slow frame or a stall): resync instead of
// burst-catching-up, so animation never runs fast to "make up"
// lost frames.
nextFrame = nowMs;
} else {
SDL_Delay((Uint32)(nextFrame - nowMs));
}
nextFrame += FRAME_MS;
}
// --- Save last frame as BMP for AI inspection ---