cleanup remembering last windowed position code

This commit is contained in:
thrust26 2019-05-18 08:43:02 +02:00
parent 22b15d7f37
commit 53597938b6
4 changed files with 30 additions and 30 deletions

View File

@ -51,6 +51,9 @@ FrameBufferSDL2::FrameBufferSDL2(OSystem& osystem)
// since the structure may be needed before any FBSurface's have // since the structure may be needed before any FBSurface's have
// been created // been created
myPixelFormat = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888); myPixelFormat = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888);
myPosX = myOSystem.settings().getInt("pos.x");
myPosY = myOSystem.settings().getInt("pos.y");
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -164,23 +167,17 @@ Int32 FrameBufferSDL2::getCurrentDisplayIndex()
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FrameBufferSDL2::getCurrentWindowPos(int& x, int&y) void FrameBufferSDL2::updateWindowedPos()
{ {
ASSERT_MAIN_THREAD; ASSERT_MAIN_THREAD;
if (myCenter || // only save if the window is not centered and not in full screen mode
!myWindow || (SDL_GetWindowFlags(myWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP)) if (!myCenter && myWindow && !(SDL_GetWindowFlags(myWindow) & SDL_WINDOW_FULLSCREEN_DESKTOP))
{ {
// restore to last saved window position // save current windowed position
x = myOSystem.settings().getInt("pos.x"); SDL_GetWindowPosition(myWindow, &myPosX, &myPosY);
y = myOSystem.settings().getInt("pos.y"); myOSystem.settings().setValue("pos.x", myPosX);
} myOSystem.settings().setValue("pos.y", myPosY);
else
{
// save current window position
SDL_GetWindowPosition(myWindow, &x, &y);
myOSystem.settings().setValue("pos.x", x);
myOSystem.settings().setValue("pos.y", y);
} }
} }
@ -209,9 +206,8 @@ bool FrameBufferSDL2::setVideoMode(const string& title, const VideoMode& mode)
} }
} }
// get current windows position // save and get last windowed window's position
int posX, posY; updateWindowedPos();
getCurrentWindowPos(posX, posY);
// Always recreate renderer (some systems need this) // Always recreate renderer (some systems need this)
if(myRenderer) if(myRenderer)
@ -220,9 +216,16 @@ bool FrameBufferSDL2::setVideoMode(const string& title, const VideoMode& mode)
myRenderer = nullptr; myRenderer = nullptr;
} }
int posX, posY;
myCenter = myOSystem.settings().getBool("center"); myCenter = myOSystem.settings().getBool("center");
if (myCenter) if (myCenter)
posX = posY = SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex); posX = posY = SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex);
else
{
posX = myPosX,
posY = myPosY;
}
uInt32 flags = mode.fsIndex != -1 ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; uInt32 flags = mode.fsIndex != -1 ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0;
// macOS seems to have issues with destroying the window, and wants to // macOS seems to have issues with destroying the window, and wants to
@ -257,7 +260,7 @@ bool FrameBufferSDL2::setVideoMode(const string& title, const VideoMode& mode)
{ {
SDL_SetWindowFullscreen(myWindow, flags); SDL_SetWindowFullscreen(myWindow, flags);
SDL_SetWindowSize(myWindow, mode.screen.w, mode.screen.h); SDL_SetWindowSize(myWindow, mode.screen.w, mode.screen.h);
SDL_SetWindowPosition(myWindow, pos, pos); SDL_SetWindowPosition(myWindow, posX, posY);
SDL_SetWindowTitle(myWindow, title.c_str()); SDL_SetWindowTitle(myWindow, title.c_str());
} }
#endif #endif

View File

@ -107,12 +107,9 @@ class FrameBufferSDL2 : public FrameBuffer
Int32 getCurrentDisplayIndex() override; Int32 getCurrentDisplayIndex() override;
/** /**
This method is called to query the current window position. This method is called to preserve the last current windowed position.
@param x The x-position retrieved
@param y The y-position retrieved
*/ */
void getCurrentWindowPos(int& x, int& y) override; void updateWindowedPos() override;
/** /**
Clear the frame buffer Clear the frame buffer
@ -195,6 +192,10 @@ class FrameBufferSDL2 : public FrameBuffer
// center setting of curent window // center setting of curent window
bool myCenter; bool myCenter;
// last position of windowed window
int myPosX;
int myPosY;
private: private:
// Following constructors and assignment operators not supported // Following constructors and assignment operators not supported
FrameBufferSDL2() = delete; FrameBufferSDL2() = delete;

View File

@ -345,12 +345,9 @@ class FrameBuffer
virtual Int32 getCurrentDisplayIndex() = 0; virtual Int32 getCurrentDisplayIndex() = 0;
/** /**
This method is called to query the current window position. This method is called to preserve the last current windowed position.
@param x The x-position retrieved
@param y The y-position retrieved
*/ */
virtual void getCurrentWindowPos(int& x, int& y) = 0; virtual void updateWindowedPos() = 0;
/** /**
Clear the framebuffer. Clear the framebuffer.

View File

@ -233,9 +233,8 @@ void OSystem::loadConfig(const Settings::Options& options)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void OSystem::saveConfig() void OSystem::saveConfig()
{ {
// Save the current window position and display on system shutdown // Save the last windowed position and display on system shutdown
int x, y; myFrameBuffer->updateWindowedPos();
myFrameBuffer->getCurrentWindowPos(x, y);
settings().setValue("display", myFrameBuffer->getCurrentDisplayIndex()); settings().setValue("display", myFrameBuffer->getCurrentDisplayIndex());
// Ask all subsystems to save their settings // Ask all subsystems to save their settings