Skip to content

Commit

Permalink
SDL: Get rid of loop in OSystem_SDL::setGraphicsMode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Schickel authored and Kamil Zbróg committed Oct 24, 2013
1 parent b85989b commit 960b47d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 64 deletions.
113 changes: 49 additions & 64 deletions backends/platform/sdl/sdl.cpp
Expand Up @@ -551,16 +551,9 @@ int OSystem_SDL::getDefaultGraphicsMode() const {
}

bool OSystem_SDL::setGraphicsMode(int mode) {
const OSystem::GraphicsMode *srcMode;
int i;

// Check if mode is from SDL or OpenGL
if (mode < _sdlModesCount) {
srcMode = SurfaceSdlGraphicsManager::supportedGraphicsModes();
i = 0;
} else {
srcMode = OpenGLSdlGraphicsManager::supportedGraphicsModes();
i = _sdlModesCount;
// Check whether a invalid mode is requested.
if (mode < 0 || (uint)mode >= _graphicsModeIds.size()) {
return false;
}

// Very hacky way to set up the old graphics manager state, in case we
Expand All @@ -579,74 +572,64 @@ bool OSystem_SDL::setGraphicsMode(int mode) {

bool switchedManager = false;

// Loop through modes
while (srcMode->name) {
if (i == mode) {
// If the new mode and the current mode are not from the same graphics
// 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);

switchedManager = true;
} else if (_graphicsMode < _sdlModesCount && mode >= _sdlModesCount) {
debug(1, "switching to OpenGL graphics");
_graphicsManager->deactivateManager();
delete _graphicsManager;
_graphicsManager = new OpenGLSdlGraphicsManager(_desktopWidth, _desktopHeight, _eventSource);
// If the new mode and the current mode are not from the same graphics
// 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);

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

switchedManager = true;
}

_graphicsMode = mode;
_graphicsMode = mode;

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

_graphicsManager->beginGFXTransaction();
_graphicsManager->beginGFXTransaction();
#ifdef USE_RGB_COLOR
_graphicsManager->initSize(screenWidth, screenHeight, &pixelFormat);
_graphicsManager->initSize(screenWidth, screenHeight, &pixelFormat);
#else
_graphicsManager->initSize(screenWidth, screenHeight, 0);
_graphicsManager->initSize(screenWidth, screenHeight, 0);
#endif
_graphicsManager->setFeatureState(kFeatureAspectRatioCorrection, arState);
_graphicsManager->setFeatureState(kFeatureFullscreenMode, fullscreen);
_graphicsManager->setFeatureState(kFeatureCursorPalette, cursorPalette);
_graphicsManager->setFeatureState(kFeatureAspectRatioCorrection, arState);
_graphicsManager->setFeatureState(kFeatureFullscreenMode, fullscreen);
_graphicsManager->setFeatureState(kFeatureCursorPalette, cursorPalette);

// Worst part about this right now, tell the cursor manager to
// resetup the cursor + cursor palette if necessarily
// Worst part about this right now, tell the cursor manager to
// resetup the cursor + cursor palette if necessarily

// First we need to try to setup the old state on the new manager...
if (_graphicsManager->endGFXTransaction() != kTransactionSuccess) {
// Oh my god if this failed the client code might just explode.
return false;
}

// Next setup the cursor again
CursorMan.pushCursor(0, 0, 0, 0, 0, 0);
CursorMan.popCursor();
// First we need to try to setup the old state on the new manager...
if (_graphicsManager->endGFXTransaction() != kTransactionSuccess) {
// Oh my god if this failed the client code might just explode.
return false;
}

// Next setup cursor palette if needed
if (cursorPalette) {
CursorMan.pushCursorPalette(0, 0, 0);
CursorMan.popCursorPalette();
}
// Next setup the cursor again
CursorMan.pushCursor(0, 0, 0, 0, 0, 0);
CursorMan.popCursor();

_graphicsManager->beginGFXTransaction();
// Oh my god if this failed the client code might just explode.
return _graphicsManager->setGraphicsMode(srcMode->id);
} else {
return _graphicsManager->setGraphicsMode(srcMode->id);
}
// Next setup cursor palette if needed
if (cursorPalette) {
CursorMan.pushCursorPalette(0, 0, 0);
CursorMan.popCursorPalette();
}

i++;
srcMode++;
_graphicsManager->beginGFXTransaction();
// Oh my god if this failed the client code might just explode.
return _graphicsManager->setGraphicsMode(_graphicsModeIds[mode]);
} else {
return _graphicsManager->setGraphicsMode(_graphicsModeIds[mode]);
}

return false;
}

int OSystem_SDL::getGraphicsMode() const {
Expand All @@ -655,6 +638,7 @@ int OSystem_SDL::getGraphicsMode() const {

void OSystem_SDL::setupGraphicsModes() {
_graphicsModes.clear();
_graphicsModeIds.clear();

const OSystem::GraphicsMode *sdlGraphicsModes = SurfaceSdlGraphicsManager::supportedGraphicsModes();
const OSystem::GraphicsMode *openglGraphicsModes = OpenGLSdlGraphicsManager::supportedGraphicsModes();
Expand Down Expand Up @@ -684,6 +668,7 @@ void OSystem_SDL::setupGraphicsModes() {
int i = 0;
OSystem::GraphicsMode *mode = _graphicsModes.begin();
while (mode->name) {
_graphicsModeIds.push_back(mode->id);
mode->id = i++;
mode++;
}
Expand Down
1 change: 1 addition & 0 deletions backends/platform/sdl/sdl.h
Expand Up @@ -112,6 +112,7 @@ class OSystem_SDL : public ModularBackend {

typedef Common::Array<GraphicsMode> GraphicsModeArray;
GraphicsModeArray _graphicsModes;
Common::Array<int> _graphicsModeIds;
int _graphicsMode;
int _sdlModesCount;
int _glModesCount;
Expand Down

0 comments on commit 960b47d

Please sign in to comment.