Files
Dejvino 0f22c6afca Add raylib-esp32 lib and FloorPlan3D demo (3D floor plan via rlsw)
Vendor raylib-esp32 v1.0.3 (63db94b) under lib/ — raylib 6.x core with
the rlsw software renderer plus the ESP32-S3 port layer (SPI display
drivers, async double-buffered flush). raylib/examples, projects, tools
and logo are trimmed (~80 MB of content nothing here compiles); see
lib/raylib-esp32/VENDOR.md for provenance.

New floorplan3d-demo (emulator-template base) renders the NetDeck demo
map (nd_demomap.h) as a 3D floor plan — bulkhead walls, blast door,
cover crates, airlock pad, terminal — through the real on-device 3D
stack on the desktop:

- rcore_emu.c: desktop PLATFORM_CUSTOM layer mirroring rcore_esp32.c,
  flushing the RGB565 sw-renderer buffer into emuGfx::fb instead of SPI
- raylib_emu_config.h: same module/limit defines as the device build
- HUD via 3x5 bitmap font into fb: rlsw's 2D textured-quad path
  (DrawText/DrawRectangle) misplaces/flips quads and samples wrong
  atlas texels in this build; the 3D path is pixel-accurate

Verified: dual build (make netdeck) green, demo smoke test clean at
~59 FPS, 600-frame stability run OK.
2026-11-16 21:46:31 +01:00

315 lines
12 KiB
C++

/**
* FloorPlan3D — NetDeck demo map rendered as a 3D floor plan with raylib.
*
* Uses raylib-esp32's raylib 6.x core + rlsw software renderer (the exact
* same library and renderer that runs on the T-Deck's ESP32-S3 — see
* lib/raylib-esp32), driven by the emulator-template SDL host. The platform
* layer (rcore_emu.c) hands the RGB565 software-renderer framebuffer to
* emu_raylib_present() below, which blits it into emuGfx::fb.
*
* Content: nd::DEMO_MAP from src/netdeck (the NetDeck "Breach & Board"
* demo ship: airlock, L-shaped main corridor, server room) extruded into
* 3D — bulkhead walls, blast door, cover crates, airlock pad, terminal.
*
* Rendering note: only the 3D pipeline (cubes/planes/lines, vertex colors)
* is used. The rlsw 2D quad path (DrawText / DrawRectangle with the font
* atlas) misplaces and corrupts quads in this build, so the HUD is drawn
* with a tiny bitmap font directly into emuGfx::fb after the raylib frame.
*
* Controls (emulator keyboard queue): a/d spin the camera, w/s tilt,
* q/e zoom, r reset.
*/
#include "shim.h"
#include "raylib.h"
#include "../../../src/netdeck/nd_demomap.h"
#include <deque>
#include <cmath>
#include <cstdio>
// Emulator state provided by the host (src/main.cpp)
extern uint32_t emuGfx::fb[320 * 240];
extern bool emuGfx::dirty;
extern std::deque<uint8_t> emu::keyboard;
// Platform layer hooks (rcore_emu.c)
extern "C" void emu_raylib_set_framebuffer(int w, int h);
// ---------------------------------------------------------------------------
// RGB565 → ABGR8888 blit: the software renderer's buffer lands in the
// emulator framebuffer. Called from rcore_emu.c's SwapScreenBuffer().
// ---------------------------------------------------------------------------
extern "C" void emu_raylib_present(const unsigned short *rgb565, int w, int h)
{
if (w != 320 || h != 240) return;
for (int i = 0; i < 320 * 240; i++) {
uint16_t px = rgb565[i];
uint8_t r5 = (px >> 11) & 0x1F;
uint8_t g6 = (px >> 5) & 0x3F;
uint8_t b5 = px & 0x1F;
// Bit-replication expansion 5→8 / 6→8
uint8_t r = (uint8_t)((r5 << 3) | (r5 >> 2));
uint8_t g = (uint8_t)((g6 << 2) | (g6 >> 4));
uint8_t b = (uint8_t)((b5 << 3) | (b5 >> 2));
emuGfx::fb[i] = 0xFF000000u | ((uint32_t)b << 16) | ((uint32_t)g << 8) | r;
}
emuGfx::dirty = true;
}
// ---------------------------------------------------------------------------
// HUD drawing — tiny 3x5 bitmap font straight into emuGfx::fb.
// (Pattern borrowed from cyberspace-demo; raylib's 2D quad path is
// unreliable under rlsw, see note at the top.)
// ---------------------------------------------------------------------------
static constexpr int W = 320;
static constexpr int H = 240;
static uint32_t rgb(uint8_t r, uint8_t g, uint8_t b)
{
return 0xFF000000u | ((uint32_t)b << 16) | ((uint32_t)g << 8) | r;
}
#define F3x5(a, b, c, d, e) \
((uint16_t)(((a) << 12) | ((b) << 9) | ((c) << 6) | ((d) << 3) | (e)))
static uint16_t glyph(char ch)
{
switch (ch) {
case 'A': return F3x5(7, 5, 7, 5, 5);
case 'B': return F3x5(6, 5, 6, 5, 6);
case 'C': return F3x5(7, 4, 4, 4, 7);
case 'D': return F3x5(6, 5, 5, 5, 6);
case 'E': return F3x5(7, 4, 7, 4, 7);
case 'F': return F3x5(7, 4, 7, 4, 4);
case 'G': return F3x5(7, 4, 5, 5, 7);
case 'H': return F3x5(5, 5, 7, 5, 5);
case 'I': return F3x5(7, 2, 2, 2, 7);
case 'L': return F3x5(4, 4, 4, 4, 7);
case 'M': return F3x5(5, 7, 7, 5, 5);
case 'N': return F3x5(6, 5, 5, 5, 5);
case 'O': return F3x5(7, 5, 5, 5, 7);
case 'P': return F3x5(7, 5, 7, 4, 4);
case 'R': return F3x5(7, 5, 6, 5, 5);
case 'S': return F3x5(7, 4, 7, 1, 7);
case 'T': return F3x5(7, 2, 2, 2, 2);
case 'U': return F3x5(5, 5, 5, 5, 7);
case 'W': return F3x5(5, 5, 7, 7, 5);
case 'X': return F3x5(5, 5, 2, 5, 5);
case 'Y': return F3x5(5, 5, 2, 2, 2);
case 'Z': return F3x5(7, 1, 2, 4, 7);
case '0': return F3x5(7, 5, 5, 5, 7);
case '1': return F3x5(2, 6, 2, 2, 7);
case '2': return F3x5(7, 1, 7, 4, 7);
case '3': return F3x5(7, 1, 7, 1, 7);
case '4': return F3x5(5, 5, 7, 1, 1);
case '5': return F3x5(7, 4, 7, 1, 7);
case '6': return F3x5(4, 4, 7, 5, 7);
case '7': return F3x5(7, 1, 1, 2, 2);
case '8': return F3x5(7, 5, 7, 5, 7);
case '9': return F3x5(7, 5, 7, 1, 7);
case '/': return F3x5(1, 1, 2, 4, 4);
case '-': return F3x5(0, 0, 7, 0, 0);
case ':': return F3x5(0, 2, 0, 2, 0);
case '.': return F3x5(0, 0, 0, 0, 2);
case ' ': default: return 0;
}
}
static void pset(int x, int y, uint32_t c)
{
if (x >= 0 && x < W && y >= 0 && y < H) emuGfx::fb[y * W + x] = c;
}
static void drawText(int x, int y, const char *s, uint32_t c, int scale = 1)
{
while (*s) {
if (*s != ' ') {
uint16_t g = glyph(*s);
for (int r = 0; r < 5; r++) {
int bits = (g >> (12 - 3 * r)) & 7;
for (int col = 0; col < 3; col++) {
if (!(bits & (4 >> col))) continue;
for (int py = 0; py < scale; py++)
for (int px = 0; px < scale; px++)
pset(x + col * scale + px, y + r * scale + py, c);
}
}
}
x += 4 * scale;
s++;
}
}
// ---------------------------------------------------------------------------
// Demo state
// ---------------------------------------------------------------------------
static constexpr float TILE_W = 1.0f; // one tile = 1 world unit
static constexpr float MAP_W = nd::TILE_COLS * TILE_W; // 40
static constexpr float MAP_H = nd::TILE_ROWS * TILE_W; // 24
static constexpr float WALL_H = 1.6f;
// Camera orbit parameters (auto-rotating inspection tour)
static float camYawDeg = -35.0f; // rotation around the map
static float camPitchDeg = 55.0f; // tilt down from horizontal
static float camDist = 44.0f; // distance from map center
static float yawSpeed = 14.0f; // deg/s auto-spin
static bool autoSpin = true;
static unsigned long lastMs = 0;
static unsigned long fpsFrames = 0, fpsT0 = 0, fpsShow = 0;
// Palette (NetDeck-ish: steel walls, cyan network accents)
static const Color COL_FLOOR = { 38, 44, 54, 255 };
static const Color COL_WALL = { 124, 134, 142, 255 };
static const Color COL_WALLTOP = { 168, 178, 186, 255 };
static const Color COL_DOOR = { 232, 62, 48, 255 };
static const Color COL_CRATE = { 240, 156, 40, 255 };
static const Color COL_AIRLOCK = { 46, 196, 118, 255 };
static const Color COL_TERM = { 72, 207, 229, 255 };
static const Color COL_SKY = { 18, 20, 28, 255 };
static const uint32_t HUD_MAIN = rgb( 72, 207, 229);
static const uint32_t HUD_DIM = rgb(150, 158, 168);
static const uint32_t HUD_BAR = rgb( 12, 14, 20);
static const uint32_t HUD_OK = rgb( 46, 196, 118);
static void tileToWorld(float tx, float ty, float *wx, float *wz)
{
*wx = (tx - nd::TILE_COLS / 2.0f + 0.5f) * TILE_W;
*wz = (ty - nd::TILE_ROWS / 2.0f + 0.5f) * TILE_W;
}
static void applyCamera(Camera3D *cam)
{
float yaw = camYawDeg * DEG2RAD;
float pitch = camPitchDeg * DEG2RAD;
float cx = cosf(pitch) * sinf(yaw) * camDist;
float cy = sinf(pitch) * camDist;
float cz = cosf(pitch) * cosf(yaw) * camDist;
cam->position = (Vector3){ cx, cy, cz };
cam->target = (Vector3){ 0.0f, 0.0f, 0.0f };
cam->up = (Vector3){ 0.0f, 1.0f, 0.0f };
cam->fovy = 45.0f;
cam->projection = CAMERA_PERSPECTIVE;
}
static void drawHud()
{
// Bottom strip (NetDeck layout: 320x48 HUD bar under the tactical view)
for (int y = nd::VIEW_H; y < H; y++)
for (int x = 0; x < W; x++) pset(x, y, HUD_BAR);
drawText(8, nd::VIEW_H + 8, "NETDECK :: BREACH & BOARD", HUD_MAIN);
drawText(8, nd::VIEW_H + 24, "A/D SPIN W/S TILT Q/E ZOOM R RESET", HUD_DIM);
drawText(262, nd::VIEW_H + 8, "RLSW 3D", HUD_DIM);
char buf[16];
snprintf(buf, sizeof(buf), "%lu FPS", (unsigned long)fpsShow);
drawText(262, nd::VIEW_H + 24, buf, HUD_OK);
}
void setup()
{
emu_raylib_set_framebuffer(W, H); // platform layer needs the size first
InitWindow(W, H, "FloorPlan3D");
SetTargetFPS(60);
}
void loop()
{
// Delta time (seconds since previous frame), clamped after stalls
unsigned long now = millis();
float dt = lastMs ? (now - lastMs) / 1000.0f : 1.0f / 60.0f;
lastMs = now;
if (dt > 0.1f) dt = 0.1f;
// FPS counter (1 s window, measured with millis() like on hardware)
fpsFrames++;
if (!fpsT0) fpsT0 = now;
if (now - fpsT0 >= 1000) {
fpsShow = fpsFrames * 1000 / (now - fpsT0);
fpsFrames = 0; fpsT0 = now;
}
// --- Keyboard (emulator queue: 'a', 'd', ..., 0x08, 0x7F) ---
while (!emu::keyboard.empty()) {
uint8_t k = emu::keyboard.front();
emu::keyboard.pop_front();
switch (k) {
case 'a': camYawDeg -= 15.0f; autoSpin = false; break;
case 'd': camYawDeg += 15.0f; autoSpin = false; break;
case 'w': camPitchDeg += 6.0f; if (camPitchDeg > 85) camPitchDeg = 85; break;
case 's': camPitchDeg -= 6.0f; if (camPitchDeg < 15) camPitchDeg = 15; break;
case 'q': camDist += 2.0f; if (camDist > 60) camDist = 60; break;
case 'e': camDist -= 2.0f; if (camDist < 8) camDist = 8; break;
case 'r': camYawDeg = -35; camPitchDeg = 55; camDist = 44; autoSpin = true; break;
default: break;
}
}
if (autoSpin) camYawDeg += yawSpeed * dt;
// --- Render the 3D floor plan through raylib + rlsw ---
Camera3D cam;
applyCamera(&cam);
BeginDrawing();
ClearBackground(COL_SKY);
BeginMode3D(cam);
// Floor slab
DrawPlane((Vector3){ 0, 0, 0 }, (Vector2){ MAP_W, MAP_H }, COL_FLOOR);
// Map grid aligned with tile boundaries
DrawGrid((int)fmaxf(MAP_W, MAP_H), TILE_W);
// Extrude the tile map
for (int ty = 0; ty < nd::TILE_ROWS; ty++) {
for (int tx = 0; tx < nd::TILE_COLS; tx++) {
char c = nd::DEMO_MAP[ty][tx];
if (c == ' ') continue;
float wx, wz;
tileToWorld((float)tx, (float)ty, &wx, &wz);
switch (c) {
case '#': // bulkhead wall
DrawCube((Vector3){ wx, WALL_H / 2, wz },
TILE_W, WALL_H, TILE_W, COL_WALL);
break;
case '+': // blast door
DrawCube((Vector3){ wx, WALL_H / 2, wz },
TILE_W * 0.9f, WALL_H, TILE_W * 0.9f, COL_DOOR);
break;
case 'o': // cover crate
DrawCube((Vector3){ wx, 0.35f, wz },
TILE_W * 0.7f, 0.7f, TILE_W * 0.7f, COL_CRATE);
break;
case 'A': // airlock pad
DrawCube((Vector3){ wx, 0.05f, wz },
TILE_W * 0.95f, 0.1f, TILE_W * 0.95f, COL_AIRLOCK);
break;
case 'T': // terminal
DrawCube((Vector3){ wx, 0.25f, wz },
TILE_W * 0.8f, 0.5f, TILE_W * 0.8f, COL_TERM);
break;
default: break;
}
}
}
// Wall tops: a lighter cap plate makes the wall run read at a glance
for (int ty = 0; ty < nd::TILE_ROWS; ty++) {
for (int tx = 0; tx < nd::TILE_COLS; tx++) {
if (nd::DEMO_MAP[ty][tx] != '#') continue;
float wx, wz;
tileToWorld((float)tx, (float)ty, &wx, &wz);
DrawCube((Vector3){ wx, WALL_H + 0.03f, wz },
TILE_W, 0.06f, TILE_W, COL_WALLTOP);
}
}
EndMode3D();
EndDrawing();
// --- HUD: drawn straight into the emulator framebuffer ---
drawHud();
}