From 3276373745c57fd755788c7465b09a52dc8b4597 Mon Sep 17 00:00:00 2001 From: abcdefg30 Date: Sat, 3 Oct 2020 15:14:18 +0200 Subject: [PATCH] Pause rendering when the window is minimized --- OpenRA.Game/Game.cs | 16 ++++++++++++++-- OpenRA.Game/Graphics/PlatformInterfaces.cs | 1 + OpenRA.Game/Renderer.cs | 1 + OpenRA.Platforms.Default/Sdl2Input.cs | 12 ++++++++++++ OpenRA.Platforms.Default/Sdl2PlatformWindow.cs | 2 ++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index 8a4a5b274b..d5fcf952bc 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -828,8 +828,7 @@ namespace OpenRA var haveSomeTimeUntilNextLogic = now < nextLogic; var isTimeToRender = now >= nextRender; - - if ((isTimeToRender && haveSomeTimeUntilNextLogic) || forceRender) + if (!Renderer.WindowIsSuspended && ((isTimeToRender && haveSomeTimeUntilNextLogic) || forceRender)) { nextRender = now + renderInterval; @@ -844,6 +843,19 @@ namespace OpenRA RenderTick(); renderBeforeNextTick = false; } + + // Simulate a render tick if it was time to render but we skip actually rendering + if (Renderer.WindowIsSuspended && isTimeToRender) + { + // Make sure that nextUpdate is set to a proper minimum interval + nextRender = now + renderInterval; + + // Still process SDL events to allow a restore to come through + Renderer.Window.PumpInput(new NullInputHandler()); + + // Ensure that we still logic tick despite not rendering + renderBeforeNextTick = false; + } } else Thread.Sleep((int)(nextUpdate - now)); diff --git a/OpenRA.Game/Graphics/PlatformInterfaces.cs b/OpenRA.Game/Graphics/PlatformInterfaces.cs index 0e2fd853b3..3f30b9e7c6 100644 --- a/OpenRA.Game/Graphics/PlatformInterfaces.cs +++ b/OpenRA.Game/Graphics/PlatformInterfaces.cs @@ -59,6 +59,7 @@ namespace OpenRA int DisplayCount { get; } int CurrentDisplay { get; } bool HasInputFocus { get; } + bool IsSuspended { get; } event Action OnWindowScaleChanged; diff --git a/OpenRA.Game/Renderer.cs b/OpenRA.Game/Renderer.cs index 6503d466b8..290db21739 100644 --- a/OpenRA.Game/Renderer.cs +++ b/OpenRA.Game/Renderer.cs @@ -33,6 +33,7 @@ namespace OpenRA public RgbaSpriteRenderer RgbaSpriteRenderer { get; private set; } public bool WindowHasInputFocus => Window.HasInputFocus; + public bool WindowIsSuspended => Window.IsSuspended; public IReadOnlyDictionary Fonts; diff --git a/OpenRA.Platforms.Default/Sdl2Input.cs b/OpenRA.Platforms.Default/Sdl2Input.cs index 4e853369e0..d741dfd7d1 100644 --- a/OpenRA.Platforms.Default/Sdl2Input.cs +++ b/OpenRA.Platforms.Default/Sdl2Input.cs @@ -94,6 +94,18 @@ namespace OpenRA.Platforms.Default case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED: device.WindowSizeChanged(); break; + + case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_HIDDEN: + case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MINIMIZED: + device.IsSuspended = true; + break; + + case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_EXPOSED: + case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SHOWN: + case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MAXIMIZED: + case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_RESTORED: + device.IsSuspended = false; + break; } break; diff --git a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs index 345b228e8c..15bc5eab4a 100644 --- a/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs +++ b/OpenRA.Platforms.Default/Sdl2PlatformWindow.cs @@ -99,6 +99,8 @@ namespace OpenRA.Platforms.Default public bool HasInputFocus { get; internal set; } + public bool IsSuspended { get; internal set; } + public GLProfile GLProfile { get