Skip to content

Commit

Permalink
SDL: Do not reset window size when engines update rendering surface
Browse files Browse the repository at this point in the history
This change allows:

* Engines to update their target rendering surface/size and pixel
  format with the backend multiple times during gameplay;
* Users to resize the ScummVM window without having it reset
  size/position every time an engine updates its target surface
  format;
* Conversions/scaling to continue to run efficiently in hardware,
  instead of requiring engines to pick their maximum possible
  output format once and upscale inefficiently in software;
* The window to reset size once when an engine calls to set its
  initial output size, and to reset again once ScummVM returns to
  the launcher.

This is relevant for at least SCI32 and DreamWeb engines, which
perform graphics mode switches during games.
  • Loading branch information
csnover committed Sep 20, 2017
1 parent 9fc24ed commit 56a79e9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
12 changes: 2 additions & 10 deletions backends/graphics/openglsdl/openglsdl-graphics.cpp
Expand Up @@ -509,16 +509,8 @@ bool OpenGLSdlGraphicsManager::setupMode(uint width, uint height) {
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, _glContextMajor);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, _glContextMinor);

if (!_window->createOrUpdateWindow(width, height, flags)) {
// We treat fullscreen requests as a "hint" for now. This means in
// case it is not available we simply ignore it.
if (_wantsFullScreen) {
_window->createOrUpdateWindow(width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
}

if (!_window->getSDLWindow()) {
return false;
}
if (!createOrUpdateWindow(width, height, flags)) {
return false;
}

_glContext = SDL_GL_CreateContext(_window->getSDLWindow());
Expand Down
30 changes: 28 additions & 2 deletions backends/graphics/sdl/sdl-graphics.cpp
Expand Up @@ -21,13 +21,16 @@
*/

#include "backends/graphics/sdl/sdl-graphics.h"

#include "backends/platform/sdl/sdl-sys.h"
#include "backends/events/sdl/sdl-events.h"
#include "common/textconsole.h"

SdlGraphicsManager::SdlGraphicsManager(SdlEventSource *source, SdlWindow *window)
: _eventSource(source), _window(window) {
: _eventSource(source), _window(window)
#if SDL_VERSION_ATLEAST(2, 0, 0)
, _allowWindowSizeReset(false), _lastFlags(0)
#endif
{
}

SdlGraphicsManager::~SdlGraphicsManager() {
Expand Down Expand Up @@ -73,3 +76,26 @@ bool SdlGraphicsManager::setState(const State &state) {
}
}

#if SDL_VERSION_ATLEAST(2, 0, 0)
bool SdlGraphicsManager::createOrUpdateWindow(const int width, const int height, const Uint32 flags) {
if (!_window) {
return false;
}

// We only update the actual window when flags change (which usually means
// fullscreen mode is entered/exited) or when updates are forced so that we
// do not reset the window size whenever a game makes a call to change the
// size or pixel format of the internal game surface (since a user may have
// resized the game window)
if (!_window->getSDLWindow() || _lastFlags != flags || _allowWindowSizeReset) {
if (!_window->createOrUpdateWindow(width, height, flags)) {
return false;
}

_lastFlags = flags;
_allowWindowSizeReset = false;
}

return true;
}
#endif
11 changes: 11 additions & 0 deletions backends/graphics/sdl/sdl-graphics.h
Expand Up @@ -123,6 +123,17 @@ class SdlGraphicsManager : virtual public GraphicsManager {
SdlWindow *getWindow() const { return _window; }

protected:
#if SDL_VERSION_ATLEAST(2, 0, 0)
public:
void unlockWindowSize() { _allowWindowSizeReset = true; }

protected:
Uint32 _lastFlags;
bool _allowWindowSizeReset;

bool createOrUpdateWindow(const int width, const int height, const Uint32 flags);
#endif

SdlEventSource *_eventSource;
SdlWindow *_window;
};
Expand Down
9 changes: 7 additions & 2 deletions backends/graphics/surfacesdl/surfacesdl-graphics.cpp
Expand Up @@ -784,9 +784,14 @@ void SurfaceSdlGraphicsManager::initSize(uint w, uint h, const Graphics::PixelFo
}
#endif

// Avoid redundant res changes
#if !SDL_VERSION_ATLEAST(2, 0, 0)
// Avoid redundant res changes, only in SDL1. In SDL2, redundancies may not
// actually be redundant if ScummVM is switching between game engines and
// the screen dimensions are being reinitialized, since window resizing is
// supposed to reset when this happens
if ((int)w == _videoMode.screenWidth && (int)h == _videoMode.screenHeight)
return;
#endif

_videoMode.screenWidth = w;
_videoMode.screenHeight = h;
Expand Down Expand Up @@ -2797,7 +2802,7 @@ SDL_Surface *SurfaceSdlGraphicsManager::SDL_SetVideoMode(int width, int height,
createWindowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}

if (!_window->createOrUpdateWindow(width, height, createWindowFlags)) {
if (!createOrUpdateWindow(width, height, createWindowFlags)) {
return nullptr;
}

Expand Down
12 changes: 10 additions & 2 deletions backends/platform/sdl/sdl.cpp
Expand Up @@ -297,20 +297,28 @@ void OSystem_SDL::initBackend() {
dynamic_cast<SdlGraphicsManager *>(_graphicsManager)->activateManager();
}

#if defined(USE_TASKBAR)
void OSystem_SDL::engineInit() {
#if SDL_VERSION_ATLEAST(2, 0, 0)
dynamic_cast<SdlGraphicsManager *>(_graphicsManager)->unlockWindowSize();
#endif
#ifdef USE_TASKBAR
// Add the started engine to the list of recent tasks
_taskbarManager->addRecent(ConfMan.getActiveDomainName(), ConfMan.get("description"));

// Set the overlay icon the current running engine
_taskbarManager->setOverlayIcon(ConfMan.getActiveDomainName(), ConfMan.get("description"));
#endif
}

void OSystem_SDL::engineDone() {
#if SDL_VERSION_ATLEAST(2, 0, 0)
dynamic_cast<SdlGraphicsManager *>(_graphicsManager)->unlockWindowSize();
#endif
#ifdef USE_TASKBAR
// Remove overlay icon
_taskbarManager->setOverlayIcon("", "");
}
#endif
}

void OSystem_SDL::initSDL() {
// Check if SDL has not been initialized
Expand Down
2 changes: 0 additions & 2 deletions backends/platform/sdl/sdl.h
Expand Up @@ -59,10 +59,8 @@ class OSystem_SDL : public ModularBackend {

// Override functions from ModularBackend and OSystem
virtual void initBackend();
#if defined(USE_TASKBAR)
virtual void engineInit();
virtual void engineDone();
#endif
virtual void quit();
virtual void fatalError();

Expand Down

0 comments on commit 56a79e9

Please sign in to comment.