Game dev, Suit’em up – SpriteManager with animated sprites

We created an early SpriteManager in the start of the project but it couldn’t load animated sprites or load sprites from a part in a spritesheet. I have now added this functionality.

I started with creating a wrapper around sf::Sprite and I have created the AnimatedSprite class. I’m not really that proud of the wrapper since I put a lot of virtual functions in there that are only used by AnimatedSprite.

Sprite wrapper

AnimatedSprite_class

After adding the sprite types I added methods in the SpriteManager for loading animated sprites using .txt-files, just as we have learned to do in our first programming course. Here is an example of such a file:

Animation_txt_example

And here is the code for loading it:

SpriteManager_load_animated

There is another function for adding more frames, it’s a bit shorter and looks like this:

SpriteManager_add_animation

Much of the code in these to methods (LoadAnimation() and AddAnimation()) are written twice and I should put this together. I will probably do this in the future but right now there are more pressing matters to attend to in the project.

A frame is a struct within the AnimatedSprite class. It holds these variables: x position, y position, width, height, timer.

The timer counts down when the sprites Update(float p_fDeltaTime) method is called and when it reaches zero the animated sprite changes frame. The timer is reduced with deltatime, like so: timer -= deltatime.

The SpriteManager is pretty robust right now. It saves Sprite pointers in a std::map and the same goes for sf::Texture pointers. When loading any type of sprite, the sf::Texture map re-uses the same file if it exists in the std::map, otherwise it attempts to load it from file.

Loading sprites now takes parameters for location in the spritesheet (texture) and width/height. To set or change this location and width/height the Sprite->setTextureRect(sf::Intrect) function is used. It looks like this:

Sprite->setTextureRect(sf::IntRect(p_iPosX, p_iPosY, p_iWidth, p_iHeight))

Next up is setting and changing the animations of a gameobject sprite. This is done by calling the SetAnimation(const std::string &p_sIdentifier) method. So for example, the player avatar “Barney” is supposed to have different sprites making up all parts his body. His legs is one of these parts. When loading the sprite it first uses LoadAnimated() and then a couple of AddAnimation() for each animation type (i.e. Idle_right, Running_left).

When the player makes Barney run to the right a check is done, then the animation is changed using the SetAnimation(“running_right”) method. The same principle is used for all times when changing animation, be it the avatar, enemies or what ever.

This entry was posted in 5SD023, 5SD033 and tagged , , , , , . Bookmark the permalink.

1 Response to Game dev, Suit’em up – SpriteManager with animated sprites

  1. Well, this is looking great so far! I can’t find anything out of the ordinary that I would have done in a different way.

    I like the way you keep your code structured with each part of the code being separated by an empty line. I personally don’t like to have a prefix on every variable which defines what type of data type the variable is but I see the benefits of using them.

    Something I noticed though is that you’ve mixed English “printf” functions together with comments in Swedish. But that’s a really minor complaint.

    It’s good that you use the struct: “Frame” to define each frame instead of using member variables which dictates what dimensions and position for all the frames. I and my group use member variables and so far we haven’t had any troubles but it’s only a matter of time until we need to have separate durations for each frame.

    Your post has a good structure and it’s easy to follow every step on the way and everything is presented in such a way that nothing is hard or difficult to understand.

    Good job and good luck with the project!
    ~Lead Programmer, Per “Gimmic” Johansson

Leave a comment