Gama C Library
Gama C API Documentation
sprite.h File Reference

Defines structures and functions for managing 2D sprites and animations. More...

#include "image.h"
#include <stdint.h>
#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  gmSpriteAnim
 Structure representing a sprite animation sequence. More...
struct  gmSprite
 Structure representing a sprite with animation capabilities. More...

Macros

#define GAMA_MAX_SPRITE_ANIM_LENGTH   10
 Defines the maximum number of frames an animation sequence can have.

Functions

gmSpriteAnim gm_sprite_anim_browse (int n_sprites, double interval)
 Creates a sprite animation that sequentially browses through all available frames.
gmSpriteAnim gm_sprite_anim_create (double interval, const char *pattern)
 Creates a sprite animation based on a pattern string.
gmSprite gm_sprite_create (gmImage img, int n_frames)
 Creates a new sprite from an image with a specified number of frames.
void gm_sprite_update_dt (gmSprite *sprite, double dt)
 Updates the sprite's animation state based on elapsed time.
void gm_sprite_draw (gmSprite *sprite, double x, double y, double width, double height)
 Draws the current frame of a sprite at the specified position and size.

Detailed Description

Defines structures and functions for managing 2D sprites and animations.

This file provides utilities for creating animated sprites from sprite sheets, controlling their animation, and drawing them to the screen.

Macro Definition Documentation

◆ GAMA_MAX_SPRITE_ANIM_LENGTH

#define GAMA_MAX_SPRITE_ANIM_LENGTH   10

Defines the maximum number of frames an animation sequence can have.

Function Documentation

◆ gm_sprite_anim_browse()

gmSpriteAnim gm_sprite_anim_browse ( int n_sprites,
double interval )

Creates a sprite animation that sequentially browses through all available frames.

This function creates a simple animation where each frame of the sprite sheet is played in sequential order from 0 to n_sprites - 1.

Parameters
n_spritesThe total number of frames in the sprite sheet.
intervalThe time interval in seconds between each frame.
Returns
A new gmSpriteAnim instance.

◆ gm_sprite_anim_create()

gmSpriteAnim gm_sprite_anim_create ( double interval,
const char * pattern )

Creates a sprite animation based on a pattern string.

Each character in the pattern string corresponds to a frame index, where 'a' maps to frame 0, 'b' to frame 1, and so on. This allows for custom animation sequences.

Parameters
intervalThe time interval in seconds between frames.
patternA string where each character represents a frame index (e.g., "abcba" for a looping animation).
Returns
A new gmSpriteAnim instance.

◆ gm_sprite_create()

gmSprite gm_sprite_create ( gmImage img,
int n_frames )

Creates a new sprite from an image with a specified number of frames.

This function initializes a sprite from a gmImage that is assumed to be a sprite sheet. The n_frames parameter specifies how many equal-width frames are contained horizontally within the image.

Parameters
imgThe gmImage containing the sprite sheet.
n_framesThe number of animation frames horizontally arranged in the sprite sheet.
Returns
A new gmSprite instance.

◆ gm_sprite_draw()

void gm_sprite_draw ( gmSprite * sprite,
double x,
double y,
double width,
double height )

Draws the current frame of a sprite at the specified position and size.

This function automatically calculates the correct slice of the sprite sheet to draw based on the current animation frame.

Parameters
spritePointer to the sprite to draw.
xThe x-coordinate of the center of the sprite.
yThe y-coordinate of the center of the sprite.
widthThe width to draw the sprite.
heightThe height to draw the sprite.

◆ gm_sprite_update_dt()

void gm_sprite_update_dt ( gmSprite * sprite,
double dt )

Updates the sprite's animation state based on elapsed time.

Parameters
spritePointer to the sprite to update.
dtDelta time since the last update.