1
0
mirror of https://github.com/blupi-games/planetblupi synced 2024-12-30 10:15:36 +01:00

Fix mouse position when changing zoom and fullscreen

This commit is contained in:
Mathieu Schroeter 2018-06-25 22:17:39 +02:00
parent 3834861c1e
commit 375388b22f
5 changed files with 44 additions and 27 deletions

View File

@ -410,7 +410,10 @@ HandleEvent (const SDL_Event & event)
case EV_WARPMOUSE:
{
const SDL_Point * coord = static_cast<SDL_Point *> (event.user.data1);
SDL_WarpMouseInWindow (g_window, coord->x, coord->y);
Sint32 x = coord->x, y = coord->y;
g_pPixmap->FromGameToDisplay (x, y);
SDL_WarpMouseInWindow (g_window, x, y);
delete coord;
break;
}

View File

@ -1723,6 +1723,10 @@ CEvent::GetMousePos ()
void
CEvent::SetFullScreen (bool bFullScreen)
{
int x, y;
SDL_GetMouseState (&x, &y);
this->m_pPixmap->FromDisplayToGame (x, y);
g_bFullScreen = bFullScreen;
int displayIndex = 0;
@ -1762,12 +1766,15 @@ CEvent::SetFullScreen (bool bFullScreen)
m_pPixmap->LoadCursors ();
if (g_bFullScreen)
{
Sint32 w, h;
SDL_GetWindowSize (g_window, &w, &h);
SDL_WarpMouseGlobal (w / 2, h / 2);
}
/* Force this update before otherwise the coordinates retrieved with
* the Warp SDL function are corresponding to the previous size.
*/
CEvent::PushUserEvent (EV_UPDATE);
auto coord = new SDL_Point; // Released by the event handler.
coord->x = x;
coord->y = y;
CEvent::PushUserEvent (EV_WARPMOUSE, coord);
}
/**
@ -1803,10 +1810,11 @@ CEvent::SetWindowSize (Uint8 newScale)
* \param[in] newScale - The new scale.
*/
void
CEvent::SetWindowSize (Uint8 prevScale, Uint8 newScale)
CEvent::SetWindowSize (double prevScale, double newScale)
{
int x, y;
SDL_GetMouseState (&x, &y);
this->m_pPixmap->FromDisplayToGame (x, y, prevScale);
if (g_bFullScreen && newScale == 2)
newScale = 1;
@ -1829,8 +1837,8 @@ CEvent::SetWindowSize (Uint8 prevScale, Uint8 newScale)
CEvent::PushUserEvent (EV_UPDATE);
auto coord = new SDL_Point; // Released by the event handler.
coord->x = newScale < prevScale ? x / prevScale : x * newScale;
coord->y = newScale < prevScale ? y / prevScale : x * newScale;
coord->x = x;
coord->y = y;
CEvent::PushUserEvent (EV_WARPMOUSE, coord);
}
@ -4245,16 +4253,21 @@ CEvent::ChangeButtons (Sint32 message)
break;
}
case EV_BUTTON4:
{
Sint32 w1;
SDL_GetWindowSize (g_window, &w1, nullptr);
SetFullScreen (false);
SetWindowSize (g_zoom, g_zoom);
SetWindowSize (g_zoom * static_cast<double> (w1) / LXIMAGE, g_zoom);
break;
}
case EV_BUTTON5:
{
auto scale = g_zoom;
if (g_zoom > 1)
--g_zoom;
SetWindowSize (scale, g_zoom);
SetFullScreen (g_bFullScreen);
if (g_bFullScreen)
SetFullScreen (g_bFullScreen);
break;
}
case EV_BUTTON6:
@ -4263,7 +4276,8 @@ CEvent::ChangeButtons (Sint32 message)
if (g_zoom < 2)
++g_zoom;
SetWindowSize (scale, g_zoom);
SetFullScreen (g_bFullScreen);
if (g_bFullScreen)
SetFullScreen (g_bFullScreen);
break;
}
case EV_BUTTON7:

View File

@ -167,7 +167,7 @@ protected:
Language GetStartLanguage ();
Language GetLanguage ();
void SetLanguage (Language lang = Language::undef);
void SetWindowSize (Uint8 prevScale, Uint8 newScale);
void SetWindowSize (double prevScale, double newScale);
void ChangeButtons (Sint32 message);

View File

@ -979,24 +979,24 @@ CPixmap::GetDisplayScale ()
}
void
CPixmap::FromDisplayToGame (Sint32 & x, Sint32 & y)
CPixmap::FromDisplayToGame (Sint32 & x, Sint32 & y, double prevScale)
{
if (this->event->IsDemoPlaying ())
return;
SDL_DisplayMode displayMode;
SDL_GetWindowDisplayMode (g_window, &displayMode);
Sint32 w, h;
SDL_GetWindowSize (g_window, &w, &h);
if (
static_cast<double> (displayMode.w) / displayMode.h ==
static_cast<double> (SCRNUM) / SCRDEN)
double factor = 1;
if (!g_bFullScreen)
factor = prevScale;
x /= factor;
y /= factor;
if (!g_bFullScreen)
return;
double w = displayMode.w, h = displayMode.h;
double ratio = w * SCRDEN / SCRNUM;
x = (x - (w - ratio) / 2) / (ratio / LXIMAGE);
y = y / (h / LYIMAGE);
}
void

View File

@ -95,7 +95,7 @@ public:
public:
double GetDisplayScale ();
void FromDisplayToGame (Sint32 & x, Sint32 & y);
void FromDisplayToGame (Sint32 & x, Sint32 & y, double prevScale = 1);
void FromGameToDisplay (Sint32 & x, Sint32 & y);
protected: