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:
parent
9d4ba30060
commit
feac996b50
3 changed files with 26 additions and 9 deletions
|
@ -230,14 +230,7 @@ bool SdlGraphicsManager::notifyMousePosition(Common::Point &mouse) {
|
|||
// 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
|
||||
// SDL window size vs the SDL drawable size.
|
||||
float dpiScale = 1.f;
|
||||
#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
|
||||
float dpiScale = _window->getSdlDpiScalingFactor();
|
||||
mouse.x = (int)(mouse.x * dpiScale + 0.5f);
|
||||
mouse.y = (int)(mouse.y * dpiScale + 0.5f);
|
||||
bool valid = true;
|
||||
|
|
|
@ -272,6 +272,17 @@ float SdlWindow::getDpiScalingFactor() const {
|
|||
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)
|
||||
SDL_Surface *copySDLSurface(SDL_Surface *src) {
|
||||
|
|
|
@ -87,7 +87,17 @@ public:
|
|||
*/
|
||||
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
|
||||
|
@ -115,6 +125,9 @@ private:
|
|||
Common::Rect _desktopRes;
|
||||
bool _inputGrabState, _inputLockState;
|
||||
|
||||
protected:
|
||||
void getDisplayDpi(float *dpi, float *defaultDpi) const;
|
||||
|
||||
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
||||
public:
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue