From 0e312538764de215f16ff37ac22ceb68d65379ce Mon Sep 17 00:00:00 2001 From: NyakoFox Date: Sat, 3 May 2025 15:29:24 -0300 Subject: [PATCH] Fix mouse coordinates being wrong on HiDPI displays When writing the initial stretch mode code, the existence of HiDPI displays completely slipped my mind -- or at least I didn't realize that they'd be a problem. We implement scaling modes ourselves, so transforming the mouse coordinates to our 320x240 viewport is done manually. Unfortunately, that code did not take into account HiDPI scaling whatsoever, meaning that all of the math which assumes the window size and the renderer size is wrong. To fix this, we use `SDL_GetWindowSizeInPixels` and `SDL_GetWindow` to find the scaling factor, and then apply that to the mouse coordinates to get the mouse coordinates in the pixel-space instead, and then we do all of our normal logic after. Due to our usage of `SDL_GetWindowSizeInPixels`, this bumps the minimum SDL version to 2.26.0. Closes #1235. --- .github/workflows/ci.yml | 2 +- desktop_version/src/KeyPoll.cpp | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bc814578..7ba9eae1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -101,7 +101,7 @@ jobs: runs-on: windows-latest env: - SDL_VERSION: 2.24.0 + SDL_VERSION: 2.26.0 steps: - uses: actions/checkout@v1 diff --git a/desktop_version/src/KeyPoll.cpp b/desktop_version/src/KeyPoll.cpp index 44ef3aec..4439d470 100644 --- a/desktop_version/src/KeyPoll.cpp +++ b/desktop_version/src/KeyPoll.cpp @@ -571,8 +571,20 @@ void KeyPoll::Poll(void) SDL_Rect rect; graphics.get_stretch_info(&rect); - mousex = (raw_mousex - rect.x) * SCREEN_WIDTH_PIXELS / rect.w; - mousey = (raw_mousey - rect.y) * SCREEN_HEIGHT_PIXELS / rect.h; + int window_width; + int window_height; + SDL_GetWindowSizeInPixels(gameScreen.m_window, &window_width, &window_height); + + int scaled_window_width; + int scaled_window_height; + SDL_GetWindowSize(gameScreen.m_window, &scaled_window_width, &scaled_window_height); + + float scale_x = (float)window_width / (float)scaled_window_width; + float scale_y = (float)window_height / (float)scaled_window_height; + + // Use screen stretch information to modify the coordinates (as we implement stretching manually) + mousex = ((raw_mousex * scale_x) - rect.x) * SCREEN_WIDTH_PIXELS / rect.w; + mousey = ((raw_mousey * scale_y) - rect.y) * SCREEN_HEIGHT_PIXELS / rect.h; active_input_device_changed = keyboard_was_active != BUTTONGLYPHS_keyboard_is_active(); should_recompute_textboxes |= active_input_device_changed;