diff --git a/source/main.cpp b/source/main.cpp index 07dcb232ea..4d90e76533 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -109,6 +109,8 @@ static InReaction MainInputHandler(const SDL_Event_* ev) g_ResizedW = ev->ev.window.data1; g_ResizedH = ev->ev.window.data2; break; + case SDL_WINDOWEVENT_MOVED: + g_VideoMode.UpdatePosition(ev->ev.window.data1, ev->ev.window.data2); } break; #else diff --git a/source/ps/VideoMode.cpp b/source/ps/VideoMode.cpp index e97ff41cff..540635b24d 100644 --- a/source/ps/VideoMode.cpp +++ b/source/ps/VideoMode.cpp @@ -50,7 +50,7 @@ CVideoMode::CVideoMode() : m_IsFullscreen(false), m_IsInitialised(false), m_Window(NULL), m_PreferredW(0), m_PreferredH(0), m_PreferredBPP(0), m_PreferredFreq(0), m_ConfigW(0), m_ConfigH(0), m_ConfigBPP(0), m_ConfigFullscreen(false), m_ConfigForceS3TCEnable(true), - m_WindowedW(DEFAULT_WINDOW_W), m_WindowedH(DEFAULT_WINDOW_H) + m_WindowedW(DEFAULT_WINDOW_W), m_WindowedH(DEFAULT_WINDOW_H), m_WindowedX(0), m_WindowedY(0) { // (m_ConfigFullscreen defaults to false, so users don't get stuck if // e.g. half the filesystem is missing and the config files aren't loaded) @@ -72,16 +72,17 @@ void CVideoMode::ReadConfig() bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen) { #if SDL_VERSION_ATLEAST(2, 0, 0) - - Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; + Uint32 flags = 0; if (fullscreen) flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; - else - flags |= SDL_WINDOW_RESIZABLE; if (!m_Window) { - m_Window = SDL_CreateWindow("0 A.D.", SDL_WINDOWPOS_UNDEFINED_DISPLAY(m_ConfigDisplay), SDL_WINDOWPOS_UNDEFINED_DISPLAY(m_ConfigDisplay), w, h, flags); + // Note: these flags only take affect in SDL_CreateWindow + flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE; + m_WindowedX = m_WindowedY = SDL_WINDOWPOS_CENTERED_DISPLAY(m_ConfigDisplay); + + m_Window = SDL_CreateWindow("0 A.D.", m_WindowedX, m_WindowedY, w, h, flags); if (!m_Window) { // If fullscreen fails, try windowed mode @@ -121,6 +122,14 @@ bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen) { if (m_IsFullscreen != fullscreen) { + if (!fullscreen) + { + // For some reason, when switching from fullscreen to windowed mode, + // we have to set the window size and position before and after switching + SDL_SetWindowSize(m_Window, w, h); + SDL_SetWindowPosition(m_Window, m_WindowedX, m_WindowedY); + } + if (SDL_SetWindowFullscreen(m_Window, flags) < 0) { LOGERROR(L"SetVideoMode failed in SDL_SetWindowFullscreen: %dx%d:%d %d (\"%hs\")", @@ -130,7 +139,10 @@ bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen) } if (!fullscreen) + { SDL_SetWindowSize(m_Window, w, h); + SDL_SetWindowPosition(m_Window, m_WindowedX, m_WindowedY); + } } // Grab the current video settings @@ -282,7 +294,7 @@ bool CVideoMode::InitSDL() u16 ramp[256]; SDL_CalculateGammaRamp(g_Gamma, ramp); if (SDL_SetWindowGammaRamp(m_Window, ramp, ramp, ramp) < 0) - LOGWARNING(L"SDL_SetGamma failed"); + LOGWARNING(L"SDL_SetWindowGammaRamp failed"); #else # if OS_MACOSX // Workaround for crash on Mavericks, see http://trac.wildfiregames.com/ticket/2272 @@ -450,6 +462,15 @@ bool CVideoMode::ToggleFullscreen() return SetFullscreen(!m_IsFullscreen); } +void CVideoMode::UpdatePosition(int x, int y) +{ + if (!m_IsFullscreen) + { + m_WindowedX = x; + m_WindowedY = y; + } +} + void CVideoMode::UpdateRenderer(int w, int h) { if (w < 2) w = 2; // avoid GL errors caused by invalid sizes diff --git a/source/ps/VideoMode.h b/source/ps/VideoMode.h index 69dfc9f6bc..0a820da611 100644 --- a/source/ps/VideoMode.h +++ b/source/ps/VideoMode.h @@ -57,6 +57,11 @@ public: */ bool ToggleFullscreen(); + /** + * Update window position, to restore later if necessary (SDL2 only). + */ + void UpdatePosition(int x, int y); + /** * Update the graphics code to start drawing to the new size. * This should be called after the GL context has been resized. @@ -104,10 +109,12 @@ private: bool m_ConfigFullscreen; bool m_ConfigForceS3TCEnable; - // If we're fullscreen, size of window when we were last windowed (or the default window size - // if we started fullscreen), to support switching back to the old window size + // If we're fullscreen, size/position of window when we were last windowed (or the default window + // size/position if we started fullscreen), to support switching back to the old window size/position int m_WindowedW; int m_WindowedH; + int m_WindowedX; + int m_WindowedY; // Whether we're currently being displayed fullscreen bool m_IsFullscreen;