SDL_GetTicks in OS X

Member
Posts: 184
Joined: 2004.07
Post: #1
Hey everyone,
I noticed in my port to Mac OS X, SDL_GetTicks/SDL_Delay seemed to run faster than on my PC (or slower, maybe, the point was it was a different speed than the PC.) I couldn't find this documented anywhere, so I thought I'd ask you all about it- perhaps my programming is off or something, or I'm not using SDL in the right way.
As an additional, sort of related question, is there a standard UNIX way to sleep? I noticed the nanosleep function, but I don't know if that's available on any MacOS. usleep maybe?

Any responses are appreciated!

p.
Quote this message in a reply
wally
Unregistered
 
Post: #2
SDL_GetTicks() should be accurate enough. The problem is that SDL_Delay() is not guaranteed to sleep for the exact time you specify (nor is the OS sleep function). In fact, the minimum guaranteed sleep time for most platforms is 10ms (This is related to the fact that the OS scheduler can switch tasks every 10ms). So you can't sleep for 15ms, only multiples of 10ms.

What might help is if you put a separate SDL_GetTicks() around SDL_Delay(). If you know how long you actually delayed, you can use that information to compensate for the error.

If that's not good enough, throw out SDL_Delay() alltogether and just call SDL_GetTicks() in a busy loop to obtain the "idle" time you want. You'll be hogging the CPU but that is expected and acceptable behavior for a game.
Quote this message in a reply
Member
Posts: 204
Joined: 2002.09
Post: #3
Quote:Originally posted by phydeaux

As an additional, sort of related question, is there a standard UNIX way to sleep? I noticed the nanosleep function, but I don't know if that's available on any MacOS. usleep maybe?

To sleep a specified number of milliseconds:

Code:
void sleepThread(unsigned int ms)
{
    timeval tv = {ms / 1000, ms % 1000};
    select(0, 0, 0, &tv);
}

Cheers,
Rocco
Quote this message in a reply
Member
Posts: 184
Joined: 2004.07
Post: #4
Thanks a bundle, wally, that was the problem. I was only calling SDL_Delay once per drawing cycle, and I figured it was going to be exact. For precision what I'm probably going to do now is SDL_Delay 20 ms less or so to make sure I wake up before when I'm actually _supposed_ to wake up, then busy wait like you suggested.
Quote this message in a reply
Post Reply