SDLGLFB: Implement mouse coordinate scaling for the menus.

This commit is contained in:
svdijk 2017-05-13 23:39:54 +02:00 committed by alexey.lysiuk
parent 788b9f6d54
commit b6ef9fae3e
5 changed files with 65 additions and 25 deletions

View file

@ -555,3 +555,38 @@ int SDLGLFB::GetClientHeight()
SDL_GL_GetDrawableSize(Screen, nullptr, &height); SDL_GL_GetDrawableSize(Screen, nullptr, &height);
return 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);
}
}

View file

@ -72,6 +72,8 @@ public:
int GetClientWidth(); int GetClientWidth();
int GetClientHeight(); int GetClientHeight();
virtual void ScaleCoordsFromWindow(int16_t &x, int16_t &y);
SDL_Window *GetSDLWindow() override { return Screen; } SDL_Window *GetSDLWindow() override { return Screen; }
protected: protected:

View file

@ -112,31 +112,6 @@ static cycle_t SDLFlipCycles;
// CODE -------------------------------------------------------------------- // 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 ----------------------------------------------- // FrameBuffer implementation -----------------------------------------------
SDLFB::SDLFB (int width, int height, bool bgra, bool fullscreen, SDL_Window *oldwin) SDLFB::SDLFB (int width, int height, bool bgra, bool fullscreen, SDL_Window *oldwin)

View file

@ -1747,6 +1747,32 @@ bool AspectTallerThanWide(float aspect)
return aspect < 1.333f; 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 () void IVideo::DumpAdapters ()
{ {
Printf("Multi-monitor support unavailable.\n"); Printf("Multi-monitor support unavailable.\n");

View file

@ -565,6 +565,8 @@ int AspectBaseHeight(float aspect);
double AspectPspriteOffset(float aspect); double AspectPspriteOffset(float aspect);
int AspectMultiplier(float aspect); int AspectMultiplier(float aspect);
bool AspectTallerThanWide(float aspect); bool AspectTallerThanWide(float aspect);
void ScaleWithAspect(int &w, int &h, int Width, int Height);
int GetUIScale(int altval); int GetUIScale(int altval);
EXTERN_CVAR(Int, uiscale); EXTERN_CVAR(Int, uiscale);