CU_MDX_Timer.zip (63.5 KiB, 2,595 hits)
CUnitFramework.zip (101.1 KiB, 20,443 hits)
inside the project (not solution) directory.
The C-Unit Framework provides a high resolution timer for use in you applications. Timing is necessary because faster computers can process more game loops than slower computers. Therefore, we should update_ our game based on how much time has passed since we rendered the last frame. This will create_ a constant experience over multiple systems. We can also use our timing information to calculate the frame rate of our demos, which gives us a good performance rating. To manage all the timing functionality, the C-Unit Framework has a timing class called, Timer. The calculations in Timer use the high performance hardware counter found in most modern computers. Older computers may not support the high performance counter but I’m assuming that the people reading this tutorial have a fairly decent computer (heck, mine’s an old Pentium 4 1.8 GHz and I still have support for the timer). If you want to support older computers, you would want to fallback on the Windows timer function, timeGetTime, if there was no hardware support. However, I don’t do that in this tutorial.
public static extern bool QueryPerformanceFrequency( ref long PerformanceFrequency );
public static extern bool QueryPerformanceCounter( ref long PerformanceCount );
To create_ our timer, we will need access to a couple of native methods: QueryPerformanceFrequency. and QueryPerformanceCounter. The first function determines how many times per second your system’s counter fires. We need this value to calculate all of our timing values such as the time passed between frames and the frames per second. The second function determines what the counter’s current value is set to.
To gain access to these functions, we need to add them to our NativeMethods class.
public class Timer
private long m_currentTime;
private long m_lastTime;
private long m_lastFPSUpdate;
private long m_FPSUpdateInterval;
private uint m_numFrames;
private float m_runningTime;
private float m_timeElapsed;
private float m_fps;
private bool m_timerStopped;
NativeMethods.QueryPerformanceFrequency( ref m_ticksPerSecond );
m_timerStopped = true;
// Update the FPS every half second.
m_FPSUpdateInterval = m_ticksPerSecond >> 1;
public void Start()
NativeMethods.QueryPerformanceCounter( ref m_lastTime );
m_timerStopped = false;
public void Stop()
long stopTime = 0;
NativeMethods.QueryPerformanceCounter( ref stopTime );
m_runningTime += (float)(stopTime – m_lastTime) / (float)m_ticksPerSecond;
m_timerStopped = true;
public void Update()
// Get the current time
NativeMethods.QueryPerformanceCounter( ref m_currentTime );
// Update time elapsed since last frame
m_timeElapsed = (float)(m_currentTime – m_lastTime) / (float)m_ticksPerSecond;
m_runningTime += m_timeElapsed;
// Update FPS
if ( m_currentTime – m_lastFPSUpdate >= m_FPSUpdateInterval )
float lastTime = (float)m_lastFPSUpdate / (float)m_ticksPerSecond;
m_fps = (float)m_numFrames / (currentTime – lastTime);
m_lastFPSUpdate = m_currentTime;
m_numFrames = 0;
m_lastTime = m_currentTime;
public bool Stopped
public float FPS
public float ElapsedTime
public float RunningTime
The Timer class include methods to Start, Stop, and Update the counter. Most of the code is pretty self-explanatory. To calculate the elapsed time since the last update, we need to get the difference between the current counter value and the counter value from the last update. To convert the result into seconds, we divide it by the number of ticks the counter fires per second. The total running time is simply updated each frame with the current elapsed time value.
Calculating the frames per second involves a few steps. First, to prevent the FPS from updating every frame, we need to store an FPS update_ interval. If we updated the FPS every frame, it would change too fast for us to see any single value. The update_ interval is set to half the counter frequency. This means the timer will perform the FPS calculation every half second. Second, we need a variable that counts the number of times the Update method is called. If we call Timer.Update every frame, it will represent the number of frames rendered. To calculate the FPS, we divide this frame counter by the time passed since the last time the FPS was calculated. Once the FPS is calculated, we reset the frame counter to 0 and repeat.
m_gameStates.OnRenderFrame( m_graphics.Device, m_timer.ElapsedTime );
When the Framework updates and renders a frame, it sends the elapsed time to the executing GameState, which allows us to keep a consistant animation speed.