diff options
author | kcgen <kcgen@users.noreply.github.com> | 2022-10-17 23:44:01 +0300 |
---|---|---|
committer | kcgen <1557255+kcgen@users.noreply.github.com> | 2022-10-18 21:45:12 +0300 |
commit | 889ce1ee99dbfdc1a92a5c9f35c0fa46949dc3f9 (patch) | |
tree | 51498b02917d19b33a6508131f901d561aff8d3f | |
parent | e39f8dfa35cc1e5562588532b88468778fb5021b (diff) |
Initialize the window's canvas size during its setup phase
-rw-r--r-- | include/sdlmain.h | 7 | ||||
-rw-r--r-- | src/gui/sdlmain.cpp | 48 |
2 files changed, 38 insertions, 17 deletions
diff --git a/include/sdlmain.h b/include/sdlmain.h index 06743e873..03f978fba 100644 --- a/include/sdlmain.h +++ b/include/sdlmain.h @@ -135,6 +135,7 @@ struct SDL_Block { bool display_res = false; } full = {}; struct { + // user-configured window size int width = 0; int height = 0; bool resizable = false; @@ -142,6 +143,8 @@ struct SDL_Block { bool adjusted_initial_size = false; int initial_x_pos = -1; int initial_y_pos = -1; + // instantaneous canvas size of the window + SDL_Rect canvas_size = {}; } window = {}; struct { int width = 0; @@ -152,10 +155,6 @@ struct SDL_Block { double dpi_scale = 1.0; bool fullscreen = false; - // Records the canvas size (actual window-size in pixels) prior - // to switching to fullscreen - SDL_Rect canvas_size_before_fullscreen_switch = {}; - // This flag indicates, that we are in the process of switching // between fullscreen or window (as oppososed to changing // rendering size due to rotating screen, emulation state, or diff --git a/src/gui/sdlmain.cpp b/src/gui/sdlmain.cpp index 97a9a51d1..15f4caccd 100644 --- a/src/gui/sdlmain.cpp +++ b/src/gui/sdlmain.cpp @@ -2305,9 +2305,10 @@ void GFX_SwitchFullScreen() { sdl.desktop.switching_fullscreen = true; + // Record the window's current canvas size if we're departing window-mode + auto &window_canvas_size = sdl.desktop.window.canvas_size; if (!sdl.desktop.fullscreen) - sdl.desktop.canvas_size_before_fullscreen_switch = get_canvas_size( - sdl.desktop.type); + window_canvas_size = get_canvas_size(sdl.desktop.type); #if defined(WIN32) // We are about to switch to the opposite of our current mode @@ -2321,10 +2322,11 @@ void GFX_SwitchFullScreen() FocusInput(); setup_presentation_mode(sdl.frame.mode); - const std::optional<SDL_Rect> canvas_size = - !sdl.desktop.fullscreen - ? sdl.desktop.canvas_size_before_fullscreen_switch - : std::optional<SDL_Rect>(); + // After switching modes, get the current canvas size, which might be + // the windowed size or full screen size. + auto canvas_size = sdl.desktop.fullscreen + ? get_canvas_size(sdl.desktop.type) + : window_canvas_size; log_display_properties(sdl.draw.width, sdl.draw.height, @@ -3003,6 +3005,25 @@ static void setup_initial_window_position_from_conf(const std::string &window_po sdl.desktop.window.initial_y_pos = y; } +// Writes to the window-size member should be done via this function +static void save_window_size(const int w, const int h) +{ + assert(w > 0 && h > 0); + + // The desktop.window size stores the user-configured window size. + // During runtime, the actual SDL window size might differ from this + // depending on the aspect ratio, window DPI, or manual resizing. + sdl.desktop.window.width = w; + sdl.desktop.window.height = h; + + // Initialize the window's canvas size if it hasn't yet been set. + auto &window_canvas_size = sdl.desktop.window.canvas_size; + if (window_canvas_size.w <= 0 || window_canvas_size.h <= 0) { + window_canvas_size.w = w; + window_canvas_size.h = h; + } +} + // Takes in: // - The user's windowresolution: default, WxH, small, medium, large, // desktop, or an invalid setting. @@ -3032,8 +3053,12 @@ static void setup_window_sizes_from_conf(const char *windowresolution_val, { // TODO: Deprecate SURFACE output and remove this. // For now, let the DOS-side determine the window's resolution. - if (sdl.desktop.want_type == SCREEN_SURFACE) + if (sdl.desktop.want_type == SCREEN_SURFACE) { + // ensure our window sizes are populated + save_window_size(FALLBACK_WINDOW_DIMENSIONS.x, + FALLBACK_WINDOW_DIMENSIONS.y); return; + } // Can the window be resized? sdl.desktop.want_resizable_window = detect_resizable_window(); @@ -3075,8 +3100,7 @@ static void setup_window_sizes_from_conf(const char *windowresolution_val, should_stretch_pixels); } assert(refined_size.x <= UINT16_MAX && refined_size.y <= UINT16_MAX); - sdl.desktop.window.width = refined_size.x; - sdl.desktop.window.height = refined_size.y; + save_window_size(refined_size.x, refined_size.y); auto describe_scaling_mode = [scaling_mode]() -> const char * { switch (scaling_mode) { @@ -3699,10 +3723,8 @@ static void HandleVideoResize(int width, int height) sdl.clip = calc_viewport(canvas.w, canvas.h); glViewport(sdl.clip.x, sdl.clip.y, sdl.clip.w, sdl.clip.h); - if (!sdl.desktop.fullscreen) { - sdl.desktop.window.width = width; - sdl.desktop.window.height = height; - } + if (!sdl.desktop.fullscreen) + save_window_size(width, height); // Ensure mouse emulation knows the current parameters NewMouseScreenParams(); |