SDL: Move code to handle cursor coordinate HiDPI scale to SdlWindow

SDL handles HiDPI scaling differently depending on the system. On
macOS for example the SDL window size and SDL drawable area have a
different size (factor 2 usually) while on Windows they are the
same. When HiDPI is disabled (for the SDL Surface mode for example)
they are always the same.

We need to appl this scaling when converting cursor position between
the drawable area and the SDL window.
This commit is contained in:
Thierry Crozat 2021-08-20 00:12:51 +01:00
parent 9d4ba30060
commit feac996b50
3 changed files with 26 additions and 9 deletions

View file

@ -230,14 +230,7 @@ bool SdlGraphicsManager::notifyMousePosition(Common::Point &mouse) {
// Currently on macOS we need to scale the events for HiDPI screen, but on // Currently on macOS we need to scale the events for HiDPI screen, but on
// Windows we do not. We can find out if we need to do it by querying the // Windows we do not. We can find out if we need to do it by querying the
// SDL window size vs the SDL drawable size. // SDL window size vs the SDL drawable size.
float dpiScale = 1.f; float dpiScale = _window->getSdlDpiScalingFactor();
#if SDL_VERSION_ATLEAST(2, 0, 0)
int windowWidth, windowHeight;
SDL_GetWindowSize(_window->getSDLWindow(), &windowWidth, &windowHeight);
int realWidth, realHeight;
SDL_GL_GetDrawableSize(_window->getSDLWindow(), &realWidth, &realHeight);
dpiScale = (float)realWidth / (float)windowWidth;
#endif
mouse.x = (int)(mouse.x * dpiScale + 0.5f); mouse.x = (int)(mouse.x * dpiScale + 0.5f);
mouse.y = (int)(mouse.y * dpiScale + 0.5f); mouse.y = (int)(mouse.y * dpiScale + 0.5f);
bool valid = true; bool valid = true;

View file

@ -272,6 +272,17 @@ float SdlWindow::getDpiScalingFactor() const {
return ratio; return ratio;
} }
float SdlWindow::getSdlDpiScalingFactor() const {
#if SDL_VERSION_ATLEAST(2, 0, 0)
int windowWidth, windowHeight;
SDL_GetWindowSize(getSDLWindow(), &windowWidth, &windowHeight);
int realWidth, realHeight;
SDL_GL_GetDrawableSize(getSDLWindow(), &realWidth, &realHeight);
return (float)realWidth / (float)windowWidth;
#else
return 1.f;
#endif
}
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
SDL_Surface *copySDLSurface(SDL_Surface *src) { SDL_Surface *copySDLSurface(SDL_Surface *src) {

View file

@ -87,7 +87,17 @@ public:
*/ */
Common::Rect getDesktopResolution(); Common::Rect getDesktopResolution();
void getDisplayDpi(float *dpi, float *defaultDpi) const; /*
* Get the scaling between the SDL Window size and the SDL
* drawable area size. On some system, when HiDPI support is
* enabled, those two sizes are different.
*
* To convert from window coordinate to drawable area coordinate,
* multiple the coordinate by this scaling factor. To convert
* from drawable area coordinate to window coordinate, divide the
* coordinate by this scaling factor.
*/
float getSdlDpiScalingFactor() const;
/** /**
* Returns the scaling mode based on the display DPI * Returns the scaling mode based on the display DPI
@ -115,6 +125,9 @@ private:
Common::Rect _desktopRes; Common::Rect _desktopRes;
bool _inputGrabState, _inputLockState; bool _inputGrabState, _inputLockState;
protected:
void getDisplayDpi(float *dpi, float *defaultDpi) const;
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
public: public:
/** /**