Entities / Animations / Sprites


timbobsteve

Member
Joined
Oct 4, 2005
Messages
301
Hi All,

I'm currently trying to extend my little SDL demo to include animations and I'm wondering how best to implement the classes to achieve this. Currently I have the following:

Engine
Entity
Player

The Engine runs the game loop and has a member "vector<Entity*> EntityList". On each loop it iterates through EntityList and calls EntityList[x].Update() and EntityList[x].Render(SDL_Surface* screen).

Currently the player has it's own SDL_Surface* that is just a blue square and just draws itself to the surface passed in Render(SDL_Surface*).

I want to throw animations into the mix, so I was thinking of having a new class called Sprite. In this class I would load a single animation (say player_walk.png) and set the size of each frame. I was going to do this for each animation that the player has to do, e.g.

Code:
class Player {
....
private:
    Sprite* currentAnimation;
    Sprite  walk;
    Sprite  jump;
    Sprite  idle;
....
};
Then on Update() I could set the *currentAnimation to be &walk if walking, &idle if idle etc. All the frame update stuff was going to be done in the Sprite class itself and the Player's Render(SDL_Surface*) function was just going to call currentAnimation->GetFrame() to return the current frame based on the animations position.

What do people think of this way of handling animation? Is it a bad/dodgy way of implementing animation? Are there better or easier methods?

Also, in the Sprite::Load() function I was going to pass a filename. In the file I would define all the sprite attributed, like frame speed, number of frames, width, height etc. Do you think having these values outside of the code is a good idea, or am I just asking for issues?

I appreciate any and all input, but be warned... I might not understand you :p

Cheers,
Dave / Timbobsteve

PS: If these entry-level queries are clogging up the Development thread, perhaps we can arrange a sub-forum of "Development" for the n00bs to play around in? Just a thought.
 
I used to have a great way of doing this. I'd have a class with a std::vector of SDL_Surfaces, some integers and two functions.

I'd then have a text file, first line would be the total number of frames, then the time between each frame in ms then in each following line I would list the filenames of the frames. (Kinda like this)

Code:
10 //10 frames
100 //100ms between each frame
sprites/walk1.png
sprites/walk2.png
sprites/walk3.png
sprites/walk4.png
sprites/walk5.png
etc...


I'd load the text file directly from the class, parse it, load each individual frame. Then each time draw is called, the function checks the last time draw was called and works out how long ago that was and what frame the animation should be on now.

Pros:
- Easy for artist to modify animations from outside the game.
- Two functions.
- Simple.
 
Hi Butterman,

That's pretty much identical to what I was starting to implement, except I wouldn't parse each frame as a separate file, I'd instead have 1 sprite-sheet for each animation (e.g walk, jump, idle). So it looks like this method isn't as crazy as I first thought :p

Cheers,
Dave / Timbobsteve
 
In my engine I load animations from a file, store them as pointers to an animation struct in an std::vector in what I call a game scene.
This way you can have one generic animation used by a lot of different objects.
Especially if your animation data contain relative x-y coordinates, i.e. your animation position data is added to the original source position.
 
Yeah, that's true. I do something similar so I don't have to load duplicate sprites.

All sprites are loaded into the same place, and dupes are ignored. At draw time a sprite can be requested by using its filename.
 
Yup, sounds pretty similar to what I do, works fine. My description file is XML, which is read with TinyXml, although my original implementation (from long ago) was just plain text. Here is an example XML:

<?xml version="1.0" standalone="no" ?>
<animation filename="Run.png" speed="0.06666">
<frames number="12" width="42" height="42" />
<attributes hflip="1" vflip="0" />
<position x="150" y="136" z="17" />
</animation>

There are a few extras like being able to hflip/vflip, which was requested by a guy who did artwork for me straight away, so you can have characters walking left/right with same graphic (for example). The position stuff is optional, and gets used by a standalone animation testing program, that the artist uses for testing sprite sequences (rather than having to load into a game level). Even when not testing, I encourage the artists to setup the z value correctly, as I keep my scene graph baked as much as possible, so changing z depths on the fly isn't encouraged, so loaded sprites will have a default z depth as set in the XML.

Steve
 
I also was using something very similar. Had I known about TinyXML at the the time I would have used that. I think its a pretty neat approach. If done right, you can scale animation frame count/size/whatever without having to recompile. It is almost engine-like. I'm about to re-implement this approach for deving for the Pandora. (I didn't back up my code and lost it in the great hardware/OS upgrade crash of 2010 :-(). I think there are some articles on GameDev.net has a more generic approach for "universal" resource management, so you use this for managing all kinds of content, not just images/animation.
 
Back
Top