Spacefight – Sprites and animations – part 3

Sprites. The small animated objects are essential to a 2D game. My game will contain quite a number of sprites like enemies, bullets and of course the the player. There are different approaches how to arrange your sprites. Some developers prefer to have a sprite sheet. Which means the animations is read in from one file, divided into rectangles. But I want to have a few files for the animation and call them enemy01.png, enemy02.png and so on. I don’t know whether this is more expensive than reading in from a sprite sheet, but it should work. If you know any drawback from reading in a lot of files, please let me know. Here’s an example of an enemy in the game:

For most graphics in the game I’ve been using Inkscape and Gimp, neither programs I’m very good at, or familiar with. I’m too used to Adobe Photoshop and Illustrator. (Still waiting for the big Linux commitment, Adobe!) Hence the enemies looks.

Now I need to put the animation together. So I created an animation class. It contains a vector (a list/hashlist) that contains pointers to SDL_Surfaces. The class also uses some variables to keep track on the time in ms and a FPS value. This is how the animation class ended up.

And this is how it’s being used:

This will create a pointer to a new animation and it loads the files. What the getFrame() does is that it checks when it was called last time, how much time has passed and what pointer to an SDL_Surface to deliver. It’s also calling delete after it’s finished with it. The destructor of the object will clean up all the loaded images loaded in the animation. I also added a function that clones an animation and flip it either horizontal or vertical. This will be handy when you need to reuse an animation that just needs to be mirrored. Of a function for mirroring an image is not part of SDL, so I followed Lazy Foo’s guide Pixel Manipulation and Surface Flipping to write a mirror function by reading pixels and invert their position.

So this is all fine and dandy. But imagine if you have a character in your game walking to the right, it would look pretty silly (see: moonwalk) if you use the same animation when the character moves to the left. So I need a few animations per object in the game.


I decided to create an animation library that all important objects in the game has access to. It’s an object that makes use of a dictionary as a key that is a string and a value that is a pointer to an animation. So each object can request a certain animation at a certain time, and also share same animation. This works pretty much like this:

I will probably put the code that loads all images, in the objects that will be using them. So let say I have an object called Sven. Sven will load the images and create animations for the entity Sven. Sven will save the animations in the AnimationLibrary with some tags so it knows how to access them later on.

Now I have a good way of handling animations in the game. But still they won’t move around on the screen. I need a generic object that holds the objects postion, state and other important values. Next up specifing the GameObject.

0 Comments on “Spacefight – Sprites and animations – part 3

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.