Jump to content
  • Advertisement
Sign in to follow this  

the best way of doing animation in SDL?

This topic is 3894 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There's several ways, actually. One way is to hold the entire animation in one image, with each frame next to(or below) the previous.

You can then create a array of SDL_Rects that each hold a different frame, and cycle through the frames using SDL_BlitSurface(animationImageFile, rectArray[animationFrame], surfaceToDrawTo, NULL). You'd increment the int, animationFrame, after a set amount of time, for example, every 100 millaseconds, to get 10 frames per second animation.

This is very simular to what LazyFoo does in his SDL tutorial in lesson 20.

You could also have a array of SDL_Surfaces, instead of SDL_Rects. Same concept, pretty much.

'The best' way would be to wrap it in a class, or struct if using C and not C++, and allow for different sizes of animations, time between frames, etc... without having to hardcode it all, perhaps even loading a small .txt file that describes the animation.

Something like:

If you want to make it even more flexible, for slightly more work you can even get the different frames in the same animation, to stay on-screen for longer or shorter periods of time. You could do this by having a structure like this:

struct AnimationFrame
SDL_Surface *frame; //Or 'SDL_Rect frame', if using tilesheets or spritesheets.
Uint32 delay; //Delay until the next frame.
AnimationFrame *nextFrame; //A pointer to the next frame.

struct Animation
AnimationFrame *frames; //Pointer to a array of frames.
int numFrames; //The number of frames in the array.
int currentFrame; //The current frame.
Uint32 lastChange; //The SDL_GetTicks() of when the frame last changed.

void UpdateAnimation(Animation *animation)
Uint32 currentTime = SDL_GetTicks();
Uint32 lastChange = animation->lastChange;
Uint32 delay = animation->frames[animation->currentFrame].delay;

if((lastChange + delay) < currentTime)
animation->lastChange = SDL_GetTicks();
if(animation->currentFrame >= animation->numFrames)
animation->currentFrame = 0;

To draw, just draw:

To update, just call:

This is just a general idea, didn't try it in a compiler. You'll want to code a function to load a animation from a text file or custom filetype, and to load and fill the structure with the proper delays and images.

This is simular to what a article I read a while back did. I don't remember the article's name, though. Hope it helps give you ideas on how to do it in your project.

Share this post

Link to post
Share on other sites
Original post by BS-VecVidjeno
What is the best way and simplest of achieving animation in SDL?

What do you mean by animation? Do you mean an image that changes, or do you mean a moving object?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!