Emulator: migrate SDL2 -> SDL3
Port the shared emulator harness (all apps) to SDL 3.4 + SDL3_ttf 3.2:
- window creation without position args; renderer logical size via
SDL_SetRenderLogicalPresentation(LETTERBOX); SDL_RenderTexture;
SDL_SaveBMP_IO frame dumps
- SDL_EVENT_* event scheme, flattened SDL_KeyboardEvent, float mouse
coords; clicks mapped with SDL_ConvertEventToRenderCoordinates, keeping
the Xwayland global-state cross-check from 260a2b3
- audio shim on the stream API (SDL_OpenAudioDeviceStream +
SDL_PutAudioStreamData) since SDL_QueueAudio is gone
- TTF_RenderText_Blended (explicit length); SDL3_ttf header layout
handled via __has_include (Ubuntu installs under SDL3_ttf/)
- e2e freeze-tint pixel assertions updated to the committed
diagonal-stripe pattern (stale since the freeze rework; pre-existing
failures at HEAD under SDL2), sampled away from the freeze text
Full suite green: 14 unit + 7 integration + 3 E2E journeys, 0 failures.
MissileCommand, MiataDrive, NetDeck build; CrtTerminal (missing sndBlip
declaration) and Lotus3 (sketch not in repo) fail identically at HEAD -
pre-existing, out of scope.
This commit is contained in:
+56
-59
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* SDL2 host for T-Deck example sketches — run the app on your PC.
|
||||
* SDL3 host for T-Deck example sketches — run the app on your PC.
|
||||
* The sketch is selected with -DAPP_INO (see the Makefile, APP=...).
|
||||
*
|
||||
* Input mapping:
|
||||
@@ -11,8 +11,12 @@
|
||||
* Backspace/Delete keyboard device (0x08 / 0x7F)
|
||||
* --type "text" queue keys programmatically (for smoke tests)
|
||||
*/
|
||||
#include <SDL.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#if __has_include(<SDL3/SDL_ttf.h>) // upstream install layout
|
||||
#include <SDL3/SDL_ttf.h>
|
||||
#else // Debian/Ubuntu package layout
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#endif
|
||||
#include <cstdio>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
@@ -60,12 +64,16 @@ const FreeFontStub FreeSans12pt7b {0};
|
||||
|
||||
static void saveBmp(const char *path)
|
||||
{
|
||||
SDL_Surface *s = SDL_CreateRGBSurfaceWithFormatFrom(
|
||||
emuGfx::fb, emuGfx::W, emuGfx::H, 32, emuGfx::W * 4, SDL_PIXELFORMAT_ABGR8888);
|
||||
SDL_Surface *s = SDL_CreateSurfaceFrom(
|
||||
emuGfx::W, emuGfx::H, SDL_PIXELFORMAT_ABGR8888,
|
||||
emuGfx::fb, emuGfx::W * 4);
|
||||
if (s) {
|
||||
SDL_SaveBMP(s, path);
|
||||
SDL_FreeSurface(s);
|
||||
std::printf("[emu] frame dumped to %s\n", path);
|
||||
SDL_IOStream *dst = SDL_IOFromFile(path, "wb");
|
||||
if (dst) {
|
||||
SDL_SaveBMP_IO(s, dst, true); // true = close the stream for us
|
||||
std::printf("[emu] frame dumped to %s\n", path);
|
||||
}
|
||||
SDL_DestroySurface(s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,18 +127,18 @@ int main(int argc, char **argv)
|
||||
#endif
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
|
||||
std::printf("SDL_Init failed: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
TTF_Init();
|
||||
|
||||
SDL_Window *win = SDL_CreateWindow("T-Deck emulator — " APP_NAME,
|
||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
640, 480, SDL_WINDOW_RESIZABLE);
|
||||
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!ren) ren = SDL_CreateRenderer(win, -1, 0); // software fallback (headless)
|
||||
SDL_RenderSetLogicalSize(ren, emuGfx::W, emuGfx::H);
|
||||
SDL_Renderer *ren = SDL_CreateRenderer(win, NULL);
|
||||
if (!ren) ren = SDL_CreateRenderer(win, "software"); // fallback (headless)
|
||||
SDL_SetRenderLogicalPresentation(ren, emuGfx::W, emuGfx::H,
|
||||
SDL_LOGICAL_PRESENTATION_LETTERBOX);
|
||||
emuGfx::tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ABGR8888,
|
||||
SDL_TEXTUREACCESS_STREAMING, emuGfx::W, emuGfx::H);
|
||||
|
||||
@@ -149,13 +157,13 @@ int main(int argc, char **argv)
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_QUIT:
|
||||
case SDL_EVENT_QUIT:
|
||||
running = false;
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN: {
|
||||
case SDL_EVENT_KEY_DOWN: {
|
||||
if (e.key.repeat) break;
|
||||
SDL_Keycode k = e.key.keysym.sym;
|
||||
SDL_Keycode k = e.key.key;
|
||||
switch (k) {
|
||||
case SDLK_RIGHT: held[0] = true; emu::pinLevels[dirPin[0]] = 0; break;
|
||||
case SDLK_UP: held[1] = true; emu::pinLevels[dirPin[1]] = 0; break;
|
||||
@@ -191,8 +199,8 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_KEYUP: {
|
||||
SDL_Keycode k = e.key.keysym.sym;
|
||||
case SDL_EVENT_KEY_UP: {
|
||||
SDL_Keycode k = e.key.key;
|
||||
switch (k) {
|
||||
case SDLK_RIGHT: held[0] = false; emu::pinLevels[dirPin[0]] = 1; break;
|
||||
case SDLK_UP: held[1] = false; emu::pinLevels[dirPin[1]] = 1; break;
|
||||
@@ -207,71 +215,60 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEWHEEL: {
|
||||
case SDL_EVENT_MOUSE_WHEEL: {
|
||||
// Each wheel notch = a burst of ball edges on the matching
|
||||
// direction pin (emitted over the next few iterations).
|
||||
const int perNotch = 6;
|
||||
if (e.wheel.y > 0) wheelPulse[1] += perNotch * e.wheel.y; // up
|
||||
if (e.wheel.y < 0) wheelPulse[3] += perNotch * -e.wheel.y; // down
|
||||
if (e.wheel.x > 0) wheelPulse[0] += perNotch * e.wheel.x; // right
|
||||
if (e.wheel.x < 0) wheelPulse[2] += perNotch * -e.wheel.x; // left
|
||||
if (e.wheel.y > 0) wheelPulse[1] += perNotch * (int)e.wheel.y; // up
|
||||
if (e.wheel.y < 0) wheelPulse[3] += perNotch * (int)-e.wheel.y; // down
|
||||
if (e.wheel.x > 0) wheelPulse[0] += perNotch * (int)e.wheel.x; // right
|
||||
if (e.wheel.x < 0) wheelPulse[2] += perNotch * (int)-e.wheel.x; // left
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP: {
|
||||
// Work out the true window-relative cursor position. Two
|
||||
// pitfalls to sidestep here:
|
||||
// 1. SDL_RenderSetLogicalSize letterboxes the 320x240
|
||||
// surface inside the (resizable) window when it is not
|
||||
// exactly 4:3, so event coords must be mapped through
|
||||
// that centered viewport, not scaled against the whole
|
||||
// window.
|
||||
// 2. Under Xwayland the X server may deliver pointer event
|
||||
// coords in a scaled space (observed: exactly half the
|
||||
// window size on a scaled desktop), so e.button.x/y
|
||||
// cannot be trusted blindly. Reconstruct the position
|
||||
// from the global cursor minus the window position
|
||||
// (raw root coords on X11, unaffected by event scaling)
|
||||
// and use it when it lands inside the window.
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
||||
case SDL_EVENT_MOUSE_BUTTON_UP: {
|
||||
// SDL3 converts window-relative event coords into the
|
||||
// renderer's logical (320x240) space, handling HiDPI and
|
||||
// the letterboxed viewport officially.
|
||||
SDL_ConvertEventToRenderCoordinates(ren, &e);
|
||||
// Caveat kept from the SDL2 era (commit 260a2b3): under
|
||||
// Xwayland the X server may deliver pointer event coords in
|
||||
// a scaled space (observed: exactly half the true position),
|
||||
// so cross-check against the cursor position reconstructed
|
||||
// from the global state minus the window position (raw root
|
||||
// coords on X11, unaffected by event scaling) and prefer it
|
||||
// when it lands inside the window.
|
||||
int ww, wh;
|
||||
SDL_GetWindowSize(win, &ww, &wh);
|
||||
int srcX = e.button.x, srcY = e.button.y;
|
||||
if (ww > 0 && wh > 0) {
|
||||
int gx, gy, wpx, wpy;
|
||||
float gx, gy;
|
||||
int wpx, wpy;
|
||||
Uint32 gbtn = SDL_GetGlobalMouseState(&gx, &gy);
|
||||
SDL_GetWindowPosition(win, &wpx, &wpy);
|
||||
int relX = gx - wpx, relY = gy - wpy;
|
||||
int relX = (int)(gx - wpx), relY = (int)(gy - wpy);
|
||||
if (gbtn && relX >= 0 && relX < ww && relY >= 0 && relY < wh) {
|
||||
srcX = relX;
|
||||
srcY = relY;
|
||||
float lx, ly;
|
||||
if (SDL_RenderCoordinatesFromWindow(ren, (float)relX, (float)relY, &lx, &ly)) {
|
||||
e.button.x = lx;
|
||||
e.button.y = ly;
|
||||
}
|
||||
}
|
||||
}
|
||||
int ow, oh;
|
||||
SDL_GetRendererOutputSize(ren, &ow, &oh);
|
||||
int wx = 0, wy = 0;
|
||||
if (ww > 0 && wh > 0) {
|
||||
float scale = (ww * emuGfx::H <= wh * emuGfx::W)
|
||||
? (float)ww / emuGfx::W
|
||||
: (float)wh / emuGfx::H;
|
||||
float vw = emuGfx::W * scale, vh = emuGfx::H * scale;
|
||||
float vx = (ww - vw) / 2.0f, vy = (wh - vh) / 2.0f;
|
||||
wx = (int)((srcX - vx) / scale);
|
||||
wy = (int)((srcY - vy) / scale);
|
||||
}
|
||||
int wx = (int)e.button.x, wy = (int)e.button.y;
|
||||
if (wx < 0) wx = 0;
|
||||
if (wx > emuGfx::W - 1) wx = emuGfx::W - 1;
|
||||
if (wy < 0) wy = 0;
|
||||
if (wy > emuGfx::H - 1) wy = emuGfx::H - 1;
|
||||
|
||||
if (e.button.button == SDL_BUTTON_LEFT) {
|
||||
bool down = (e.type == SDL_MOUSEBUTTONDOWN);
|
||||
bool down = (e.type == SDL_EVENT_MOUSE_BUTTON_DOWN);
|
||||
emu::touchDown = down;
|
||||
emu::touchX = wx;
|
||||
emu::touchY = wy;
|
||||
} else if (e.button.button == SDL_BUTTON_RIGHT) {
|
||||
emu::pinLevels[BOARD_BOOT_PIN] =
|
||||
(e.type == SDL_MOUSEBUTTONDOWN) ? 0 : 1;
|
||||
(e.type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? 0 : 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -368,7 +365,7 @@ int main(int argc, char **argv)
|
||||
emuGfx::dirty = false;
|
||||
}
|
||||
SDL_RenderClear(ren);
|
||||
SDL_RenderCopy(ren, emuGfx::tex, nullptr, nullptr);
|
||||
SDL_RenderTexture(ren, emuGfx::tex, nullptr, nullptr);
|
||||
SDL_RenderPresent(ren);
|
||||
|
||||
if (maxFrames > 0 && --maxFrames == 0) {
|
||||
|
||||
Reference in New Issue
Block a user