From 375388b22f74707a0588de098d847bbf9959344f Mon Sep 17 00:00:00 2001 From: Mathieu Schroeter Date: Mon, 25 Jun 2018 22:17:39 +0200 Subject: [PATCH] Fix mouse position when changing zoom and fullscreen --- src/blupi.cxx | 5 ++++- src/event.cxx | 38 ++++++++++++++++++++++++++------------ src/event.h | 2 +- src/pixmap.cxx | 24 ++++++++++++------------ src/pixmap.h | 2 +- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/blupi.cxx b/src/blupi.cxx index 9ecdc61..58a897e 100644 --- a/src/blupi.cxx +++ b/src/blupi.cxx @@ -410,7 +410,10 @@ HandleEvent (const SDL_Event & event) case EV_WARPMOUSE: { const SDL_Point * coord = static_cast (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; } diff --git a/src/event.cxx b/src/event.cxx index 510bfef..9ca08b0 100644 --- a/src/event.cxx +++ b/src/event.cxx @@ -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 (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: diff --git a/src/event.h b/src/event.h index 1f4b1cd..2cd6db3 100644 --- a/src/event.h +++ b/src/event.h @@ -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); diff --git a/src/pixmap.cxx b/src/pixmap.cxx index 606782c..87177b6 100644 --- a/src/pixmap.cxx +++ b/src/pixmap.cxx @@ -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 (displayMode.w) / displayMode.h == - static_cast (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 diff --git a/src/pixmap.h b/src/pixmap.h index 0b315fa..3b660da 100644 --- a/src/pixmap.h +++ b/src/pixmap.h @@ -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: