From de2bbe3b9738ef95b2529db989570770ef434f9d Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Wed, 19 Jul 2017 19:15:12 -0500 Subject: BACKENDS: Refactor OpenGL & SDL graphics backends This patch refactors the OpenGL and SDL graphics backends, primarily to unify window scaling and mouse handling, and to fix coordinate mapping between the ScummVM window and the virtual game screen when they have different aspect ratios. Unified code for these two backends has been moved to a new header-only WindowedGraphicsManager class, so named because it contains code for managing graphics managers that interact with a windowing system and render virtual screens within a larger physical content window. The biggest behavioral change here is with the coordinate system mapping: Previously, mouse offsets were converted by mapping the whole space within the window as input to the virtual game screen without maintaining aspect ratio. This was done to prevent 'stickiness' when the mouse cursor was within the window but outside of the virtual game screen, but it caused noticeable distortion of mouse movement speed on the axis with blank space. Instead of introducing mouse speed distortion to prevent stickiness, this patch changes coordinate transformation to show the system cursor when the mouse moves outside of the virtual game screen when mouse grab is off, or by holding the mouse inside the virtual game screen (instead of the entire window) when mouse grab is on. This patch also improves some other properties of the GraphicsManager/PaletteManager interfaces: * Nullipotent operations (getWidth, getHeight, etc.) of the PaletteManager/GraphicsManager interfaces are now const * Methods marked `virtual` but not inherited by any subclass have been de-virtualized * Extra unnecessary calculations of hardware height in SurfaceSdlGraphicsManager have been removed * Methods have been renamed where appropriate for clarity (setWindowSize -> handleResize, etc.) * C++11 support improved with `override` specifier added on overridden virtual methods in subclasses (primarily to avoid myself accidentally creating new methods in the subclasses by changing types/names during refactoring) Additional refactoring can and should be done at some point to continue to deduplicate code between the OpenGL and SDL backends. Since the primary goal here was to improve the coordinate mapping, full refactoring of these backends was not completed here. --- backends/platform/sdl/sdl-window.cpp | 12 ++++++++---- backends/platform/sdl/sdl-window.h | 9 +++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) (limited to 'backends/platform') diff --git a/backends/platform/sdl/sdl-window.cpp b/backends/platform/sdl/sdl-window.cpp index 16b978dca4..a550fbbd40 100644 --- a/backends/platform/sdl/sdl-window.cpp +++ b/backends/platform/sdl/sdl-window.cpp @@ -135,8 +135,10 @@ void SdlWindow::toggleMouseGrab() { #else if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) { SDL_WM_GrabInput(SDL_GRAB_ON); + _inputGrabState = true; } else { SDL_WM_GrabInput(SDL_GRAB_OFF); + _inputGrabState = false; } #endif } @@ -154,13 +156,15 @@ bool SdlWindow::hasMouseFocus() const { } void SdlWindow::warpMouseInWindow(int x, int y) { + if (hasMouseFocus()) { #if SDL_VERSION_ATLEAST(2, 0, 0) - if (_window && hasMouseFocus()) { - SDL_WarpMouseInWindow(_window, x, y); - } + if (_window) { + SDL_WarpMouseInWindow(_window, x, y); + } #else - SDL_WarpMouse(x, y); + SDL_WarpMouse(x, y); #endif + } } void SdlWindow::iconifyWindow() { diff --git a/backends/platform/sdl/sdl-window.h b/backends/platform/sdl/sdl-window.h index fb7607be55..4f6d924411 100644 --- a/backends/platform/sdl/sdl-window.h +++ b/backends/platform/sdl/sdl-window.h @@ -56,7 +56,8 @@ public: bool hasMouseFocus() const; /** - * Warp the mouse to the specified position in window coordinates. + * Warp the mouse to the specified position in window coordinates. The mouse + * will only be warped if the window is focused in the window manager. */ void warpMouseInWindow(int x, int y); @@ -73,6 +74,11 @@ public: */ bool getSDLWMInformation(SDL_SysWMinfo *info) const; + bool mouseIsGrabbed() const { return _inputGrabState; } + +private: + bool _inputGrabState; + #if SDL_VERSION_ATLEAST(2, 0, 0) public: /** @@ -108,7 +114,6 @@ private: */ int _lastX, _lastY; - bool _inputGrabState; Common::String _windowCaption; #endif }; -- cgit v1.2.3