Sign in to follow this  
SilencedViolence

Best timer to use?

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
Silenced,

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 counts;
LARGE_INTEGER prevCounts;
LARGE_INTEGER frequency;

// Get the frequency
QueryPerformanceFrequency(&frequency);

// Set an initial value for 'counts' before your main loop begins
QueryPerformanceCounter(&prevCounts);

while( condtionIsTrue )
{
// Get the current counts
QueryPerformanceCounter(&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.

Cheers!

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this