From 69b08d55ca8e177a311587720e81502dca7e293d Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Tue, 22 Apr 2025 23:26:07 +0200 Subject: [PATCH] tr2/shell: center offscreen window on game launch --- docs/tr2/CHANGELOG.md | 1 + src/tr2/game/shell/common.c | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/tr2/CHANGELOG.md b/docs/tr2/CHANGELOG.md index bc13bb77c..890c3213d 100644 --- a/docs/tr2/CHANGELOG.md +++ b/docs/tr2/CHANGELOG.md @@ -15,6 +15,7 @@ - added the current music track and timestamp to the savegame so they now persist on load (#2579) - added waterfalls to the savegame so that they now persist on load (#2686) - added support for aspect ratio-specific images (#1840) +- added a guard to ensure the game always starts on a visible screen even after unplugging displays (#2819) - changed savegame files to be stored in the `saves` directory (#2087) - changed the default fog distance to 22 tiles cutting off at 30 tiles to match TR1X (#1622) - changed the number of static mesh slots from 50 to 256 (#2734) diff --git a/src/tr2/game/shell/common.c b/src/tr2/game/shell/common.c index b767444d8..c3603f996 100644 --- a/src/tr2/game/shell/common.c +++ b/src/tr2/game/shell/common.c @@ -138,10 +138,35 @@ static void M_SyncToWindow(void) width = 1280; height = 720; } - if (x == -1 && y == -1) { // default config state - const SHELL_SIZE display_size = Shell_GetCurrentDisplaySize(); - x = (display_size.w - width) / 2; - y = (display_size.h - height) / 2; + + // Handle default position + if (x == -1 && y == -1) { + SDL_DisplayMode display_mode; + SDL_GetCurrentDisplayMode(0, &display_mode); + x = (display_mode.w - width) / 2; + y = (display_mode.h - height) / 2; + } else { + // Adjust window position if completely offscreen + bool on_screen = false; + const int32_t num_displays = SDL_GetNumVideoDisplays(); + for (int32_t i = 0; i < num_displays; i++) { + SDL_Rect bounds; + SDL_GetDisplayBounds(i, &bounds); + if (x + width > bounds.x && x < bounds.x + bounds.w + && y + height > bounds.y && y < bounds.y + bounds.h) { + on_screen = true; + break; + } + } + if (!on_screen) { + x = 0; + y = 0; + // Find the first display to reposition the window + SDL_Rect bounds; + SDL_GetDisplayBounds(0, &bounds); + x = bounds.x + (bounds.w - width) / 2; + y = bounds.y + (bounds.h - height) / 2; + } } SDL_SetWindowFullscreen(g_SDLWindow, 0);