From 656a2601716433eed70c5f7fd25f31095037d387 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 8 Dec 2019 20:47:08 +0000 Subject: [PATCH] Add VSync setting. --- OpenRA.Game/Graphics/PlatformInterfaces.cs | 1 + OpenRA.Game/Renderer.cs | 5 +++++ OpenRA.Game/Settings.cs | 3 +++ OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs | 10 ++++++++++ OpenRA.Platforms.Default/Sdl2GraphicsContext.cs | 6 ++++++ OpenRA.Platforms.Default/Sdl2PlatformWindow.cs | 2 ++ .../ThreadedGraphicsContext.cs | 7 +++++++ mods/cnc/chrome/settings.yaml | 15 +++++++++++---- mods/common/chrome/settings.yaml | 15 +++++++++++---- 9 files changed, 56 insertions(+), 8 deletions(-) diff --git a/OpenRA.Game/Graphics/PlatformInterfaces.cs b/OpenRA.Game/Graphics/PlatformInterfaces.cs index 0dd6db0fce..e71e14407d 100644 --- a/OpenRA.Game/Graphics/PlatformInterfaces.cs +++ b/OpenRA.Game/Graphics/PlatformInterfaces.cs @@ -73,6 +73,7 @@ namespace OpenRA void DisableDepthBuffer(); void ClearDepthBuffer(); void SetBlendMode(BlendMode mode); + void SetVSyncEnabled(bool enabled); string GLVersion { get; } } diff --git a/OpenRA.Game/Renderer.cs b/OpenRA.Game/Renderer.cs index e5af279020..aa91821b7e 100644 --- a/OpenRA.Game/Renderer.cs +++ b/OpenRA.Game/Renderer.cs @@ -427,6 +427,11 @@ namespace OpenRA Window.Dispose(); } + public void SetVSyncEnabled(bool enabled) + { + Window.Context.SetVSyncEnabled(enabled); + } + public string GetClipboardText() { return Window.GetClipboardText(); diff --git a/OpenRA.Game/Settings.cs b/OpenRA.Game/Settings.cs index 55fc2559f5..35e7ca69bd 100644 --- a/OpenRA.Game/Settings.cs +++ b/OpenRA.Game/Settings.cs @@ -138,6 +138,9 @@ namespace OpenRA [Desc("This can be set to Windowed, Fullscreen or PseudoFullscreen.")] public WindowMode Mode = WindowMode.PseudoFullscreen; + [Desc("Enable VSync.")] + public bool VSync = true; + [Desc("Screen resolution in fullscreen mode.")] public int2 FullscreenSize = new int2(0, 0); diff --git a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs index f53edd9d82..7e75f9b437 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/SettingsLogic.cs @@ -205,6 +205,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic BindCheckboxPref(panel, "HARDWARECURSORS_CHECKBOX", ds, "HardwareCursors"); BindCheckboxPref(panel, "CURSORDOUBLE_CHECKBOX", ds, "CursorDouble"); + BindCheckboxPref(panel, "VSYNC_CHECKBOX", ds, "VSync"); BindCheckboxPref(panel, "FRAME_LIMIT_CHECKBOX", ds, "CapFramerate"); BindCheckboxPref(panel, "PLAYER_STANCE_COLORS_CHECKBOX", gs, "UsePlayerStanceColors"); @@ -232,6 +233,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic battlefieldCameraDropDown.OnMouseDown = _ => ShowBattlefieldCameraDropdown(battlefieldCameraDropDown, ds); battlefieldCameraDropDown.GetText = () => battlefieldCameraLabel.Update(ds.ViewportDistance); + // Update vsync immediately + var vsyncCheckbox = panel.Get("VSYNC_CHECKBOX"); + var vsyncOnClick = vsyncCheckbox.OnClick; + vsyncCheckbox.OnClick = () => + { + vsyncOnClick(); + Game.Renderer.SetVSyncEnabled(ds.VSync); + }; + panel.Get("WINDOW_RESOLUTION").IsVisible = () => ds.Mode == WindowMode.Windowed; var windowWidth = panel.Get("WINDOW_WIDTH"); windowWidth.Text = ds.WindowedSize.X.ToString(); diff --git a/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs b/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs index 3779ca84e7..de51ee627f 100644 --- a/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs +++ b/OpenRA.Platforms.Default/Sdl2GraphicsContext.cs @@ -231,6 +231,12 @@ namespace OpenRA.Platforms.Default OpenGL.CheckGLError(); } + public void SetVSyncEnabled(bool enabled) + { + VerifyThreadAffinity(); + SDL.SDL_GL_SetSwapInterval(enabled ? 1 : 0); + } + public void Dispose() { if (disposed) diff --git a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs index e61e80d278..cfab6fe81f 100644 --- a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs +++ b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs @@ -241,6 +241,8 @@ namespace OpenRA.Platforms.Default else context = new ThreadedGraphicsContext(new Sdl2GraphicsContext(this), batchSize); + context.SetVSyncEnabled(Game.Settings.Graphics.VSync); + SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE); input = new Sdl2Input(); } diff --git a/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs b/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs index da6aee2fa1..f4e8ace61a 100644 --- a/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs +++ b/OpenRA.Platforms.Default/ThreadedGraphicsContext.cs @@ -49,6 +49,7 @@ namespace OpenRA.Platforms.Default Action doDrawPrimitives; Action doEnableScissor; Action doSetBlendMode; + Action doSetVSync; public ThreadedGraphicsContext(Sdl2GraphicsContext context, int batchSize) { @@ -107,6 +108,7 @@ namespace OpenRA.Platforms.Default context.EnableScissor(t.Item1, t.Item2, t.Item3, t.Item4); }; doSetBlendMode = mode => { context.SetBlendMode((BlendMode)mode); }; + doSetVSync = enabled => { context.SetVSyncEnabled((bool)enabled); }; Monitor.Pulse(syncObject); } @@ -445,6 +447,11 @@ namespace OpenRA.Platforms.Default { Post(doSetBlendMode, mode); } + + public void SetVSyncEnabled(bool enabled) + { + Post(doSetVSync, enabled); + } } class ThreadedFrameBuffer : IFrameBuffer diff --git a/mods/cnc/chrome/settings.yaml b/mods/cnc/chrome/settings.yaml index 45366fef24..66dcdd867f 100644 --- a/mods/cnc/chrome/settings.yaml +++ b/mods/cnc/chrome/settings.yaml @@ -145,29 +145,36 @@ Container@SETTINGS_PANEL: Font: Tiny Align: Center Text: Mode, resolution, and cursor changes will be applied after the game is restarted + Checkbox@VSYNC_CHECKBOX: + X: 80 + Y: 125 + Width: 200 + Height: 20 + Font: Regular + Text: Enable VSync Checkbox@FRAME_LIMIT_CHECKBOX: - X: 15 + X: 310 Y: 125 Width: 200 Height: 20 Font: Regular Text: Enable Frame Limiter Label@FRAME_LIMIT_DESC_A: - X: 45 + X: 340 Y: 153 Width: 50 Height: 25 Text: Limit to Align: Right TextField@FRAME_LIMIT_TEXTFIELD: - X: 100 + X: 395 Y: 153 Width: 45 Height: 25 MaxLength: 3 Type: Integer Label@FRAME_LIMIT_DESC_B: - X: 150 + X: 445 Y: 153 Height: 25 Text: FPS diff --git a/mods/common/chrome/settings.yaml b/mods/common/chrome/settings.yaml index 3cf2df5e34..bddd79d518 100644 --- a/mods/common/chrome/settings.yaml +++ b/mods/common/chrome/settings.yaml @@ -159,29 +159,36 @@ Background@SETTINGS_PANEL: Font: Tiny Align: Center Text: Mode, resolution, and cursor changes will be applied after the game is restarted + Checkbox@VSYNC_CHECKBOX: + X: 80 + Y: 125 + Width: 200 + Height: 20 + Font: Regular + Text: Enable VSync Checkbox@FRAME_LIMIT_CHECKBOX: - X: 15 + X: 310 Y: 125 Width: 200 Height: 20 Font: Regular Text: Enable Frame Limiter Label@FRAME_LIMIT_DESC_A: - X: 45 + X: 340 Y: 159 Width: 50 Height: 25 Text: Limit to Align: Right TextField@FRAME_LIMIT_TEXTFIELD: - X: 100 + X: 395 Y: 158 Width: 45 Height: 25 MaxLength: 3 Type: Integer Label@FRAME_LIMIT_DESC_B: - X: 150 + X: 445 Y: 159 Height: 25 Text: FPS