/** * TronRoom — TRON-like scanlines pseudo-3D render of a room * with blocks and entities. * * Target: T-Deck emulator (320x240, ABGR8888 framebuffer). * * 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) * - 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) * * All motion is delta-time based (units per second scaled by dt), so the * speed is the same no matter what fps the host actually runs at. */ #include "shim.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 int HORIZON = 70; static constexpr float K = 170.0f; // perspective constant (camera height * focal) static constexpr float FX = 140.0f; // horizontal focal static constexpr float WALL_X = 6.0f; static constexpr float CEIL_H = 2.5f; static constexpr float FAR_Z = 24.0f; static constexpr float NEAR_Z = 1.2f; // --- Drawing helpers --------------------------------------------------------- static inline void putPixel(int x, int y, uint32_t c) { if (x < 0 || x >= W || y < 0 || y >= H) return; emuGfx::fb[y * W + x] = c; } static void drawLine(int x0, int y0, int x1, int y1, uint32_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, uint32_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); } } } // Project a 3D point (X,Y,Z) to screen. Y up. Returns false if behind camera. static bool project(float X, float Y, float Z, int &sx, int &sy) { if (Z < 0.5f) return false; sx = (int)(160.0f + X * FX / Z); sy = (int)(HORIZON + (K - Y * FX) / Z); return true; } // --- World state ------------------------------------------------------------ struct Block { float x, z; // position on floor (center) float size; // half-extent float rot; // yaw float rotSpeed; uint32_t color; }; struct Entity { float x, y, z; float vx, vy, vz; uint32_t color; int trailLen; }; static const int NUM_BLOCKS = 6; static Block blocks[NUM_BLOCKS]; static const int NUM_ENTS = 5; static Entity ents[NUM_ENTS]; static float camSway = 0.0f; static unsigned long lastMs = 0; // --- Setup ------------------------------------------------------------------ void setup() { randomSeed(millis()); for (int i = 0; i < NUM_BLOCKS; i++) { blocks[i].x = (random(200) / 100.0f - 1.0f) * 4.0f; blocks[i].z = 3.0f + (float)(i) * 3.5f + random(100) / 50.0f; if (blocks[i].z > FAR_Z - 2) blocks[i].z = FAR_Z - 2; blocks[i].size = 0.4f + random(60) / 100.0f; blocks[i].rot = random(628) / 100.0f; blocks[i].rotSpeed = (random(200) - 100) * 0.012f; // rad/s (-1.2..1.2) uint32_t cols[3] = { 0xFFFF00FF, 0xFFFFFF00, 0xFF00FFFF }; // magenta/yellow/cyan blocks[i].color = cols[i % 3]; } for (int i = 0; i < NUM_ENTS; i++) { ents[i].x = (random(200) / 100.0f - 1.0f) * 3.0f; ents[i].y = 0.3f + random(80) / 100.0f; ents[i].z = 4.0f + random(100) / 25.0f; ents[i].vx = (random(200) - 100) * 0.03f; // units/s (-3..3) ents[i].vy = (random(100) - 50) * 0.012f; // units/s ents[i].vz = -0.72f - random(20) * 0.03f; // units/s toward camera uint32_t cols[2] = { 0xFF00FFFF, 0xFFFFFF00 }; ents[i].color = cols[i % 2]; ents[i].trailLen = 0; } } // --- Helpers to draw a cube (wireframe) ------------------------------------ static void drawCube(float cx, float cy, float cz, float s, float rot, uint32_t color) { // 8 corners in local space, then rotate around Y, translate float c = cosf(rot), sn = sinf(rot); float corners[8][3] = { {-s,-s,-s},{s,-s,-s},{s,s,-s},{-s,s,-s}, {-s,-s,s},{s,-s,s},{s,s,s},{-s,s,s} }; int sx[8], sy[8]; bool vis[8]; for (int i = 0; i < 8; i++) { float lx = corners[i][0], ly = corners[i][1], lz = corners[i][2]; // rotate around Y float rx = lx * c + lz * sn; float rz = -lx * sn + lz * c; float wx = cx + rx; float wy = cy + ly; float wz = cz + rz; vis[i] = project(wx, wy, wz, sx[i], sy[i]); } static const int edges[12][2] = { {0,1},{1,2},{2,3},{3,0}, {4,5},{5,6},{6,7},{7,4}, {0,4},{1,5},{2,6},{3,7} }; for (int e = 0; e < 12; e++) { int a = edges[e][0], b = edges[e][1]; if (vis[a] && vis[b]) drawLine(sx[a], sy[a], sx[b], sy[b], color); } } // --- Loop ------------------------------------------------------------------- void loop() { // Input while (!emu::keyboard.empty()) { uint8_t k = emu::keyboard.front(); emu::keyboard.pop_front(); if (k == 'q' || k == 0x1b) emu::quitRequested = true; } // 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) // Update (all speeds are in units per second) camSway += 0.09f * dt; for (int i = 0; i < NUM_BLOCKS; i++) { blocks[i].rot += blocks[i].rotSpeed * dt; } for (int i = 0; i < NUM_ENTS; i++) { ents[i].x += ents[i].vx * dt; ents[i].y += ents[i].vy * dt; ents[i].z += ents[i].vz * dt; if (ents[i].y < 0.2f || ents[i].y > 1.2f) ents[i].vy = -ents[i].vy; if (ents[i].x < -4.5f || ents[i].x > 4.5f) ents[i].vx = -ents[i].vx; if (ents[i].z < NEAR_Z) { // respawn at far ents[i].x = (random(200) / 100.0f - 1.0f) * 3.0f; ents[i].y = 0.3f + random(80) / 100.0f; ents[i].z = FAR_Z - 1.0f; ents[i].vz = -0.72f - random(20) * 0.03f; // units/s toward camera } } // Clear framebuffer to deep blue-black for (int i = 0; i < W * H; i++) emuGfx::fb[i] = 0xFF080018; // --- Floor scanlines (cyan grid on dark) --- for (int y = HORIZON + 1; y < H; y++) { float z = K / (float)(y - HORIZON); if (z > FAR_Z) z = FAR_Z; float fade = 1.0f - (float)(y - HORIZON) / (float)(H - HORIZON); if (fade < 0.15f) fade = 0.15f; int v = (int)(220 * fade); if (v > 255) v = 255; for (int x = 0; x < W; x++) { float wx = (x - 160) * z / FX; float fx = fabsf(wx - floorf(wx + 0.5f)); float fzfrac = fabsf(z - floorf(z + 0.5f)); bool onGrid = (fx < 0.06f) || (fzfrac < 0.06f); uint32_t col; if (onGrid) { // cyan in ABGR = 0xFFFFFF00 col = (0xFFu << 24) | ((uint32_t)v << 8) | ((uint32_t)v << 16); } else { col = 0xFF000010u; } emuGfx::fb[y * W + x] = col; } } // --- Ceiling scanlines (magenta grid) --- for (int y = 0; y < HORIZON; y++) { float dy = (float)(HORIZON - y); if (dy < 1) dy = 1; float z = K / dy; if (z > FAR_Z) z = FAR_Z; for (int x = 0; x < W; x++) { float wx = (x - 160) * z / FX; float fx = fabsf(wx - floorf(wx + 0.5f)); float fzfrac = fabsf(z - floorf(z + 0.5f)); bool onGrid = (fx < 0.06f) || (fzfrac < 0.06f); if (!onGrid) { emuGfx::fb[y * W + x] = 0xFF040008; continue; } float fade = 1.0f - (float)(HORIZON - y) / (float)HORIZON; if (fade < 0.15f) fade = 0.15f; int v = (int)(180 * fade); if (v > 255) v = 255; // magenta in ABGR = 0xFFFF00FF uint32_t col = (0xFFu << 24) | ((uint32_t)v << 16) | ((uint32_t)v << 0); // B? // magenta = R+B: 0xFF00FF => ABGR 0xFFFF00FF col = (0xFFu << 24) | ((uint32_t)v << 0) | ((uint32_t)v << 16); emuGfx::fb[y * W + x] = col; } } // --- Walls (left/right/back) as wireframe grids --- // Vertical side edges // Draw grid lines along z for left/right walls int prevLX=0, prevLYf=0, prevLYc=0, prevRX=0, prevRYf=0, prevRYc=0; bool first = true; for (float z = NEAR_Z; z <= FAR_Z; z += 0.5f) { int lx, lyf, lyc, rx, ryf, ryc; if (!project(-WALL_X, 0, z, lx, lyf)) continue; if (!project(-WALL_X, CEIL_H, z, lx, lyc)) lyc = lyf; if (!project( WALL_X, 0, z, rx, ryf)) continue; if (!project( WALL_X, CEIL_H, z, rx, ryc)) ryc = ryf; // verticals drawLine(lx, lyf, lx, lyc, 0xFF4040FF); // blue-ish? use cyan dim drawLine(rx, ryf, rx, ryc, 0xFF4040FF); // connect along z if (!first) { drawLine(prevLX, prevLYf, lx, lyf, 0xFF303080); drawLine(prevLX, prevLYc, lx, lyc, 0xFF303080); drawLine(prevRX, prevRYf, rx, ryf, 0xFF303080); drawLine(prevRX, prevRYc, rx, ryc, 0xFF303080); } prevLX=lx; prevLYf=lyf; prevLYc=lyc; prevRX=rx; prevRYf=ryf; prevRYc=ryc; first=false; } // back wall { int x0,y0,x1,y1; if (project(-WALL_X,0,FAR_Z,x0,y0) && project(WALL_X,0,FAR_Z,x1,y1)) drawLine(x0,y0,x1,y1,0xFF303080); if (project(-WALL_X,CEIL_H,FAR_Z,x0,y0) && project(WALL_X,CEIL_H,FAR_Z,x1,y1)) drawLine(x0,y0,x1,y1,0xFF303080); if (project(-WALL_X,0,FAR_Z,x0,y0) && project(-WALL_X,CEIL_H,FAR_Z,x1,y1)) drawLine(x0,y0,x1,y1,0xFF303080); if (project(WALL_X,0,FAR_Z,x0,y0) && project(WALL_X,CEIL_H,FAR_Z,x1,y1)) drawLine(x0,y0,x1,y1,0xFF303080); } // --- Horizon line --- for (int x = 0; x < W; x++) emuGfx::fb[HORIZON * W + x] = 0xFF404040; // --- Blocks --- for (int i = 0; i < NUM_BLOCKS; i++) { drawCube(blocks[i].x, blocks[i].size, blocks[i].z, blocks[i].size, blocks[i].rot, blocks[i].color); } // --- Entities with glow --- for (int i = 0; i < NUM_ENTS; i++) { int sx, sy; if (!project(ents[i].x, ents[i].y, ents[i].z, sx, sy)) continue; int r = (int)(6.0f + 2.0f * (FAR_Z - ents[i].z)); if (r < 2) r = 2; if (r > 12) r = 12; // glow fillCircle(sx, sy, r + 2, (ents[i].color & 0x00FFFFFF) | 0x40000000); fillCircle(sx, sy, r, ents[i].color); // trail line downward to floor int fx, fy; if (project(ents[i].x, 0, ents[i].z, fx, fy)) { drawLine(sx, sy, fx, fy, (ents[i].color & 0x00FFFFFF) | 0x60000000); } } emuGfx::dirty = true; }