diff --git a/tron-room-demo/README.md b/tron-room-demo/README.md index 114dd74..7b59e0a 100644 --- a/tron-room-demo/README.md +++ b/tron-room-demo/README.md @@ -52,7 +52,39 @@ make clean ``` The Makefile is the template's, with two changes: `APP ?= RoomView` -and `-I ../../src/netdeck` on the include path. +and `-I ../src/netdeck` on the include path. + +## On the real T-Deck (ESP32-S3) — free-running benchmark + +The renderer lives in `RoomViewCore.h`, device-agnostic and shared 1:1 +between the emulator sketch and the firmware twin +`fw/RoomViewHW.ino`. On hardware the core draws straight into a +`TFT_eSprite`'s `frameBuffer()` (16-bit RGB565, PSRAM — the same +full-frame pipeline as NetDeck's M1 anti-flicker pattern) and pushes +with `startWrite()/pushSprite(0,0)/endWrite()`. + +The loop is deliberately **free-running** — no pacing — and prints a +report over USB serial every 2 s: + +``` +RoomViewHW: 120 frames in 2001ms = 60.0 fps | render 12.3 ms, push 14.8 ms +``` + +That `render` / `push` split is the answer to "how quickly does this +run on real hardware": render is the software rasterizer, push is the +SPI transfer to the panel. + +Build, flash and watch: + +```bash +pio run -d tron-room-demo/fw -t upload +pio device monitor # 115200 baud +``` + +(The demo has its own `fw/platformio.ini` pointing at the shared +`boards/`, `lib/` and `src/netdeck` headers — NetDeck's own builds are +untouched. Keyboard input is emulator-only; on hardware the camera +drifts on its own orbit.) ## Safety hatch / CLI options diff --git a/tron-room-demo/RoomViewCore.h b/tron-room-demo/RoomViewCore.h new file mode 100644 index 0000000..dca0178 --- /dev/null +++ b/tron-room-demo/RoomViewCore.h @@ -0,0 +1,498 @@ +/** + * RoomViewCore — the RoomView renderer, shared between targets. + * + * Header-only, device-agnostic: renders the whole scene into a caller + * supplied 320x240 RGB565 buffer (the exact pixel format the T-Deck's + * ST7789 panel consumes over SPI). On hardware the caller hands in a + * TFT_eSprite's frameBuffer() pointer so the rasterizer draws straight + * into the sprite — zero copies. In the emulator it is a static array. + * + * Palette comes from NetDeck's nd_layout.h RGB565 constants; the world + * is NetDeck's real demo floorplan via nd_demomap.h / MapSource. + * + * Public API: + * rvInit(fb) hand over the 320x240 RGB565 target buffer + * rvInput(key) feed one ASCII key (0x1b = quit request) + * rvQuitRequested() true once the user asked to quit + * rvFrame(dt, nowMs) simulate + render one frame into the buffer + * + * All motion is delta-time based, so the same core runs paced at 60 fps + * in the emulator and free-running (true fps) on the ESP32-S3. + */ + +#pragma once + +#include +#include +#include +#include + +#include "nd_layout.h" +#include "nd_mapsource.h" +#include "nd_demomap.h" + +namespace rvcore { + +static constexpr int W = 320; +static constexpr int H = 240; +static constexpr float TILEF = 1.0f; // one map tile = one world unit + +// --- The render target: RGB565, exactly what the panel gets ------------------ +static uint16_t *g_fb = nullptr; + +// --- Palette: RGB565 straight from NetDeck's firmware palette --------------- +static const uint16_t COL_BG = 0x0002; // near-black navy void +static const uint16_t COL_FLOOR_A = nd::C_FLOOR; // near-black blue corridor floor +static const uint16_t COL_FLOOR_B = 0x2146; // checker variant (32,40,48) +static const uint16_t COL_WALL = nd::C_WALL; // steel blue-gray hull +static const uint16_t COL_CRATE = nd::C_COVER; // industrial crate brown-gray +static const uint16_t COL_DOOR = nd::C_DOOR; // amber blast door +static const uint16_t COL_TERMINAL = nd::C_GOOD; // green terminal glow +static const uint16_t COL_AIRLOCK = nd::C_AIRLOCK; // cyan airlock ring +static const uint16_t COL_MARINE = nd::C_AIRLOCK; // cyan squad +static const uint16_t COL_DROID = 0xFD20; // orange droid +static const uint16_t COL_HEAVY = nd::C_HOT; // red heavy +static const uint16_t COL_TURRET = nd::C_HIVE; // magenta turret +static const uint16_t COL_LINK = nd::C_LINK; // teal network link trace +static const uint16_t COL_PACKET = nd::C_PACKET;// cyan data packet + +// --- RGB565 shading ---------------------------------------------------------- +// Fixed-point brightness scale: factor 0..32 (5 bits), applied per channel. +// Integer-only, per-pixel — the way it would be done on the MCU. +static inline uint16_t shade565(uint16_t c, int f32) { + if (f32 < 0) f32 = 0; + if (f32 > 32) f32 = 32; + int r = (((c >> 11) & 31) * f32) >> 5; if (r > 31) r = 31; + int g = (((c >> 5) & 63) * f32) >> 5; if (g > 63) g = 63; + int b = ((c & 31) * f32) >> 5; if (b > 31) b = 31; + return (uint16_t)((r << 11) | (g << 5) | b); +} + +// Fog factor as a 0..32 fixed-point factor for a camera-space distance. +static const int FAR_D = 44; // fog distance (camera units) +static inline int fogFor(float d) { + int f = 32 - (int)(d * 32.0f / FAR_D); + if (f < 19) f = 19; // floor at ~0.60 — distant walls never vanish + if (f > 32) f = 32; + return f; +} + +// --- Drawing helpers (uint16_t throughout) ----------------------------------- +static inline void putPixel(int x, int y, uint16_t c) { + if (x < 0 || x >= W || y < 0 || y >= H || !g_fb) return; + g_fb[y * W + x] = c; +} + +static void drawLine(int x0, int y0, int x1, int y1, uint16_t color) { + int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1; + int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1; + int err = dx + dy, e2; + for (;;) { + putPixel(x0, y0, color); + if (x0 == x1 && y0 == y1) break; + e2 = 2 * err; + if (e2 >= dy) { err += dy; x0 += sx; } + if (e2 <= dx) { err += dx; y0 += sy; } + } +} + +static void fillCircle(int cx, int cy, int r, uint16_t color) { + for (int y = -r; y <= r; y++) + for (int x = -r; x <= r; x++) + if (x * x + y * y <= r * r) putPixel(cx + x, cy + y, color); +} + +// Horizontal span with clipping. +static inline void hline(int y, int xa, int xb, uint16_t c) { + if (y < 0 || y >= H || !g_fb) return; + if (xa > xb) { int t = xa; xa = xb; xb = t; } + if (xb < 0 || xa >= W) return; + if (xa < 0) xa = 0; + if (xb >= W) xb = W - 1; + for (int x = xa; x <= xb; x++) g_fb[y * W + x] = c; +} + +// Scanline triangle fill (float vertices, clips to screen). +static void fillTri(float x0, float y0, float x1, float y1, float x2, float y2, uint16_t c) { + if (y1 < y0) { float t; t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } + if (y2 < y0) { float t; t = x0; x0 = x2; x2 = t; t = y0; y0 = y2; y2 = t; } + if (y2 < y1) { float t; t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; } + if (y2 - y0 < 0.5f) return; + int yA = (int)ceilf(y0), yB = (int)y2; + if (yA < 0) yA = 0; + if (yB > H - 1) yB = H - 1; + for (int y = yA; y <= yB; y++) { + float yc = (float)y + 0.5f; + float t = (yc - y0) / (y2 - y0); + if (t < 0) t = 0; + if (t > 1) t = 1; + float xa = x0 + (x2 - x0) * t; + float xb; + if (yc < y1) { + float t2 = (y1 - y0 < 0.5f) ? 1.0f : (yc - y0) / (y1 - y0); + if (t2 < 0) t2 = 0; + if (t2 > 1) t2 = 1; + xb = x0 + (x1 - x0) * t2; + } else { + float t2 = (y2 - y1 < 0.5f) ? 0.0f : (yc - y1) / (y2 - y1); + if (t2 < 0) t2 = 0; + if (t2 > 1) t2 = 1; + xb = x1 + (x2 - x1) * t2; + } + hline(y, (int)floorf(xa), (int)ceilf(xb), c); + } +} + +static void fillQuad(float x0, float y0, float x1, float y1, + float x2, float y2, float x3, float y3, uint16_t c) { + fillTri(x0, y0, x1, y1, x2, y2, c); + fillTri(x0, y0, x2, y2, x3, y3, c); +} + +// --- Camera ------------------------------------------------------------------ +// Orbiting camera: position camPos, looking at lookAt, up = world +Z. +// Basis vectors are rebuilt each frame; project() does the perspective divide. +static float camPos[3] = { 20, -6, 30 }; +static float lookAt[3] = { 20, 12, 0 }; +static float camF[3], camR[3], camU[3]; +static float FX = 235.0f; // focal length (zoom) + +static void updateBasis() { + // forward = normalize(lookAt - camPos) + float f[3] = { lookAt[0] - camPos[0], lookAt[1] - camPos[1], lookAt[2] - camPos[2] }; + float fl = sqrtf(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]); + for (int i = 0; i < 3; i++) f[i] /= fl; + // right = normalize(cross(f, worldUp(0,0,1))) + float r[3] = { f[1], -f[0], 0 }; + float rl = sqrtf(r[0] * r[0] + r[1] * r[1]); + if (rl < 1e-4f) { r[0] = 1; r[1] = 0; r[2] = 0; } else { r[0] /= rl; r[1] /= rl; } + float u[3] = { + r[1] * f[2] - r[2] * f[1], + r[2] * f[0] - r[0] * f[2], + r[0] * f[1] - r[1] * f[0] + }; + for (int i = 0; i < 3; i++) { camF[i] = f[i]; camR[i] = r[i]; camU[i] = u[i]; } +} + +// Project a world point (X = west-east, Y = south-north, Z = up). +// Returns false if behind the near plane. +static inline bool project(float X, float Y, float Z, int &sx, int &sy, float &depth) { + float d[3] = { X - camPos[0], Y - camPos[1], Z - camPos[2] }; + float rz = d[0] * camF[0] + d[1] * camF[1] + d[2] * camF[2]; + if (rz < 0.6f) return false; + float rx = d[0] * camR[0] + d[1] * camR[1] + d[2] * camR[2]; + float ry = d[0] * camU[0] + d[1] * camU[1] + d[2] * camU[2]; + sx = (int)(160.0f + rx * FX / rz); + sy = (int)(120.0f - ry * FX / rz); + depth = rz; + return true; +} + +// --- Solid shaded box (painter's algorithm, backface culled) ----------------- +struct Box { + float x, y; // center on the floor + float hx, hy; // half extents + float z0, z1; // base / top height + uint16_t col; // RGB565 base color +}; + +// Face corner indices into an 8-corner box, and world normals. +// Corners 0-3 sit at z0 (bottom), corners 4-7 at z1 (top). +static const int FACES[6][4] = { + {4,5,6,7}, // top (+Z) — corners at z1 + {0,1,2,3}, // bottom (-Z) — corners at z0 + {0,1,5,4}, // -Y side + {2,3,7,6}, // +Y side + {1,2,6,5}, // +X side + {0,3,7,4}, // -X side +}; +static const float NORMALS[6][3] = { + {0,0,1}, {0,0,-1}, {0,-1,0}, {0,1,0}, {1,0,0}, {-1,0,0}, +}; +static const float LIGHT[3] = { -0.35f, -0.25f, 0.90f }; // from above, slight SW + +static void drawBox(const Box &b, float dist) { + // 8 corners: bottom 0-3 (CCW seen from above), top 4-7 + static const float SIGN[8][3] = { + {-1,-1,0},{1,-1,0},{1,1,0},{-1,1,0}, + {-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1}, + }; + int px[8], py[8]; + bool vis[8]; + int fog = fogFor(dist); + int minX = W, maxX = -1, minY = H, maxY = -1; + for (int i = 0; i < 8; i++) { + float X = b.x + SIGN[i][0] * b.hx; + float Y = b.y + SIGN[i][1] * b.hy; + float Z = SIGN[i][2] ? b.z1 : b.z0; + float d; + vis[i] = project(X, Y, Z, px[i], py[i], d); + if (vis[i]) { + if (px[i] < minX) minX = px[i]; + if (px[i] > maxX) maxX = px[i]; + if (py[i] < minY) minY = py[i]; + if (py[i] > maxY) maxY = py[i]; + } + } + if (maxX < 0 || minX >= W || maxY < 0 || minY >= H) return; // offscreen + + for (int f = 0; f < 6; f++) { + const int *q = FACES[f]; + bool ok = true; + for (int k = 0; k < 4; k++) if (!vis[q[k]]) { ok = false; break; } + if (!ok) continue; + // backface cull: skip faces pointing away from the camera + float fcx = b.x + NORMALS[f][0] * b.hx; + float fcy = b.y + NORMALS[f][1] * b.hy; + float fcz = (NORMALS[f][2] > 0) ? b.z1 : (NORMALS[f][2] < 0 ? b.z0 : (b.z0 + b.z1) * 0.5f); + float toCam[3] = { camPos[0] - fcx, camPos[1] - fcy, camPos[2] - fcz }; + float nd = NORMALS[f][0] * toCam[0] + NORMALS[f][1] * toCam[1] + NORMALS[f][2] * toCam[2]; + if (nd <= 0.001f) continue; + float ndl = NORMALS[f][0] * LIGHT[0] + NORMALS[f][1] * LIGHT[1] + NORMALS[f][2] * LIGHT[2]; + if (ndl < 0.55f) ndl = 0.55f; + uint16_t col = shade565(b.col, (int)(ndl * fog)); + fillQuad((float)px[q[0]], (float)py[q[0]], + (float)px[q[1]], (float)py[q[1]], + (float)px[q[2]], (float)py[q[2]], + (float)px[q[3]], (float)py[q[3]], col); + // bright rim on the top edges (f == 0 is the top face) + if (f == 0) { + uint16_t rim = shade565(b.col, (int)((0.55f + 0.45f * ndl) * fog)); + drawLine(px[q[0]], py[q[0]], px[q[1]], py[q[1]], rim); + drawLine(px[q[1]], py[q[1]], px[q[2]], py[q[2]], rim); + drawLine(px[q[2]], py[q[2]], px[q[3]], py[q[3]], rim); + drawLine(px[q[3]], py[q[3]], px[q[0]], py[q[0]], rim); + } + } +} + +// Distance from the camera to a box center (for fog + depth sort). +static inline float boxDist(const Box &b) { + float dx = b.x - camPos[0], dy = b.y - camPos[1]; + float dz = (b.z0 + b.z1) * 0.5f - camPos[2]; + return sqrtf(dx * dx + dy * dy + dz * dz); +} + +// --- World state -------------------------------------------------------------- +static nd::DemoMapSource mapSource; + +struct Walker { + float ax, ay, bx, by; // patrol endpoints (world units, tile centers) + float t; // 0..1 along the route + float dir; // +1 or -1 + float speed; // tiles per second + uint8_t kind; +}; + +static const int MAX_WALKERS = nd::MAX_UNITS > 20 ? nd::MAX_UNITS : 20; +static Walker walkers[MAX_WALKERS]; +static int walkerCount = 0; + +static void buildWalkers() { + walkerCount = 0; + for (int i = 0; i < mapSource.spawnCount() && walkerCount < MAX_WALKERS; i++) { + const nd::UnitSpawn &s = mapSource.spawn(i); + Walker &w = walkers[walkerCount]; + w.ax = (s.tx % nd::TILE_COLS) + 0.5f; + w.ay = (s.tx / nd::TILE_COLS) + 0.5f; + if (s.patA >= 0 && s.patB >= 0) { + w.bx = (s.patA % nd::TILE_COLS) + 0.5f; + w.by = (s.patA / nd::TILE_COLS) + 0.5f; + } else { + w.bx = w.ax; w.by = w.ay; // stationary + } + w.t = 0; + w.dir = 1; + w.speed = (s.kind == 2) ? 0.0f : (s.kind == 3 ? 0.0f : 1.1f + (i % 3) * 0.15f); + w.kind = s.kind; + walkerCount++; + } +} + +static inline float lerp(float a, float b, float t) { return a + (b - a) * t; } + +// --- Public state knobs (the emulator shell maps keys onto these) ------------ +static float orbitSpeed = 0.10f; // rad/s +static bool paused = false; +static bool quitRequested = false; + +// --- Public API --------------------------------------------------------------- +static inline void rvInit(uint16_t *fb) { + g_fb = fb; + buildWalkers(); + // initial camera pose: south of the map, looking north at the deck + camPos[0] = 20.0f; camPos[1] = 12.0f - 22.0f; camPos[2] = 30.0f; +} + +static inline void rvInput(uint8_t k) { + if (k == 'q' || k == 0x1b) quitRequested = true; + if (k == 'a') { orbitSpeed += 0.03f; if (orbitSpeed > 0.60f) orbitSpeed = 0.60f; } + if (k == 'd') { orbitSpeed -= 0.03f; if (orbitSpeed < -0.60f) orbitSpeed = -0.60f; } + if (k == 'w') { FX += 18.0f; if (FX > 320.0f) FX = 320.0f; } + if (k == 's') { FX -= 18.0f; if (FX < 90.0f) FX = 90.0f; } + if (k == 'r') { camPos[2] += 2.0f; if (camPos[2] > 60.0f) camPos[2] = 60.0f; } + if (k == 'f') { camPos[2] -= 2.0f; if (camPos[2] < 14.0f) camPos[2] = 14.0f; } + if (k == ' ') paused = !paused; +} + +static inline bool rvQuitRequested() { return quitRequested; } + +// --- One frame: simulate + render --------------------------------------------- +static void rvFrame(float dt, unsigned long nowMs) { + // --- camera drift: slow orbit + breathing radius/height --- + static float orbit = 0.0f; // angle around the map center + static float breathe = 0.0f; + if (!paused) { + orbit += orbitSpeed * dt; + breathe += dt * 0.35f; + } + float radius = 15.0f + 3.0f * sinf(breathe * 0.7f); + float height = 21.0f + 3.5f * sinf(breathe * 0.45f + 1.3f); + lookAt[0] = 20.0f + 1.5f * sinf(breathe * 0.5f); + lookAt[1] = 12.0f + 1.0f * cosf(breathe * 0.33f); + camPos[0] = lookAt[0] + cosf(orbit) * radius; + camPos[1] = lookAt[1] + sinf(orbit) * radius; + camPos[2] = height; + updateBasis(); + + // --- entities walk their patrol routes --- + if (!paused) { + for (int i = 0; i < walkerCount; i++) { + Walker &w = walkers[i]; + if (w.speed <= 0) continue; + float len = sqrtf((w.bx - w.ax) * (w.bx - w.ax) + (w.by - w.ay) * (w.by - w.ay)); + if (len < 0.01f) continue; + w.t += w.dir * w.speed * dt / len; + if (w.t > 1) { w.t = 1; w.dir = -1; } + if (w.t < 0) { w.t = 0; w.dir = 1; } + } + } + + // --- clear the 565 device framebuffer --- + for (int i = 0; i < W * H; i++) g_fb[i] = COL_BG; + + // --- floor: flat lit quads for every non-wall tile, checker shaded --- + for (int ty = 0; ty < nd::TILE_ROWS; ty++) { + for (int tx = 0; tx < nd::TILE_COLS; tx++) { + char ch = mapSource.tileAt(tx, ty); + if (ch == '#') continue; + uint16_t base = ((tx + ty) & 1) ? COL_FLOOR_B : COL_FLOOR_A; + int px[4], py[4]; + float d; + bool ok = true; + const float OFF[4][2] = { {0,0},{1,0},{1,1},{0,1} }; + for (int k = 0; k < 4; k++) + if (!project((float)(tx + OFF[k][0]), (float)(ty + OFF[k][1]), 0.0f, px[k], py[k], d)) { ok = false; break; } + if (!ok) continue; + if (px[0] < -8 && px[1] < -8 && px[2] < -8 && px[3] < -8) continue; + if (px[0] > W + 8 && px[1] > W + 8 && px[2] > W + 8 && px[3] > W + 8) continue; + float cx = tx + 0.5f, cy = ty + 0.5f; + fillQuad((float)px[0], (float)py[0], (float)px[1], (float)py[1], + (float)px[2], (float)py[2], (float)px[3], (float)py[3], + shade565(base, fogFor(sqrtf((cx - camPos[0]) * (cx - camPos[0]) + + (cy - camPos[1]) * (cy - camPos[1]) + + camPos[2] * camPos[2])))); + } + } + + // --- collect solid geometry: walls, crates, door, terminal, node pylons --- + struct Solid { Box b; }; + static Solid solids[1024]; + int nSolids = 0; + + for (int ty = 0; ty < nd::TILE_ROWS; ty++) { + for (int tx = 0; tx < nd::TILE_COLS; tx++) { + char ch = mapSource.tileAt(tx, ty); + if (ch == '.') continue; + Box b; + b.x = tx + 0.5f; b.y = ty + 0.5f; + b.z0 = 0.0f; + b.hx = b.hy = 0.5f; + switch (ch) { + case '#': b.z1 = 1.6f; b.col = COL_WALL; break; + case 'o': b.z1 = 0.9f; b.col = COL_CRATE; break; + case '+': b.z1 = 1.3f; b.col = COL_DOOR; break; + case 'T': b.z1 = 0.7f; b.col = COL_TERMINAL; break; + case 'A': b.z1 = 0.15f; b.col = COL_AIRLOCK; break; + default: continue; + } + solids[nSolids++].b = b; + } + } + + // network nodes: glowing pylons at their pinned tiles (palette passthrough) + static Box nodeBoxes[nd::MAX_NODES]; + for (int i = 0; i < mapSource.nodeCount(); i++) { + const nd::NetNodeDef &n = mapSource.node(i); + Box &b = nodeBoxes[i]; + b.x = n.tx + 0.5f; b.y = n.ty + 0.5f; + b.hx = b.hy = 0.28f; + b.z0 = 0.0f; + b.z1 = 1.9f; + b.col = n.color; // already RGB565, straight from the map source + solids[nSolids++].b = b; + } + + // --- entities as small glowing boxes --- + static Box entBoxes[MAX_WALKERS]; + for (int i = 0; i < walkerCount; i++) { + const Walker &w = walkers[i]; + Box &b = entBoxes[i]; + b.x = lerp(w.ax, w.bx, w.t); + b.y = lerp(w.ay, w.by, w.t); + b.hx = b.hy = 0.30f; + b.z0 = 0.0f; + b.z1 = (w.kind == 3) ? 0.45f : (w.kind == 2 ? 0.85f : 0.65f); + b.col = (w.kind == 0) ? COL_MARINE : (w.kind == 1) ? COL_DROID + : (w.kind == 2) ? COL_HEAVY : COL_TURRET; + solids[nSolids++].b = b; + } + + // --- depth sort far -> near (painter's algorithm) --- + static float dists[1024 + nd::MAX_NODES + MAX_WALKERS]; + static int order[1024 + nd::MAX_NODES + MAX_WALKERS]; + for (int i = 0; i < nSolids; i++) { dists[i] = boxDist(solids[i].b); order[i] = i; } + for (int a = 1; a < nSolids; a++) { + int v = order[a], j = a - 1; + while (j >= 0 && dists[order[j]] < dists[v]) { order[j + 1] = order[j]; j--; } + order[j + 1] = v; + } + for (int i = 0; i < nSolids; i++) drawBox(solids[order[i]].b, dists[order[i]]); + + // --- network links + traveling packets (overhead lines between pylon tops) + for (int e = 0; e < mapSource.edgeCount(); e++) { + const nd::NetEdgeDef &ed = mapSource.edge(e); + const nd::NetNodeDef &na = mapSource.node(ed.a); + const nd::NetNodeDef &nb = mapSource.node(ed.b); + float ax = na.tx + 0.5f, ay = na.ty + 0.5f; + float bx = nb.tx + 0.5f, by = nb.ty + 0.5f; + int x0, y0, x1, y1; + float d0, d1; + if (!project(ax, ay, 1.9f, x0, y0, d0)) continue; + if (!project(bx, by, 1.9f, x1, y1, d1)) continue; + drawLine(x0, y0, x1, y1, shade565(COL_LINK, fogFor((d0 + d1) * 0.5f))); + // packet: a small bright dot sweeping the edge + float ph = fmodf(nowMs / 1000.0f * 0.35f + e * 0.37f, 1.0f); + int pkx, pky; + float pdd; + if (project(lerp(ax, bx, ph), lerp(ay, by, ph), 1.9f + 0.15f, pkx, pky, pdd)) { + int r = (int)(0.14f * FX / pdd); + if (r < 1) r = 1; + if (r > 4) r = 4; + fillCircle(pkx, pky, r, shade565(COL_PACKET, fogFor(pdd))); + } + } + + // --- pulsing halo over the airlock tile (squad entry marker) --- + { + int ax_, ay_; + float dd; + const nd::NetNodeDef &root = mapSource.node(mapSource.rootNode()); + if (project(root.tx + 0.5f, root.ty + 0.5f, 0.2f, ax_, ay_, dd)) { + int r = (int)(0.55f * FX / dd) + (int)(2.0f * sinf(nowMs / 1000.0f * 3.0f)); + if (r > 2) fillCircle(ax_, ay_, r, shade565(COL_AIRLOCK, 11)); + } + } +} + +} // namespace rvcore diff --git a/tron-room-demo/examples/RoomView/RoomView.cpp b/tron-room-demo/examples/RoomView/RoomView.cpp index a884125..1acf9af 100644 --- a/tron-room-demo/examples/RoomView/RoomView.cpp +++ b/tron-room-demo/examples/RoomView/RoomView.cpp @@ -3,6 +3,12 @@ * * Target: T-Deck emulator (320x240, RGB565 device framebuffer). * + * This file is only the emulator shell: keyboard pump, delta time, and + * the 565 -> 8888 presentation blit (the stand-in for the SPI push). + * The whole renderer lives in the device-agnostic RoomViewCore.h, + * shared 1:1 with the ESP32-S3 firmware build (fw/RoomViewHW.ino), so + * benchmark numbers measured on hardware describe exactly this code. + * * Contract with the emulator host (src/main.cpp): * - implement setup() — called once at launch * - implement loop() — called once per frame (host paces at ~60 fps) @@ -10,515 +16,45 @@ * - set emuGfx::dirty = true each frame you want presented * - read keys from emu::keyboard (ASCII queue, filled from SDL events) * - * REAL-HW COLOR PIPELINE: everything in this sketch renders into a - * 16-bit RGB565 framebuffer (fb565) — exactly what the T-Deck's ST7789 - * panel consumes over SPI — using the RGB565 palette constants straight - * out of src/netdeck/nd_layout.h. Shading and fog are integer fixed-point - * on 5/6/5 channels, so the banding you see is the banding real hardware - * would show. Only the final blit expands 565 -> 8888 for the desktop - * window; on the device that step doesn't exist, the 565 buffer is pushed - * to the panel as-is. - * - * The world is NetDeck's real demo floorplan: the hardcoded 3-room - * 40x24-tile Breach & Board map from src/netdeck/nd_demomap.h, consumed - * through the generic MapSource interface (tiles, rooms, doors, network - * graph, entity spawn list). Nothing about the map is duplicated here — - * edit the demo map in the firmware tree and this view follows it. - * - * Rendering (all software, no GPU): - * - a true 3D orbiting camera: position + look-at basis, full - * perspective divide, looking down at the deck from above - * - walls / crates / doors / terminals as solid shaded boxes with - * backface culling, painter's-algorithm depth sorting, lambert - * lighting from above and distance fog - * - floor tiles as flat lit quads with a subtle checker - * - the network graph drawn as glowing pylons with link lines and - * data packets traveling along the edges - * - entities from the demo spawn list walking their patrol routes - * - * The camera drifts on its own (slow orbit + breathing radius/height); - * keys adjust: a/d orbit speed, w/s zoom, r/f altitude, space pause, - * q/ESC quit. + * Keys: a/d orbit speed, w/s zoom, r/f altitude, space pause, q/ESC quit. */ #include "shim.h" -#include "nd_layout.h" -#include "nd_mapsource.h" -#include "nd_demomap.h" +#include "RoomViewCore.h" extern uint32_t emuGfx::fb[320 * 240]; extern bool emuGfx::dirty; extern std::deque emu::keyboard; -static constexpr int W = 320; -static constexpr int H = 240; -static constexpr float TILEF = 1.0f; // one map tile = one world unit +// The "device" framebuffer the core renders into. +static uint16_t fb565[rvcore::W * rvcore::H]; -// --- The "device" framebuffer: RGB565, exactly what the panel gets ---------- -static uint16_t fb565[W * H]; - -// --- Palette: RGB565 straight from NetDeck's firmware palette --------------- -static const uint16_t COL_BG = 0x0002; // near-black navy void -static const uint16_t COL_FLOOR_A = nd::C_FLOOR; // near-black blue corridor floor -static const uint16_t COL_FLOOR_B = 0x2146; // checker variant (32,40,48) -static const uint16_t COL_WALL = nd::C_WALL; // steel blue-gray hull -static const uint16_t COL_CRATE = nd::C_COVER; // industrial crate brown-gray -static const uint16_t COL_DOOR = nd::C_DOOR; // amber blast door -static const uint16_t COL_TERMINAL = nd::C_GOOD; // green terminal glow -static const uint16_t COL_AIRLOCK = nd::C_AIRLOCK; // cyan airlock ring -static const uint16_t COL_MARINE = nd::C_AIRLOCK; // cyan squad -static const uint16_t COL_DROID = 0xFD20; // orange droid -static const uint16_t COL_HEAVY = nd::C_HOT; // red heavy -static const uint16_t COL_TURRET = nd::C_HIVE; // magenta turret -static const uint16_t COL_LINK = nd::C_LINK; // teal network link trace -static const uint16_t COL_PACKET = nd::C_PACKET;// cyan data packet - -// --- RGB565 shading ---------------------------------------------------------- -// Fixed-point brightness scale: factor 0..32 (5 bits), applied per channel. -// Integer-only, per-pixel — the way it would be done on the MCU. -static inline uint16_t shade565(uint16_t c, int f32) { - if (f32 < 0) f32 = 0; - if (f32 > 32) f32 = 32; - int r = (((c >> 11) & 31) * f32) >> 5; if (r > 31) r = 31; - int g = (((c >> 5) & 63) * f32) >> 5; if (g > 63) g = 63; - int b = ((c & 31) * f32) >> 5; if (b > 31) b = 31; - return (uint16_t)((r << 11) | (g << 5) | b); -} - -// Fog factor as a 0..32 fixed-point factor for a camera-space distance. -static const int FAR_D = 44; // fog distance (camera units) -static inline int fogFor(float d) { - int f = 32 - (int)(d * 32.0f / FAR_D); - if (f < 19) f = 19; // floor at ~0.60 — distant walls never vanish - if (f > 32) f = 32; - return f; -} - -// --- Drawing helpers (uint16_t throughout) ----------------------------------- -static inline void putPixel(int x, int y, uint16_t c) { - if (x < 0 || x >= W || y < 0 || y >= H) return; - fb565[y * W + x] = c; -} - -static void drawLine(int x0, int y0, int x1, int y1, uint16_t color) { - int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1; - int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1; - int err = dx + dy, e2; - for (;;) { - putPixel(x0, y0, color); - if (x0 == x1 && y0 == y1) break; - e2 = 2 * err; - if (e2 >= dy) { err += dy; x0 += sx; } - if (e2 <= dx) { err += dx; y0 += sy; } - } -} - -static void fillCircle(int cx, int cy, int r, uint16_t color) { - for (int y = -r; y <= r; y++) - for (int x = -r; x <= r; x++) - if (x * x + y * y <= r * r) putPixel(cx + x, cy + y, color); -} - -// Horizontal span with clipping. -static inline void hline(int y, int xa, int xb, uint16_t c) { - if (y < 0 || y >= H) return; - if (xa > xb) { int t = xa; xa = xb; xb = t; } - if (xb < 0 || xa >= W) return; - if (xa < 0) xa = 0; - if (xb >= W) xb = W - 1; - for (int x = xa; x <= xb; x++) fb565[y * W + x] = c; -} - -// Scanline triangle fill (float vertices, clips to screen). -static void fillTri(float x0, float y0, float x1, float y1, float x2, float y2, uint16_t c) { - if (y1 < y0) { float t; t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } - if (y2 < y0) { float t; t = x0; x0 = x2; x2 = t; t = y0; y0 = y2; y2 = t; } - if (y2 < y1) { float t; t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; } - if (y2 - y0 < 0.5f) return; - int yA = (int)ceilf(y0), yB = (int)y2; - if (yA < 0) yA = 0; - if (yB > H - 1) yB = H - 1; - for (int y = yA; y <= yB; y++) { - float yc = (float)y + 0.5f; - float t = (yc - y0) / (y2 - y0); - if (t < 0) t = 0; - if (t > 1) t = 1; - float xa = x0 + (x2 - x0) * t; - float xb; - if (yc < y1) { - float t2 = (y1 - y0 < 0.5f) ? 1.0f : (yc - y0) / (y1 - y0); - if (t2 < 0) t2 = 0; - if (t2 > 1) t2 = 1; - xb = x0 + (x1 - x0) * t2; - } else { - float t2 = (y2 - y1 < 0.5f) ? 0.0f : (yc - y1) / (y2 - y1); - if (t2 < 0) t2 = 0; - if (t2 > 1) t2 = 1; - xb = x1 + (x2 - x1) * t2; - } - hline(y, (int)floorf(xa), (int)ceilf(xb), c); - } -} - -static void fillQuad(float x0, float y0, float x1, float y1, - float x2, float y2, float x3, float y3, uint16_t c) { - fillTri(x0, y0, x1, y1, x2, y2, c); - fillTri(x0, y0, x2, y2, x3, y3, c); -} - -// --- Camera ------------------------------------------------------------------ -// Orbiting camera: position camPos, looking at lookAt, up = world +Z. -// Basis vectors are rebuilt each frame; project() does the perspective divide. -static float camPos[3] = { 20, -6, 30 }; -static float lookAt[3] = { 20, 12, 0 }; -static float camF[3], camR[3], camU[3]; -static float FX = 235.0f; // focal length (zoom) - -static void updateBasis() { - // forward = normalize(lookAt - camPos) - float f[3] = { lookAt[0] - camPos[0], lookAt[1] - camPos[1], lookAt[2] - camPos[2] }; - float fl = sqrtf(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]); - for (int i = 0; i < 3; i++) f[i] /= fl; - // right = normalize(cross(f, worldUp(0,0,1))) - float r[3] = { f[1], -f[0], 0 }; - float rl = sqrtf(r[0] * r[0] + r[1] * r[1]); - if (rl < 1e-4f) { r[0] = 1; r[1] = 0; r[2] = 0; } else { r[0] /= rl; r[1] /= rl; } - float u[3] = { - r[1] * f[2] - r[2] * f[1], - r[2] * f[0] - r[0] * f[2], - r[0] * f[1] - r[1] * f[0] - }; - for (int i = 0; i < 3; i++) { camF[i] = f[i]; camR[i] = r[i]; camU[i] = u[i]; } -} - -// Project a world point (X = west-east, Y = south-north, Z = up). -// Returns false if behind the near plane. -static inline bool project(float X, float Y, float Z, int &sx, int &sy, float &depth) { - float d[3] = { X - camPos[0], Y - camPos[1], Z - camPos[2] }; - float rz = d[0] * camF[0] + d[1] * camF[1] + d[2] * camF[2]; - if (rz < 0.6f) return false; - float rx = d[0] * camR[0] + d[1] * camR[1] + d[2] * camR[2]; - float ry = d[0] * camU[0] + d[1] * camU[1] + d[2] * camU[2]; - sx = (int)(160.0f + rx * FX / rz); - sy = (int)(120.0f - ry * FX / rz); - depth = rz; - return true; -} - -// --- Solid shaded box (painter's algorithm, backface culled) ----------------- -struct Box { - float x, y; // center on the floor - float hx, hy; // half extents - float z0, z1; // base / top height - uint16_t col; // RGB565 base color -}; - -// Face corner indices into an 8-corner box, and world normals. -// Corners 0-3 sit at z0 (bottom), corners 4-7 at z1 (top). -static const int FACES[6][4] = { - {4,5,6,7}, // top (+Z) — corners at z1 - {0,1,2,3}, // bottom (-Z) — corners at z0 - {0,1,5,4}, // -Y side - {2,3,7,6}, // +Y side - {1,2,6,5}, // +X side - {0,3,7,4}, // -X side -}; -static const float NORMALS[6][3] = { - {0,0,1}, {0,0,-1}, {0,-1,0}, {0,1,0}, {1,0,0}, {-1,0,0}, -}; -static const float LIGHT[3] = { -0.35f, -0.25f, 0.90f }; // from above, slight SW - -static void drawBox(const Box &b, float dist) { - // 8 corners: bottom 0-3 (CCW seen from above), top 4-7 - static const float SIGN[8][3] = { - {-1,-1,0},{1,-1,0},{1,1,0},{-1,1,0}, - {-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1}, - }; - int px[8], py[8]; - bool vis[8]; - int fog = fogFor(dist); - int minX = W, maxX = -1, minY = H, maxY = -1; - for (int i = 0; i < 8; i++) { - float X = b.x + SIGN[i][0] * b.hx; - float Y = b.y + SIGN[i][1] * b.hy; - float Z = SIGN[i][2] ? b.z1 : b.z0; - float d; - vis[i] = project(X, Y, Z, px[i], py[i], d); - if (vis[i]) { - if (px[i] < minX) minX = px[i]; - if (px[i] > maxX) maxX = px[i]; - if (py[i] < minY) minY = py[i]; - if (py[i] > maxY) maxY = py[i]; - } - } - if (maxX < 0 || minX >= W || maxY < 0 || minY >= H) return; // offscreen - - for (int f = 0; f < 6; f++) { - const int *q = FACES[f]; - bool ok = true; - for (int k = 0; k < 4; k++) if (!vis[q[k]]) { ok = false; break; } - if (!ok) continue; - // backface cull: skip faces pointing away from the camera - float fcx = b.x + NORMALS[f][0] * b.hx; - float fcy = b.y + NORMALS[f][1] * b.hy; - float fcz = (NORMALS[f][2] > 0) ? b.z1 : (NORMALS[f][2] < 0 ? b.z0 : (b.z0 + b.z1) * 0.5f); - float toCam[3] = { camPos[0] - fcx, camPos[1] - fcy, camPos[2] - fcz }; - float nd = NORMALS[f][0] * toCam[0] + NORMALS[f][1] * toCam[1] + NORMALS[f][2] * toCam[2]; - if (nd <= 0.001f) continue; - float ndl = NORMALS[f][0] * LIGHT[0] + NORMALS[f][1] * LIGHT[1] + NORMALS[f][2] * LIGHT[2]; - if (ndl < 0.55f) ndl = 0.55f; - uint16_t col = shade565(b.col, (int)(ndl * fog)); - fillQuad((float)px[q[0]], (float)py[q[0]], - (float)px[q[1]], (float)py[q[1]], - (float)px[q[2]], (float)py[q[2]], - (float)px[q[3]], (float)py[q[3]], col); - // bright rim on the top edges (f == 0 is the top face) - if (f == 0) { - uint16_t rim = shade565(b.col, (int)((0.55f + 0.45f * ndl) * fog)); - drawLine(px[q[0]], py[q[0]], px[q[1]], py[q[1]], rim); - drawLine(px[q[1]], py[q[1]], px[q[2]], py[q[2]], rim); - drawLine(px[q[2]], py[q[2]], px[q[3]], py[q[3]], rim); - drawLine(px[q[3]], py[q[3]], px[q[0]], py[q[0]], rim); - } - } -} - -// Distance from the camera to a box center (for fog + depth sort). -static inline float boxDist(const Box &b) { - float dx = b.x - camPos[0], dy = b.y - camPos[1]; - float dz = (b.z0 + b.z1) * 0.5f - camPos[2]; - return sqrtf(dx * dx + dy * dy + dz * dz); -} - -// --- World state -------------------------------------------------------------- -static nd::DemoMapSource mapSource; - -struct Walker { - float ax, ay, bx, by; // patrol endpoints (world units, tile centers) - float t; // 0..1 along the route - float dir; // +1 or -1 - float speed; // tiles per second - uint8_t kind; -}; - -static const int MAX_WALKERS = nd::MAX_UNITS > 20 ? nd::MAX_UNITS : 20; -static Walker walkers[MAX_WALKERS]; -static int walkerCount = 0; - -static void buildWalkers() { - walkerCount = 0; - for (int i = 0; i < mapSource.spawnCount() && walkerCount < MAX_WALKERS; i++) { - const nd::UnitSpawn &s = mapSource.spawn(i); - Walker &w = walkers[walkerCount]; - w.ax = (s.tx % nd::TILE_COLS) + 0.5f; - w.ay = (s.tx / nd::TILE_COLS) + 0.5f; - if (s.patA >= 0 && s.patB >= 0) { - w.bx = (s.patA % nd::TILE_COLS) + 0.5f; - w.by = (s.patA / nd::TILE_COLS) + 0.5f; - } else { - w.bx = w.ax; w.by = w.ay; // stationary - } - w.t = 0; - w.dir = 1; - w.speed = (s.kind == 2) ? 0.0f : (s.kind == 3 ? 0.0f : 1.1f + (i % 3) * 0.15f); - w.kind = s.kind; - walkerCount++; - } -} - -static inline float lerp(float a, float b, float t) { return a + (b - a) * t; } - -// --- Setup -------------------------------------------------------------------- void setup() { - buildWalkers(); - // initial camera pose: south of the map, looking north at the deck - camPos[0] = 20.0f; camPos[1] = 12.0f - 22.0f; camPos[2] = 30.0f; + rvcore::rvInit(fb565); } -// --- Loop ---------------------------------------------------------------------- void loop() { - // --- input --- - static float orbitSpeed = 0.10f; // rad/s - static bool paused = false; + // --- keyboard pump --- while (!emu::keyboard.empty()) { uint8_t k = emu::keyboard.front(); emu::keyboard.pop_front(); - if (k == 'q' || k == 0x1b) { emu::quitRequested = true; } - if (k == 'a') { orbitSpeed += 0.03f; if (orbitSpeed > 0.60f) orbitSpeed = 0.60f; } - if (k == 'd') { orbitSpeed -= 0.03f; if (orbitSpeed < -0.60f) orbitSpeed = -0.60f; } - if (k == 'w') { FX += 18.0f; if (FX > 320.0f) FX = 320.0f; } - if (k == 's') { FX -= 18.0f; if (FX < 90.0f) FX = 90.0f; } - if (k == 'r') { camPos[2] += 2.0f; if (camPos[2] > 60.0f) camPos[2] = 60.0f; } - if (k == 'f') { camPos[2] -= 2.0f; if (camPos[2] < 14.0f) camPos[2] = 14.0f; } - if (k == ' ') paused = !paused; + rvcore::rvInput(k); } + if (rvcore::rvQuitRequested()) emu::quitRequested = true; - // --- delta time --- + // --- delta time: seconds since the previous frame --- static unsigned long lastMs = 0; unsigned long now = millis(); float dt = lastMs ? (now - lastMs) / 1000.0f : 1.0f / 60.0f; lastMs = now; - if (dt > 0.1f) dt = 0.1f; + if (dt > 0.1f) dt = 0.1f; // clamp after stalls (drag, breakpoint) - // --- camera drift: slow orbit + breathing radius/height --- - static float orbit = 0.0f; // angle around the map center - static float breathe = 0.0f; - if (!paused) { - orbit += orbitSpeed * dt; - breathe += dt * 0.35f; - } - float radius = 15.0f + 3.0f * sinf(breathe * 0.7f); - float height = 21.0f + 3.5f * sinf(breathe * 0.45f + 1.3f); - lookAt[0] = 20.0f + 1.5f * sinf(breathe * 0.5f); - lookAt[1] = 12.0f + 1.0f * cosf(breathe * 0.33f); - camPos[0] = lookAt[0] + cosf(orbit) * radius; - camPos[1] = lookAt[1] + sinf(orbit) * radius; - camPos[2] = height; - updateBasis(); - - // --- entities walk their patrol routes --- - if (!paused) { - for (int i = 0; i < walkerCount; i++) { - Walker &w = walkers[i]; - if (w.speed <= 0) continue; - float len = sqrtf((w.bx - w.ax) * (w.bx - w.ax) + (w.by - w.ay) * (w.by - w.ay)); - if (len < 0.01f) continue; - w.t += w.dir * w.speed * dt / len; - if (w.t > 1) { w.t = 1; w.dir = -1; } - if (w.t < 0) { w.t = 0; w.dir = 1; } - } - } - - // --- clear the 565 device framebuffer --- - for (int i = 0; i < W * H; i++) fb565[i] = COL_BG; - - // --- floor: flat lit quads for every non-wall tile, checker shaded --- - for (int ty = 0; ty < nd::TILE_ROWS; ty++) { - for (int tx = 0; tx < nd::TILE_COLS; tx++) { - char ch = mapSource.tileAt(tx, ty); - if (ch == '#') continue; - uint16_t base = ((tx + ty) & 1) ? COL_FLOOR_B : COL_FLOOR_A; - int px[4], py[4]; - float d; - bool ok = true; - const float OFF[4][2] = { {0,0},{1,0},{1,1},{0,1} }; - for (int k = 0; k < 4; k++) - if (!project((float)(tx + OFF[k][0]), (float)(ty + OFF[k][1]), 0.0f, px[k], py[k], d)) { ok = false; break; } - if (!ok) continue; - if (px[0] < -8 && px[1] < -8 && px[2] < -8 && px[3] < -8) continue; - if (px[0] > W + 8 && px[1] > W + 8 && px[2] > W + 8 && px[3] > W + 8) continue; - float cx = tx + 0.5f, cy = ty + 0.5f; - fillQuad((float)px[0], (float)py[0], (float)px[1], (float)py[1], - (float)px[2], (float)py[2], (float)px[3], (float)py[3], - shade565(base, fogFor(sqrtf((cx - camPos[0]) * (cx - camPos[0]) + - (cy - camPos[1]) * (cy - camPos[1]) + - camPos[2] * camPos[2])))); - } - } - - // --- collect solid geometry: walls, crates, door, terminal, node pylons --- - struct Solid { Box b; }; - static Solid solids[1024]; - int nSolids = 0; - - for (int ty = 0; ty < nd::TILE_ROWS; ty++) { - for (int tx = 0; tx < nd::TILE_COLS; tx++) { - char ch = mapSource.tileAt(tx, ty); - if (ch == '.') continue; - Box b; - b.x = tx + 0.5f; b.y = ty + 0.5f; - b.z0 = 0.0f; - b.hx = b.hy = 0.5f; - switch (ch) { - case '#': b.z1 = 1.6f; b.col = COL_WALL; break; - case 'o': b.z1 = 0.9f; b.col = COL_CRATE; break; - case '+': b.z1 = 1.3f; b.col = COL_DOOR; break; - case 'T': b.z1 = 0.7f; b.col = COL_TERMINAL; break; - case 'A': b.z1 = 0.15f; b.col = COL_AIRLOCK; break; - default: continue; - } - solids[nSolids++].b = b; - } - } - - // network nodes: glowing pylons at their pinned tiles (palette passthrough) - static Box nodeBoxes[nd::MAX_NODES]; - for (int i = 0; i < mapSource.nodeCount(); i++) { - const nd::NetNodeDef &n = mapSource.node(i); - Box &b = nodeBoxes[i]; - b.x = n.tx + 0.5f; b.y = n.ty + 0.5f; - b.hx = b.hy = 0.28f; - b.z0 = 0.0f; - b.z1 = 1.9f; - b.col = n.color; // already RGB565, straight from the map source - solids[nSolids++].b = b; - } - - // --- entities as small glowing boxes --- - static Box entBoxes[MAX_WALKERS]; - for (int i = 0; i < walkerCount; i++) { - const Walker &w = walkers[i]; - Box &b = entBoxes[i]; - b.x = lerp(w.ax, w.bx, w.t); - b.y = lerp(w.ay, w.by, w.t); - b.hx = b.hy = 0.30f; - b.z0 = 0.0f; - b.z1 = (w.kind == 3) ? 0.45f : (w.kind == 2 ? 0.85f : 0.65f); - b.col = (w.kind == 0) ? COL_MARINE : (w.kind == 1) ? COL_DROID - : (w.kind == 2) ? COL_HEAVY : COL_TURRET; - solids[nSolids++].b = b; - } - - // --- depth sort far -> near (painter's algorithm) --- - static float dists[1024 + nd::MAX_NODES + MAX_WALKERS]; - static int order[1024 + nd::MAX_NODES + MAX_WALKERS]; - for (int i = 0; i < nSolids; i++) { dists[i] = boxDist(solids[i].b); order[i] = i; } - for (int a = 1; a < nSolids; a++) { - int v = order[a], j = a - 1; - while (j >= 0 && dists[order[j]] < dists[v]) { order[j + 1] = order[j]; j--; } - order[j + 1] = v; - } - for (int i = 0; i < nSolids; i++) drawBox(solids[order[i]].b, dists[order[i]]); - - // --- network links + traveling packets (overhead lines between pylon tops) - for (int e = 0; e < mapSource.edgeCount(); e++) { - const nd::NetEdgeDef &ed = mapSource.edge(e); - const nd::NetNodeDef &na = mapSource.node(ed.a); - const nd::NetNodeDef &nb = mapSource.node(ed.b); - float ax = na.tx + 0.5f, ay = na.ty + 0.5f; - float bx = nb.tx + 0.5f, by = nb.ty + 0.5f; - int x0, y0, x1, y1; - float d0, d1; - if (!project(ax, ay, 1.9f, x0, y0, d0)) continue; - if (!project(bx, by, 1.9f, x1, y1, d1)) continue; - drawLine(x0, y0, x1, y1, shade565(COL_LINK, fogFor((d0 + d1) * 0.5f))); - // packet: a small bright dot sweeping the edge - float ph = fmodf(now / 1000.0f * 0.35f + e * 0.37f, 1.0f); - int pkx, pky; - float pdd; - if (project(lerp(ax, bx, ph), lerp(ay, by, ph), 1.9f + 0.15f, pkx, pky, pdd)) { - int r = (int)(0.14f * FX / pdd); - if (r < 1) r = 1; - if (r > 4) r = 4; - fillCircle(pkx, pky, r, shade565(COL_PACKET, fogFor(pdd))); - } - } - - // --- pulsing halo over the airlock tile (squad entry marker) --- - { - int ax_, ay_; - float dd; - const nd::NetNodeDef &root = mapSource.node(mapSource.rootNode()); - if (project(root.tx + 0.5f, root.ty + 0.5f, 0.2f, ax_, ay_, dd)) { - int r = (int)(0.55f * FX / dd) + (int)(2.0f * sinf(now / 1000.0f * 3.0f)); - if (r > 2) fillCircle(ax_, ay_, r, shade565(COL_AIRLOCK, 11)); - } - } + rvcore::rvFrame(dt, now); // --- "SPI push": expand the 565 device buffer into the emulator's 8888 --- - // texture. On the T-Deck this step doesn't exist — fb565 is what the - // panel receives. This is purely a desktop-presentation shim. - for (int i = 0; i < W * H; i++) { + // texture. On the T-Deck this step doesn't exist — the 565 buffer is + // what the panel receives. This is purely a desktop-presentation shim. + for (int i = 0; i < rvcore::W * rvcore::H; i++) { uint16_t c = fb565[i]; uint32_t r5 = (c >> 11) & 31, g6 = (c >> 5) & 63, b5 = c & 31; uint32_t r = (r5 << 3) | (r5 >> 2); // 5 -> 8 bit expansion diff --git a/tron-room-demo/fw/RoomViewHW.ino b/tron-room-demo/fw/RoomViewHW.ino new file mode 100644 index 0000000..81acc47 --- /dev/null +++ b/tron-room-demo/fw/RoomViewHW.ino @@ -0,0 +1,124 @@ +/** + * RoomViewHW — RoomView on the real T-Deck (ESP32-S3 + ST7789), with + * frame-time instrumentation so you can measure how quickly it runs. + * + * This is the firmware twin of the emulator sketch + * (examples/RoomView/RoomView.cpp): both share the device-agnostic + * RoomViewCore.h renderer, which draws the whole scene in RGB565 into a + * single 320x240 buffer. Here that buffer IS a TFT_eSprite's + * frameBuffer() — the rasterizer writes straight into the sprite, and + * the sprite is pushed to the panel with the proven NetDeck M1 + * anti-flicker pattern (startWrite / pushSprite / endWrite). + * + * Build & flash (from the repo root): + * pio run -e RoomViewHW -t upload + * pio device monitor # 115200 baud + * + * The loop is deliberately free-running (no pacing): the serial report + * every 2 s is the true throughput of the renderer + SPI push on this + * hardware: + * + * RoomViewHW: 120 frames in 2001ms = 60.0 fps | render 12.3 ms, push 14.8 ms + * + * No input is wired: the camera drifts on its own orbit. Keys (a/d, w/s, + * r/f, space) only work in the emulator build. + */ + +#include +#include +#include +#include "utilities.h" +#include "RoomViewCore.h" + +TFT_eSPI tft; +static TFT_eSprite fb(&tft); // full-frame 320x240x16 buffer (PSRAM) + +// --- frame-time accounting --------------------------------------------------- +static uint32_t accRenderUs = 0; +static uint32_t accPushUs = 0; +static uint32_t accFrames = 0; +static uint32_t reportT0 = 0; + +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.println("RoomViewHW — NetDeck demo map, orbiting 3D view (RGB565)"); + Serial.println("Free-running benchmark: report every 2 s over USB serial."); + + // Proven T-Deck bring-up (same sequence as NetDeck.ino hardwareInit) + pinMode(BOARD_POWERON, OUTPUT); + digitalWrite(BOARD_POWERON, HIGH); + + pinMode(BOARD_SDCARD_CS, OUTPUT); + pinMode(RADIO_CS_PIN, OUTPUT); + pinMode(BOARD_TFT_CS, OUTPUT); + digitalWrite(BOARD_SDCARD_CS, HIGH); + digitalWrite(RADIO_CS_PIN, HIGH); + digitalWrite(BOARD_TFT_CS, HIGH); + + pinMode(BOARD_SPI_MISO, INPUT_PULLUP); + SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI); + + tft.begin(); + tft.setRotation(1); // 320x240 landscape + tft.fillScreen(TFT_BLACK); + + pinMode(BOARD_BL_PIN, OUTPUT); + digitalWrite(BOARD_BL_PIN, HIGH); + + // Full-frame 16-bit sprite; the renderer draws directly into its buffer. + fb.setColorDepth(16); + if (!fb.createSprite(320, 240)) { + Serial.println("RoomViewHW: framebuffer alloc failed (stopping)"); + while (true) delay(1000); + } + uint16_t *buf = (uint16_t *)fb.frameBuffer(0); + if (!buf) { + Serial.println("RoomViewHW: frameBuffer() unavailable (stopping)"); + while (true) delay(1000); + } + rvcore::rvInit(buf); + + reportT0 = millis(); +} + +void loop() +{ + static uint32_t lastUs = 0; + uint32_t t0 = micros(); + + // Delta time from real elapsed time — motion speed stays correct no + // matter the achieved fps. + float dt = lastUs ? (t0 - lastUs) / 1e6f : 1.0f / 60.0f; + lastUs = t0; + if (dt > 0.1f) dt = 0.1f; + + rvcore::rvFrame(dt, millis()); + uint32_t renderUs = micros() - t0; + + // Panel push: the M1 anti-flicker pattern (NetDeck.ino present()). + uint32_t p0 = micros(); + tft.startWrite(); + fb.pushSprite(0, 0); + tft.endWrite(); + uint32_t pushUs = micros() - p0; + + // --- accounting --- + accRenderUs += renderUs; + accPushUs += pushUs; + accFrames++; + + uint32_t elapsed = millis() - reportT0; + if (elapsed >= 2000) { + float fps = accFrames * 1000.0f / elapsed; + float avgRender = accFrames ? accRenderUs / (float)accFrames / 1000.0f : 0.0f; + float avgPush = accFrames ? accPushUs / (float)accFrames / 1000.0f : 0.0f; + Serial.printf("RoomViewHW: %lu frames in %lu ms = %.1f fps | " + "render %.1f ms, push %.1f ms\n", + (unsigned long)accFrames, (unsigned long)elapsed, fps, + avgRender, avgPush); + reportT0 = millis(); + accRenderUs = accPushUs = accFrames = 0; + } +} diff --git a/tron-room-demo/fw/platformio.ini b/tron-room-demo/fw/platformio.ini new file mode 100644 index 0000000..e878fd6 --- /dev/null +++ b/tron-room-demo/fw/platformio.ini @@ -0,0 +1,34 @@ +; PlatformIO project for the tron-room-demo hardware benchmark. +; +; Standalone on purpose (the repo's top-level platformio.ini keeps +; src_dir = src/netdeck for NetDeck): this points back at the shared +; boards/, lib/ and src/netdeck/ headers, so the firmware twin of the +; emulator sketch builds without touching anything NetDeck depends on. +; +; pio run -d tron-room-demo/fw -t upload # flash the T-Deck +; pio device monitor # fps report every 2 s +; +; Free-running benchmark: no frame pacing. The serial line +; RoomViewHW: N frames in M ms = X fps | render Y ms, push Z ms +; is the true throughput of the shared RoomViewCore renderer + SPI push. + +[platformio] +default_envs = RoomViewHW +src_dir = . +boards_dir = ../../boards +lib_dir = ../../lib + +[env:RoomViewHW] +platform = espressif32@6.3.0 +board = T-Deck +framework = arduino +upload_speed = 921600 +monitor_speed = 115200 +build_flags = + -DBOARD_HAS_PSRAM=1 + -DCORE_DEBUG_LEVEL=1 + -DARDUINO_USB_CDC_ON_BOOT=1 + -DDISABLE_ALL_LIBRARY_WARNINGS + -DSPI_FREQUENCY=80000000 + -I../../src/netdeck + -I..