Files
lilygo-t-deck/floorplan3d-demo/src/main.cpp
T
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

240 lines
8.6 KiB
C++

/**
* Minimal SDL3 host for the T-Deck emulator-only template.
*
* Sets up an SDL3 window/renderer, presents the user's framebuffer,
* and calls the user's sketch setup() / loop() each frame.
*
* This file is deliberately hardware-free: no TFT_eSPI, no SPI,
* no I2S, no GPIO matrix. It only depends on SDL3 and the shim.
*/
#include <SDL3/SDL.h>
#if __has_include(<SDL3/SDL_ttf.h>)
#include <SDL3/SDL_ttf.h>
#else
#include <SDL3_ttf/SDL_ttf.h>
#endif
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
// --- Include the template's minimal shim (provides millis(), random,
// randomSeed(), String, pinMode/digitalRead, etc.) ---
#include "shim.h"
// --- Emulator shared state (defined in main.cpp below) ---
// The sketch writes to emuGfx::fb; this file presents it each frame.
// ---------------------------------------------------------------------------
namespace emu {
uint8_t pinLevels[64];
const char *pinDrivenBy[64];
std::deque<PinEdge> pinEdges;
bool touchDown = false;
int touchX = 0, touchY = 0;
std::deque<uint8_t> keyboard;
bool quitRequested = false;
} // namespace emu
namespace emuGfx {
uint32_t fb[320 * 240]; // ABGR8888 pixels, 320x240
SDL_Texture *tex = nullptr;
bool dirty = true;
} // namespace emuGfx
// --- Forward declarations of the user's sketch functions ---
// These will be defined in the sketch .cpp file included below.
extern "C" void setup();
extern "C" void loop();
// --- Include the user's sketch .cpp file ---
// The path comes from the Makefile: -DAPP_SRC='"examples/$(APP)/$(APP).cpp"'.
// Change APP (or replace examples/Demo/Demo.cpp) to run your own sketch.
#ifndef APP_SRC
#define APP_SRC "examples/Demo/Demo.cpp"
#endif
#include APP_SRC
// ---------------------------------------------------------------------------
// --- Main entry point ---
int main(int argc, char **argv)
{
// Parse simple command-line options
long maxFrames = -1; // --frames N: run exactly N frames then exit
const char *dumpPath = nullptr;
bool userRun = false; // --user-run: disable the safety-hatch timeout
long timeoutMs = 10000; // safety hatch: auto-exit after 10 s by default
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--frames") && i + 1 < argc) maxFrames = atol(argv[++i]);
else if (!strcmp(argv[i], "--dump") && i + 1 < argc) dumpPath = argv[++i];
else if (!strcmp(argv[i], "--user-run")) userRun = true;
else if (!strcmp(argv[i], "--timeout") && i + 1 < argc) timeoutMs = atol(argv[++i]);
}
// SDL3 initialization
if (!SDL_Init(SDL_INIT_VIDEO)) {
std::printf("SDL_Init failed: %s\n", SDL_GetError());
return 1;
}
TTF_Init();
// Create window matching the 320x240 canvas
SDL_Window *win = SDL_CreateWindow("T-Deck: " APP_NAME,
320, 240, SDL_WINDOW_RESIZABLE);
if (!win) {
std::printf("SDL_CreateWindow failed: %s\n", SDL_GetError());
TTF_Quit();
SDL_Quit();
return 1;
}
SDL_Renderer *ren = SDL_CreateRenderer(win, NULL);
if (!ren) ren = SDL_CreateRenderer(win, "software");
SDL_SetRenderLogicalPresentation(ren, 320, 240,
SDL_LOGICAL_PRESENTATION_LETTERBOX);
emuGfx::tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ABGR8888,
SDL_TEXTUREACCESS_STREAMING, 320, 240);
if (!emuGfx::tex) {
std::printf("SDL_CreateTexture failed: %s\n", SDL_GetError());
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
TTF_Quit();
SDL_Quit();
return 1;
}
// --- User sketch initialization ---
setup();
// --- Main loop ---
bool running = true;
// Frame pacing: the sketch contract is "loop() called once per frame
// (~60 fps)". Without pacing this loop busy-spins at hundreds/thousands
// of fps on a desktop and every frame-based animation runs too fast,
// so we delay until the next 60 Hz tick before starting the next frame.
const Uint64 FRAME_MS = 1000 / 60; // ~16.7 ms per frame
Uint64 nextFrame = SDL_GetTicks() + FRAME_MS;
while (running && !emu::quitRequested) {
// --- Event pump ---
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
running = false;
break;
}
// Forward key presses to the emulated I2C keyboard device
if (e.type == SDL_EVENT_KEY_DOWN) {
if (e.key.repeat) break;
SDL_Keycode k = e.key.key;
if (k == SDLK_RETURN || k == SDLK_KP_ENTER) {
emu::keyboard.push_back(' ');
}
else if (k == SDLK_SPACE) {
emu::keyboard.push_back(' ');
}
else if (k == SDLK_BACKSPACE) {
emu::keyboard.push_back(0x08);
}
else if (k == SDLK_DELETE) {
emu::keyboard.push_back(0x7F);
}
else if (k == SDLK_ESCAPE) {
running = false;
}
else {
const char *name = SDL_GetKeyName(k);
if (name && strlen(name) == 1) {
emu::keyboard.push_back((uint8_t)tolower((unsigned char)name[0]));
}
}
}
}
// --- Safety hatch: auto-exit unless the user asked to play ---
if (!userRun && timeoutMs > 0 && millis() >= (unsigned long)timeoutMs) {
std::printf("[emu] safety hatch: exiting after %ld ms "
"(pass --user-run to run indefinitely)\n", timeoutMs);
running = false;
break;
}
// --- Call the user's simulation step ---
loop();
// --- Present frame if dirty ---
if (emuGfx::dirty) {
SDL_UpdateTexture(emuGfx::tex, nullptr, emuGfx::fb,
320 * sizeof(uint32_t));
emuGfx::dirty = false;
}
// Render: clear -> texture -> present
SDL_RenderClear(ren);
SDL_RenderTexture(ren, emuGfx::tex, nullptr, nullptr);
SDL_RenderPresent(ren);
// --- Frame counting / dump ---
if (maxFrames > 0 && --maxFrames == 0) {
running = false;
if (dumpPath) {
// Create a surface from the texture and save as BMP
SDL_Surface *s = SDL_CreateSurface(320, 240, SDL_PIXELFORMAT_ABGR8888);
if (s) {
// Read pixels from the texture
void *pixels;
int pitch;
SDL_LockTexture(emuGfx::tex, nullptr, &pixels, &pitch);
// Copy the framebuffer directly to the surface
Uint32 *sp = (Uint32 *)s->pixels;
for (int y = 0; y < 240; y++) {
memcpy(sp + y * s->w, emuGfx::fb + y * 320, 320 * sizeof(Uint32));
}
SDL_UnlockTexture(emuGfx::tex);
SDL_SaveBMP(s, dumpPath);
SDL_DestroySurface(s);
std::printf("[emu] frame dumped to %s\n", dumpPath);
}
}
}
// --- Frame pacing: sleep until the next 60 Hz tick ---
Uint64 nowMs = SDL_GetTicks();
if (nowMs >= nextFrame) {
// Behind schedule (a slow frame or a stall): resync instead of
// burst-catching-up, so animation never runs fast to "make up"
// lost frames.
nextFrame = nowMs;
} else {
SDL_Delay((Uint32)(nextFrame - nowMs));
}
nextFrame += FRAME_MS;
}
// --- Save last frame as BMP for AI inspection ---
{
SDL_Surface *s = SDL_CreateSurface(320, 240, SDL_PIXELFORMAT_ABGR8888);
if (s) {
void *pixels;
int pitch;
SDL_LockTexture(emuGfx::tex, nullptr, &pixels, &pitch);
Uint32 *sp = (Uint32 *)s->pixels;
for (int y = 0; y < 240; y++) {
memcpy(sp + y * s->w, emuGfx::fb + y * 320, 320 * sizeof(Uint32));
}
SDL_UnlockTexture(emuGfx::tex);
SDL_SaveBMP(s, "frame.bmp");
SDL_DestroySurface(s);
std::printf("[emu] last frame saved to frame.bmp\n");
}
}
// --- Cleanup ---
TTF_Quit();
SDL_DestroyTexture(emuGfx::tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}