PDA

View Full Version : Timers via clock()


WhatMeWorry
2005.08.01, 03:20 AM
I create a timer like this:

// CLOCKS_PER_SEC is number of clock ticks per second

clock_t future = ( (clock_t) seconds * CLOCKS_PER_SEC );

u->timerExpiration = clock() + future;


and then check for the expiration of said timer with:

clock_t ticksRightNow = clock();

if ( ticksRightNow > u->timerExpiration )
{
// timer expiration code here
}

The problem is that the ticksRightNow is _painfully_ slow. It should
be 100 ticks per second according to CLOCKS_PER_SEC. Its more
like 1 tick a second :)

I wrote a little test program outside of my game, and the ticksRightNow
was very fast, that is, was being incremented 100 times second.

So, I'm wondering if something in my game could be screwing up
the time. I do use glutTimerFunc(). Could that be goofing everything up?
I'd get rid of it, but the glutTimerFunc is was drives the openGL
display() function to update the screen. I've got lots of objects moving
around.

I've heard (read here) that glut can be problematic. Should I move to SDL?

OneSadCookie
2005.08.01, 04:06 AM
clock() tells you how much CPU time your app has used, nothing about wall clock time.

In GLUT, glutGet(GLUT_ELAPSED_TIME) is a pretty good timer. Don't use glutTimerFunc for anything, it's not useful :)

TomorrowPlusX
2005.08.01, 08:06 AM
I don't know how glutTimerFunc made it past any sort of QA.

nabobnick
2005.08.01, 10:00 AM
glutGet(GLUT_ELAPSED_TIME) =
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
val = (int)((now * 1000.0) - (__glutStartupTime * 1000.0));


__glutStartupTime = [NSDate timeIntervalSinceReferenceDate];

WhatMeWorry
2005.08.01, 05:28 PM
Thanks for the suggestions! Got rid of gluTimerFunc() and I'm using
glutGet(GLUT_ELAPSED_TIME) and life is good...at least until the next bug :)

Andrew
2005.08.02, 01:50 AM
There's also CFAbsoluteTimeGetCurrent() (http://developer.apple.com/documentation/CoreFoundation/Reference/CFTimeUtils/Reference/chapter_1.2_section_3.html)

CFAbsoluteTime is just a double.