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.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# Build artifacts
|
||||
*-emu
|
||||
build/
|
||||
|
||||
# Captured frames
|
||||
*.bmp
|
||||
@@ -0,0 +1,74 @@
|
||||
# Makefile for the FloorPlan3D demo — emulator-template base + raylib-esp32.
|
||||
#
|
||||
# Builds raylib 6.x (from lib/raylib-esp32's raylib submodule) with the rlsw
|
||||
# SOFTWARE renderer behind a custom platform layer (rcore_emu.c) that blits
|
||||
# the RGB565 framebuffer into the SDL host — the same library, renderer and
|
||||
# SwapScreenBuffer flow that runs on the T-Deck's ESP32-S3.
|
||||
#
|
||||
# Build & run: make run (from this directory)
|
||||
# Smoke test: make smoke
|
||||
|
||||
APP ?= FloorPlan
|
||||
APP_DIR := examples/$(APP)
|
||||
|
||||
# raylib-esp32 sources (raylib core + software renderer)
|
||||
RAYLIB := ../lib/raylib-esp32/raylib
|
||||
|
||||
# SDL3 flags via pkg-config
|
||||
SDLFLAGS := $(shell pkg-config --cflags sdl3 sdl3-ttf)
|
||||
LIBS := $(shell pkg-config --libs sdl3 sdl3-ttf)
|
||||
|
||||
CXX ?= g++
|
||||
CC ?= cc
|
||||
CXXFLAGS ?= -std=gnu++17 -O2 -Wall -Wextra
|
||||
CFLAGS := -std=gnu11 -O2 \
|
||||
-DGRAPHICS_API_OPENGL_SOFTWARE -DPLATFORM_CUSTOM -DEXTERNAL_CONFIG_FLAGS \
|
||||
-include raylib_emu_config.h \
|
||||
-Wno-incompatible-pointer-types -Wno-uninitialized -Wno-nonnull \
|
||||
-Wno-return-type -Wno-unused-variable -Wno-unused-function \
|
||||
-Wno-unused-label -Wno-stringop-truncation
|
||||
|
||||
# Include current dir for shim.h/rcore_emu.c, the sketch dir, raylib, NetDeck
|
||||
INC := -I . -I $(APP_DIR) -I $(RAYLIB)/src -I ../src/netdeck
|
||||
|
||||
# Preprocessor definitions: app name, .ino filename, and the sketch source
|
||||
# path. main.cpp does `#include APP_SRC`, so switching APP is enough.
|
||||
DEFS += -DAPP_INO='"$(APP).ino"' -DAPP_NAME='"$(APP)"' \
|
||||
-DAPP_SRC='"$(APP_DIR)/$(APP).cpp"'
|
||||
|
||||
SHIMS := $(wildcard *.h)
|
||||
|
||||
# Main entry point for this template (standalone, no HW deps)
|
||||
EMU_MAIN := src/main.cpp
|
||||
|
||||
EMU := $(APP)-emu
|
||||
|
||||
# --- raylib core translation units (same set as raylib-esp32 builds) -------
|
||||
RL_SRCS := rcore.c rshapes.c rtextures.c rtext.c rmodels.c
|
||||
RL_SRCDIR := $(RAYLIB)/src
|
||||
RLOBJS := $(addprefix build/,$(RL_SRCS:.c=.o)) build/rcore_emu.o
|
||||
|
||||
all: $(EMU)
|
||||
|
||||
build/%.o: $(RL_SRCDIR)/%.c
|
||||
@mkdir -p build
|
||||
$(CC) $(CFLAGS) $(INC) -c $< -o $@
|
||||
|
||||
build/rcore_emu.o: rcore_emu.c $(SHIMS)
|
||||
@mkdir -p build
|
||||
$(CC) $(CFLAGS) $(INC) -c $< -o $@
|
||||
|
||||
# Compile + link: main.cpp includes the sketch .cpp, forming one TU.
|
||||
$(EMU): $(EMU_MAIN) $(SHIMS) $(APP_DIR)/$(APP).cpp $(RLOBJS)
|
||||
$(CXX) $(CXXFLAGS) $(SDLFLAGS) $(INC) $(DEFS) $(EMU_MAIN) $(RLOBJS) -o $@ $(LIBS)
|
||||
|
||||
run: $(EMU)
|
||||
./$(EMU)
|
||||
|
||||
smoke: $(EMU)
|
||||
./$(EMU) --frames 240 --dump smoke_frame.bmp
|
||||
|
||||
clean:
|
||||
rm -rf build $(EMU)
|
||||
|
||||
.PHONY: all run smoke clean
|
||||
@@ -0,0 +1,81 @@
|
||||
# FloorPlan3D — NetDeck demo map in real 3D (raylib-esp32 + rlsw)
|
||||
|
||||
A 320x240 emulator-only demo that renders the **NetDeck "Breach & Board"
|
||||
demo ship** (`src/netdeck/nd_demomap.h`: airlock, L-shaped main corridor,
|
||||
server room) as a 3D floor plan — bulkhead walls, blast door, cover
|
||||
crates, airlock pad and terminal extruded from the 40x24 tile grid.
|
||||
|
||||
It is the first demo to run the **actual on-device 3D stack** on the
|
||||
desktop: [raylib-esp32](https://github.com/jensroth-git/raylib-esp32)
|
||||
(raylib 6.x + the `rlsw` **software renderer**, the same code that runs on
|
||||
the T-Deck's ESP32-S3), vendored at `../lib/raylib-esp32`.
|
||||
|
||||
## Why this matters for NetDeck
|
||||
|
||||
Previous demos (cyberspace-demo, tron-demo) hand-rolled their 3D-ish
|
||||
effects into `emuGfx::fb`. This one instead runs raylib's real 3D
|
||||
pipeline (projection matrices, depth buffer, mesh batching) through
|
||||
`rlsw`, with only the final display flush swapped:
|
||||
|
||||
| | On device (raylib-esp32) | This demo (desktop) |
|
||||
|---|---|---|
|
||||
| Renderer | `rlsw` software renderer, RGB565 + D16 | identical (`-DGRAPHICS_API_OPENGL_SOFTWARE`) |
|
||||
| Platform layer | `src/platform/rcore_esp32.c` (SPI panel flush) | `rcore_emu.c` (blits into `emuGfx::fb`) |
|
||||
| Config | `include/raylib_esp32_config.h` | `raylib_emu_config.h` (same module/limit defines) |
|
||||
|
||||
What renders correctly here is what the T-Deck would render — the demo
|
||||
is a faithful preview of a firmware-side raylib integration.
|
||||
|
||||
## Build & run
|
||||
|
||||
```bash
|
||||
make run # builds and opens a window
|
||||
make smoke # headless: 240 frames, dumps smoke_frame.bmp
|
||||
make clean
|
||||
```
|
||||
|
||||
(The raylib core is compiled from `../lib/raylib-esp32/raylib/src` as C
|
||||
objects in `build/`, then linked with the SDL3 host. First build takes a
|
||||
few seconds longer than the plain template.)
|
||||
|
||||
## Controls
|
||||
|
||||
`a`/`d` spin the camera · `w`/`s` tilt · `q`/`e` zoom · `r` reset ·
|
||||
auto-spin resumes on its own; the 10 s auto-exit safety hatch from the
|
||||
template still applies (`--user-run` to disable, see template README).
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
emulator-template SDL3 host (src/main.cpp, shim.h)
|
||||
└─ examples/FloorPlan/FloorPlan.cpp setup()/loop() sketch
|
||||
├─ emu_raylib_set_framebuffer() tell the platform layer the size
|
||||
├─ InitWindow/BeginDrawing/... plain raylib API
|
||||
│ └─ rlsw software renderer RGB565 + D16 framebuffers
|
||||
├─ rcore_emu.c (PLATFORM_CUSTOM) SwapScreenBuffer → present hook
|
||||
├─ emu_raylib_present() RGB565 → emuGfx::fb (ABGR8888)
|
||||
└─ HUD drawn straight into fb 3x5 bitmap font, raylib-2D-free
|
||||
```
|
||||
|
||||
The platform layer mirrors raylib-esp32's `rcore_esp32.c`: same expected
|
||||
symbols for a `PLATFORM_CUSTOM` rcore build, same `swSwapColorBuffer()`
|
||||
double-buffer flow in `SwapScreenBuffer()` — only the flush target is the
|
||||
emulator framebuffer instead of an SPI panel.
|
||||
|
||||
## Known limitation: rlsw 2D quads
|
||||
|
||||
The rlsw software renderer in raylib-esp32 1.0.3 misrenders the 2D
|
||||
textured-quad path used by `DrawText`/`DrawRectangle`: glyph quads come
|
||||
out vertically flipped and displaced, solid fills can sample wrong atlas
|
||||
texels, and quad placement shifts with geometry (verified by dumping the
|
||||
raw RGB565 buffer). The 3D path (cubes, planes, lines,
|
||||
vertex colors, depth) is pixel-accurate. This demo therefore renders
|
||||
**everything 3D through raylib** and draws the HUD with a 3x5 bitmap
|
||||
font directly into `emuGfx::fb` (same approach as cyberspace-demo).
|
||||
If you need raylib 2D on device, debug `rlsw`'s textured-quad sampler
|
||||
first — the 3D-only workaround below sidesteps it entirely.
|
||||
|
||||
## License
|
||||
|
||||
MIT template + zlib raylib; raylib-esp32 is MIT/zlib (see
|
||||
`../lib/raylib-esp32/LICENSE`).
|
||||
@@ -0,0 +1,314 @@
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* raylib_emu_config.h — raylib build configuration for the FloorPlan3D
|
||||
* emulator demo (desktop port of raylib-esp32's raylib_esp32_config.h).
|
||||
*
|
||||
* raylib-esp32 (lib/raylib-esp32) runs raylib 6.x on the ESP32-S3 using the
|
||||
* rlsw software renderer (GRAPHICS_API_OPENGL_SOFTWARE) behind a custom
|
||||
* platform layer. This demo uses the exact same raylib code and renderer on
|
||||
* the desktop, so what you see in the emulator is what the T-Deck would
|
||||
* render — only the display flush differs (SDL framebuffer instead of SPI).
|
||||
*
|
||||
* This header is force-included (-include) into every raylib translation
|
||||
* unit by the Makefile, mirroring the library's pio_build.py.
|
||||
*/
|
||||
#ifndef RAYLIB_EMU_CONFIG_H
|
||||
#define RAYLIB_EMU_CONFIG_H
|
||||
|
||||
#ifndef EXTERNAL_CONFIG_FLAGS
|
||||
#define EXTERNAL_CONFIG_FLAGS
|
||||
#endif
|
||||
|
||||
// Software renderer framebuffer — same format as on the ESP32 build so the
|
||||
// 565→8888 blit into emuGfx::fb is byte-identical to the device path.
|
||||
#define SW_FRAMEBUFFER_COLOR_TYPE R5G6B5
|
||||
#define SW_FRAMEBUFFER_DEPTH_TYPE D16
|
||||
#define SW_FRAMEBUFFER_OUTPUT_BGRA false
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Module selection (audio off, same as on-device)
|
||||
// ---------------------------------------------------------------------------
|
||||
#define SUPPORT_MODULE_RSHAPES 1
|
||||
#define SUPPORT_MODULE_RTEXTURES 1
|
||||
#define SUPPORT_MODULE_RTEXT 1
|
||||
#define SUPPORT_MODULE_RMODELS 1
|
||||
#define SUPPORT_MODULE_RAUDIO 0
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// rcore
|
||||
// ---------------------------------------------------------------------------
|
||||
#define SUPPORT_CAMERA_SYSTEM 1
|
||||
#define SUPPORT_GESTURES_SYSTEM 1
|
||||
#define SUPPORT_RPRAND_GENERATOR 1
|
||||
#define SUPPORT_MOUSE_GESTURES 0
|
||||
#define SUPPORT_SSH_KEYBOARD_RPI 0
|
||||
#define SUPPORT_WINMM_HIGHRES_TIMER 0
|
||||
#define SUPPORT_PARTIALBUSY_WAIT_LOOP 0
|
||||
#define SUPPORT_SCREEN_CAPTURE 0
|
||||
#define SUPPORT_GIF_RECORDING 0
|
||||
#define SUPPORT_AUTOMATION_EVENTS 0
|
||||
#define SUPPORT_CLIPBOARD_IMAGE 0
|
||||
|
||||
#define MAX_FILEPATH_CAPACITY 512
|
||||
#define MAX_FILEPATH_LENGTH 256
|
||||
#define MAX_KEYBOARD_KEYS 128
|
||||
#define MAX_MOUSE_BUTTONS 4
|
||||
#define MAX_GAMEPADS 1
|
||||
#define MAX_GAMEPAD_AXES 4
|
||||
#define MAX_GAMEPAD_BUTTONS 16
|
||||
#define MAX_GAMEPAD_VIBRATION_TIME 2.0f
|
||||
#define MAX_TOUCH_POINTS 4
|
||||
#define MAX_KEY_PRESSED_QUEUE 8
|
||||
#define MAX_CHAR_PRESSED_QUEUE 8
|
||||
#define MAX_DECOMPRESSION_SIZE 16
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// rlgl (embedded-friendly batch sizes, same as the ESP32 build)
|
||||
// ---------------------------------------------------------------------------
|
||||
#define RL_SUPPORT_MESH_GPU_SKINNING 0
|
||||
#define RL_DEFAULT_BATCH_BUFFERS 1
|
||||
#define RL_DEFAULT_BATCH_DRAWCALLS 128
|
||||
#define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 2
|
||||
#define RL_MAX_MATRIX_STACK_SIZE 16
|
||||
#define RL_MAX_SHADER_LOCATIONS 16
|
||||
#define RL_CULL_DISTANCE_NEAR 0.05
|
||||
#define RL_CULL_DISTANCE_FAR 1000.0
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// rshapes
|
||||
// ---------------------------------------------------------------------------
|
||||
#define SUPPORT_QUADS_DRAW_MODE 1
|
||||
#define SPLINE_SEGMENT_DIVISIONS 16
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// rtextures — image file formats (PNG for the window icon, same as device)
|
||||
// ---------------------------------------------------------------------------
|
||||
#define SUPPORT_FILEFORMAT_PNG 1
|
||||
#define SUPPORT_FILEFORMAT_BMP 0
|
||||
#define SUPPORT_FILEFORMAT_TGA 0
|
||||
#define SUPPORT_FILEFORMAT_JPG 0
|
||||
#define SUPPORT_FILEFORMAT_GIF 0
|
||||
#define SUPPORT_FILEFORMAT_QOI 1
|
||||
#define SUPPORT_FILEFORMAT_PSD 0
|
||||
#define SUPPORT_FILEFORMAT_DDS 0
|
||||
#define SUPPORT_FILEFORMAT_HDR 0
|
||||
#define SUPPORT_FILEFORMAT_PIC 0
|
||||
#define SUPPORT_FILEFORMAT_KTX 0
|
||||
#define SUPPORT_FILEFORMAT_ASTC 0
|
||||
#define MAX_IMAGE_FLIP_QUEUE 2
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// rtext — font file formats (embedded default font only, like on device)
|
||||
// ---------------------------------------------------------------------------
|
||||
#define SUPPORT_FILEFORMAT_FNT 1
|
||||
#define SUPPORT_FILEFORMAT_TTF 0
|
||||
#define SUPPORT_FILEFORMAT_BMF 0
|
||||
#define MAX_TEXT_BUFFER_LENGTH 512
|
||||
#define MAX_TEXTSPLIT_COUNT 64
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// rmodels — 3D model file formats
|
||||
// ---------------------------------------------------------------------------
|
||||
#define SUPPORT_FILEFORMAT_OBJ 1
|
||||
#define SUPPORT_FILEFORMAT_MTL 1
|
||||
#define SUPPORT_FILEFORMAT_GLTF 1
|
||||
#define SUPPORT_FILEFORMAT_M3D 1
|
||||
#define SUPPORT_FILEFORMAT_VOX 0
|
||||
#define SUPPORT_FILEFORMAT_IQM 0
|
||||
#define SUPPORT_MESH_GENERATION 1
|
||||
|
||||
#endif // RAYLIB_EMU_CONFIG_H
|
||||
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* rcore_emu.c — raylib platform layer for the FloorPlan3D emulator demo.
|
||||
*
|
||||
* Desktop counterpart of raylib-esp32's src/platform/rcore_esp32.c: provides
|
||||
* all symbols that rcore.c expects from a PLATFORM_CUSTOM backend, but
|
||||
* instead of pushing the RGB565 software-renderer buffer to an SPI panel it
|
||||
* hands it to the SDL host via emu_raylib_present() (implemented by the
|
||||
* sketch in C++, which converts 565→8888 into emuGfx::fb).
|
||||
*
|
||||
* Everything else mirrors rcore_esp32.c so the demo and the on-device build
|
||||
* exercise the same code paths (rlsw renderer, SwapScreenBuffer flow).
|
||||
*/
|
||||
|
||||
#include "raylib_emu_config.h"
|
||||
#include "raylib.h"
|
||||
#include "rlgl.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
// --- Internal state --------------------------------------------------------
|
||||
static int screen_width = 0;
|
||||
static int screen_height = 0;
|
||||
|
||||
extern void *swGetColorBuffer(int *width, int *height);
|
||||
extern void *swSwapColorBuffer(int *width, int *height);
|
||||
|
||||
// Provided by the sketch (C++): converts the RGB565 buffer into the
|
||||
// emulator's ABGR8888 framebuffer and marks it dirty.
|
||||
extern void emu_raylib_present(const unsigned short *rgb565, int w, int h);
|
||||
|
||||
// Called by the sketch before InitWindow(), mirroring raylib_esp32_init()
|
||||
// (which tells the port layer the panel size on device).
|
||||
void emu_raylib_set_framebuffer(int w, int h)
|
||||
{
|
||||
screen_width = w;
|
||||
screen_height = h;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Window management stubs
|
||||
// ---------------------------------------------------------------------------
|
||||
bool WindowShouldClose(void) { return false; }
|
||||
void ToggleFullscreen(void) { TRACELOG(LOG_WARNING, "ToggleFullscreen() not available in the emulator"); }
|
||||
void ToggleBorderlessWindowed(void) { TRACELOG(LOG_WARNING, "ToggleBorderlessWindowed() not available in the emulator"); }
|
||||
void MaximizeWindow(void) { TRACELOG(LOG_WARNING, "MaximizeWindow() not available in the emulator"); }
|
||||
void MinimizeWindow(void) { TRACELOG(LOG_WARNING, "MinimizeWindow() not available in the emulator"); }
|
||||
void RestoreWindow(void) { TRACELOG(LOG_WARNING, "RestoreWindow() not available in the emulator"); }
|
||||
void SetWindowState(unsigned int flags) { (void)flags; }
|
||||
void ClearWindowState(unsigned int flags){ (void)flags; }
|
||||
void SetWindowIcon(Image image) { (void)image; }
|
||||
void SetWindowIcons(Image *imgs, int n) { (void)imgs; (void)n; }
|
||||
void SetWindowTitle(const char *title) { (void)title; }
|
||||
void SetWindowPosition(int x, int y) { (void)x; (void)y; }
|
||||
void SetWindowMonitor(int m) { (void)m; }
|
||||
void SetWindowMinSize(int w, int h) { (void)w; (void)h; }
|
||||
void SetWindowMaxSize(int w, int h) { (void)w; (void)h; }
|
||||
void SetWindowSize(int w, int h) { (void)w; (void)h; }
|
||||
void SetWindowOpacity(float o) { (void)o; }
|
||||
void SetWindowFocused(void) {}
|
||||
|
||||
void *GetWindowHandle(void) { return NULL; }
|
||||
|
||||
int GetCurrentMonitor(void) { return 0; }
|
||||
Vector2 GetMonitorPosition(int m) { (void)m; return (Vector2){0, 0}; }
|
||||
int GetMonitorWidth(int m) { (void)m; return screen_width; }
|
||||
int GetMonitorHeight(int m) { (void)m; return screen_height; }
|
||||
int GetMonitorPhysicalWidth(int m) { (void)m; return 0; }
|
||||
int GetMonitorPhysicalHeight(int m) { (void)m; return 0; }
|
||||
int GetMonitorRefreshRate(int m) { (void)m; return 60; }
|
||||
const char *GetMonitorName(int m) { (void)m; return "T-Deck emulator"; }
|
||||
Vector2 GetWindowPosition(void) { return (Vector2){0, 0}; }
|
||||
Vector2 GetWindowScaleDPI(void) { return (Vector2){1.0f, 1.0f}; }
|
||||
|
||||
void SetClipboardText(const char *t) { (void)t; }
|
||||
const char *GetClipboardText(void) { return ""; }
|
||||
Image GetClipboardImage(void) { return (Image){0}; }
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Cursor stubs
|
||||
// ---------------------------------------------------------------------------
|
||||
void ShowCursor(void) {}
|
||||
void HideCursor(void) {}
|
||||
void EnableCursor(void) {}
|
||||
void DisableCursor(void) {}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Input stubs (the sketch reads emu::keyboard directly instead)
|
||||
// ---------------------------------------------------------------------------
|
||||
int SetGamepadMappings(const char *m) { (void)m; return 0; }
|
||||
void SetMousePosition(int x, int y) { (void)x; (void)y; }
|
||||
void SetMouseCursor(int c) { (void)c; }
|
||||
const char *GetKeyName(int key) { (void)key; return ""; }
|
||||
void PollInputEvents(void) {}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SwapScreenBuffer — hand the rendered frame to the SDL host
|
||||
// ---------------------------------------------------------------------------
|
||||
void SwapScreenBuffer(void)
|
||||
{
|
||||
int sw_w, sw_h;
|
||||
uint16_t *rendered = (uint16_t *)swSwapColorBuffer(&sw_w, &sw_h);
|
||||
if (!rendered || sw_w != screen_width || sw_h != screen_height) {
|
||||
TRACELOG(LOG_ERROR, "PLATFORM: Framebuffer mismatch");
|
||||
return;
|
||||
}
|
||||
|
||||
emu_raylib_present(rendered, sw_w, sw_h);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Timing
|
||||
// ---------------------------------------------------------------------------
|
||||
double GetTime(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Misc
|
||||
// ---------------------------------------------------------------------------
|
||||
void OpenURL(const char *url)
|
||||
{
|
||||
if (strchr(url, '\'') != NULL)
|
||||
TRACELOG(LOG_WARNING, "SYSTEM: Provided URL contains suspicious character");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Platform init / close
|
||||
// ---------------------------------------------------------------------------
|
||||
int InitPlatform(void);
|
||||
bool InitGraphicsDevice(void);
|
||||
|
||||
bool CreateWindowFramebuffer(int width, int height)
|
||||
{
|
||||
screen_width = width;
|
||||
screen_height = height;
|
||||
TRACELOG(LOG_INFO, "PLATFORM: Framebuffer %dx%d (RGB565, software renderer)", width, height);
|
||||
return true;
|
||||
}
|
||||
|
||||
int InitPlatform(void)
|
||||
{
|
||||
if (screen_width == 0 || screen_height == 0) {
|
||||
// Mirrors the ESP32 port: display size must be known before InitWindow().
|
||||
TRACELOG(LOG_ERROR, "PLATFORM: Framebuffer size unknown — call "
|
||||
"emu_raylib_set_framebuffer(w, h) before InitWindow()");
|
||||
return -1;
|
||||
}
|
||||
|
||||
CreateWindowFramebuffer(screen_width, screen_height);
|
||||
TRACELOG(LOG_INFO, "PLATFORM: Emulator initialized (%dx%d)", screen_width, screen_height);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ClosePlatform(void)
|
||||
{
|
||||
TRACELOG(LOG_INFO, "PLATFORM: Emulator closed");
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* Minimal emulator shim — provides only the subset of Arduino API needed
|
||||
* for graphics/engine prototypes running on the SDL3 emulator.
|
||||
*
|
||||
* Include this single header in your sketch:
|
||||
* #include "shim.h"
|
||||
*
|
||||
* The template's main.cpp includes this file and sets up the emulated state.
|
||||
* This header defines: millis(), random(), randomSeed(), String, and basic
|
||||
* type definitions. No GPIO, no interrupts, no peripheral dependencies.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <cmath>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
#include <deque>
|
||||
#include <thread> // for std::this_thread::sleep_for
|
||||
#include <chrono> // for std::chrono::milliseconds, microseconds
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Platform marker: the sketch can #ifdef EMULATOR for emulator-only code
|
||||
// ---------------------------------------------------------------------------
|
||||
#define EMULATOR 1
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Flash-resident data macros (no-op on emulator; flash-mapped memory)
|
||||
// ---------------------------------------------------------------------------
|
||||
#define PROGMEM
|
||||
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
|
||||
#define pgm_read_word(addr) (*(const unsigned short *)(addr))
|
||||
#define pgm_read_dword(addr) (*(const unsigned long *)(addr))
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Time
|
||||
// ---------------------------------------------------------------------------
|
||||
inline unsigned long millis()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
static const auto start = steady_clock::now();
|
||||
return (unsigned long)duration_cast<milliseconds>(steady_clock::now() - start).count();
|
||||
}
|
||||
|
||||
inline unsigned long micros()
|
||||
{
|
||||
using namespace std::chrono;
|
||||
static const auto start = steady_clock::now();
|
||||
return (unsigned long)duration_cast<microseconds>(steady_clock::now() - start).count();
|
||||
}
|
||||
|
||||
inline void delay(unsigned long ms)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
|
||||
}
|
||||
|
||||
inline void delayMicroseconds(unsigned int us)
|
||||
{
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(us));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Random (Arduino-style)
|
||||
// ---------------------------------------------------------------------------
|
||||
inline long random(long maxv) { return maxv > 0 ? std::rand() % maxv : 0; }
|
||||
inline long random(long lo, long hi) { return hi > lo ? lo + std::rand() % (hi - lo) : lo; }
|
||||
inline void randomSeed(unsigned long seed) { std::srand((unsigned)seed); }
|
||||
|
||||
// Emulated esp_random (used by some sketches)
|
||||
inline uint32_t esp_random()
|
||||
{
|
||||
return ((uint32_t)std::rand() << 17) ^ (uint32_t)std::rand() ^ millis();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Minimal String class (Arduino-style)
|
||||
// ---------------------------------------------------------------------------
|
||||
class String {
|
||||
public:
|
||||
std::string s;
|
||||
String() {}
|
||||
String(const char *c) : s(c) {}
|
||||
String(const std::string &x) : s(x) {}
|
||||
String(char c) : s(1, c) {}
|
||||
String(int v) : s(std::to_string(v)) {}
|
||||
String(long v) : s(std::to_string(v)) {}
|
||||
String(unsigned v) : s(std::to_string(v)) {}
|
||||
String(unsigned long v) : s(std::to_string(v)) {}
|
||||
String(float v) : s(std::to_string(v)) {}
|
||||
const char *c_str() const { return s.c_str(); }
|
||||
unsigned length() const { return (unsigned)s.length(); }
|
||||
};
|
||||
|
||||
inline String operator+(const String &a, const String &b) { return String(a.s + b.s); }
|
||||
inline String operator+(const String &a, const char *b) { return String(a.s + b); }
|
||||
inline String operator+(const char *a, const String &b) { return String(std::string(a) + b.s); }
|
||||
|
||||
// -------------------------------------------------------------------------//
|
||||
// Minimal GPIO — emulated pin levels (idle HIGH). Access via
|
||||
// emu::pinLevels in the main.cpp; this shim provides a safe read API.
|
||||
// For the template's purposes, users should use the emu::keyboard queue
|
||||
// for input instead of raw GPIO reads.
|
||||
// -------------------------------------------------------------------------//
|
||||
#define INPUT 0
|
||||
#define OUTPUT 1
|
||||
#define INPUT_PULLUP 2
|
||||
#define HIGH 1
|
||||
#define LOW 0
|
||||
|
||||
inline void pinMode(int, int) {} // no-op in emulator
|
||||
|
||||
inline int digitalRead(int pin)
|
||||
{
|
||||
(void)pin;
|
||||
return 1; // idle HIGH
|
||||
}
|
||||
|
||||
inline void digitalWrite(int, int) {}
|
||||
|
||||
// Pin edge structure — used by the emulator's ISR dispatch logic.
|
||||
struct PinEdge { int pin, from, to; };
|
||||
|
||||
// -------------------------------------------------------------------------//
|
||||
// Serial simulation (minimal)
|
||||
// -------------------------------------------------------------------------//
|
||||
struct SerialSim {
|
||||
std::deque<std::string> *sink = nullptr;
|
||||
void capture(const char *s)
|
||||
{
|
||||
if (!sink) return;
|
||||
sink->push_back(s);
|
||||
while (sink->size() > 512) sink->pop_front();
|
||||
}
|
||||
template <typename... A> void printf(const char *fmt, A... a)
|
||||
{
|
||||
char buf[512];
|
||||
std::snprintf(buf, sizeof(buf), fmt, a...);
|
||||
std::fputs(buf, stdout);
|
||||
capture(buf);
|
||||
}
|
||||
void println() { std::printf("\n"); capture("\n"); }
|
||||
void println(const char *s) { std::printf("%s\n", s); capture(s); capture("\n"); }
|
||||
void println(const String &s) { std::printf("%s\n", s.c_str()); capture(s.c_str()); capture("\n"); }
|
||||
void println(int v) { std::printf("%d\n", v); capture(std::to_string(v).c_str()); capture("\n"); }
|
||||
};
|
||||
|
||||
// External instance — the emulator main.cpp defines this.
|
||||
extern SerialSim Serial;
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 7.8 KiB |
@@ -0,0 +1,240 @@
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user