   [ SDL ] Animation ingame with FPS
  Hello everyone!I'm rebuilding my small wrapper I always test things on and I wanted to implement animations, I had this before but because back then I didn't had the slightest idea how to do it I deleted it. But the way I had it planned before was an image containing all the frames of the animation.However here comes the problem, what would be the proper way to handle animation?I learned a lot a while back from the Lazyfoo tutorials, and I'm using his way to deal with time/FPS.I tried to look for tutorials on this but I found really none that goes into detail how this can be done best.I'm more of a 2d guy for now, I don't want to go 3d until I grasped more of the 2d world and made at least a few games I can be happy with. ;]Normally I'd call for the wrapper's Graphics module method, say:drawAnimation( int bufferID, int xpos, int ypos );I store images through Vector. A few weeks ago I gave this a try but I have no idea how to limit the frames to be displayed a certain frames. Say if the game is going 30fps, I'd like the animation to go say.. 5fps, a regular framerate for many 2d games I seen in the past. (The sprite moves 4~5 times a second when standing still, breathing/pose)While writing this I did stumble on a small idea of running a 2nd timer, hm. I'll have to work it out when I get back. I have to head out in a few, I just thought I'd consult the pro's on here incase I don't figure it out with the thought that just popped in my mind. ;pThanks in advance for any help/replies, I'll check back in a few hours! ;]   
  Since you are using the LazyFoo method for time-keeping, I am assuming you are using SDL. I would recommend loading in an image with all of the frames of the animation you want, and lining up those frames in the image so that each frame takes up the same amount of space. Then you can create a structure / class such as thisclass Animation{public:Animation( char* imagename, int frame_width, int frame_height, int frame_count, float frame_length );~Animation();void Update( float timeslice );void Render( int x, int y );private:SDL_Surface* Image;int FrameWidth;int FrameHeight;int FrameCount;float FrameLength;int CurrentFrame;float CurrentFrameTime;};The constructor should load in the image and set Image to the SDL_Surface* of the image, then set the FrameWidth, FrameHeight, FrameCount, and FrameLength. It should also set CurrentFrame to 0 and CurrentFrameTime to 0.0fThe destructor should just clean up the SDL_Surface* memory.The Update function should do the following:- Increment CurrentFrameTime with the time slice. If CurrentFrameTime >= FrameLength, increment CurrentFrame by 1 and decrement CurrentFrameTime by FrameLength.- If CurrentFrame >= FrameCount, set CurrentFrame to 0The Render function should render the part of the image that is made by the rectangle:- Left: CurrentFrame * FrameWidth- Right: CurrentFrame * FrameWidth + FrameWidth- Top: 0- Bottom: FrameHeightI might be missing something, but that is pretty much simple animation in a nutshell.   