From b6ef9fae3eff3b6b8f30a5581f054edeaf9ed5c0 Mon Sep 17 00:00:00 2001 From: svdijk Date: Sat, 13 May 2017 23:39:54 +0200 Subject: [PATCH] SDLGLFB: Implement mouse coordinate scaling for the menus. --- src/posix/sdl/sdlglvideo.cpp | 35 +++++++++++++++++++++++++++++++++++ src/posix/sdl/sdlglvideo.h | 2 ++ src/posix/sdl/sdlvideo.cpp | 25 ------------------------- src/v_video.cpp | 26 ++++++++++++++++++++++++++ src/v_video.h | 2 ++ 5 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/posix/sdl/sdlglvideo.cpp b/src/posix/sdl/sdlglvideo.cpp index 0aacb8454..e2318a1fb 100644 --- a/src/posix/sdl/sdlglvideo.cpp +++ b/src/posix/sdl/sdlglvideo.cpp @@ -555,3 +555,38 @@ int SDLGLFB::GetClientHeight() SDL_GL_GetDrawableSize(Screen, nullptr, &height); return height; } + +void SDLGLFB::ScaleCoordsFromWindow(int16_t &x, int16_t &y) +{ + int w, h; + SDL_GetWindowSize (Screen, &w, &h); + + // Detect if we're doing scaling in the Window and adjust the mouse + // coordinates accordingly. This could be more efficent, but I + // don't think performance is an issue in the menus. + if(IsFullscreen()) + { + int realw = w, realh = h; + ScaleWithAspect (realw, realh, SCREENWIDTH, SCREENHEIGHT); + if (realw != SCREENWIDTH || realh != SCREENHEIGHT) + { + double xratio = (double)SCREENWIDTH/realw; + double yratio = (double)SCREENHEIGHT/realh; + if (realw < w) + { + x = (x - (w - realw)/2)*xratio; + y *= yratio; + } + else + { + y = (y - (h - realh)/2)*yratio; + x *= xratio; + } + } + } + else + { + x = (int16_t)(x*Width/w); + y = (int16_t)(y*Height/h); + } +} diff --git a/src/posix/sdl/sdlglvideo.h b/src/posix/sdl/sdlglvideo.h index 69472c848..8fcc184e6 100644 --- a/src/posix/sdl/sdlglvideo.h +++ b/src/posix/sdl/sdlglvideo.h @@ -72,6 +72,8 @@ public: int GetClientWidth(); int GetClientHeight(); + virtual void ScaleCoordsFromWindow(int16_t &x, int16_t &y); + SDL_Window *GetSDLWindow() override { return Screen; } protected: diff --git a/src/posix/sdl/sdlvideo.cpp b/src/posix/sdl/sdlvideo.cpp index 5186b2553..828a47f74 100644 --- a/src/posix/sdl/sdlvideo.cpp +++ b/src/posix/sdl/sdlvideo.cpp @@ -112,31 +112,6 @@ static cycle_t SDLFlipCycles; // CODE -------------------------------------------------------------------- -void ScaleWithAspect (int &w, int &h, int Width, int Height) -{ - int resRatio = CheckRatio (Width, Height); - int screenRatio; - CheckRatio (w, h, &screenRatio); - if (resRatio == screenRatio) - return; - - double yratio; - switch(resRatio) - { - case 0: yratio = 4./3.; break; - case 1: yratio = 16./9.; break; - case 2: yratio = 16./10.; break; - case 3: yratio = 17./10.; break; - case 4: yratio = 5./4.; break; - default: return; - } - double y = w/yratio; - if (y > h) - w = h*yratio; - else - h = y; -} - // FrameBuffer implementation ----------------------------------------------- SDLFB::SDLFB (int width, int height, bool bgra, bool fullscreen, SDL_Window *oldwin) diff --git a/src/v_video.cpp b/src/v_video.cpp index 651d3d7de..a3c7d3878 100644 --- a/src/v_video.cpp +++ b/src/v_video.cpp @@ -1747,6 +1747,32 @@ bool AspectTallerThanWide(float aspect) return aspect < 1.333f; } +void ScaleWithAspect (int &w, int &h, int Width, int Height) +{ + int resRatio = CheckRatio (Width, Height); + int screenRatio; + CheckRatio (w, h, &screenRatio); + if (resRatio == screenRatio) + return; + + double yratio; + switch(resRatio) + { + case 0: yratio = 4./3.; break; + case 1: yratio = 16./9.; break; + case 2: yratio = 16./10.; break; + case 3: yratio = 17./10.; break; + case 4: yratio = 5./4.; break; + case 6: yratio = 21./9.; break; + default: return; + } + double y = w/yratio; + if (y > h) + w = h*yratio; + else + h = y; +} + void IVideo::DumpAdapters () { Printf("Multi-monitor support unavailable.\n"); diff --git a/src/v_video.h b/src/v_video.h index c4f9bdb95..fc4d3924d 100644 --- a/src/v_video.h +++ b/src/v_video.h @@ -565,6 +565,8 @@ int AspectBaseHeight(float aspect); double AspectPspriteOffset(float aspect); int AspectMultiplier(float aspect); bool AspectTallerThanWide(float aspect); +void ScaleWithAspect(int &w, int &h, int Width, int Height); + int GetUIScale(int altval); EXTERN_CVAR(Int, uiscale);