The Need for Time
When designing a game title, the recurring concern is game performance - will the game play fast enough?
An equally important question is whether the game will play smoothly. No matter how optimized your engine may be, it will never be able to provide a full sense of realism if object motion is not fluid, or if the frame rate is not consistent.
To achieve this, it is important that we are able to achieve an accurate measure of time. There are a number of reasons that your software will need to use timekeeping functions, including :
Available Time Keeping Functions
There are a variety of functions available for dealing with timing issues, but they basically fall into two categories :
For the purpose of this article we will be dealing with the former. While timers do find use in game titles, their reliance on the message system means that your application will not be notified until all other tasks are completed. There are ways to work with this, but that is the makings for another article....
Below is as explanation of three basic time keeping services. Note that these functions do not provide an absolute time (i.e. time of day), but rather provide a relative time to system or Windows startup.
The Proof is in The Numbers
Of course, we never really know something until we have proven it to ourselves ;-)
To this end, I once set out to test these facts for myself, as any good programmer will. For this purpose, I created a short profiler that tests each of these three methods, by performing each test 10,000 times in a tight loop. The total time to execute was measured using the Performance Counter. Another test was performed running each function 100 times and analyzing the results to determine resolution and accuracy.
Note that this is based on a small sample, and is intended to give a rough idea of the operation of these functions. The times listed do not take into account the amount of time required to run the loop, or to acquire the results - this is mean merely as a qualitative test. With that said, here are the general results of my testing, along with a critique of each function :
So, what to do with this new found knowledge? Now that we understand the benefits and downfalls of the various timer functions, the following articles will provide tutorials on implementing timing in a real-time game environment:
The game loop drives the flow of the program, providing a heartbeat for synchronizing object motion and rendering of frames in a steady, consistent manner. Provides source code for a basic game loop using the Performance Counter, and provides automatic detection of the Performance Counter and fallback to timeGetTime if it is not available.
To allow for variations in playback, it is important that motion within the game be scaled to frame time. This insures that motion is steady, and aids in synchronizing the motion of objects.
The use of threads can allow your title to make use of time that is often spent idle, providing better performance and tolerance for changes in system overhead. Learn how and when to implement threads in your game, and how properly time and synchronize their execution.
Visitors Since 1/1/2000: