Dynamic Animation?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Dynamic Animation?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
kostiak2
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 74
Joined: Tue Mar 24, 2009 4:08 pm

Re: Dynamic Animation?

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Dynamic Animation?

Post 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-)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
kostiak2
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 74
Joined: Tue Mar 24, 2009 4:08 pm

Re: Dynamic Animation?

Post 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].
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Dynamic Animation?

Post 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. :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: Dynamic Animation?

Post 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.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Dynamic Animation?

Post 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:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Dynamic Animation?

Post 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:
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Dynamic Animation?

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply