/**
* Code from Xenko GameTime
*/
using System;
namespace NETCoreTest.Framework
{
///
/// Current timing used for variable-step (real time) or fixed-step (game time) games.
///
public class GameTime
{
private TimeSpan _accumulatedElapsedTime;
private int _accumulatedFrameCountPerSecond;
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
public GameTime()
{
_accumulatedElapsedTime = TimeSpan.Zero;
}
///
/// Initializes a new instance of the class.
///
/// The total game time since the start of the game.
/// The elapsed game time since the last update.
public GameTime(TimeSpan totalTime, TimeSpan elapsedTime)
{
Total = totalTime;
Elapsed = elapsedTime;
_accumulatedElapsedTime = TimeSpan.Zero;
}
///
/// Initializes a new instance of the class.
///
/// The total game time since the start of the game.
/// The elapsed game time since the last update.
/// True if the game is running unexpectedly slowly.
public GameTime(TimeSpan totalTime, TimeSpan elapsedTime, bool isRunningSlowly)
{
Total = totalTime;
Elapsed = elapsedTime;
IsRunningSlowly = isRunningSlowly;
_accumulatedElapsedTime = TimeSpan.Zero;
}
#endregion
#region Public Properties
///
/// Gets the elapsed game time since the last update
///
/// The elapsed game time.
public TimeSpan Elapsed { get; private set; }
///
/// Gets a value indicating whether the game is running slowly than its TargetElapsedTime. This can be used for example to render less details...etc.
///
/// true if this instance is running slowly; otherwise, false.
public bool IsRunningSlowly { get; private set; }
///
/// Gets the amount of game time since the start of the game.
///
/// The total game time.
public TimeSpan Total { get; private set; }
///
/// Gets the current frame count since the start of the game.
///
public int FrameCount { get; private set; }
///
/// Gets the number of frame per second (FPS) for the current running game.
///
/// The frame per second.
public float FramePerSecond { get; private set; }
///
/// Gets the time per frame.
///
/// The time per frame.
public TimeSpan TimePerFrame { get; private set; }
///
/// Gets a value indicating whether the and were updated for this frame.
///
/// true if the and were updated for this frame; otherwise, false.
public bool FramePerSecondUpdated { get; private set; }
internal void Update(TimeSpan totalGameTime, TimeSpan elapsedGameTime, TimeSpan elapsedUpdateTime, bool isRunningSlowly, bool incrementFrameCount)
{
Total = totalGameTime;
Elapsed = elapsedGameTime;
IsRunningSlowly = isRunningSlowly;
FramePerSecondUpdated = false;
if (incrementFrameCount)
{
_accumulatedElapsedTime += elapsedGameTime;
double accumulatedElapsedGameTimeInSecond = _accumulatedElapsedTime.TotalSeconds;
if (_accumulatedFrameCountPerSecond > 0 && accumulatedElapsedGameTimeInSecond > 1.0)
{
TimePerFrame = TimeSpan.FromTicks(_accumulatedElapsedTime.Ticks / _accumulatedFrameCountPerSecond);
FramePerSecond = (float) (_accumulatedFrameCountPerSecond / accumulatedElapsedGameTimeInSecond);
_accumulatedFrameCountPerSecond = 0;
_accumulatedElapsedTime = TimeSpan.Zero;
FramePerSecondUpdated = true;
}
_accumulatedFrameCountPerSecond++;
FrameCount++;
}
}
internal void Reset(TimeSpan totalGameTime)
{
Update(totalGameTime, TimeSpan.Zero, TimeSpan.Zero, false, false);
_accumulatedElapsedTime = TimeSpan.Zero;
_accumulatedFrameCountPerSecond = 0;
FrameCount = 0;
}
#endregion
}
}