diff --git a/docs/dev/findings.md b/docs/dev/findings.md index 450856f3..25b8c8c6 100644 --- a/docs/dev/findings.md +++ b/docs/dev/findings.md @@ -401,6 +401,7 @@ covers D24–D69; the log continues in §H (D32 procedure, D70–D121). | D179 | **Packaged build crashes after the logos — the D43/D69 model + bg sidecars are ROM-derived and absent from any build that has no ROM (M-34, alpha-release QA).** The first `goldeneye-pc-port-*-win64.zip` from CI (bundle-win.sh) shows the Rare/Nintendo logos (compiled-in `assets/rarewarelogo.c`) then AVs at `0x6b157a88`/`0x70157a88` — symbolised: `modelPromoteNodeOffsetsToPointers` (`model.c`) ← `load_object_fill_header` (`objecthandler_2.c`) ← first prop/item model load. Root cause is **not** a code regression: the port loads PC-layout model geometry from `data/pcmodels-/{pcmodels.bin,manifest.csv}` and stage bg/stan from `data/pccg-/{pccg.bin,manifest.csv}`, both produced offline by `tools_pc/d43_emit.py` / `d69_emit.py` from the ROM (see `port/src/pcmodels.c` / `pccg.c`; `pcmodelsReserveSize` logs *"pcmodels.bin not found — model loads will fail"* and returns 0). CI has no ROM so it never runs the emit scripts, and the two `data/` dirs are (correctly) gitignored ROM-derived game data — **cannot be committed or shipped** (converted Nintendo/Rare geometry, DLs, collision, stan nav; = distributing assets). A local build with those dirs present runs clean (verified M-34: 1741 frames, `romdataInit ... mapped at 0x10000000`, `pcmodels: 512 sidecars`, `pccg: 73 sidecars`, no crash — so `0x10000000` is *not* the problem here). The `docs/building.md` sidecar-gen step was also missing entirely. **Fix = release-side, backlogged** (`docs/BACKLOG.md` → "Alpha release"): bundle the emit scripts + their committed inputs as a pure-stdlib-Python asset-prep tool the user runs once against their own ROM (~5 MB output, no MIPS toolchain). Secondary/latent: the fixed-address `VirtualAlloc((LPVOID)0x10000000)` in `romdata.c` has no working fallback (`"using heap copy — direct ROM reads will fail"` then limps into the same crash) — didn't bite M-34 but will on a machine where something occupies `0x10000000`; harden separately (reserve earliest in `main`, or retry low bases and derive all segment math from the base obtained). | Diagnosed, not fixed — release packaging gap + latent `romdata.c` fallback | | D180 | **Native-PC input pass (M-XX QoL run, `qol/native-pc-input-menu`, port-only).** Three parts, all config-gated with default = prior behaviour: **(WI-1) `Input.MouseCaptureMode`** (0 = legacy always-grab; 1 = Quake-style click-to-lock) in `port/src/input.c` + `port/src/video.c` — in mode 1 the OS cursor is free until a click lands in the game window (`SDL_MOUSEBUTTONDOWN` → `inputNotifyClick`), and ESC / focus-loss / entering any front-end menu (`current_menu != MENU_RUN_STAGE`) frees it again; re-entering a stage while still "armed" re-locks so unpausing needs no click. `reconcileGrab()` runs once per controller-0 poll. Mouse buttons are suppressed from the game while the cursor is free in-stage (no phantom fire). Controller paths untouched. **(WI-2) absolute-cursor menu tracking** — when capture mode is on and the cursor is free in a menu, the D165/D169 pointer P-controller takes its target from the real cursor's absolute window position mapped onto the live virtual front-end rect (`getPlayer_c_screen*`), giving true 1:1 tracking instead of the relative-delta estimator. Relative path unchanged for legacy/grabbed. **(B3) `Input.MouseAimSpeed` default 25 → 16** (aim mode still overshot at 25 per the backlog). | LANDED, port-only, no `#ifdef PORT`. Build 241/241, `-level_09` framediff 3/3 within threshold (unregressed), `GE_STARTMENU` menu boot crash-free. **Feel-checks owed** (headless can't drive the mouse): click-to-lock ergonomics, absolute menu tracking, the new aim-speed default. | | D181 | **`Game.ScreenShakeIntensity` — first route-(b) `src/` gameplay-cosmetic hook (M-XX QoL run).** `src/fr.c viShake()` gains a single `#ifdef PORT` line — `intensity *= portScreenShakeScale;` (extern `f32`, defined + `configRegisterFloat`'d 0.0–10.0 in `port/src/video.c`) — before the existing clamp. Default `1.0f` ⇒ exact no-op, every headless golden dump byte-unaffected; `0.0` disables explosion/effect screen shake, up to `10.0` exaggerates it. N64 build (no `-DPORT`) keeps the original line verbatim. Precedent per `docs/BACKLOG.md` "Fun features → screen-shake intensity slider" and AGENTS.md #2's documented-exception path. Same policy class as a future FOV hook. | LANDED behind config, default = original. Build green. | +| D184 | **F10 port-layer options overlay — the approach-(C) surface from `OPTIONS-MENU-PLAN.md` (M-37).** New `port/src/optionsoverlay.c` + `port/include/optionsoverlay.h`: an immediate-mode overlay drawn as its own fast3d 2D DL, appended after the game DL in `gfx_pc.cpp gfx_run()` (`optionsOverlayEmit()` → NULL when closed ⇒ **zero bytes appended, golden dumps byte-identical**). F10 toggles (`video.c videoPumpEvents`, next to F12); while open, `input.c inputComputePad(0)` returns a neutral pad and routes arrows/enter/wheel/mouse-buttons to `optionsOverlayHandleInput()` (mirrors the WI-1/D180 swallow). ESC closes; `configSave()` on close. `config.c` gains `configForEachOption()` + a `configSetOptionMeta()` label/step/enum side table (no per-key knowledge in config.c). Live knobs (VSync/FpsCap/TextureFilter via `videoRequestLiveConfig()`; mouse + screen-shake via the registered pointer) apply immediately; MSAA tagged "(restart)". v1 rows per plan §3: VSync, FpsCap, MSAA, TextureFilter, MouseAimSpeed, MouseTurnSpeed, MouseInvertY, MouseCaptureMode, ScreenShakeIntensity. | **LANDED, port-only, no `#ifdef PORT` in `src/`.** Build links 241/241. **Runtime verification NOT run this session** — the headless env kills the GUI process after ~8 s and `gfx_opengl_dump_bound_fbo` writes 0-byte PPMs here, so framediff / 60 s soak / overlay-frame eyeball are all owed on an interactive machine. Overlay-closed no-op is code-evident (emit returns NULL before touching the DL buffer). | | D85 | room primary/secondary DL → `texLoadFromGdl` garbage | OPEN (safety-netted; widen/pool fixes landed, geometry now renders) | | D86 · D87 | modelInitRwData truncated ptr · attract-demo BE `ramromfilestructure` | resolved | | D88.1–D88.3 · D88.5–D88.6 | `Usetup*Z` header + sub-table width/endian conversion (`d88_emit.py`) | resolved | @@ -4782,8 +4783,9 @@ Touched: `src/game/stan.c` (macro + `stanCheckLinkedSpecialTile` + All `#ifdef PORT`, N64 path kept verbatim under `#else`. Candidate root-cause came from an M-36 subagent; parts 1/3 were its work, part 2 (the local size) found on review. **Verified:** build links clean; `-level_09` framediff 3/3; -`-level_36` (Surface, has mandatory ladders) boots crash-free. **Interactive -climb test owed** — headless can't drive the climb input. +`-level_36` (Surface, has mandatory ladders) boots crash-free. +**Interactive climb test — DONE (M-37): user confirmed climbing works +in-game.** Merged to `main` via PR #5 (`955349db`). porting-notes.md §C (pointer-width in ROM/record structs). diff --git a/docs/porting-notes.md b/docs/porting-notes.md index bd73be7c..bc782a40 100644 --- a/docs/porting-notes.md +++ b/docs/porting-notes.md @@ -387,6 +387,22 @@ through a converter or a runtime bswap fixup reads scrambled. ## D. N64 hardware idioms fast3d does not emulate +- **Appending a port-owned 2D overlay to the game frame: hook inside + `gfx_run()`, not `videoSubmitCommands`.** `gfx_run()` (`gfx_pc.cpp`) is + monolithic — it does `start_frame` → `gfx_run_dl` → `gfx_flush` → + `end_frame` → `swap_buffers_begin` in one call, and the game DL is executed + from `osSpTaskStartGo` (`libultra.c`), *not* through `videoSubmitCommands` + (which is dead). So a second `gfx_run()` per frame is not possible (it swaps + buffers). The clean seam is a one-line C hook between `gfx_run_dl(commands)` + and `gfx_flush()` that runs a second `gfx_run_dl()` on a port-built DL when + non-NULL (D184, F10 options overlay). The overlay DL is self-sufficient: + `gDPPipeSync` + `gDPSetCycleType(G_CYC_1CYCLE)` + `gDPSetTexturePersp(G_TP_NONE)` + + a full-screen `gDPSetScissor` + `microcode_constructor()` (the game's own + 2D combiner/rendermode prologue), then `gDPFillRectangle` / `textRender` + (game symbols, extern'd like `input.c` externs `current_menu` — UI, not + logic). 2D pixel space is `viGetX()` x `viGetY()`. Return NULL when the + overlay is closed so the frame is byte-for-byte unchanged. + - Z buffer cleared by pointing the colour image at it + fill-rect → does nothing in fast3d; must emit `G_CLEAR_DEPTH_EXT` (D105). - LOD / detail mip tiles: fast3d fabricates a crop when detail textures diff --git a/port/fast3d/gfx_pc.cpp b/port/fast3d/gfx_pc.cpp index e0b7daeb..b7e064bb 100644 --- a/port/fast3d/gfx_pc.cpp +++ b/port/fast3d/gfx_pc.cpp @@ -3068,6 +3068,12 @@ extern "C" void gfx_start_frame(void) { uint32_t num_dls = 0; +/* F10 port-layer options overlay (port/src/optionsoverlay.c). Returns a + * self-contained 2D display list to draw on top of the game's frame, or NULL + * when the overlay is closed -- in which case nothing is appended and the + * frame is byte-identical to before (golden dumps unaffected). */ +extern "C" Gfx* optionsOverlayEmit(void); + extern "C" void gfx_run(Gfx* commands) { ++num_dls; gfx_sp_reset(); @@ -3091,6 +3097,12 @@ extern "C" void gfx_run(Gfx* commands) { rendering_state.viewport = {}; rendering_state.scissor = {}; gfx_run_dl(commands); + { + Gfx* overlay = optionsOverlayEmit(); + if (overlay != nullptr) { + gfx_run_dl(overlay); + } + } gfx_flush(); gfxFramebuffer = 0; diff --git a/port/include/config.h b/port/include/config.h index 22aa1ac4..d2dfc0bb 100644 --- a/port/include/config.h +++ b/port/include/config.h @@ -33,6 +33,39 @@ void configRegisterString(const char *key, char *value, int bufSize); const char *configGetFrameDump(void); /* "lo-hi[:step]" or NULL if unset */ int configGetInputLog(void); +/* ------------------------------------------------------------------------ + * Option enumeration (for the F10 port-layer options overlay). + * + * configForEachOption() walks every registered option; configSetOptionMeta() + * attaches display metadata (label / adjust step / enum-value names) keyed by + * the dotted key. Meta is an optional side table -- config.c stays ignorant of + * which specific keys exist; the overlay populates it at init time. + * ---------------------------------------------------------------------- */ +enum { + CONFIG_OPT_INT = 0, + CONFIG_OPT_UINT, + CONFIG_OPT_FLOAT, + CONFIG_OPT_STR, +}; + +/* key : dotted option key + * type : CONFIG_OPT_* + * ptr : &int / &unsigned / &float / char* (the live variable) + * min,max : registered clamp bounds (min==max -> unclamped) + * step : adjust step from the meta side table (0 if none) + * label : display label from the meta side table (NULL if none) + * enumNames: NULL-terminated value-name list from the meta side table (or NULL) + * ctx : opaque pointer passed straight through + */ +typedef void (*ConfigOptionCb)(const char *key, int type, void *ptr, + double min, double max, double step, + const char *label, const char *const *enumNames, + void *ctx); + +void configForEachOption(ConfigOptionCb cb, void *ctx); +void configSetOptionMeta(const char *key, const char *label, double step, + const char *const *enumNames); + #ifdef __cplusplus } #endif diff --git a/port/include/input.h b/port/include/input.h index ea57e382..693b0ccc 100644 --- a/port/include/input.h +++ b/port/include/input.h @@ -54,6 +54,10 @@ void inputNotifyClick(void); int inputReleaseCapture(void); int inputMouseCaptureActive(void); +/* F10 options overlay: force the OS cursor free + visible while the overlay + * owns the mouse. Safe to call every poll. */ +void inputSuspendForOverlay(void); + /* Queue a mouse-wheel weapon-cycle input (one short A-button press). Sign is * ignored -- GE only cycles forward on a bare A edge. */ void inputPostWheel(int notches); diff --git a/port/include/optionsoverlay.h b/port/include/optionsoverlay.h new file mode 100644 index 00000000..df4add48 --- /dev/null +++ b/port/include/optionsoverlay.h @@ -0,0 +1,50 @@ +#ifndef PORT_OPTIONSOVERLAY_H +#define PORT_OPTIONSOVERLAY_H + +/* + * F10 in-game options overlay (approach C from docs/dev/OPTIONS-MENU-PLAN.md). + * + * A self-contained port-layer immediate-mode overlay: it draws its own 2D + * display list on top of the game's frame (appended in fast3d's gfx_run, + * after the game DL) and handles its own keyboard/mouse nav. It edits the + * port-owned config.c options directly, so changes apply live for the live + * ones. When closed it renders NOTHING (zero bytes appended) -- golden dumps + * stay byte-identical. + * + * Hooks: + * video.c videoPumpEvents : F10 -> optionsOverlayToggle(); ESC closes; + * wheel -> optionsOverlayScroll() + * input.c inputComputePad : controller 0 swallowed while open; + * nav routed to optionsOverlayHandleInput() + * gfx_pc.cpp gfx_run : optionsOverlayEmit() appended after the game DL + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Toggle open/closed. On the closing edge the config is saved. */ +void optionsOverlayToggle(void); + +/* 1 while the overlay is on screen. */ +int optionsOverlayIsOpen(void); + +/* Called from inputComputePad(0) while open: reads SDL keyboard edges and + * drives the cursor / value adjustments. */ +void optionsOverlayHandleInput(void); + +/* Mouse-wheel notch -> move the selection (host event pump). */ +void optionsOverlayScroll(int dir); + +/* Build the overlay's 2D display list for this frame, or return NULL when the + * overlay is closed. Called by fast3d after running the game DL. */ +Gfx *optionsOverlayEmit(void); + +#ifdef __cplusplus +} +#endif + +#endif /* PORT_OPTIONSOVERLAY_H */ diff --git a/port/include/video.h b/port/include/video.h index 346e2210..d9bbe152 100644 --- a/port/include/video.h +++ b/port/include/video.h @@ -48,6 +48,20 @@ void videoResetTextureCache(void); /* Current FPS (measured). */ float videoGetFPS(void); +/* Re-apply the live-tunable [Video] knobs (VSync / FpsCap / TextureFilter) on + * the next frame start. Called by the F10 options overlay after an edit. */ +void videoRequestLiveConfig(void); + +/* F10 options overlay -> window/fullscreen changes. The overlay input handler + * runs on the scheduler thread; SDL window ops must run on the thread that + * created the window, so these only post a request that videoPumpEvents() + * (host thread) applies. The Get* helpers are read-only and thread-safe. */ +void videoRequestWindowSize(int w, int h); +void videoRequestFullscreen(int on); +void videoGetWindowSize(int *w, int *h); +void videoGetDesktopSize(int *w, int *h); +int videoIsFullscreen(void); + /* Snapshot live window geometry into the config vars (call before configSave * on a clean exit). No-op if the window isn't up. */ void videoSaveWindowState(void); diff --git a/port/src/config.c b/port/src/config.c index 6d41cb27..211d51fe 100644 --- a/port/src/config.c +++ b/port/src/config.c @@ -109,6 +109,81 @@ void configRegisterString(const char *key, char *value, int bufSize) o->bufSize = bufSize; } +/* ------------------------------------------------------------------------ + * Option metadata side table + enumeration (F10 options overlay). config.c + * gains no knowledge of specific keys: the overlay calls configSetOptionMeta() + * for each row it draws, and configForEachOption() joins that against the + * registered options. + * ---------------------------------------------------------------------- */ +struct OptionMeta { + char key[64]; + const char *label; + double step; + const char *const *enumNames; +}; +static struct OptionMeta metaOpts[MAX_OPTIONS]; +static int numMetaOpts = 0; + +static const struct OptionMeta *findMeta(const char *key) +{ + for (int i = 0; i < numMetaOpts; i++) { + if (strcmp(metaOpts[i].key, key) == 0) { + return &metaOpts[i]; + } + } + return NULL; +} + +void configSetOptionMeta(const char *key, const char *label, double step, + const char *const *enumNames) +{ + struct OptionMeta *m = NULL; + for (int i = 0; i < numMetaOpts; i++) { + if (strcmp(metaOpts[i].key, key) == 0) { + m = &metaOpts[i]; + break; + } + } + if (!m) { + if (numMetaOpts >= MAX_OPTIONS) return; + m = &metaOpts[numMetaOpts++]; + strncpy(m->key, key, sizeof(m->key) - 1); + m->key[sizeof(m->key) - 1] = 0; + } + m->label = label; + m->step = step; + m->enumNames = enumNames; +} + +void configForEachOption(ConfigOptionCb cb, void *ctx) +{ + if (!cb) return; + for (int i = 0; i < numIntOpts; i++) { + const struct OptionMeta *m = findMeta(intOpts[i].key); + cb(intOpts[i].key, CONFIG_OPT_INT, intOpts[i].value, + (double)intOpts[i].min, (double)intOpts[i].max, + m ? m->step : 0.0, m ? m->label : NULL, m ? m->enumNames : NULL, ctx); + } + for (int i = 0; i < numUIntOpts; i++) { + const struct OptionMeta *m = findMeta(uintOpts[i].key); + cb(uintOpts[i].key, CONFIG_OPT_UINT, uintOpts[i].value, + (double)uintOpts[i].min, (double)uintOpts[i].max, + m ? m->step : 0.0, m ? m->label : NULL, m ? m->enumNames : NULL, ctx); + } + for (int i = 0; i < numFloatOpts; i++) { + const struct OptionMeta *m = findMeta(floatOpts[i].key); + cb(floatOpts[i].key, CONFIG_OPT_FLOAT, floatOpts[i].value, + (double)floatOpts[i].min, (double)floatOpts[i].max, + m ? m->step : 0.0, m ? m->label : NULL, m ? m->enumNames : NULL, ctx); + } + for (int i = 0; i < numStrOpts; i++) { + const struct OptionMeta *m = findMeta(strOpts[i].key); + cb(strOpts[i].key, CONFIG_OPT_STR, strOpts[i].value, + 0.0, 0.0, m ? m->step : 0.0, m ? m->label : NULL, + m ? m->enumNames : NULL, ctx); + } +} + static int parseInt(const char *s) { while (*s == ' ' || *s == '\t') s++; diff --git a/port/src/input.c b/port/src/input.c index 9e85f6cb..122b0161 100644 --- a/port/src/input.c +++ b/port/src/input.c @@ -79,6 +79,7 @@ #include "system.h" #include "config.h" #include "input.h" +#include "optionsoverlay.h" /* N64 button bits (from PR/os.h -- duplicated here to avoid pulling os.h, * whose `u8 errno;` field collides with 's macro). */ @@ -500,6 +501,17 @@ unsigned inputComputePad(int idx, signed char *stick_x, signed char *stick_y) return 0; } + /* F10 options overlay: while it is open, controller 0 is fully swallowed + * (neutral pad, no stick) and the nav keys / wheel drive the overlay + * instead. Mirrors the WI-1 "cursor free in a stage -> withhold input" + * pattern. Controllers 1-3 are untouched. */ + if (idx == 0 && optionsOverlayIsOpen()) { + optionsOverlayHandleInput(); + if (stick_x) *stick_x = 0; + if (stick_y) *stick_y = 0; + return 0; + } + /* ---- keyboard + mouse: controller 0 only ---- */ if (idx == 0) { const Uint8 *ks = SDL_GetKeyboardState(NULL); @@ -866,6 +878,22 @@ int inputReleaseCapture(void) int inputMouseCaptureActive(void) { return mouseCaptureMode && !mouseGrabbed; } +/* F10 options overlay: while it owns controller 0 the inputComputePad poll + * early-returns before reconcileGrab()/applyCursorVisibility(), so whatever + * grab state was live when F10 was pressed (relative mode + hidden cursor in a + * stage) would persist and the mouse UI would be unusable. Force the cursor + * free + visible every poll; the normal reconcile resumes once the overlay + * closes and the early-return no longer fires. */ +void inputSuspendForOverlay(void) +{ + if (mouseGrabbed) { + mouseGrabbed = 0; + SDL_SetRelativeMouseMode(SDL_FALSE); + mouseDX = mouseDY = 0.0; + } + SDL_ShowCursor(SDL_ENABLE); +} + void inputPostWheel(int notches) { if (notches < 0) notches = -notches; /* both directions cycle forward */ diff --git a/port/src/optionsoverlay.c b/port/src/optionsoverlay.c new file mode 100644 index 00000000..018412dc --- /dev/null +++ b/port/src/optionsoverlay.c @@ -0,0 +1,626 @@ +/* + * F10 in-game options overlay -- approach (C) from docs/dev/OPTIONS-MENU-PLAN.md. + * + * Port-layer only. No src/ menu code is touched: the overlay draws its own + * fast3d 2D display list (appended after the game DL in gfx_run) and edits the + * port-owned config.c variables directly. Live knobs apply immediately; the + * two that need an FBO/window rebuild (MSAA, Fullscreen) are tagged "(restart)". + * + * Text + fill helpers are the game's own (textRender / microcode_constructor / + * gDPFillRectangle) reached by extern -- same pattern input.c uses to read + * current_menu / cursor_h_pos. This is a rendering/UI view, not a logic change. + * + * Diagnostic: set GE_OPTIONSOVERLAY=1 to auto-open at boot (headless layout + * check). Env-gated, harmless when unset. + */ + +#include +#include +#include +#include + +#include + +#include +/* gbi.h's gDP* DL macros use _SHIFTL/_SHIFTR but do not define them -- game TUs + * get them from /, which also drags in N64 OS headers that + * shadow libc here. Define the two pure macros locally (verbatim from mbi.h) so + * this stays a plain port TU. Without them GCC/ld fails "undefined reference to + * _SHIFTL" (MinGW's chain happens to provide it). */ +#ifndef _SHIFTL +#define _SHIFTL(v, s, w) ((u32)(((u32)(v) & ((0x01 << (w)) - 1)) << (s))) +#define _SHIFTR(v, s, w) ((u32)(((u32)(v) >> (s)) & ((0x01 << (w)) - 1))) +#endif +#include + +#include "platform.h" +#include "system.h" +#include "config.h" +#include "video.h" +#include "input.h" +#include "optionsoverlay.h" + +/* ---- game symbols (rendering/UI only; see input.c for the same pattern) ---- */ +struct font; +struct fontchar; +extern struct font *ptrFontBankGothic; +extern struct fontchar *ptrFontBankGothicChars; +extern Gfx *microcode_constructor(Gfx *gdl); +extern Gfx *textRender(Gfx *gdl, s32 *x, s32 *y, char *text, struct fontchar *chars, + struct font *font, u32 colour, s32 width, s32 height, + u32 yOffset, s32 lineheight); +extern void textMeasure(s32 *textheight, s32 *textwidth, char *text, + struct fontchar *chars, struct font *font, s32 lineheight); +extern s16 viGetX(void); +extern s16 viGetY(void); + +/* ------------------------------------------------------------------------ */ + +enum { ROW_TOGGLE, ROW_SLIDER, ROW_ENUM, ROW_MSAA, ROW_RES }; + +static const char *const kOnOff[] = { "OFF", "ON", NULL }; +static const char *const kTexFilter[] = { "NEAREST", "BILINEAR", "3-POINT", NULL }; +static const char *const kCapture[] = { "ALWAYS GRAB", "CLICK-TO-LOCK", NULL }; +static const int kMsaaSeq[] = { 1, 2, 4, 8 }; + +/* Windowed-mode resolution presets. Filtered at init to those that fit the + * desktop; the Resolution row cycles the surviving list. */ +static const int kResList[][2] = { + { 640, 480 }, { 800, 600 }, { 960, 720 }, { 1024, 768 }, + { 1152, 864 }, { 1280, 720 }, { 1280, 800 }, { 1280, 960 }, + { 1366, 768 }, { 1440, 900 }, { 1600, 900 }, { 1600, 1200 }, + { 1680, 1050 }, { 1920, 1080 }, { 1920, 1200 }, { 2560, 1440 }, + { 3200, 1800 }, { 3840, 2160 }, +}; +#define NUM_RES ((int)(sizeof(kResList) / sizeof(kResList[0]))) +static int s_resFit[NUM_RES]; /* indices into kResList that fit the desktop */ +static int s_resFitN = 0; +static int s_resSel = 0; /* index into s_resFit */ + +struct Row { + const char *key; + const char *label; + int kind; + double step; + const char *const *names; /* ROW_TOGGLE / ROW_ENUM value names */ + int restart; /* value change needs a restart */ + double uiMin, uiMax; /* 0,0 -> use the registered clamp */ + + /* resolved from config.c at init */ + int found; + int type; /* CONFIG_OPT_* */ + void *ptr; + double cfgMin, cfgMax; +}; + +static struct Row rows[] = { + { "Video.Fullscreen", "Fullscreen", ROW_TOGGLE, 1, kOnOff, 0, 0, 0, 0,0,0,0,0 }, + { "__Resolution", "Resolution", ROW_RES, 0, NULL, 0, 0, 0, 0,0,0,0,0 }, + { "Video.VSync", "VSync", ROW_TOGGLE, 1, kOnOff, 0, 0, 0, 0,0,0,0,0 }, + { "Video.FpsCap", "Frame cap", ROW_SLIDER, 10, NULL, 0, 0, 360, 0,0,0,0,0 }, + { "Video.MSAA", "MSAA", ROW_MSAA, 0, NULL, 1, 0, 0, 0,0,0,0,0 }, + { "Video.TextureFilter", "Texture filter", ROW_ENUM, 1, kTexFilter, 0, 0, 0, 0,0,0,0,0 }, + { "Input.MouseAimSpeed", "Mouse aim speed", ROW_SLIDER, 1, NULL, 0, 0, 100, 0,0,0,0,0 }, + { "Input.MouseTurnSpeed", "Mouse turn speed", ROW_SLIDER, 1, NULL, 0, 0, 100, 0,0,0,0,0 }, + { "Input.MouseInvertY", "Mouse invert Y", ROW_TOGGLE, 1, kOnOff, 0, 0, 0, 0,0,0,0,0 }, + { "Input.MouseCaptureMode", "Mouse capture", ROW_TOGGLE, 1, kCapture, 0, 0, 0, 0,0,0,0,0 }, + { "Game.ScreenShakeIntensity","Screen shake", ROW_SLIDER, 0.25, NULL, 0, 0, 3, 0,0,0,0,0 }, +}; +#define NUM_ROWS ((int)(sizeof(rows) / sizeof(rows[0]))) + +static int s_inited = 0; +static volatile int s_open = 0; +static int s_sel = 0; + +/* Layout (game 2D pixel space = viGetX() x viGetY(), ~320x240). Shared by the + * emit path and the mouse hit-testing in optionsOverlayHandleInput(). + * BankGothic caps are ~9 units tall here, so rows need ~16 units of pitch and + * values are right-aligned to the panel edge to survive the wide font. */ +#define OV_X0 20 +#define OV_LABEL_X 28 +#define OV_TOP 12 +#define OV_LINE 15 /* row pitch (13 rows must fit ~240) */ +#define OV_HDR 2 /* header rows above row 0 (title+hint) */ +#define OV_ROW_Y(i) (OV_TOP + ((i) + OV_HDR) * OV_LINE) +#define OV_RIGHT (viGetX() - OV_X0) /* right edge for right-aligned text */ +#define OV_NUM_W 36 /* reserved width for a slider's number */ +#define OV_BAR_X 150 + +/* Slider fill bar span in overlay space (shared by emit + hit-testing). */ +static void sliderBarSpan(s32 *x0, s32 *x1) +{ + *x0 = OV_BAR_X; + *x1 = OV_RIGHT - OV_NUM_W; + if (*x1 < *x0 + 16) { + *x1 = *x0 + 16; + } +} + +/* Row index the given overlay-space y falls in, or -1. */ +static int overlayRowAtY(double oy) +{ + for (int i = 0; i < NUM_ROWS; i++) { + double top = OV_ROW_Y(i) - 3; + if (oy >= top && oy < top + OV_LINE) { + return i; + } + } + return -1; +} + +/* The close box brackets the title row at the panel's right edge. */ +#define OV_CB_X0 (OV_RIGHT - 14) +#define OV_CB_X1 (OV_RIGHT + 7) +#define OV_CB_Y0 (OV_TOP - 3) +#define OV_CB_Y1 (OV_TOP + 12) +static int overlayInCloseBox(double ox, double oy) +{ + return ox >= OV_CB_X0 && ox <= OV_CB_X1 && + oy >= OV_CB_Y0 && oy <= OV_CB_Y1; +} + +/* ------------------------------------------------------------------------ */ + +static void resolveCb(const char *key, int type, void *ptr, double min, double max, + double step, const char *label, const char *const *names, + void *ctx) +{ + (void)step; (void)label; (void)names; (void)ctx; + for (int i = 0; i < NUM_ROWS; i++) { + if (strcmp(rows[i].key, key) == 0) { + rows[i].found = 1; + rows[i].type = type; + rows[i].ptr = ptr; + rows[i].cfgMin = min; + rows[i].cfgMax = max; + return; + } + } +} + +static void overlayInit(void) +{ + if (s_inited) { + return; + } + s_inited = 1; + + /* Publish display metadata so config.c / future consumers can see it, + * without config.c knowing any specific key. */ + for (int i = 0; i < NUM_ROWS; i++) { + configSetOptionMeta(rows[i].key, rows[i].label, rows[i].step, rows[i].names); + } + configForEachOption(resolveCb, NULL); + + for (int i = 0; i < NUM_ROWS; i++) { + if (rows[i].kind == ROW_RES) { + rows[i].found = 1; /* not config-backed; driven via video.c */ + continue; + } + if (!rows[i].found) { + sysLogPrintf(LOG_WARNING, "optionsoverlay: option '%s' not registered", + rows[i].key); + } + } + + /* Build the windowed-resolution preset list: presets that fit the desktop, + * plus the current window size snapped to the nearest surviving entry. */ + { + int dw = 1920, dh = 1080; + videoGetDesktopSize(&dw, &dh); + s_resFitN = 0; + for (int i = 0; i < NUM_RES; i++) { + if (kResList[i][0] <= dw && kResList[i][1] <= dh) { + s_resFit[s_resFitN++] = i; + } + } + if (s_resFitN == 0) { + s_resFit[s_resFitN++] = 0; + } + int cw = 0, ch = 0; + videoGetWindowSize(&cw, &ch); + long best = -1; + for (int k = 0; k < s_resFitN; k++) { + int i = s_resFit[k]; + long d = labs((long)kResList[i][0] - cw) + + labs((long)kResList[i][1] - ch); + if (best < 0 || d < best) { best = d; s_resSel = k; } + } + } + + const char *e = getenv("GE_OPTIONSOVERLAY"); + if (e && atoi(e) != 0) { + s_open = 1; + sysLogPrintf(LOG_INFO, "optionsoverlay: auto-opened (GE_OPTIONSOVERLAY)"); + } +} + +static double rowGet(const struct Row *r) +{ + if (!r->found || !r->ptr) { + return 0.0; + } + switch (r->type) { + case CONFIG_OPT_INT: return (double)*(int *)r->ptr; + case CONFIG_OPT_UINT: return (double)*(unsigned int *)r->ptr; + case CONFIG_OPT_FLOAT: return (double)*(float *)r->ptr; + default: return 0.0; + } +} + +static double rowLo(const struct Row *r) +{ + return (r->uiMin != r->uiMax) ? r->uiMin : r->cfgMin; +} +static double rowHi(const struct Row *r) +{ + return (r->uiMin != r->uiMax) ? r->uiMax : r->cfgMax; +} + +static void rowSet(struct Row *r, double v) +{ + double lo = rowLo(r), hi = rowHi(r); + if (lo != hi) { + if (v < lo) v = lo; + if (v > hi) v = hi; + } + switch (r->type) { + case CONFIG_OPT_INT: *(int *)r->ptr = (int)lround(v); break; + case CONFIG_OPT_UINT: *(unsigned int *)r->ptr = (unsigned int)(v < 0 ? 0 : lround(v)); break; + case CONFIG_OPT_FLOAT: *(float *)r->ptr = (float)v; break; + default: return; + } + + /* Live-apply the video knobs that need a fast3d/SDL call. Everything else + * is read straight off the pointer by its owner every frame/poll. */ + if (strcmp(r->key, "Video.Fullscreen") == 0) { + videoRequestFullscreen((int)lround(v)); + } else if (strncmp(r->key, "Video.", 6) == 0 && !r->restart) { + videoRequestLiveConfig(); + } +} + +static void rowAdjust(struct Row *r, int dir) +{ + if (!r->found) { + return; + } + double v = rowGet(r); + switch (r->kind) { + case ROW_TOGGLE: + rowSet(r, (v != 0.0) ? 0.0 : 1.0); + break; + case ROW_MSAA: { + int idx = 0; + for (int i = 0; i < 4; i++) { + if (kMsaaSeq[i] == (int)lround(v)) idx = i; + } + idx += dir; + if (idx < 0) idx = 0; + if (idx > 3) idx = 3; + rowSet(r, (double)kMsaaSeq[idx]); + break; + } + case ROW_ENUM: { + double lo = r->cfgMin, hi = r->cfgMax; + v += dir; + if (v < lo) v = hi; + if (v > hi) v = lo; + rowSet(r, v); + break; + } + case ROW_RES: { + if (s_resFitN <= 0 || videoIsFullscreen()) { + break; /* resolution is windowed-only */ + } + s_resSel += (dir >= 0) ? 1 : -1; + if (s_resSel < 0) s_resSel = s_resFitN - 1; + if (s_resSel >= s_resFitN) s_resSel = 0; + int i = s_resFit[s_resSel]; + videoRequestWindowSize(kResList[i][0], kResList[i][1]); + break; + } + default: /* ROW_SLIDER */ + rowSet(r, v + dir * r->step); + break; + } +} + +/* ------------------------------------------------------------------------ */ + +void optionsOverlayToggle(void) +{ + overlayInit(); + s_open = !s_open; + sysLogPrintf(LOG_INFO, "optionsoverlay: %s", s_open ? "opened" : "closed"); + if (!s_open) { + configSave(); + } +} + +int optionsOverlayIsOpen(void) +{ + if (!s_inited) { + overlayInit(); + } + return s_open; +} + +void optionsOverlayScroll(int dir) +{ + if (!s_open || dir == 0) { + return; + } + s_sel += (dir > 0) ? -1 : 1; /* wheel-up -> move up the list */ + if (s_sel < 0) s_sel = NUM_ROWS - 1; + if (s_sel >= NUM_ROWS) s_sel = 0; +} + +/* Set a slider row from an overlay-space x inside its value bar, snapped to + * the row's step. */ +static void sliderSetFromX(struct Row *r, double ox) +{ + double lo = rowLo(r), hi = rowHi(r); + if (hi <= lo) { + return; + } + s32 bx0, bx1; + sliderBarSpan(&bx0, &bx1); + double f = (ox - bx0) / (double)(bx1 - bx0); + if (f < 0) f = 0; + if (f > 1) f = 1; + double v = lo + f * (hi - lo); + double step = (r->step > 0.0) ? r->step : 1.0; + v = lround(v / step) * step; + rowSet(r, v); +} + +void optionsOverlayHandleInput(void) +{ + static int prevUp, prevDn, prevLf, prevRt, prevLmb, prevRmb; + static int dragRow = -1; + + if (!s_open) { + prevUp = prevDn = prevLf = prevRt = prevLmb = prevRmb = 0; + dragRow = -1; + return; + } + + /* The overlay owns the mouse while it is open: force the OS cursor free + + * visible (a stage poll would otherwise leave it locked/hidden). */ + inputSuspendForOverlay(); + + const Uint8 *ks = SDL_GetKeyboardState(NULL); + int mx = 0, my = 0; + Uint32 mb = SDL_GetMouseState(&mx, &my); + int lmb = (mb & SDL_BUTTON(SDL_BUTTON_LEFT)) ? 1 : 0; + int rmb = (mb & SDL_BUTTON(SDL_BUTTON_RIGHT)) ? 1 : 0; + + int up = ks[SDL_SCANCODE_UP] || ks[SDL_SCANCODE_KP_8]; + int dn = ks[SDL_SCANCODE_DOWN] || ks[SDL_SCANCODE_KP_2]; + int lf = ks[SDL_SCANCODE_LEFT] || ks[SDL_SCANCODE_KP_4]; + int rt = ks[SDL_SCANCODE_RIGHT] || ks[SDL_SCANCODE_KP_6] + || ks[SDL_SCANCODE_RETURN] || ks[SDL_SCANCODE_KP_ENTER]; + + /* ---- keyboard / D-pad nav ---- */ + if (up && !prevUp) s_sel = (s_sel + NUM_ROWS - 1) % NUM_ROWS; + if (dn && !prevDn) s_sel = (s_sel + 1) % NUM_ROWS; + if (lf && !prevLf) rowAdjust(&rows[s_sel], -1); + if (rt && !prevRt) rowAdjust(&rows[s_sel], +1); + + /* ---- mouse ---- */ + int ww = 0, wh = 0; + videoGetWindowSize(&ww, &wh); + if (ww > 0 && wh > 0) { + double ox = (double)mx * (double)viGetX() / ww; + double oy = (double)my * (double)viGetY() / wh; + int hoverRow = overlayRowAtY(oy); + int onClose = overlayInCloseBox(ox, oy); + + if (hoverRow >= 0 && !onClose) { + s_sel = hoverRow; /* hover-to-highlight */ + } + + /* left press. A click in the label column only focuses the row; a + * click in the value/control column (>= the bar-span start) changes + * it -- so clicking to select a toggle doesn't also flip it. */ + s32 bx0, bx1; + sliderBarSpan(&bx0, &bx1); + if (lmb && !prevLmb) { + if (onClose) { + optionsOverlayToggle(); /* close + configSave */ + return; + } + if (hoverRow >= 0 && ox >= bx0) { + struct Row *r = &rows[hoverRow]; + if (r->kind == ROW_SLIDER && r->found) { + sliderSetFromX(r, ox); + dragRow = hoverRow; + } else { + rowAdjust(r, +1); /* toggle / cycle forward */ + } + } + } + /* drag a slider */ + if (lmb && dragRow >= 0 && rows[dragRow].kind == ROW_SLIDER) { + sliderSetFromX(&rows[dragRow], ox); + } + if (!lmb) { + dragRow = -1; + } + /* right press in the value column: cycle back / decrement */ + if (rmb && !prevRmb && hoverRow >= 0 && !onClose && ox >= bx0) { + rowAdjust(&rows[hoverRow], -1); + } + } + + prevUp = up; prevDn = dn; prevLf = lf; prevRt = rt; + prevLmb = lmb; prevRmb = rmb; +} + +/* ------------------------------------------------------------------------ */ + +#define OV_BUF_CMDS 8192 +static Gfx s_buf[OV_BUF_CMDS]; + +static Gfx *fillRect(Gfx *gdl, s32 x0, s32 y0, s32 x1, s32 y1, + u8 r, u8 g, u8 b, u8 a) +{ + gDPSetRenderMode(gdl++, G_RM_XLU_SURF, G_RM_XLU_SURF2); + gDPSetCombineMode(gdl++, G_CC_PRIMITIVE, G_CC_PRIMITIVE); + gDPSetPrimColor(gdl++, 0, 0, r, g, b, a); + gDPFillRectangle(gdl++, x0, y0, x1, y1); + return gdl; +} + +static void valueText(const struct Row *r, char *out, int n) +{ + double v = rowGet(r); + if (r->kind == ROW_RES) { + if (videoIsFullscreen()) { + snprintf(out, n, "(fullscreen)"); + } else if (s_resFitN <= 0) { + snprintf(out, n, "n/a"); + } else { + int i = s_resFit[s_resSel]; + snprintf(out, n, "%d x %d", kResList[i][0], kResList[i][1]); + } + return; + } + if ((r->kind == ROW_TOGGLE || r->kind == ROW_ENUM) && r->names) { + int idx = (int)lround(v); + int cnt = 0; + while (r->names[cnt]) cnt++; + if (idx >= 0 && idx < cnt) { + snprintf(out, n, "%s", r->names[idx]); + return; + } + } + if (r->kind == ROW_MSAA) { + if ((int)lround(v) <= 1) snprintf(out, n, "OFF"); + else snprintf(out, n, "%dx", (int)lround(v)); + return; + } + if (r->kind == ROW_SLIDER && r->type == CONFIG_OPT_FLOAT) { + snprintf(out, n, "%.2f", v); + return; + } + if (r->kind == ROW_SLIDER && (int)lround(v) == 0 && + strcmp(r->key, "Video.FpsCap") == 0) { + snprintf(out, n, "OFF"); + return; + } + snprintf(out, n, "%d", (int)lround(v)); +} + +static Gfx *drawText(Gfx *gdl, s32 x, s32 y, const char *str, u32 colour) +{ + s32 px = x, py = y; + /* width/height are the on-screen CLIP rect textRenderGlyph tests against + * (clipX=start x, clipY=start y, +clipWidth/+clipHeight), NOT the text's + * own measured size -- passing the measured w/h clipped every glyph out + * (baseline+height > measured h => nothing drawn). Match the game: pass + * the full 2D viewport, like bondview2.c's debug-text path. */ + return textRender(gdl, &px, &py, (char *)str, ptrFontBankGothicChars, + ptrFontBankGothic, colour, viGetX(), viGetY(), 0, 0); +} + +static s32 measureText(const char *str) +{ + s32 h = 0, w = 0; + textMeasure(&h, &w, (char *)str, ptrFontBankGothicChars, ptrFontBankGothic, 0); + return w; +} + +/* Right-aligned: the string ends at xr. */ +static Gfx *drawTextR(Gfx *gdl, s32 xr, s32 y, const char *str, u32 colour) +{ + return drawText(gdl, xr - measureText(str), y, str, colour); +} + +Gfx *optionsOverlayEmit(void) +{ + if (!s_inited) { + overlayInit(); + } + if (!s_open) { + return NULL; /* nothing appended -> golden dumps byte-identical */ + } + + const s32 W = viGetX(); + const s32 H = viGetY(); + const s32 right = OV_RIGHT; + const s32 panelTop = OV_TOP - 9; + const s32 panelBottom = OV_ROW_Y(NUM_ROWS - 1) + OV_LINE / 2 + 3; + s32 bx0, bx1; + sliderBarSpan(&bx0, &bx1); + Gfx *gdl = s_buf; + + gDPPipeSync(gdl++); + gDPSetCycleType(gdl++, G_CYC_1CYCLE); + gDPSetTexturePersp(gdl++, G_TP_NONE); + gDPSetScissor(gdl++, G_SC_NON_INTERLACE, 0, 0, W, H); + + /* ---- pass 1: all fills (G_CC_PRIMITIVE) ---- */ + gdl = fillRect(gdl, 0, 0, W, H, 0, 0, 0, 150); /* dim */ + gdl = fillRect(gdl, OV_X0 - 8, panelTop, W - (OV_X0 - 8), panelBottom, + 8, 10, 24, 210); /* panel */ + gdl = fillRect(gdl, OV_CB_X0, OV_CB_Y0, OV_CB_X1, OV_CB_Y1, + 150, 40, 40, 235); /* close */ + + for (int i = 0; i < NUM_ROWS; i++) { + s32 rowY = OV_ROW_Y(i); + if (i == s_sel) { + gdl = fillRect(gdl, OV_X0 - 4, rowY - 3, W - (OV_X0 - 4), + rowY + OV_LINE - 4, 40, 46, 96, 220); + } + if (rows[i].kind == ROW_SLIDER && rows[i].found) { + double lo = rowLo(&rows[i]), hi = rowHi(&rows[i]); + double f = (hi > lo) ? (rowGet(&rows[i]) - lo) / (hi - lo) : 0.0; + if (f < 0) f = 0; if (f > 1) f = 1; + s32 by = rowY + 3; + gdl = fillRect(gdl, bx0, by, bx1, by + 5, 60, 60, 70, 220); + gdl = fillRect(gdl, bx0, by, bx0 + (s32)((bx1 - bx0) * f), by + 5, + 210, 200, 90, 255); + } + } + + /* ---- pass 2: text ---- */ + gdl = microcode_constructor(gdl); + + gdl = drawText(gdl, OV_X0, OV_TOP, "PC OPTIONS", 0xffe040ff); + gdl = drawText(gdl, OV_X0, OV_TOP + OV_LINE, "click value / drag / arrows", + 0x8890a0ff); /* hint line */ + gdl = drawText(gdl, (OV_CB_X0 + OV_CB_X1) / 2 - measureText("X") / 2, + OV_TOP, "X", 0xffffffff); /* close glyph */ + + for (int i = 0; i < NUM_ROWS; i++) { + s32 rowY = OV_ROW_Y(i); + u32 col = (i == s_sel) ? 0xffffffff : 0xc0c0c8ff; + char val[48]; + + gdl = drawText(gdl, OV_LABEL_X, rowY, (char *)rows[i].label, + rows[i].found ? col : 0x808080ff); + if (!rows[i].found) { + gdl = drawTextR(gdl, right, rowY, "(n/a)", 0x808080ff); + continue; + } + + valueText(&rows[i], val, sizeof(val)); + if (rows[i].restart) { + /* value left of the bar span, "(restart)" pinned to the edge */ + gdl = drawText(gdl, bx0, rowY, val, col); + gdl = drawTextR(gdl, right, rowY, "(restart)", 0x909090ff); + } else { + gdl = drawTextR(gdl, right, rowY, val, col); + } + } + + gDPPipeSync(gdl++); + gSPEndDisplayList(gdl++); + + if ((gdl - s_buf) > OV_BUF_CMDS) { + sysLogPrintf(LOG_ERROR, "optionsoverlay: DL overflow (%d)", (int)(gdl - s_buf)); + } + return s_buf; +} diff --git a/port/src/video.c b/port/src/video.c index 10fef104..9ebcb634 100644 --- a/port/src/video.c +++ b/port/src/video.c @@ -28,6 +28,7 @@ #include "config.h" #include "video.h" #include "input.h" +#include "optionsoverlay.h" #include "../fast3d/gfx_api.h" #include "../fast3d/gfx_sdl.h" @@ -96,6 +97,110 @@ PD_CONSTRUCTOR static void videoConfigInit(void) configRegisterInt("Window.Maximized", &cfgWinMax, 0, 1); } +/* Set by videoRequestLiveConfig() (F10 overlay, host thread); consumed on the + * scheduler thread in videoStartFrame() where the GL context is bound. */ +static volatile int liveCfgDirty = 0; + +static void videoApplyTexFilter(void) +{ + if (cfgTexFilter >= 2) { + gfx_set_texture_filter(FILTER_THREE_POINT); + gfx_set_mipmap_filter(MIPMAP_LINEAR); + } else if (cfgTexFilter == 1) { + gfx_set_texture_filter(FILTER_LINEAR); + gfx_set_mipmap_filter(MIPMAP_LINEAR); + } else { + gfx_set_texture_filter(FILTER_NONE); + gfx_set_mipmap_filter(MIPMAP_NEAREST); + } +} + +/* Re-apply the live-tunable [Video] knobs (VSync / FpsCap / TextureFilter). + * MSAA and Fullscreen are FBO/window rebuilds -> "(restart)" in the overlay. */ +void videoRequestLiveConfig(void) +{ + liveCfgDirty = 1; +} + +/* --- F10 overlay: window / fullscreen changes, deferred to the host thread --- + * optionsOverlayHandleInput() runs on the scheduler thread; SDL_SetWindowSize / + * SDL_SetWindowFullscreen pump the Win32 message loop and must run on the + * window's creating thread. The overlay posts a request here; the host-thread + * event pump drains it in videoDrainWindowRequests(). */ +static volatile int winReqKind = 0; /* 0 none, 1 resize, 2 fullscreen */ +static volatile int winReqA = 0, winReqB = 0; + +void videoRequestWindowSize(int w, int h) +{ + winReqA = w; winReqB = h; winReqKind = 1; +} + +void videoRequestFullscreen(int on) +{ + winReqA = on ? 1 : 0; winReqKind = 2; +} + +void videoGetWindowSize(int *w, int *h) +{ + uint32_t ww = 0, hh = 0; int32_t x = 0, y = 0; + if (initDone && wmAPI && wmAPI->get_dimensions) { + wmAPI->get_dimensions(&ww, &hh, &x, &y); + } + if (w) *w = (int)ww; + if (h) *h = (int)hh; +} + +void videoGetDesktopSize(int *w, int *h) +{ + SDL_DisplayMode m; + memset(&m, 0, sizeof(m)); + if (SDL_GetDesktopDisplayMode(0, &m) != 0 || m.w <= 0 || m.h <= 0) { + m.w = 1920; m.h = 1080; + } + if (w) *w = m.w; + if (h) *h = m.h; +} + +int videoIsFullscreen(void) +{ + return (initDone && wmAPI && wmAPI->get_fullscreen_state) + ? (wmAPI->get_fullscreen_state() ? 1 : 0) : 0; +} + +static void videoDrainWindowRequests(void) +{ + int kind = winReqKind; + if (!kind || !wmAPI) { + winReqKind = 0; + return; + } + winReqKind = 0; + + if (kind == 1) { + int w = winReqA, h = winReqB; + int32_t px = 100, py = 100; + if (wmAPI->get_fullscreen_state && wmAPI->get_fullscreen_state()) { + if (wmAPI->set_fullscreen) wmAPI->set_fullscreen(false); + cfgFullscreen = 0; + } + if (wmAPI->get_centered_positions) { + wmAPI->get_centered_positions(w, h, &px, &py); + } + if (wmAPI->set_dimensions) { + wmAPI->set_dimensions((uint32_t)w, (uint32_t)h, px, py); + } + gfx_sdl_update_cached_size(); + cfgWinW = w; cfgWinH = h; + sysLogPrintf(LOG_INFO, "video: window -> %dx%d", w, h); + } else if (kind == 2) { + int on = winReqA; + if (wmAPI->set_fullscreen) wmAPI->set_fullscreen(on != 0); + gfx_sdl_update_cached_size(); + cfgFullscreen = on ? 1 : 0; + sysLogPrintf(LOG_INFO, "video: fullscreen %s", on ? "on" : "off"); + } +} + static u32 frames = 0; /* Set by the host event pump (F12), consumed on the render thread in * videoEndFrame where a GL context is current. */ @@ -165,16 +270,7 @@ int videoInit(void) gfx_set_fix_mip_textures(cfgFixMipTex); gfx_set_wrap_fix(cfgWrapFix); - if (cfgTexFilter >= 2) { - gfx_set_texture_filter(FILTER_THREE_POINT); - gfx_set_mipmap_filter(MIPMAP_LINEAR); - } else if (cfgTexFilter == 1) { - gfx_set_texture_filter(FILTER_LINEAR); - gfx_set_mipmap_filter(MIPMAP_LINEAR); - } else { - gfx_set_texture_filter(FILTER_NONE); - gfx_set_mipmap_filter(MIPMAP_NEAREST); - } + videoApplyTexFilter(); /* The GL context is currently current on this (host main) thread, but all * rendering happens on the game's scheduler thread. WGL only allows a @@ -209,6 +305,17 @@ void videoStartFrame(void) /* Rendering runs on the game's scheduler thread; the GL context was * created on the host main thread. */ gfx_sdl_make_context_current(); + + if (liveCfgDirty) { + liveCfgDirty = 0; + wmAPI->set_swap_interval(cfgVSync ? 1 : 0); + gfx_set_target_fps(cfgFpsCap); /* 0 = uncapped */ + videoApplyTexFilter(); + sysLogPrintf(LOG_INFO, "video: live config applied " + "(vsync=%d fpscap=%d texfilter=%d)", + cfgVSync, cfgFpsCap, cfgTexFilter); + } + gfx_start_frame(); } @@ -227,6 +334,11 @@ void videoPumpEvents(void) if (!initDone) { return; } + + /* Apply any window/fullscreen change the F10 overlay posted from the + * scheduler thread (must run here, on the window's creating thread). */ + videoDrainWindowRequests(); + SDL_Event ev; while (SDL_PollEvent(&ev)) { switch (ev.type) { @@ -246,20 +358,33 @@ void videoPumpEvents(void) exit(0); } else if (ev.key.keysym.sym == SDLK_F12 && !ev.key.repeat) { screenshotReq = 1; + } else if (ev.key.keysym.sym == SDLK_F10 && !ev.key.repeat) { + optionsOverlayToggle(); /* F10: port-layer options overlay */ } else if (ev.key.keysym.sym == SDLK_ESCAPE && !ev.key.repeat) { - /* WI-1: in click-to-lock mode ESC frees the captured cursor - * (and is swallowed); otherwise it falls through to input.c - * where it feeds the N64 B button (D145). */ - inputReleaseCapture(); + /* Overlay open: ESC closes it (and is swallowed). Otherwise + * WI-1: in click-to-lock mode ESC frees the captured cursor + * (and is swallowed); else it falls through to input.c where + * it feeds the N64 B button (D145). */ + if (optionsOverlayIsOpen()) { + optionsOverlayToggle(); + } else { + inputReleaseCapture(); + } } break; case SDL_MOUSEBUTTONDOWN: /* WI-1: a click in the window (re)locks the cursor in * click-to-lock mode; a no-op otherwise. */ - inputNotifyClick(); + if (!optionsOverlayIsOpen()) { + inputNotifyClick(); + } break; case SDL_MOUSEWHEEL: - inputPostWheel(ev.wheel.y); /* weapon cycle */ + if (optionsOverlayIsOpen()) { + optionsOverlayScroll(ev.wheel.y); /* move the selection */ + } else { + inputPostWheel(ev.wheel.y); /* weapon cycle */ + } break; case SDL_CONTROLLERDEVICEADDED: case SDL_CONTROLLERDEVICEREMOVED: