Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
Added UpdateTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
craftersmine committed Apr 1, 2019
1 parent 3413da4 commit 1358a10
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
105 changes: 105 additions & 0 deletions craftersmine.EtherEngine.Utilities/UpdateTimer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Diagnostics;
using System.Threading;

namespace craftersmine.EtherEngine.Utilities
{
/// <summary>
/// Represents a specific frequency timer. This class cannot be inherited
/// </summary>
public sealed class UpdateTimer
{
private DateTime _last;
private TimeSpan _lag;
private DateTime _current;
private TimeSpan _elapsed;
private Thread _updaterThread;
private TimeSpan _updaterFrequency;

/// <summary>
/// Gets true if timer is active, otherwise false
/// </summary>
public bool IsActive { get; private set; }
/// <summary>
/// Gets current timer frequency
/// </summary>
public float UpdaterFrequency { get; private set; }
/// <summary>
/// Gets last fixed update elapsed time
/// </summary>
public TimeSpan FixedUpdateTime { get { return _elapsed; } }
/// <summary>
/// Gets last update elapsed time
/// </summary>
public TimeSpan UpdateTime { get { return FixedUpdateTime + Lag; } }
/// <summary>
/// Gets lag time
/// </summary>
public TimeSpan Lag { get { return _lag; } }

/// <summary>
/// Occurs when FixedUpdate being called
/// </summary>
public event EventHandler<UpdateEventArgs> FixedUpdate;
/// <summary>
/// Occurs when Update beind called
/// </summary>
public event EventHandler<UpdateEventArgs> Update;
//public TimeSpan LastUpdateTime { get { return _last; } }
//public TimeSpan LagTime { get { return _lag; } }

/// <summary>
/// Creates new <see cref="UpdateTimer"/> instance with specific frequency
/// </summary>
/// <param name="frequency">Timer update frequency</param>
public UpdateTimer(float frequency)
{
IsActive = false;
UpdaterFrequency = frequency;
_updaterThread = new Thread(new ThreadStart(Updater));
_updaterFrequency = TimeSpan.FromMilliseconds(1000.0f / UpdaterFrequency);
}

/// <summary>
/// Stops timer
/// </summary>
public void Stop()
{
IsActive = false;
}

/// <summary>
/// Starts timer
/// </summary>
public void Start()
{
IsActive = true;
_updaterThread.Start();
}

private void Updater()
{
_last = DateTime.Now;
_lag = TimeSpan.Zero;
while (IsActive)
{
_current = DateTime.Now;
_elapsed = _current - _last;
_last = _current;
_lag += _elapsed;

FixedUpdate?.Invoke(this, new UpdateEventArgs() { DeltaTime = _elapsed });

while (_lag >= _updaterFrequency) {
Update?.Invoke(this, new UpdateEventArgs() { DeltaTime = _elapsed + _lag });
_lag -= _updaterFrequency;
}
}
}
}

public sealed class UpdateEventArgs : EventArgs
{
public TimeSpan DeltaTime { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Debugging.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UpdateTimer.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit 1358a10

Please sign in to comment.