Fixes some issues when toggling fullscreen mode with SDL2, refs #2041:
* SDL_WINDOW_RESIZABLE must be set when creating the window, apparently only fullscreen flags take effect in SDL_SetWindowFullscreen * Stores window position, so a window can be restored to both its original position and size, rather than defaults * Changes initial window position from undefined to centered, it caused some problems with window positioning returning from fullscreen mode This was SVN commit r15827.
This commit is contained in:
parent
2afeec0c01
commit
f10bdf5089
@ -109,6 +109,8 @@ static InReaction MainInputHandler(const SDL_Event_* ev)
|
|||||||
g_ResizedW = ev->ev.window.data1;
|
g_ResizedW = ev->ev.window.data1;
|
||||||
g_ResizedH = ev->ev.window.data2;
|
g_ResizedH = ev->ev.window.data2;
|
||||||
break;
|
break;
|
||||||
|
case SDL_WINDOWEVENT_MOVED:
|
||||||
|
g_VideoMode.UpdatePosition(ev->ev.window.data1, ev->ev.window.data2);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
#else
|
#else
|
||||||
|
@ -50,7 +50,7 @@ CVideoMode::CVideoMode() :
|
|||||||
m_IsFullscreen(false), m_IsInitialised(false), m_Window(NULL),
|
m_IsFullscreen(false), m_IsInitialised(false), m_Window(NULL),
|
||||||
m_PreferredW(0), m_PreferredH(0), m_PreferredBPP(0), m_PreferredFreq(0),
|
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_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
|
// (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)
|
// 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)
|
bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen)
|
||||||
{
|
{
|
||||||
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
#if SDL_VERSION_ATLEAST(2, 0, 0)
|
||||||
|
Uint32 flags = 0;
|
||||||
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
|
|
||||||
if (fullscreen)
|
if (fullscreen)
|
||||||
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
|
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||||
else
|
|
||||||
flags |= SDL_WINDOW_RESIZABLE;
|
|
||||||
|
|
||||||
if (!m_Window)
|
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 (!m_Window)
|
||||||
{
|
{
|
||||||
// If fullscreen fails, try windowed mode
|
// 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 (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)
|
if (SDL_SetWindowFullscreen(m_Window, flags) < 0)
|
||||||
{
|
{
|
||||||
LOGERROR(L"SetVideoMode failed in SDL_SetWindowFullscreen: %dx%d:%d %d (\"%hs\")",
|
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)
|
if (!fullscreen)
|
||||||
|
{
|
||||||
SDL_SetWindowSize(m_Window, w, h);
|
SDL_SetWindowSize(m_Window, w, h);
|
||||||
|
SDL_SetWindowPosition(m_Window, m_WindowedX, m_WindowedY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab the current video settings
|
// Grab the current video settings
|
||||||
@ -282,7 +294,7 @@ bool CVideoMode::InitSDL()
|
|||||||
u16 ramp[256];
|
u16 ramp[256];
|
||||||
SDL_CalculateGammaRamp(g_Gamma, ramp);
|
SDL_CalculateGammaRamp(g_Gamma, ramp);
|
||||||
if (SDL_SetWindowGammaRamp(m_Window, ramp, ramp, ramp) < 0)
|
if (SDL_SetWindowGammaRamp(m_Window, ramp, ramp, ramp) < 0)
|
||||||
LOGWARNING(L"SDL_SetGamma failed");
|
LOGWARNING(L"SDL_SetWindowGammaRamp failed");
|
||||||
#else
|
#else
|
||||||
# if OS_MACOSX
|
# if OS_MACOSX
|
||||||
// Workaround for crash on Mavericks, see http://trac.wildfiregames.com/ticket/2272
|
// Workaround for crash on Mavericks, see http://trac.wildfiregames.com/ticket/2272
|
||||||
@ -450,6 +462,15 @@ bool CVideoMode::ToggleFullscreen()
|
|||||||
return SetFullscreen(!m_IsFullscreen);
|
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)
|
void CVideoMode::UpdateRenderer(int w, int h)
|
||||||
{
|
{
|
||||||
if (w < 2) w = 2; // avoid GL errors caused by invalid sizes
|
if (w < 2) w = 2; // avoid GL errors caused by invalid sizes
|
||||||
|
@ -57,6 +57,11 @@ public:
|
|||||||
*/
|
*/
|
||||||
bool ToggleFullscreen();
|
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.
|
* Update the graphics code to start drawing to the new size.
|
||||||
* This should be called after the GL context has been resized.
|
* This should be called after the GL context has been resized.
|
||||||
@ -104,10 +109,12 @@ private:
|
|||||||
bool m_ConfigFullscreen;
|
bool m_ConfigFullscreen;
|
||||||
bool m_ConfigForceS3TCEnable;
|
bool m_ConfigForceS3TCEnable;
|
||||||
|
|
||||||
// If we're fullscreen, size of window when we were last windowed (or the default window size
|
// If we're fullscreen, size/position of window when we were last windowed (or the default window
|
||||||
// if we started fullscreen), to support switching back to the old window size
|
// size/position if we started fullscreen), to support switching back to the old window size/position
|
||||||
int m_WindowedW;
|
int m_WindowedW;
|
||||||
int m_WindowedH;
|
int m_WindowedH;
|
||||||
|
int m_WindowedX;
|
||||||
|
int m_WindowedY;
|
||||||
|
|
||||||
// Whether we're currently being displayed fullscreen
|
// Whether we're currently being displayed fullscreen
|
||||||
bool m_IsFullscreen;
|
bool m_IsFullscreen;
|
||||||
|
Loading…
Reference in New Issue
Block a user