Skip to content

Commit

Permalink
Merge pull request #31 from tannergooding/fps-counter
Browse files Browse the repository at this point in the history
Adding basic support for tracking and reporting the number of frames per second.
  • Loading branch information
tannergooding committed Sep 17, 2019
2 parents b95d81c + d923374 commit b6a7793
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
26 changes: 23 additions & 3 deletions sources/ApplicationModel/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ public void Run()
var dispatcher = dispatchProvider.DispatcherForCurrentThread;
var previousTimestamp = dispatchProvider.CurrentTimestamp;

var previousFrameCount = 0u;
var framesPerSecond = 0u;
var framesThisSecond = 0u;

var secondCounter = TimeSpan.Zero;

// We need to do an initial dispatch to cover the case where a quit
// message was posted before the message pump was started, otherwise
// we can end up with a NullReferenceException when we try to execute
Expand All @@ -93,10 +99,24 @@ public void Run()
while (_state == Running)
{
var currentTimestamp = dispatchProvider.CurrentTimestamp;
var frameCount = previousFrameCount++;
{
var delta = currentTimestamp - previousTimestamp;
OnIdle(delta);
secondCounter += delta;

OnIdle(delta, framesPerSecond);
framesThisSecond++;

if (secondCounter.TotalSeconds >= 1.0)
{
framesPerSecond = framesThisSecond;
framesThisSecond = 0;

var ticks = secondCounter.Ticks - TimeSpan.TicksPerSecond;
secondCounter = TimeSpan.FromTicks(ticks);
}
}
previousFrameCount = frameCount;
previousTimestamp = currentTimestamp;

dispatcher.DispatchPending();
Expand Down Expand Up @@ -138,13 +158,13 @@ private CompositionHost CreateCompositionHost()
return containerConfiguration.CreateContainer();
}

private void OnIdle(TimeSpan delta)
private void OnIdle(TimeSpan delta, uint framesPerSecond)
{
var idle = Idle;

if (idle != null)
{
var eventArgs = new ApplicationIdleEventArgs(delta);
var eventArgs = new ApplicationIdleEventArgs(delta, framesPerSecond);
idle(this, eventArgs);
}
}
Expand Down
8 changes: 7 additions & 1 deletion sources/ApplicationModel/ApplicationIdleEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ namespace TerraFX.ApplicationModel
public readonly struct ApplicationIdleEventArgs
{
private readonly TimeSpan _delta;
private readonly uint _framesPerSecond;

/// <summary>Initializes a new instance of the <see cref="ApplicationIdleEventArgs" /> class.</summary>
/// <param name="delta">The delta between the current and previous <see cref="Application.Idle" /> events.</param>
public ApplicationIdleEventArgs(TimeSpan delta)
/// <param name="framesPerSecond">The number of frames per second.</param>
public ApplicationIdleEventArgs(TimeSpan delta, uint framesPerSecond)
{
_delta = delta;
_framesPerSecond = framesPerSecond;
}

/// <summary>Gets the delta between the current and previous <see cref="Application.Idle" /> events.</summary>
public TimeSpan Delta => _delta;

/// <summary>Gets the number of frames per second.</summary>
public uint FramesPerSecond => _framesPerSecond;
}
}

0 comments on commit b6a7793

Please sign in to comment.