Skip to content

Commit 74b30a4

Browse files
committed
Add average frame time value
Smoothed frametime for UI use. There's a lot of fluctuation per frame which makes the actual frame time harder to read
1 parent 7c124e5 commit 74b30a4

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/App/App.bf

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Nanoforge.Misc.Containers;
12
using System.Collections;
23
using System.Diagnostics;
34
using System.Reflection;
@@ -16,11 +17,31 @@ namespace Nanoforge.App
1617

1718
public class FrameData
1819
{
19-
///Amount of time this frame that work was being done. Delta time can be larger if the target framerate is slower than the current framerate
20+
///Amount of time this frame that work was being done. DeltaTime includes time waiting for target framerate. This doesn't.
2021
public f32 RealFrameTime;
2122

2223
///Total time elapsed last frame. Includes time waiting until target framerate if there was time to spare
2324
public f32 DeltaTime;
25+
26+
///Number of frames to sample AverageFrameTime
27+
public const int NumFrametimeSamples = 30;
28+
29+
///Last N real frametimes. Used to calculate AverageFrameTime
30+
public append RingBuffer<f32, 30> AverageFrametimeSamples;
31+
32+
///Average of last N RealFrameTime values. Use when you want a more stable value for UI purposes. The real value can fluctuate frame by frame
33+
public f32 AverageFrameTime
34+
{
35+
get
36+
{
37+
f32 result = 0.0f;
38+
for (f32 v in AverageFrametimeSamples.[Friend]_data) //TODO: Fix the RingBuffer enumerator
39+
result += v;
40+
41+
return result / AverageFrametimeSamples.Size;
42+
}
43+
};
44+
2445
}
2546

2647
//System stages. Run each frame in this order.
@@ -116,6 +137,7 @@ namespace Nanoforge.App
116137

117138
//Wait until target frametime
118139
frameData.RealFrameTime = (f32)_frameTimer.ElapsedMicroseconds / 1000000.0f;
140+
frameData.AverageFrametimeSamples.Push(frameData.RealFrameTime);
119141
while(((float)_frameTimer.ElapsedMicroseconds / 1000000.0f) < _maxFrameRateDelta)
120142
{
121143

0 commit comments

Comments
 (0)