diff --git a/src/lang/english.txt b/src/lang/english.txt index 0f4b4a99d4..9bccd86df2 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -1006,6 +1006,9 @@ STR_GAME_OPTIONS_VIDEO_ACCELERATION :{BLACK}Hardware STR_GAME_OPTIONS_VIDEO_ACCELERATION_TOOLTIP :{BLACK}Check this box to allow OpenTTD to try to use hardware acceleration. A changed setting will only be applied upon game restart STR_GAME_OPTIONS_VIDEO_ACCELERATION_RESTART :{WHITE}The setting will only take effect after a game restart +STR_GAME_OPTIONS_VIDEO_VSYNC :{BLACK}VSync +STR_GAME_OPTIONS_VIDEO_VSYNC_TOOLTIP :{BLACK}Check this box to v-sync the screen. A changed setting will only be applied upon game restart. Only works with hardware acceleration enabled + STR_GAME_OPTIONS_GUI_ZOOM_FRAME :{BLACK}Interface size STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_TOOLTIP :{BLACK}Select the interface element size to use diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 6788d1ae39..afc8f080ef 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -458,6 +458,19 @@ struct GameOptionsWindow : Window { _video_hw_accel = !_video_hw_accel; ShowErrorMessage(STR_GAME_OPTIONS_VIDEO_ACCELERATION_RESTART, INVALID_STRING_ID, WL_INFO); this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel); +#ifndef __APPLE__ + this->SetWidgetDisabledState(WID_GO_VIDEO_VSYNC_BUTTON, !_video_hw_accel); +#endif + this->SetDirty(); + break; + + case WID_GO_VIDEO_VSYNC_BUTTON: + if (!_video_hw_accel) break; + + _video_vsync = !_video_vsync; + VideoDriver::GetInstance()->ToggleVsync(_video_vsync); + + this->SetWidgetLoweredState(WID_GO_VIDEO_VSYNC_BUTTON, _video_vsync); this->SetDirty(); break; @@ -598,6 +611,11 @@ struct GameOptionsWindow : Window { this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen); this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel); +#ifndef __APPLE__ + this->SetWidgetLoweredState(WID_GO_VIDEO_VSYNC_BUTTON, _video_vsync); + this->SetWidgetDisabledState(WID_GO_VIDEO_VSYNC_BUTTON, !_video_hw_accel); +#endif + bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0; this->GetWidget(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL); @@ -647,7 +665,10 @@ static const NWidgetPart _nested_game_options_widgets[] = { NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL), SetPadding(0, 0, 2, 0), NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_REFRESH_RATE, STR_NULL), SetPadding(0, 0, 2, 0), NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL), SetPadding(0, 0, 2, 0), - NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_VIDEO_ACCELERATION, STR_NULL), + NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_VIDEO_ACCELERATION, STR_NULL), SetPadding(0, 0, 2, 0), +#ifndef __APPLE__ + NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_VIDEO_VSYNC, STR_NULL), +#endif EndContainer(), NWidget(NWID_VERTICAL), NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_RESOLUTION_DROPDOWN), SetMinimalSize(100, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_RESOLUTION_TOOLTIP), SetFill(1, 0), SetPadding(0, 0, 2, 0), @@ -656,10 +677,16 @@ static const NWidgetPart _nested_game_options_widgets[] = { NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0), NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP), EndContainer(), - NWidget(NWID_HORIZONTAL), + NWidget(NWID_HORIZONTAL), SetPadding(0, 0, 2, 0), NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0), NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_VIDEO_ACCEL_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_VIDEO_ACCELERATION_TOOLTIP), EndContainer(), +#ifndef __APPLE__ + NWidget(NWID_HORIZONTAL), + NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0), + NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_VIDEO_VSYNC_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_VIDEO_VSYNC_TOOLTIP), + EndContainer(), +#endif EndContainer(), EndContainer(), EndContainer(), diff --git a/src/table/misc_settings.ini b/src/table/misc_settings.ini index 9286d1c959..49a4caa14c 100644 --- a/src/table/misc_settings.ini +++ b/src/table/misc_settings.ini @@ -77,6 +77,12 @@ var = _video_hw_accel def = true cat = SC_BASIC +[SDTG_BOOL] +name = ""video_vsync"" +var = _video_vsync +def = false +cat = SC_BASIC + [SDTG_OMANY] name = ""support8bpp"" type = SLE_UINT8 diff --git a/src/video/sdl2_opengl_v.cpp b/src/video/sdl2_opengl_v.cpp index 5df2837513..08718a01e3 100644 --- a/src/video/sdl2_opengl_v.cpp +++ b/src/video/sdl2_opengl_v.cpp @@ -70,8 +70,6 @@ const char *VideoDriver_SDL_OpenGL::Start(const StringList ¶m) SDL_GetWindowSize(this->sdl_window, &w, &h); this->ClientSizeChanged(w, h, true); - SDL_GL_SetSwapInterval(GetDriverParamBool(param, "vsync") ? 1 : 0); - return nullptr; } @@ -91,6 +89,11 @@ void VideoDriver_SDL_OpenGL::DestroyContext() } } +void VideoDriver_SDL_OpenGL::ToggleVsync(bool vsync) +{ + SDL_GL_SetSwapInterval(vsync); +} + const char *VideoDriver_SDL_OpenGL::AllocateContext() { SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); @@ -107,6 +110,8 @@ const char *VideoDriver_SDL_OpenGL::AllocateContext() this->gl_context = SDL_GL_CreateContext(this->sdl_window); if (this->gl_context == nullptr) return "SDL2: Can't active GL context"; + ToggleVsync(_video_vsync); + return OpenGLBackend::Create(&GetOGLProcAddressCallback); } diff --git a/src/video/sdl2_opengl_v.h b/src/video/sdl2_opengl_v.h index f749b1f459..c7e647ca88 100644 --- a/src/video/sdl2_opengl_v.h +++ b/src/video/sdl2_opengl_v.h @@ -29,6 +29,8 @@ public: bool HasAnimBuffer() override { return true; } uint8 *GetAnimBuffer() override { return this->anim_buffer; } + void ToggleVsync(bool vsync) override; + const char *GetName() const override { return "sdl-opengl"; } protected: diff --git a/src/video/video_driver.cpp b/src/video/video_driver.cpp index bee67e1ea0..eaff0b7414 100644 --- a/src/video/video_driver.cpp +++ b/src/video/video_driver.cpp @@ -21,6 +21,7 @@ #include "video_driver.hpp" bool _video_hw_accel; ///< Whether to consider hardware accelerated video drivers. +bool _video_vsync; ///< Whether we should use vsync (only if _video_hw_accel is enabled). void VideoDriver::GameLoop() { diff --git a/src/video/video_driver.hpp b/src/video/video_driver.hpp index 7a859565a6..f143b61a80 100644 --- a/src/video/video_driver.hpp +++ b/src/video/video_driver.hpp @@ -28,6 +28,7 @@ extern std::vector _resolutions; extern Dimension _cur_resolution; extern bool _rightclick_emulate; extern bool _video_hw_accel; +extern bool _video_vsync; /** The base of all video drivers. */ class VideoDriver : public Driver { @@ -66,6 +67,12 @@ public: */ virtual bool ToggleFullscreen(bool fullscreen) = 0; + /** + * Change the vsync setting. + * @param vsync The new setting. + */ + virtual void ToggleVsync(bool vsync) {} + /** * Callback invoked after the blitter was changed. * @return True if no error. diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 426d74c0aa..4674784618 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -1290,7 +1290,6 @@ const char *VideoDriver_Win32OpenGL::Start(const StringList ¶m) if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported"; Dimension old_res = _cur_resolution; // Save current screen resolution in case of errors, as MakeWindow invalidates it. - this->vsync = GetDriverParamBool(param, "vsync"); LoadWGLExtensions(); @@ -1335,6 +1334,15 @@ void VideoDriver_Win32OpenGL::DestroyContext() } } +void VideoDriver_Win32OpenGL::ToggleVsync(bool vsync) +{ + if (_wglSwapIntervalEXT != nullptr) { + _wglSwapIntervalEXT(vsync); + } else if (vsync) { + DEBUG(driver, 0, "OpenGL: Vsync requested, but not supported by driver"); + } +} + const char *VideoDriver_Win32OpenGL::AllocateContext() { this->dc = GetDC(this->main_wnd); @@ -1363,12 +1371,7 @@ const char *VideoDriver_Win32OpenGL::AllocateContext() } if (!wglMakeCurrent(this->dc, rc)) return "Can't active GL context"; - /* Enable/disable Vsync if supported. */ - if (_wglSwapIntervalEXT != nullptr) { - _wglSwapIntervalEXT(this->vsync ? 1 : 0); - } else if (vsync) { - DEBUG(driver, 0, "OpenGL: Vsync requested, but not supported by driver"); - } + this->ToggleVsync(_video_vsync); this->gl_rc = rc; return OpenGLBackend::Create(&GetOGLProcAddressCallback); diff --git a/src/video/win32_v.h b/src/video/win32_v.h index f6ca291f10..8c63aeedf4 100644 --- a/src/video/win32_v.h +++ b/src/video/win32_v.h @@ -138,12 +138,13 @@ public: bool HasAnimBuffer() override { return true; } uint8 *GetAnimBuffer() override { return this->anim_buffer; } + void ToggleVsync(bool vsync) override; + const char *GetName() const override { return "win32-opengl"; } protected: HDC dc; ///< Window device context. HGLRC gl_rc; ///< OpenGL context. - bool vsync; ///< Enable VSync? uint8 *anim_buffer; ///< Animation buffer from OpenGL back-end. uint8 GetFullscreenBpp() override { return 32; } // OpenGL is always 32 bpp. diff --git a/src/widgets/settings_widget.h b/src/widgets/settings_widget.h index ae9c22c1a8..c897927341 100644 --- a/src/widgets/settings_widget.h +++ b/src/widgets/settings_widget.h @@ -35,6 +35,7 @@ enum GameOptionsWidgets { WID_GO_BASE_MUSIC_DESCRIPTION = WID_GO_BASE_MUSIC_TEXTFILE + TFT_END, ///< Description of selected base music set. WID_GO_FONT_ZOOM_DROPDOWN, ///< Dropdown for the font zoom level. WID_GO_VIDEO_ACCEL_BUTTON, ///< Toggle for video acceleration. + WID_GO_VIDEO_VSYNC_BUTTON, ///< Toggle for video vsync. WID_GO_REFRESH_RATE_DROPDOWN, ///< Dropdown for all available refresh rates. };