Skip to content

Commit

Permalink
SDL: Clean up graphics manager switching slighty.
Browse files Browse the repository at this point in the history
Sadly this also requires us to extend GraphicsManager for this SDL specific
feature. However, since that's only used in the SDL backend and Tizen it
should be fine for now...
  • Loading branch information
Johannes Schickel authored and Kamil Zbróg committed Oct 24, 2013
1 parent aebe01f commit 2784901
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 29 deletions.
21 changes: 21 additions & 0 deletions backends/graphics/graphics.h
Expand Up @@ -37,6 +37,27 @@ class GraphicsManager : public PaletteManager {
public:
virtual ~GraphicsManager() {}

/**
* Makes this graphics manager active. That means it should be ready to
* process inputs now. However, even without being active it should be
* able to query the supported modes and other bits.
*
* HACK: Actually this is specific to SdlGraphicsManager subclasses.
* But sadly we cannot cast from GraphicsManager to SdlGraphicsManager
* because there is no relation between these two.
*/
virtual void activateManager() {}

/**
* Makes this graphics manager inactive. This should allow another
* graphics manager to become active again.
*
* HACK: Actually this is specific to SdlGraphicsManager subclasses.
* But sadly we cannot cast from GraphicsManager to SdlGraphicsManager
* because there is no relation between these two.
*/
virtual void deactivateManager() {}

virtual bool hasFeature(OSystem::Feature f) = 0;
virtual void setFeatureState(OSystem::Feature f, bool enable) = 0;
virtual bool getFeatureState(OSystem::Feature f) = 0;
Expand Down
17 changes: 13 additions & 4 deletions backends/graphics/openglsdl/openglsdl-graphics.cpp
Expand Up @@ -82,15 +82,24 @@ OpenGLSdlGraphicsManager::OpenGLSdlGraphicsManager(uint desktopWidth, uint deskt
}

OpenGLSdlGraphicsManager::~OpenGLSdlGraphicsManager() {
}

void OpenGLSdlGraphicsManager::activateManager() {
OpenGLGraphicsManager::activateManager();
initEventSource();

// Register the graphics manager as a event observer
g_system->getEventManager()->getEventDispatcher()->registerObserver(this, 10, false);
}

void OpenGLSdlGraphicsManager::deactivateManager() {
// Unregister the event observer
if (g_system->getEventManager()->getEventDispatcher()) {
g_system->getEventManager()->getEventDispatcher()->unregisterObserver(this);
}
}

void OpenGLSdlGraphicsManager::initEventObserver() {
// Register the graphics manager as a event observer
g_system->getEventManager()->getEventDispatcher()->registerObserver(this, 10, false);
deinitEventSource();
OpenGLGraphicsManager::deactivateManager();
}

bool OpenGLSdlGraphicsManager::hasFeature(OSystem::Feature f) {
Expand Down
5 changes: 3 additions & 2 deletions backends/graphics/openglsdl/openglsdl-graphics.h
Expand Up @@ -35,9 +35,10 @@ class OpenGLSdlGraphicsManager : public OpenGL::OpenGLGraphicsManager, public Sd
OpenGLSdlGraphicsManager(uint desktopWidth, uint desktopHeight, SdlEventSource *eventSource);
virtual ~OpenGLSdlGraphicsManager();

void initEventObserver();

// GraphicsManager API
virtual void activateManager();
virtual void deactivateManager();

virtual bool hasFeature(OSystem::Feature f);
virtual void setFeatureState(OSystem::Feature f, bool enable);
virtual bool getFeatureState(OSystem::Feature f);
Expand Down
9 changes: 7 additions & 2 deletions backends/graphics/sdl/sdl-graphics.cpp
Expand Up @@ -26,10 +26,15 @@

SdlGraphicsManager::SdlGraphicsManager(SdlEventSource *source)
: _eventSource(source) {
_eventSource->setGraphicsManager(this);
}

SdlGraphicsManager::~SdlGraphicsManager() {
_eventSource->setGraphicsManager(0);
}

void SdlGraphicsManager::initEventSource() {
_eventSource->setGraphicsManager(this);
}

void SdlGraphicsManager::deinitEventSource() {
_eventSource->setGraphicsManager(0);
}
3 changes: 3 additions & 0 deletions backends/graphics/sdl/sdl-graphics.h
Expand Up @@ -80,6 +80,9 @@ class SdlGraphicsManager {
virtual void notifyMousePos(Common::Point mouse) = 0;

protected:
void initEventSource();
void deinitEventSource();

SdlEventSource *_eventSource;
};

Expand Down
19 changes: 14 additions & 5 deletions backends/graphics/surfacesdl/surfacesdl-graphics.cpp
Expand Up @@ -193,10 +193,6 @@ SurfaceSdlGraphicsManager::SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSou
}

SurfaceSdlGraphicsManager::~SurfaceSdlGraphicsManager() {
// Unregister the event observer
if (g_system->getEventManager()->getEventDispatcher() != NULL)
g_system->getEventManager()->getEventDispatcher()->unregisterObserver(this);

unloadGFXMode();
if (_mouseSurface)
SDL_FreeSurface(_mouseSurface);
Expand All @@ -211,11 +207,24 @@ SurfaceSdlGraphicsManager::~SurfaceSdlGraphicsManager() {
free(_mouseData);
}

void SurfaceSdlGraphicsManager::initEventObserver() {
void SurfaceSdlGraphicsManager::activateManager() {
GraphicsManager::activateManager();
initEventSource();

// Register the graphics manager as a event observer
g_system->getEventManager()->getEventDispatcher()->registerObserver(this, 10, false);
}

void SurfaceSdlGraphicsManager::deactivateManager() {
// Unregister the event observer
if (g_system->getEventManager()->getEventDispatcher()) {
g_system->getEventManager()->getEventDispatcher()->unregisterObserver(this);
}

deinitEventSource();
GraphicsManager::deactivateManager();
}

bool SurfaceSdlGraphicsManager::hasFeature(OSystem::Feature f) {
return
(f == OSystem::kFeatureFullscreenMode) ||
Expand Down
3 changes: 2 additions & 1 deletion backends/graphics/surfacesdl/surfacesdl-graphics.h
Expand Up @@ -80,7 +80,8 @@ class SurfaceSdlGraphicsManager : public GraphicsManager, public SdlGraphicsMana
SurfaceSdlGraphicsManager(SdlEventSource *sdlEventSource);
virtual ~SurfaceSdlGraphicsManager();

virtual void initEventObserver();
virtual void activateManager();
virtual void deactivateManager();

virtual bool hasFeature(OSystem::Feature f);
virtual void setFeatureState(OSystem::Feature f, bool enable);
Expand Down
24 changes: 9 additions & 15 deletions backends/platform/sdl/sdl.cpp
Expand Up @@ -89,6 +89,9 @@ OSystem_SDL::~OSystem_SDL() {
// Hence, we perform the destruction on our own.
delete _savefileManager;
_savefileManager = 0;
if (_graphicsManager) {
_graphicsManager->deactivateManager();
}
delete _graphicsManager;
_graphicsManager = 0;
delete _eventManager;
Expand Down Expand Up @@ -161,8 +164,6 @@ void OSystem_SDL::initBackend() {
if (_eventSource == 0)
_eventSource = new SdlEventSource();

int graphicsManagerType = 0;

#ifdef USE_OPENGL
// Query the desktop resolution. We simply hope nothing tried to change
// the resolution so far.
Expand Down Expand Up @@ -193,13 +194,11 @@ void OSystem_SDL::initBackend() {
// If the gfx_mode is from OpenGL, create the OpenGL graphics manager
if (use_opengl) {
_graphicsManager = new OpenGLSdlGraphicsManager(_desktopWidth, _desktopHeight, _eventSource);
graphicsManagerType = 1;
}
}
#endif
if (_graphicsManager == 0) {
_graphicsManager = new SurfaceSdlGraphicsManager(_eventSource);
graphicsManagerType = 0;
}
}

Expand Down Expand Up @@ -242,13 +241,7 @@ void OSystem_SDL::initBackend() {
// so the virtual keyboard can be initialized, but we have to add the
// graphics manager as an event observer after initializing the event
// manager.
if (graphicsManagerType == 0)
((SurfaceSdlGraphicsManager *)_graphicsManager)->initEventObserver();
#ifdef USE_OPENGL
else if (graphicsManagerType == 1)
((OpenGLSdlGraphicsManager *)_graphicsManager)->initEventObserver();
#endif

_graphicsManager->activateManager();
}

#if defined(USE_TASKBAR)
Expand Down Expand Up @@ -595,25 +588,26 @@ bool OSystem_SDL::setGraphicsMode(int mode) {
// manager, delete and create the new mode graphics manager
if (_graphicsMode >= _sdlModesCount && mode < _sdlModesCount) {
debug(1, "switching to plain SDL graphics");
_graphicsManager->deactivateManager();
delete _graphicsManager;
_graphicsManager = new SurfaceSdlGraphicsManager(_eventSource);
((SurfaceSdlGraphicsManager *)_graphicsManager)->initEventObserver();
_graphicsManager->beginGFXTransaction();

switchedManager = true;
} else if (_graphicsMode < _sdlModesCount && mode >= _sdlModesCount) {
debug(1, "switching to OpenGL graphics");
_graphicsManager->deactivateManager();
delete _graphicsManager;
_graphicsManager = new OpenGLSdlGraphicsManager(_desktopWidth, _desktopHeight, _eventSource);
((OpenGLSdlGraphicsManager *)_graphicsManager)->initEventObserver();
_graphicsManager->beginGFXTransaction();

switchedManager = true;
}

_graphicsMode = mode;

if (switchedManager) {
_graphicsManager->activateManager();

_graphicsManager->beginGFXTransaction();
#ifdef USE_RGB_COLOR
_graphicsManager->initSize(screenWidth, screenHeight, &pixelFormat);
#else
Expand Down

0 comments on commit 2784901

Please sign in to comment.