Recommended Posts

I have been working on some animation code in SDL and C, but I am stuck on what to use for calculating the animation's speed.
What I use is a bmp 192x128. Width of each frame is 32. Hight is 48;
I though about using one of these:
int SDL_AddTimer(interval, callback, param)
Add a new timer to the pool of timers already running.
SDL_Delay(ms)
Wait a specified number of milliseconds before returning.
int SDL_GetTicks()
Get the number of milliseconds since the SDL library initialization.
SDL_RemoveTimer(t)
Remove one of the multiple timers knowing its ID.
SDL_SetTimer(interval, callback)
Set a callback to run after the specified number of milliseconds has elapsed.
but not sure the proper way to calculate the speed of the animation. Hopefully someone in here can help me with that.
Here is the code I have so far. Atm all it does is play the animation over and over really fast.

Share this post

Link to post

Share on other sites

startanim creates and destroys a surface every frame. Why not keep a reference to it somewhere and load it just once?

I might be wrong, but I remember something about SDL_BlitSurface changing src or dest if any of them included an invalid area of the surface, but you could try to keep these outside the loop, since you load the same value into them every time:

You call SDL_Flip a few times per frame, when you should try to do it just once per frame. Instead of updating the screen in startanim, ask it to draw only one frame, update the frame counter inside your main loop and SDL_Flip only in there

About the timing, I'd go with SDL_GetTicks(). It will return the number of miliseconds since the library initialization. With one call to it every frame, you can find the time passed between two frames, something like this:

Say you have an animation that has 200ms between changing frames and our last iteration of the game loop took 12ms. We subtract 12 from the animation delay and see if we need to change the animation frame.

// delta is the time our last game loop iteration took to completemy_animation.change_frame -= delta;

Now say we only had 9ms left on the change_frame before this iteration of the game loop - we would have -3ms for the change_frame variable. If the change_frame variable is less than or equal to 0 we change to the next frame (or if the last frame start over depending on your needs) and reset the change_frame counter to the delay for the next frame in the animation. Since we have an extra 3 ms we subtract this from the change_frame counter as well leaving 197ms. This gives us the continuity we are looking for.

// use while in case it was an exceptionally long framewhile ( my_animation.change_frame <= 0 ){ // increment the frame but make sure we don't overstept our bounds if (++my_animation.current_frame >= my_animation.frames.size() ) my_animation.current_frame=0;

// make sure we keep the time consistent my_animation.change_frame += my_animation.frames[my_animation.current_frame].delay;}

I have been using this method (although fleshed out a little) successfully for quite while. I hope you find it useful.