Page 1 of 1

Dynamic Animation?

Posted: Mon Mar 30, 2009 8:56 pm
by dandymcgee
I'm just curious how everyone else here handles animation in their games? I know plenty of ways to hard code animation and tile sheet clips, but I'm trying to create something a little more dynamic. Not every animation will have the same number of frames, or do the same thing (ie. a character will have walking animations, maybe attacking animations, whereas water might just have 2 or 3 frames).

Ultimately characters will be created from a Lua script, so I want something that will allow the scripter to pass a filename, the number of frames, what frames are used for what or something like that (I don't need code for binding Lua, I know how to do this and I'll handle it later).

The only other way I could think of to do this, would be to handle animation from the actual Lua script, where the scripter would use input (ie. left arrow key) and then call character:setframe(int frame) themselves.. maybe?

This is my pseudo class that I was messing around with for ideas:

Code: Select all

#ifndef CHARACTER_H_INCLUDED
#define CHARACTER_H_INCLUDED

struct Animation{
    public:
        std::string name;
        int startindex;
        int frames;
};

class Character{
    private:
        std::string name;
        int x;
        int y;
        int w;
        int h;
        int frame;
        //slightly more dynamic approach, merely pseudocode
        std::vector<Animation> animData;
        //hard coded variables for each direction
        //int rightanimstart;
        //int rightanimframes;
        //int leftanimstart;
        //int leftanimframes;
        //int downanimstart;
        //int downanimframes;
        //int upanimstart;
        //int upanimframes;
        int surface;
    public:
        Character( std::string filename );
        ~Character();
        //current function user just sets x and y coords for a single image
        void Move( int newx, int newy );
        int Draw();
};

#endif // CHARACTER_H_INCLUDED
How do you guys handle this? Feel free to post anything you feel is relevant. I've never really done anything other than hard coded sprite clipping and animation, so if my attempts are way off point it out please. ;)

EDIT: Also, this is sort of an abstract future reference question, but I will eventually need to store some sort of animated tile data, so if you have recommendations as to how you store animations in your level files that would be helpful too.

Re: Dynamic Animation?

Posted: Mon Mar 30, 2009 11:26 pm
by kostiak2
The one thing I would not recommend doing is storing a string to a file or to whatever. (correct me if I'm wrong) but you seem to be going to search for a string every frame, this is a very bad idea.

Searching strings is very costly and can be a real drain on the performance of your game (believe me, I was doing that myself on my game at first). What you should do instead is at least store a pointer to SDL_Surface so you can easily access the data needed for rendering without doing a string search.

An even better idea is to make some kind of a wrapper class for SDL_Surface, that will make your life easier in terms of bliting and cliping.

Re: Dynamic Animation?

Posted: Tue Mar 31, 2009 3:52 pm
by dandymcgee
kostiak2 wrote:The one thing I would not recommend doing is storing a string to a file or to whatever. (correct me if I'm wrong) but you seem to be going to search for a string every frame, this is a very bad idea.

Searching strings is very costly and can be a real drain on the performance of your game (believe me, I was doing that myself on my game at first). What you should do instead is at least store a pointer to SDL_Surface so you can easily access the data needed for rendering without doing a string search.

An even better idea is to make some kind of a wrapper class for SDL_Surface, that will make your life easier in terms of bliting and cliping.
Lol, what? First of all this was complete pseudo code and I'm not actually using it for anything. The string in the animation struct (I'm assuming this is the string you speak of?) was just to store the name of the animation (ie. "right", "left", etc.). The int surface stores the index of the SDL_Surface in the vector of surfaces inside the render class (when you want to draw you pass this int to the Render class).

Anyways, thanks for trying to understand and taking the time to reply. I appreciate the effort. 8-)

Re: Dynamic Animation?

Posted: Tue Mar 31, 2009 4:27 pm
by kostiak2
dandymcgee wrote: Lol, what? First of all this was complete pseudo code and I'm not actually using it for anything. The string in the animation struct (I'm assuming this is the string you speak of?) was just to store the name of the animation (ie. "right", "left", etc.). The int surface stores the index of the SDL_Surface in the vector of surfaces inside the render class (when you want to draw you pass this int to the Render class).

Anyways, thanks for trying to understand and taking the time to reply. I appreciate the effort. 8-)
so you are storing the name of the animation, for what? are you searching for it anywhere? what are you using that for?

about the int surface, you better store a pointer to the SDL_Surface, it's faster to access it that way than do vectorListName[surface].

Re: Dynamic Animation?

Posted: Tue Mar 31, 2009 4:31 pm
by dandymcgee
kostiak2 wrote: so you are storing the name of the animation, for what? are you searching for it anywhere? what are you using that for?
Once again, I am not using this code for anything at all. It was a bunch of ideas I threw together in hope of it helping someone understand what I was trying to do a bit better.
kostiak2 wrote: about the int surface, you better store a pointer to the SDL_Surface, it's faster to access it that way than do vectorListName[surface].
I didn't know which was faster, I can easily change it back and if this is true then perhaps I will. :)

Re: Dynamic Animation?

Posted: Tue Mar 31, 2009 4:39 pm
by wtetzner
dandymcgee wrote: I didn't know which was faster, I can easily change it back and if this is true then perhaps I will. :)
The speed difference will be negligible, so I'd recommend using they way that is easiest for you to read and work with.

Re: Dynamic Animation?

Posted: Tue Mar 31, 2009 4:52 pm
by dandymcgee
Okay so I was talking to andrew and thinking about this..

I'm thinking I'll just allow my engine to do the actual clipping, but all of the coordinates will be sent as parameters from a Lua script, and the scripter will manually set each frame of animation when input it found. Perhaps this will be very slow an inefficient calling a function to update every frame? If anyone has a better idea.. please post! :mrgreen:

Re: Dynamic Animation?

Posted: Tue Mar 31, 2009 7:33 pm
by Ginto8
Well, though I haven't actually made the animation for my game yet, I have the basic plan for it. ;)

I have a Sprite class. Everything using a normal sprite inherits from this class. I will have an AnimatedSprite class, which inherits from Sprite. Sprite has data on texture width and clip size, and AnimatedSprite will have a timer. This timer will be checked in a refresh() function and if it is past/at the time designated for animation switching, it will move its clip over by [clip width], until it reaches the end of the texture, at which point it will go over to 0. So anything with animation will inherit from AnimatedSprite.

And that's what I'm going to do for animation in my game engine. ;) Hopefully it has few enough design flaws for you. :lol:

Re: Dynamic Animation?

Posted: Tue Mar 31, 2009 9:57 pm
by MarauderIIC
Short comment - We (will) pull sprite width/height from a .txt file that is named the same as the image file (minus the extension). We (already do) pull # of frames for each animation in the same spritesheet from the file. The animations must be in the same order every time, but they can be 0 frames long.