Sharing some early code about the game timer in my DX11 Engine. The reason why I built a new Timer is because here are the implementations available:

1. Microsoft ships a generic StepTimer.h with each DX11 Project
But it has some issues like you can’t pause the timer and resume it at will. Furthermore, it’s just a Stopwatch not a Timer.

2. Frank D. Luna’s GameTimer
This is a slightly better implementation than Microsoft’s Timer. But it lacks the Timer and Callback functionality, however it provides Pause and Resume for Stopwatch mode.

Here is my Version:

  • Timer or Stopwatch as per the Configuration
  • Start, Pause, Resume, Stop
  • Callbacks for Each Tick
  • Callback when Timer has Elapsed (only for Timer Mode)

It’s so generic that I can use it for many things like animations, timed stuff like bombs, item timers, level timers etc. I have shared the initial code on gist (embedded below). It may change later on, but not that much.

Usage

Timer Mode:

/*---- Init Code ----*/
// Tell Azura to Make a Countdown Timer
m_countdown = Timer(AzuraTimerType::Countdown);
// Set the Limit to 10s
m_countdown.SetLimit(10.0);

// For Static Member Functions - Specify a void *callback()
m_countdown.OnCountdownEnd(&Game::onCountdownEnd);

// For Regular Member Functions - Specify a Boost Binded Function
m_countdown.OnCountdownEnd(boost::bind(&Game::onCountdownEndRegular, this));

// Start the Timer
m_countdown.Start();

/*---- Frame Update Loop ----*/
m_countdown.Tick(nullptr);
// Optionally, if you want a Tick Callback for Timer
// You can also do that using:
// a void *callback() regular function
// OR a Boost binded member function (same declaration as above)

Stopwatch Mode:

/*---- Init Code ----*/
// Default is Stopwatch
m_stopwatch = Timer();
// Start the Stopwatch
m_stopwatch.Start();

/*---- Frame Update Loop ----*/
// For Static Member Functions - Specify a void *callback()
m_stopwatch.Tick(&Game::onStopWatchTick);

// For Regular Member Functions - Specify a Boost Binded Function
m_stopwatch.Tick(boost::bind(&Game::onStopWatchTick2, this));

Code

Header File:

Source File: