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
+20 -8
View File
@@ -3,11 +3,16 @@
*
* Contract with the emulator host (src/main.cpp):
* - implement setup() — called once at launch
* - implement loop() — called once per frame (~60 fps)
* - implement loop() — called once per frame (host paces at ~60 fps)
* - write pixels into emuGfx::fb[] (320x240, ABGR8888 uint32)
* - set emuGfx::dirty = true each frame you want presented
* - read keys from emu::keyboard (ASCII queue, filled from SDL events)
*
* Update state with DELTA TIME, not per-frame constants: measure the real
* seconds since the previous loop() call (see loop() below) and scale all
* speeds in "units per second" by it. Then the sketch runs at the same
* speed no matter what fps the host (or the real hardware) manages.
*
* This sample just bounces a colored box around — enough to prove the
* pipeline works. Delete everything and start from the structure below.
*/
@@ -24,23 +29,30 @@ static constexpr int H = 240;
// --- Demo state -----------------------------------------------------------
static float boxX = 40, boxY = 30;
static float velX = 1.7f, velY = 1.1f;
static float velX = 100.0f, velY = 70.0f; // pixels per second
static unsigned long lastMs = 0;
static constexpr float BOX = 24.0f;
void setup()
{
randomSeed(millis());
velX = 1.0f + random(100) / 50.0f;
velY = 1.0f + random(100) / 50.0f;
velX = 60.0f + random(100) * 1.2f; // 60..180 px/s
velY = 60.0f + random(100) * 1.2f;
}
void loop()
{
// Delta time: seconds since the previous frame.
unsigned long now = millis();
float dt = lastMs ? (now - lastMs) / 1000.0f : 1.0f / 60.0f;
lastMs = now;
if (dt > 0.1f) dt = 0.1f; // clamp after stalls (drag, breakpoint)
// Move and bounce
boxX += velX;
boxY += velY;
if (boxX < 0 || boxX + BOX > W) { velX = -velX; boxX += velX; }
if (boxY < 0 || boxY + BOX > H) { velY = -velY; boxY += velY; }
boxX += velX * dt;
boxY += velY * dt;
if (boxX < 0 || boxX + BOX > W) { velX = -velX; boxX += velX * dt; }
if (boxY < 0 || boxY + BOX > H) { velY = -velY; boxY += velY * dt; }
// Clear the framebuffer (opaque black, alpha = 0xFF)
for (int i = 0; i < W * H; i++) emuGfx::fb[i] = 0xFF000000;
+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 ---