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

Sprite animation system implementation. More...

#include <animation.h>
#include <stdlib.h>
#include <string.h>
#include <app.h>
#include <time_system.h>

Functions

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

Detailed Description

Sprite animation system implementation.

Implements a flexible animation system that supports spritesheet-based animations with multiple clips, variable frame rates, and playback controls.

Author
Mango
Date
2025-03-03

Function Documentation

◆ Animation_AddClipFromGrid()

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

Adds an animation clip from a spritesheet grid.

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.

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 resources.

Destroys an animation and frees its memory.

Parameters
animationPointer to the animation to destroy

◆ Animation_Pause()

void Animation_Pause ( Animation *  animation)

Pauses the current animation.

Pauses the animation.

Parameters
animationThe animation to pause

◆ Animation_Play()

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

Plays an animation clip by name.

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 current frame of an animation.

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 a paused animation.

Resumes the animation.

Parameters
animationThe animation to resume

◆ Animation_Stop()

void Animation_Stop ( Animation *  animation)

Stops the current animation and resets it.

Stops the animation.

Parameters
animationThe animation to stop

◆ Animation_Update()

void Animation_Update ( Animation *  animation)

Updates the current animation frame.

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