Files
lilygo-t-deck/emulator-template/examples/Conway/Conway.ino
T
Dejvino c331b55d82 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.
2026-11-16 20:22:03 +01:00

165 lines
5.9 KiB
Arduino
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file Conway.ino
* @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.
// Pull in the definitions that the emulator main.cpp provides.
// ---------------------------------------------------------------------------
#include "shim.h"
// Emulated state provided by the emulator main.cpp
extern uint32_t emuGfx::fb[320 * 240];
extern bool emuGfx::dirty;
extern std::deque<uint8_t> emu::keyboard;
// ---------------------------------------------------------------------------
// 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). Each keypress becomes an ASCII
// byte queued at I2C address 0x55.
// ---------------------------------------------------------------------------
static bool justPressedSpace = false;
static bool justPressedP = false;
// ---------------------------------------------------------------------------
// setup() — run once at launch
// ---------------------------------------------------------------------------
void setup()
{
// Fast random seed
randomSeed(millis());
// Populate a random initial state
for (int y = 0; y < H / SCALE; y++) {
for (int x = 0; x < W / SCALE; x++) {
grid[x][y] = random(2);
}
}
tick = 0;
paused = true;
}
// ---------------------------------------------------------------------------
// 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;
// wrap-around torus (simplest: no edges)
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];
}
}
// Conway's rules
if (grid[x][y]) {
next[x][y] = (neighbors == 2 || neighbors == 3) ? 1 : 0;
} else {
next[x][y] = (neighbors == 3) ? 1 : 0;
}
}
}
// Swap buffers
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
// The emulator main.cpp maps emuGfx::fb[y * W + x] to an SDL texture
// each frame. ABGR8888 pixel format.
// ---------------------------------------------------------------------------
void render()
{
for (int y = 0; y < H / SCALE; y++) {
for (int x = 0; x < W / SCALE; x++) {
// White living cell / black dead cell
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.
// Handles input and advances simulation when unpaused.
// ---------------------------------------------------------------------------
void loop()
{
// Poll keyboard queue (filled by emulator main.cpp from SDL events)
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;
}
}
// Toggle pause with Space
if (justPressedSpace) {
paused = !paused;
justPressedSpace = false;
}
// Switch view mode with P / Tab (in this simple template both modes
// render the same grid; the flag is available for future expansion)
if (justPressedP) {
justPressedP = false;
}
// Simulation step (paced ~6.6 Hz when unpaused)
static uint32_t lastStep = 0;
uint32_t now = millis();
if (!paused && (now - lastStep > 150)) {
tickOnce();
render();
lastStep = now;
}
}
// ---------------------------------------------------------------------------
// The emulator main.cpp provides the full SDL3 loop, event pump, and
// frame presentation. This sketch only supplies setup(), loop(), tickOnce(),
// render(), and the framebuffer writes above. No hardware dependencies,
// no TFT_eSPI, no SPI, no I2S — pure emulator-side C++ for rapid
// graphics/engine prototyping.
// ---------------------------------------------------------------------------