SDL: Clean up graphics manager switching slighty.

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...
This commit is contained in:
Johannes Schickel 2013-10-20 22:27:50 +02:00
parent 18d2bbc228
commit 6e46e9dfaf
8 changed files with 72 additions and 29 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -35,9 +35,10 @@ public:
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);

View File

@ -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);
}

View File

@ -80,6 +80,9 @@ public:
virtual void notifyMousePos(Common::Point mouse) = 0;
protected:
void initEventSource();
void deinitEventSource();
SdlEventSource *_eventSource;
};

View File

@ -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);
@ -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) ||

View File

@ -80,7 +80,8 @@ public:
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);

View File

@ -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;
@ -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.
@ -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;
}
}
@ -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)
@ -595,18 +588,16 @@ 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;
}
@ -614,6 +605,9 @@ bool OSystem_SDL::setGraphicsMode(int mode) {
_graphicsMode = mode;
if (switchedManager) {
_graphicsManager->activateManager();
_graphicsManager->beginGFXTransaction();
#ifdef USE_RGB_COLOR
_graphicsManager->initSize(screenWidth, screenHeight, &pixelFormat);
#else