Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
88 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

SuperQuadric Ellipsoids and Toroids,
OpenGL Lighting, and Timing

by Jonathan Metzgar


  Contents

 Super Quadrics
 A Lighting
 Class

 Timing

 Source & Demo
 Printable version

 


Timing

Timing is a critical part of developing simulations. Many simulations suffer from this because they don't have support for controlling the speed of their program. Here is a sample implementation of a timing loop in Win32.

In this tutorial's program, we have defined a few variables, DTick, Tick1, and Tick2. These are used to store the delta time, initial time, and final time, respectively. These are defined globally so they can be used by the entire program.

int Tick1, Tick2; // used to help keep speed constant float DTick;

Next we have to "prime" the variables so we do this at the very beginning of our program in WinMain. We use the GetTickCount() function to return the number of milliseconds since the computer was started.

Tick1 = GetTickCount ( ); Tick2 = GetTickCount ( ); DTick = DTick = (float)((Tick2 - Tick1) / 30.0);

Now we go to our loop and do the same calculation except that we replace Tick1 with the value from Tick2 so we can calculate the difference in time since the last frame. We get the delta time by subtracting the two and finally divide by 30.0 to get a factor for updating our variables.

// update time Tick1 = Tick2; Tick2 = GetTickCount ( ); DTick = (float)((Tick2 - Tick1) / 30.0);

That's all you need and it should keep your simulation running smoother when the computer is faster. On slow computers, it will keep the frame up to date to ensure accuracy.

If you have any questions, please free feel to e-mail me at microwerx@yahoo.com. You can also visit my website at http://www.geocities.com/microwerx/ for more information, games, articles, and more!