Operation Null Mind 0.4.1
A 2D roguelike top down shooter game made with SDL2
 
Loading...
Searching...
No Matches
animation.h File Reference

A simple animation system capable of loading a spritesheet with fixed sized frames. More...

#include <SDL.h>
#include <stdbool.h>
#include <vec2.h>

Go to the source code of this file.

Classes

struct  AnimationFrame
 Represents a single frame in an animation. More...
 
struct  AnimationClip
 Represents an animation clip, which is a sequence of frames. More...
 
struct  AnimationClipData
 Data structure for defining an animation clip. More...
 
struct  Animation
 The main animation structure. More...
 
struct  AnimationData
 Configuration data for creating an animation. More...
 

Typedefs

typedef struct AnimationFrame AnimationFrame
 Represents a single frame in an animation.
 
typedef struct AnimationClip AnimationClip
 Represents an animation clip, which is a sequence of frames.
 
typedef struct AnimationClipData AnimationClipData
 Data structure for defining an animation clip.
 
typedef struct Animation Animation
 The main animation structure.
 
typedef struct AnimationData AnimationData
 Configuration data for creating an animation.
 

Functions

Animation * Animation_Create (AnimationData *data)
 Creates a new animation from animation data.
 
void Animation_Destroy (Animation *animation)
 Destroys an animation and frees its memory.
 
int Animation_AddClipFromGrid (Animation *animation, const char *name, int startFrameIndex, int endFrameIndex, float frameDuration, bool looping)
 Adds a new animation clip from a grid-based spritesheet.
 
void Animation_Play (Animation *animation, const char *clipName)
 Plays an animation clip by name.
 
void Animation_Stop (Animation *animation)
 Stops the animation.
 
void Animation_Pause (Animation *animation)
 Pauses the animation.
 
void Animation_Resume (Animation *animation)
 Resumes the animation.
 
void Animation_Update (Animation *animation)
 Updates the animation.
 
void Animation_Render (Animation *animation, Vec2 destPosition, Vec2 destSize, float angle, SDL_Point *rotationCenter, SDL_RendererFlip flip)
 Renders the animation.
 
bool Animation_ClipIsFinished (Animation *animation)
 

Detailed Description

A simple animation system capable of loading a spritesheet with fixed sized frames.

The spritesheet you select should have all of its states on it, like running, walking etc.

Warning
This system doesn't support loading animation clips from multiple files into one animation.

Usage

To load an animation, you need 3 steps:

Step 1: Create animation with an AnimationData struct

An example of an AnimationData struct can be found in src/Game/Player/player_data.c

After that, you can call:

Animation* animation = Animation_Create(data);
Animation * Animation_Create(AnimationData *animData)
Creates a new animation from animation data.
Definition animation.c:30
The main animation structure.
Definition animation.h:83

Step 2: Play the animation based on a certain state

Animation_Play(animation, "running");
void Animation_Play(Animation *animation, const char *clipName)
Plays an animation clip by name.
Definition animation.c:221

Step 3: Update and render the animation every frame

// Inside update loop
Animation_Update(animation);
// Inside render loop
animation, // The animation
(Vec2) {x, y}, // The position to render
(Vec2) {w, h}, // The size to render
angle, // Rotation angle in degrees
NULL, // The rotation center, NULL for center
SDL_FLIP_NONE // Flip the sprite, SDL_FLIP_NONE for no flip
);
void Animation_Update(Animation *animation)
Updates the current animation frame.
Definition animation.c:75
void Animation_Render(Animation *animation, Vec2 destPosition, Vec2 destSize, float angle, SDL_Point *rotationCenter, SDL_RendererFlip flip)
Renders the current frame of an animation.
Definition animation.c:276
A 2D vector structure.
Definition vec2.h:81
Author
Mango
Date
2025-03-01

Typedef Documentation

◆ Animation

typedef struct Animation Animation

The main animation structure.

◆ AnimationClip

typedef struct AnimationClip AnimationClip

Represents an animation clip, which is a sequence of frames.

◆ AnimationClipData

Data structure for defining an animation clip.

◆ AnimationData

typedef struct AnimationData AnimationData

Configuration data for creating an animation.

◆ AnimationFrame

Represents a single frame in an animation.

Function Documentation

◆ Animation_AddClipFromGrid()

int Animation_AddClipFromGrid ( Animation *  animation,
const char *  name,
int  startFrameIndex,
int  endFrameIndex,
float  frameDuration,
bool  looping 
)

Adds a new animation clip from a grid-based spritesheet.

Parameters
animationThe animation to add the clip to.
nameThe name of the clip.
startFrameIndexThe index of the first frame.
endFrameIndexThe index of the last frame.
frameDurationThe duration of each frame in seconds.
loopingWhether the animation should loop.
Returns
The index of the newly added clip.

Adds a new animation clip from a grid-based spritesheet.

Creates a new clip by extracting frames from the spritesheet using a grid-based layout. Frame indices start from 0, reading from left to right, then top to bottom.

Parameters
animationThe animation to add the clip to
nameName of the clip (e.g., "idle", "walk")
startFrameIndexIndex of the first frame in the clip
endFrameIndexIndex of the last frame in the clip
frameDurationTime between frames in seconds
loopingWhether the animation should loop
Returns
int 0 on success, 1 on failure

◆ Animation_ClipIsFinished()

bool Animation_ClipIsFinished ( Animation *  animation)

◆ Animation_Create()

Animation * Animation_Create ( AnimationData *  animData)

Creates a new animation from animation data.

Parameters
dataThe animation data.
Returns
A pointer to the newly created animation.

This only works with spritesheets with constant frame size. After creating an animation, calling Animation_AddClipFromGrid can add additional animation clips.

Parameters
animDataPointer to the animation data configuration
Returns
Animation* Pointer to the new animation, or NULL if failed

◆ Animation_Destroy()

void Animation_Destroy ( Animation *  animation)

Destroys an animation and frees its memory.

Parameters
animationThe animation to destroy.

Destroys an animation and frees its memory.

Parameters
animationPointer to the animation to destroy

◆ Animation_Pause()

void Animation_Pause ( Animation *  animation)

Pauses the animation.

Parameters
animationThe animation to pause.

Pauses the animation.

Parameters
animationThe animation to pause

◆ Animation_Play()

void Animation_Play ( Animation *  animation,
const char *  clipName 
)

Plays an animation clip by name.

Parameters
animationThe animation to play.
clipNameThe name of the clip to play.

If the target clip is already playing, this function does nothing.

Parameters
animationThe animation to play
clipNameThe name of the clip to play

◆ Animation_Render()

void Animation_Render ( Animation *  animation,
Vec2  destPosition,
Vec2  destSize,
float  angle,
SDL_Point *  rotationCenter,
SDL_RendererFlip  flip 
)

Renders the animation.

Parameters
animationThe animation to render.
destPositionThe position to render the animation at.
destSizeThe size to render the animation at.
angleThe angle to rotate the animation.
rotationCenterThe center of rotation. NULL for center.
flipThe flip mode for the sprite.

Renders the animation.

Parameters
animationThe animation to render
destPositionPosition where the animation should be drawn
destSizeSize of the drawn animation
angleRotation angle in degrees
rotationCenterCenter point for rotation
flipFlip flags for rendering (horizontal/vertical)

◆ Animation_Resume()

void Animation_Resume ( Animation *  animation)

Resumes the animation.

Parameters
animationThe animation to resume.

Resumes the animation.

Parameters
animationThe animation to resume

◆ Animation_Stop()

void Animation_Stop ( Animation *  animation)

Stops the animation.

Parameters
animationThe animation to stop.

Stops the animation.

Parameters
animationThe animation to stop

◆ Animation_Update()

void Animation_Update ( Animation *  animation)

Updates the animation.

Parameters
animationThe animation to update.

Updates the animation.

This function should be called every frame to advance animations. It handles transitions between frames, respecting frame durations and looping settings.

Parameters
animationPointer to the animation to update