diff --git a/src/app/app.cpp b/src/app/app.cpp index ad5d02cb..1e4515ed 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -549,6 +549,8 @@ bool CApplication::Create() /* SDL initialization sequence */ + // Creating the m_engine now because it holds the vsync flag + m_engine = MakeUnique(this, m_systemUtils); Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_TIMER; @@ -693,8 +695,6 @@ bool CApplication::Create() } // Create the 3D engine - m_engine = MakeUnique(this, m_systemUtils); - m_engine->SetDevice(m_device.get()); if (! m_engine->Create() ) @@ -850,24 +850,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 +861,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 +895,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); diff --git a/src/app/app.h b/src/app/app.h index af385cf2..d238bde9 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -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(); diff --git a/src/ui/screen/screen_setup_display.cpp b/src/ui/screen/screen_setup_display.cpp index 219c7f29..f465a488 100644 --- a/src/ui/screen/screen_setup_display.cpp +++ b/src/ui/screen/screen_setup_display.cpp @@ -293,6 +293,7 @@ void CScreenSetupDisplay::UpdateApply() CWindow* pw; CButton* pb; CList* pl; + CList* pvl; CCheck* pc; int sel2; bool bFull; @@ -309,6 +310,22 @@ void CScreenSetupDisplay::UpdateApply() pc = static_cast(pw->SearchControl(EVENT_INTERFACE_FULL)); bFull = pc->TestState(STATE_CHECK); + pvl = static_cast(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 && bFull == m_setupFull ) {