Merge pull request #1387 from colobot/dev-fix-vsync

Remove vsync loop and improve vsync UI
pyro-refactor
Mateusz Przybył 2021-02-07 21:40:24 +01:00 committed by GitHub
commit 7b237ce9ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 40 deletions

View File

@ -549,6 +549,8 @@ bool CApplication::Create()
/* SDL initialization sequence */ /* SDL initialization sequence */
// Creating the m_engine now because it holds the vsync flag
m_engine = MakeUnique<Gfx::CEngine>(this, m_systemUtils);
Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_TIMER; Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_TIMER;
@ -693,8 +695,6 @@ bool CApplication::Create()
} }
// Create the 3D engine // Create the 3D engine
m_engine = MakeUnique<Gfx::CEngine>(this, m_systemUtils);
m_engine->SetDevice(m_device.get()); m_engine->SetDevice(m_device.get());
if (! m_engine->Create() ) if (! m_engine->Create() )
@ -850,24 +850,9 @@ bool CApplication::CreateVideoSurface()
int vsync = 0; int vsync = 0;
if (GetConfigFile().GetIntProperty("Setup", "VSync", vsync)) if (GetConfigFile().GetIntProperty("Setup", "VSync", vsync))
{ {
while (SDL_GL_SetSwapInterval(vsync) == -1) m_engine->SetVSync(vsync);
{ TryToSetVSync();
switch(vsync) vsync = m_engine->GetVSync();
{
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;
}
}
GetConfigFile().SetIntProperty("Setup", "VSync", vsync); GetConfigFile().SetIntProperty("Setup", "VSync", vsync);
GetLogger()->Info("Using Vsync: %s\n", (vsync == -1 ? "adaptive" : (vsync ? "true" : "false"))); GetLogger()->Info("Using Vsync: %s\n", (vsync == -1 ? "adaptive" : (vsync ? "true" : "false")));
@ -876,6 +861,32 @@ bool CApplication::CreateVideoSurface()
return true; 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) bool CApplication::ChangeVideoConfig(const Gfx::DeviceConfig &newConfig)
{ {
m_deviceConfig = newConfig; m_deviceConfig = newConfig;
@ -884,26 +895,7 @@ bool CApplication::ChangeVideoConfig(const Gfx::DeviceConfig &newConfig)
SDL_SetWindowSize(m_private->window, m_deviceConfig.size.x, m_deviceConfig.size.y); 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); SDL_SetWindowFullscreen(m_private->window, m_deviceConfig.fullScreen ? SDL_WINDOW_FULLSCREEN : 0);
int vsync = m_engine->GetVSync(); TryToSetVSync();
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);
m_device->ConfigChanged(m_deviceConfig); m_device->ConfigChanged(m_deviceConfig);

View File

@ -288,6 +288,9 @@ public:
protected: protected:
//! Creates the window's SDL_Surface //! Creates the window's SDL_Surface
bool CreateVideoSurface(); 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 //! Processes the captured SDL event to Event struct
Event ProcessSystemEvent(); Event ProcessSystemEvent();

View File

@ -293,6 +293,7 @@ void CScreenSetupDisplay::UpdateApply()
CWindow* pw; CWindow* pw;
CButton* pb; CButton* pb;
CList* pl; CList* pl;
CList* pvl;
CCheck* pc; CCheck* pc;
int sel2; int sel2;
bool bFull; bool bFull;
@ -309,6 +310,22 @@ void CScreenSetupDisplay::UpdateApply()
pc = static_cast<CCheck*>(pw->SearchControl(EVENT_INTERFACE_FULL)); pc = static_cast<CCheck*>(pw->SearchControl(EVENT_INTERFACE_FULL));
bFull = pc->TestState(STATE_CHECK); bFull = pc->TestState(STATE_CHECK);
pvl = static_cast<CList*>(pw->SearchControl(EVENT_INTERFACE_VSYNC));
if (pvl == nullptr) return;
switch (m_engine->GetVSync())
{
case -1: //Adaptive?
pvl->SetSelect(1);
break;
case 0: //Off?
pvl->SetSelect(0);
break;
case 1: //On?
pvl->SetSelect(2);
break;
}
if ( sel2 == m_setupSelMode && if ( sel2 == m_setupSelMode &&
bFull == m_setupFull ) bFull == m_setupFull )
{ {