diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 78700ed7bf..78a3a3ad23 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -82,6 +82,8 @@ namespace OpenRa.Game PlaySound("intro.aud", false); skipMakeAnims = false; + + sw = new Stopwatch(); } static void LoadMapBuildings( IniFile mapfile ) @@ -139,6 +141,10 @@ namespace OpenRa.Game const int oreFrequency = 1; static int oreTicks = oreFrequency; public static int RenderFrame = 0; + public static double RenderTime = 0.0; + public static double TickTime = 0.0; + + public static Stopwatch sw; public static void Tick() { @@ -146,6 +152,7 @@ namespace OpenRa.Game int dt = t - lastTime; if( dt >= timestep ) { + sw.Reset(); lastTime += timestep; if( orderManager.Tick() ) @@ -163,11 +170,15 @@ namespace OpenRa.Game foreach( var player in players.Values ) player.Tick(); } + + TickTime = sw.ElapsedTime(); } + sw.Reset(); ++RenderFrame; viewport.cursor = controller.ChooseCursor(); viewport.DrawRegions(); + RenderTime = sw.ElapsedTime(); } public static bool IsCellBuildable(int2 a, UnitMovementType umt) diff --git a/OpenRa.Game/Graphics/WorldRenderer.cs b/OpenRa.Game/Graphics/WorldRenderer.cs index 73e823950b..137d4dc758 100644 --- a/OpenRa.Game/Graphics/WorldRenderer.cs +++ b/OpenRa.Game/Graphics/WorldRenderer.cs @@ -99,7 +99,10 @@ namespace OpenRa.Game.Graphics lineRenderer.Flush(); - renderer.DrawText(string.Format("RenderFrame {0} Tick {1}", Game.RenderFrame, Game.orderManager.FrameNumber), new int2(5, 5), Color.White); + renderer.DrawText(string.Format("RenderFrame {0} ({2:F1} ms) Tick {1} ({3:F1} ms)", + Game.RenderFrame, Game.orderManager.FrameNumber, + Game.RenderTime * 1000, + Game.TickTime * 1000), new int2(5, 5), Color.White); } const float conditionYellow = 0.5f; /* todo: get these from gamerules */ diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 9e866d64f3..003196d3ed 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -76,6 +76,7 @@ + diff --git a/OpenRa.Game/Stopwatch.cs b/OpenRa.Game/Stopwatch.cs new file mode 100644 index 0000000000..7e9842db05 --- /dev/null +++ b/OpenRa.Game/Stopwatch.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; + +namespace OpenRa.Game +{ + class Stopwatch + { + [DllImport("kernel32.dll")] + static extern bool QueryPerformanceCounter(out long value); + [DllImport("kernel32.dll")] + static extern bool QueryPerformanceFrequency(out long frequency); + + long freq, start; + + public Stopwatch() + { + QueryPerformanceFrequency(out freq); + QueryPerformanceCounter(out start); + } + + public double ElapsedTime() + { + long current; + QueryPerformanceCounter(out current); + + return (current - start) / (double)freq; + } + + public void Reset() + { + QueryPerformanceCounter(out start); + } + } +}