emulator-template: Neutralize demo slot, add 10s auto-exit safety hatch

Make the template foolproof to copy-and-fill for a new demo:

- examples/Conway/ replaced by a neutral examples/Demo/Demo.cpp sample
  (bouncing box) documenting the full sketch contract (setup/loop,
  emuGfx::fb, emuGfx::dirty, emu::keyboard)
- Makefile passes the sketch path as -DAPP_SRC; main.cpp now includes
  APP_SRC instead of hardcoding Conway.cpp, window title uses APP_NAME
- Safety hatch: emulator auto-exits after 10 seconds by default so a
  runaway demo can't hang and eat resources; --user-run disables the
  cap, --timeout MS adjusts it (default 10000)
- README rewritten as a step-by-step recipe: replace the Demo sketch,
  run 'make run'; documents all CLI flags, drops the unimplemented
  --dump-interval flag

Verified: fresh copy + sketch swap builds and renders; hatch fires at
10s (and at --timeout 2000); --user-run runs until --frames cap.
This commit is contained in:
Dejvino
2026-11-16 20:25:41 +01:00
parent c331b55d82
commit a243f303f6
6 changed files with 152 additions and 393 deletions
+20 -5
View File
@@ -48,21 +48,28 @@ extern "C" void setup();
extern "C" void loop();
// --- Include the user's sketch .cpp file ---
// This is listed on the Makefile's g++ command line so it compiles
// as part of the same translation unit as main.cpp.
#include "examples/Conway/Conway.cpp"
// The path comes from the Makefile: -DAPP_SRC='"examples/$(APP)/$(APP).cpp"'.
// Change APP (or replace examples/Demo/Demo.cpp) to run your own sketch.
#ifndef APP_SRC
#define APP_SRC "examples/Demo/Demo.cpp"
#endif
#include APP_SRC
// ---------------------------------------------------------------------------
// --- Main entry point ---
int main(int argc, char **argv)
{
// Parse simple command-line options
long maxFrames = -1;
long maxFrames = -1; // --frames N: run exactly N frames then exit
const char *dumpPath = nullptr;
bool userRun = false; // --user-run: disable the safety-hatch timeout
long timeoutMs = 10000; // safety hatch: auto-exit after 10 s by default
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--frames") && i + 1 < argc) maxFrames = atol(argv[++i]);
else if (!strcmp(argv[i], "--dump") && i + 1 < argc) dumpPath = argv[++i];
else if (!strcmp(argv[i], "--user-run")) userRun = true;
else if (!strcmp(argv[i], "--timeout") && i + 1 < argc) timeoutMs = atol(argv[++i]);
}
// SDL3 initialization
@@ -73,7 +80,7 @@ int main(int argc, char **argv)
TTF_Init();
// Create window matching the 320x240 canvas
SDL_Window *win = SDL_CreateWindow("T-Deck Template",
SDL_Window *win = SDL_CreateWindow("T-Deck: " APP_NAME,
320, 240, SDL_WINDOW_RESIZABLE);
if (!win) {
std::printf("SDL_CreateWindow failed: %s\n", SDL_GetError());
@@ -138,6 +145,14 @@ int main(int argc, char **argv)
}
}
// --- Safety hatch: auto-exit unless the user asked to play ---
if (!userRun && timeoutMs > 0 && millis() >= (unsigned long)timeoutMs) {
std::printf("[emu] safety hatch: exiting after %ld ms "
"(pass --user-run to run indefinitely)\n", timeoutMs);
running = false;
break;
}
// --- Call the user's simulation step ---
loop();