SDL Delay

Started by
6 comments, last by Kwizatz 16 years, 6 months ago
Hello. I'm wondering if the SDL delay function is appropriate for animation with frames.
Advertisement
Not if your game will feature many such animations concurrently.

If you were making a program where the animation filled the entire window (like a simple movie player) then you could get away with it.
I'm making a game... Thanks for telling me!
What kind of technique should you use instead of calling SDL Delay?
Quote:Original post by FILO
What kind of technique should you use instead of calling SDL Delay?


Check this out ;) :
http://www.gamedev.net/community/forums/topic.asp?topic_id=69074

If you're using a delay function in your animation code, your code is wrong, instead get the current time and subtract the last time recorded, if enough time has passed between the 2 samples, switch frame, save current time as last time and repeat.

Use SDL_GetTicks() to get the number of milliseconds since the SDL library initialization, since you're working with deltas, you don't need the actual "current time".
Quote:Original post by Kwizatz
If you're using a delay function in your animation code, your code is wrong, instead get the current time and subtract the last time recorded, if enough time has passed between the 2 samples, switch frame, save current time as last time and repeat.

Use SDL_GetTicks() to get the number of milliseconds since the SDL library initialization, since you're working with deltas, you don't need the actual "current time".


Thanks for the advice guys. This is what I am currently doing:

// Ask SDL for the time in milliseconds
int tick = SDL_GetTicks();

if (tick <= gLastTick)
{
SDL_Delay(1);
return;
}

while (gLastTick < tick)
{
gLastTick += 1000 / ICSFPS;
}


where ICSFPS is defined like so in the .header:

//iterations per second
#define ICSFPS 10

This kind of works but I still have issues concerning things moving too fast. Thanks again for all the help
Try this:

// Ask SDL for the time in millisecondsint tick = SDL_GetTicks();if ((tick - gLastTick)>100) // one frame every 100 milliseconds, IE 10 frames per second{/* set next frame as current frame */gLastTick=tick;}/* render current frame */


you may want to initialize gLastTick to zero so the first time the code is run an update is performed.

This topic is closed to new replies.

Advertisement