Remove vsync disable/enable loop (fix #1383)

Also a small refactor due to repeated code.
The vsync flag in m_engine is now synchronized with the SDL state
during initial configuration of CApp.
pyro-refactor
MrSimbax 2021-02-07 12:47:32 +01:00
parent d71ee4ef68
commit 71ff89a803
2 changed files with 35 additions and 41 deletions

View File

@ -115,7 +115,8 @@ CApplication::CApplication(CSystemUtils* systemUtils)
m_configFile(MakeUnique<CConfigFile>()),
m_input(MakeUnique<CInput>()),
m_pathManager(MakeUnique<CPathManager>(systemUtils)),
m_modManager(MakeUnique<CModManager>(this, m_pathManager.get()))
m_modManager(MakeUnique<CModManager>(this, m_pathManager.get())),
m_engine(MakeUnique<Gfx::CEngine>(this, m_systemUtils))
{
m_exitCode = 0;
m_active = false;
@ -693,8 +694,6 @@ bool CApplication::Create()
}
// Create the 3D engine
m_engine = MakeUnique<Gfx::CEngine>(this, m_systemUtils);
m_engine->SetDevice(m_device.get());
if (! m_engine->Create() )
@ -850,24 +849,9 @@ bool CApplication::CreateVideoSurface()
int vsync = 0;
if (GetConfigFile().GetIntProperty("Setup", "VSync", vsync))
{
while (SDL_GL_SetSwapInterval(vsync) == -1)
{
switch(vsync)
{
case -1: //failed with adaptive sync?
GetLogger()->Warn("Adaptive sync not supported.\n");
vsync = 1;
break;
case 1: //failed with VSync enabled?
GetLogger()->Warn("Couldn't enable VSync.\n");
vsync = 0;
break;
case 0: //failed with VSync disabled?
GetLogger()->Warn("Couldn't disable VSync.\n");
vsync = 1;
break;
}
}
m_engine->SetVSync(vsync);
TryToSetVSync();
vsync = m_engine->GetVSync();
GetConfigFile().SetIntProperty("Setup", "VSync", vsync);
GetLogger()->Info("Using Vsync: %s\n", (vsync == -1 ? "adaptive" : (vsync ? "true" : "false")));
@ -876,6 +860,32 @@ bool CApplication::CreateVideoSurface()
return true;
}
void CApplication::TryToSetVSync()
{
int vsync = m_engine->GetVSync();
int result = SDL_GL_SetSwapInterval(vsync);
if (result == -1)
{
switch (vsync)
{
case -1:
GetLogger()->Warn("Adaptive sync not supported: %s\n", SDL_GetError());
m_engine->SetVSync(1);
TryToSetVSync();
break;
case 1:
GetLogger()->Warn("Couldn't enable VSync: %s\n", SDL_GetError());
m_engine->SetVSync(0);
TryToSetVSync();
break;
case 0:
GetLogger()->Warn("Couldn't disable VSync: %s\n", SDL_GetError());
m_engine->SetVSync(SDL_GL_GetSwapInterval());
break;
}
}
}
bool CApplication::ChangeVideoConfig(const Gfx::DeviceConfig &newConfig)
{
m_deviceConfig = newConfig;
@ -884,26 +894,7 @@ bool CApplication::ChangeVideoConfig(const Gfx::DeviceConfig &newConfig)
SDL_SetWindowSize(m_private->window, m_deviceConfig.size.x, m_deviceConfig.size.y);
SDL_SetWindowFullscreen(m_private->window, m_deviceConfig.fullScreen ? SDL_WINDOW_FULLSCREEN : 0);
int vsync = m_engine->GetVSync();
while (SDL_GL_SetSwapInterval(vsync) == -1)
{
switch(vsync)
{
case -1: //failed with adaptive sync?
GetLogger()->Warn("Adaptive sync not supported.\n");
vsync = 1;
break;
case 1: //failed with VSync enabled?
GetLogger()->Warn("Couldn't enable VSync.\n");
vsync = 0;
break;
case 0: //failed with VSync disabled?
GetLogger()->Warn("Couldn't disable VSync.\n");
vsync = 1;
break;
}
}
m_engine->SetVSync(vsync);
TryToSetVSync();
m_device->ConfigChanged(m_deviceConfig);

View File

@ -288,6 +288,9 @@ public:
protected:
//! Creates the window's SDL_Surface
bool CreateVideoSurface();
//! Tries to set the SDL vsync state desired by the 3D engine
//! The final state of SDL vsync is set in the 3D engine afterwards
void TryToSetVSync();
//! Processes the captured SDL event to Event struct
Event ProcessSystemEvent();