Jump to content
  • Advertisement
Sign in to follow this  

Best timer to use?

This topic is 4469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been looking through NeHe's guides, and I've seen timers done in a few different ways. I'm making a 3d game and I'd like to know how I should set up my timer. I know NeHe uses something related to a performance timer in one of the lessons, while in anotehr one something related to "Ticks" is used. I'd really appreciate if someone could link a source file that has a timer set up and uses openGL, I mean the type of timer I should use for a game. Thanks...

Share this post

Link to post
Share on other sites
What are you using the timer for? What OS are you using? Those will make a great difference in what timer is "best". If you are using SDL, it has a built in timer that is fairly decent and OS independent.

Share this post

Link to post
Share on other sites

I'm not sure which of the NeHe tutorials you're referring to, but in general game timers are all done the same, regardless of whether they're Direct3D or OpenGL.

The "performance timer" you mentioned is actually a kernel level operating system feature which is made up of two functions: 'QueryPerformanceFrequency' and 'QueryPerformanceCounter'

The first function, "QueryPerformanceFrequency" tells the caller how many "ticks" the processor's high frequency counter gets per second.

The second function, "QueryPerformanceCounter" tells the caller what the current "tick count" is.

So in your main loop if you capture the value of the performance counter each iteration and subtract the previous value from it, you can determine how many ticks occurred during a single iteration of your loop. This is where the frequency comes in. If you know the number of elapsed ticks, and you know how many ticks the high performance counter can make in a second, you can use the two facts to determine how much time has elapsed in a very accurate fashion. This computation of elapsed time can either be done in the main loop as shown below, or can be wrapped in a timer class to allow "stopping" and "starting" based upon which values you subtract. In general, all timers work like the code shown below, just with more bells and whistles.

// Declare some variables either outside of your main loop or in a Timer class
LARGE_INTEGER frequency;

// Get the frequency

// Set an initial value for 'counts' before your main loop begins

while( condtionIsTrue )
// Get the current counts

// Compute the elapsed count
LARGE_INTEGER elapsedCount = counts - prevCounts;

// Compute the elapsed time...
// divide "ticks" by "ticks per second" to get "seconds"
double elapsedTime = elapsedCount.QuadPart / frequency.QuadPart;

// Set the previous count to the current count to get prepared
prevCounts = counts;

// Perform all of your update logic, assuming "elapsedTime" has passed
// On the next iteration, elapsedTime will contain the amount of time that
// has elapsed since we were last here in the loop

// ...
// Game Logic
// ...

And that logic should work just fine for you. There are a number of example timer classes which wrap the above functionality for you, including NeHe's timer I believe.


Share this post

Link to post
Share on other sites
Wow! Thanks... I couldn't've hoped for a better response.

Thats basically all I need... I guess I'll just use NeHe's for now, since he has all the OpenGL stuff set up too + its for VS 6.0, which I'm using.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!