Animation

“Animation, switching between pictures in a spritesheet, should not be that hard.”

A simple statement, however when I started working on our AnimationManager I realised just how wrong it was. At least in my case, where I tried to create an “Animation” class and then a “AnimationManager” to hold all the different Animations.

My original thought was that in the AnimationManager, there would be a vector with all the different animations, and that all entities would then have their own little map with their individual animations (which they fetched from the AnimationManager), just so the program would not need to load all the animations over and over again.

Well the thought was not that bad and not too far off what I ended up with, however there was a little problem with the loading of animations. Since it needed the SpriteManager to actually load the images, and I did not want to include the SpriteManager all over the place.

HOW IT IS DONE, SDL STYLE

So I went back to thinking of ways to do it. Then I got a tip from a fellow classmate to check out our teachers SDL animation class, since I had not looked at it before I thought it might not be a dumb idea to get some inspiration.

SDLanimation

This is a overview of the SDL animation. Where you have a “Animation” class in the form of “AnimatedSprite”, and this class stores one animation. This animation is a bulk of frames with different X and Y values as well as a width and height. These “Frames” represent different squares on the spritesheet which will change the appearances of the sprite by moving the texture coordinates on the spritesheet, so that it fetches a new picture.

For example:

frameexample

Frame 1 (red) will have coordinates:
X = 0
Y = 0
Width = 65
Height = 65

Frame 2 (blue) will have the coordinates:
X = 65
Y = 0
Width = 65
Height = 65

And so on, where you increment the frames with the width and height of the previous frame. Well I took this concept and adjusted it to fit into our game and with the media library SFML.

HOW IT IS DONE, ZWÖLF STYLE

Here is a overview of our Animation system:

AnimspriteEntity entitymgrSpritemgr

Our system is based on the fact that every entity has a map of animatedsprites, with an enum as key. Cleverly named “AnimationName”. This map of animations is filled in the EntityManager (which already have a pointer to the spritemanager), during the “AttachEntity()” function. Where the “AddAnimation()” function is called to return an AnimatedSprite pointer.

The parameters to the “AddAnimation()” function is:

The filename for the spritesheet
The number of frames in the animation
How many columns there is in the animation (so that the spritemanager know when to break for a new row)
The width & height of the frame
The startposition in X and Y (on the spritesheet)

AnimspriteSpriteMgr

In these functions you see how the creation of the AnimatedSprites are actually processed, and why we used those previous parameters in the EntityManager. They are used within the fields of the SpriteManager, where we check if we have already loaded the spritesheet (we do not want to load the same texture several times). Then within the green field in the SpriteManager we create a frame and add it to the local AnimatedSprite pointer through it’s AddFrame() function. Then when the loop has reached the number of frames we wanted, it returns the local AnimatedSprite pointer.

In the AnimatedSprite we find another important function, the Update(). This function is essential to be sure our animation actually becomes an animation, since this is the function which changes the frame when the duration of the frame has been reached. With the “SetTexture()” function from the sf::Sprite class.

Well this would be all, if there is any unclarities please comment with any questions and I will be sure to answer in due time.