From 399cef8fb2193cfa423b52559499c3eddadf8d60 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Tue, 14 Nov 2023 19:32:10 +0000 Subject: [PATCH] Reset FPS counter on game start. This avoids this displayed counter being dragged down by lower FPS during loading prior to the game starting. --- OpenRA.Game/Game.cs | 3 ++ .../Widgets/Logic/PerfDebugLogic.cs | 28 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index b8d435db96..0cb379bfd3 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -180,6 +180,7 @@ namespace OpenRA } public static event Action BeforeGameStart = () => { }; + public static event Action AfterGameStart = () => { }; internal static void StartGame(string mapUID, WorldType type) { // Dispose of the old world before creating a new one. @@ -227,6 +228,8 @@ namespace OpenRA // PostLoadComplete is designed for anything that should trigger at the very end of loading. // e.g. audio notifications that the game is starting. OrderManager.World.PostLoadComplete(worldRenderer); + + AfterGameStart(); } public static void RestartGame() diff --git a/OpenRA.Mods.Common/Widgets/Logic/PerfDebugLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/PerfDebugLogic.cs index f6e76719fb..bd3f993d02 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/PerfDebugLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/PerfDebugLogic.cs @@ -20,6 +20,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic { public class PerfDebugLogic : ChromeLogic { + readonly Stopwatch fpsTimer; + readonly List<(int Frame, TimeSpan Time)> frameTimings = new(32); + [ObjectCreator.UseCtor] public PerfDebugLogic(Widget widget, WorldRenderer worldRenderer) { @@ -29,8 +32,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic var perfText = widget.Get("PERF_TEXT"); perfText.IsVisible = () => Game.Settings.Debug.PerfText; - var fpsTimer = Stopwatch.StartNew(); - var frameTimings = new List<(int Frame, TimeSpan Time)>(32) { (Game.RenderFrame, TimeSpan.Zero) }; + fpsTimer = Stopwatch.StartNew(); + frameTimings.Add((Game.RenderFrame, TimeSpan.Zero)); perfText.GetText = () => { // Calculate FPS as a rolling average over the last ~1 second of frames. @@ -51,6 +54,27 @@ namespace OpenRA.Mods.Common.Widgets.Logic $"Viewport Size: {viewportSize.Width} x {viewportSize.Height} / {Game.Renderer.WorldDownscaleFactor}\n" + $"WFB Size: {wfbSize.Width} x {wfbSize.Height}"; }; + + Game.AfterGameStart += OnGameStart; + } + + void OnGameStart() + { + // Reset timings so our average doesn't include loading time. + frameTimings.Clear(); + frameTimings.Add((Game.RenderFrame, fpsTimer.Elapsed)); + } + + bool disposed; + protected override void Dispose(bool disposing) + { + if (disposing && !disposed) + { + disposed = true; + Game.AfterGameStart -= OnGameStart; + } + + base.Dispose(disposing); } } }