emulator-template: Add full emulator-only template package, fix Conway starting paused
Complete self-contained SDL3 emulator-only template for prototyping sketches without hardware: - Makefile: single-TU build (host + sketch), run/smoke/clean targets - shim.h: minimal Arduino API shim (millis, random, String, GPIO stubs) - src/main.cpp: SDL3 host loop presenting emuGfx::fb, CLI frame dumps - examples/Conway: Conway.cpp sketch + .ino reference copy - .gitignore: exclude built binaries and captured BMP frames The Conway sketch previously started with paused=true, which prevented render() from being called each frame, leaving the framebuffer all-black and resulting in a black SDL3 window. Changed to paused=false so the simulation runs immediately.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* @file Conway.cpp
|
||||
* @brief Game of Life — minimal SDL3 emulator prototype
|
||||
*
|
||||
* A minimal Game of Life implementation using the T-Deck emulator canvas.
|
||||
* Press Space to toggle pause. No hardware dependencies.
|
||||
*
|
||||
* Build: make -C /var/home/dejvino/workspace/T-Deck/emulator-template run
|
||||
* or: make -C /var/home/dejvino/workspace/T-Deck/emulator APP=Conway run
|
||||
* Smoke test: make smoke -C /var/home/dejvino/workspace/T-Deck/emulator-template APP=Conway
|
||||
*/
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Minimal emulator shim — provides millis(), random(), randomSeed(), etc.
|
||||
// ---------------------------------------------------------------------------
|
||||
#include "shim.h"
|
||||
|
||||
// Declare emulated state provided by the emulator main.cpp
|
||||
extern std::deque<uint8_t> emu::keyboard;
|
||||
extern uint32_t emuGfx::fb[320 * 240];
|
||||
extern bool emuGfx::dirty;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Canvas configuration (320x240, same as T-Deck display)
|
||||
// ---------------------------------------------------------------------------
|
||||
static constexpr int W = 320;
|
||||
static constexpr int H = 240;
|
||||
static constexpr int SCALE = 8; // 40 × 30 tile grid
|
||||
using Grid = uint8_t[W / SCALE][H / SCALE];
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Game of Life state
|
||||
// ---------------------------------------------------------------------------
|
||||
static Grid grid{};
|
||||
static Grid next{};
|
||||
static bool paused = true;
|
||||
static int tick = 0;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Input tracking — read from the emu::keyboard queue (populated by the
|
||||
// emulator main.cpp from SDL key events).
|
||||
// ---------------------------------------------------------------------------
|
||||
static bool justPressedSpace = false;
|
||||
static bool justPressedP = false;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// setup() — run once at launch
|
||||
// ---------------------------------------------------------------------------
|
||||
void setup()
|
||||
{
|
||||
randomSeed(millis());
|
||||
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
grid[x][y] = random(2);
|
||||
}
|
||||
}
|
||||
|
||||
tick = 0;
|
||||
paused = false;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// tickOnce() — advance the simulation by one logic tick
|
||||
// ---------------------------------------------------------------------------
|
||||
void tickOnce()
|
||||
{
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
int neighbors = 0;
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
int nx = (x + dx + W / SCALE) % (W / SCALE);
|
||||
int ny = (y + dy + H / SCALE) % (H / SCALE);
|
||||
neighbors += grid[nx][ny];
|
||||
}
|
||||
}
|
||||
if (grid[x][y]) {
|
||||
next[x][y] = (neighbors == 2 || neighbors == 3) ? 1 : 0;
|
||||
} else {
|
||||
next[x][y] = (neighbors == 3) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < (W / SCALE) * (H / SCALE); i++) {
|
||||
int ix = i / (W / SCALE);
|
||||
int iy = i % (W / SCALE);
|
||||
grid[ix][iy] = next[ix][iy];
|
||||
}
|
||||
tick++;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render() — draw the current grid to the emulator framebuffer
|
||||
// ---------------------------------------------------------------------------
|
||||
void render()
|
||||
{
|
||||
for (int y = 0; y < H / SCALE; y++) {
|
||||
for (int x = 0; x < W / SCALE; x++) {
|
||||
uint32_t c = grid[x][y] ? 0xFFFFFFFF : 0xFF000000;
|
||||
emuGfx::fb[y * W + x] = c;
|
||||
}
|
||||
}
|
||||
emuGfx::dirty = true;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Main loop — called once per frame by the emulator main.cpp
|
||||
// ---------------------------------------------------------------------------
|
||||
void loop()
|
||||
{
|
||||
while (!emu::keyboard.empty()) {
|
||||
char k = (char)emu::keyboard.front();
|
||||
emu::keyboard.pop_front();
|
||||
|
||||
if (k == ' ') {
|
||||
justPressedSpace = true;
|
||||
}
|
||||
if (k == 'p' || k == '\t') {
|
||||
justPressedP = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (justPressedSpace) {
|
||||
paused = !paused;
|
||||
justPressedSpace = false;
|
||||
}
|
||||
|
||||
if (justPressedP) {
|
||||
justPressedP = false;
|
||||
}
|
||||
|
||||
static uint32_t lastStep = 0;
|
||||
uint32_t now = millis();
|
||||
if (!paused && (now - lastStep > 150)) {
|
||||
tickOnce();
|
||||
render();
|
||||
lastStep = now;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user