Timers via clock()

Posts: 321
Joined: 2004.10
Post: #1
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 Smile

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

I've heard (read here) that glut can be problematic. Should I move to SDL?
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
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 Smile
Quote this message in a reply
Posts: 1,199
Joined: 2004.10
Post: #3
I don't know how glutTimerFunc made it past any sort of QA.
Quote this message in a reply
Post: #4
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
val = (int)((now * 1000.0) - (__glutStartupTime * 1000.0));

__glutStartupTime = [NSDate timeIntervalSinceReferenceDate];
Quote this message in a reply
Posts: 321
Joined: 2004.10
Post: #5
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 Smile
Quote this message in a reply
Posts: 208
Joined: 2005.04
Post: #6
There's also CFAbsoluteTimeGetCurrent()

CFAbsoluteTime is just a double.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Next step after Timers nabobnick 1 2,602 May 11, 2006 01:51 PM
Last Post: OneSadCookie