Doing 2d Animations - makinggamesinc/easy_engine GitHub Wiki

Doing 2d Animations

The code for the 2d animations are found in easy_animation.h file.

There are two types that you should be aware of when using the system.

The first is an Animation. This represents a list of filenames that is a single animation, say the walk cycle of a character. You would have another animation for an attack or a jump.

To create an animation you pass a list of asset file names that are the images to the following function:

int easyAnimation_initAnimation(Animation *animation, char **fileNames, u32 fileNameCount, char *name);

This function will return the Id of the animation which you can use to search with later. You can also search later using the name parameter you pass to this function. The animation you pass this function is the one your initializing. This could exist in a long term memory arena, the stack or on the heap.

These are the two functions that you can later use to search through a list for the animation to load onto a parent are:

Animation *easyAnimation_findAnimationWithId(Animation *animations, u32 AnimationsCount, int id);
Animation *easyAnimation_findAnimation(Animation *Animations, u32 AnimationsCount, char *name);

The second type to be aware of is the animation controller (the type being EasyAnimation_Controller).

This keeps the current state of the animation. While Animation data types can be part of many animation controllers, you would create an animation controller for each animation you are running (eg. you could have multiple ones for each entity in a game).

You would init it like the following:

void easyAnimation_initController(EasyAnimation_Controller *controller);

You would then push animations onto it:

void easyAnimation_addAnimationToController(EasyAnimation_Controller **contoller, Arena *arenaYouWantToAllocateIn, EasyAnimation_Controller *freeList, Animation *animation);

Here you pass the controller your updating, an arena that will a new animation instance in if there aren't any on the freelist, a freelist that keeps track of finished animations and the animation you want to add to the controller.

The freelist can be just an null pointer stored in your game state struct. You can use the same one for all your games animations (i.e. you only need one).