Gama C Library
Gama C API Documentation
sprite.h
Go to the documentation of this file.
1/**
2 * @file sprite.h
3 * @brief Defines structures and functions for managing 2D sprites and animations.
4 *
5 * This file provides utilities for creating animated sprites from sprite sheets,
6 * controlling their animation, and drawing them to the screen.
7 */
8#pragma once
9#include "image.h"
10#include <stdint.h>
11#include <stdlib.h>
12/**
13 * @def GAMA_MAX_SPRITE_ANIM_LENGTH
14 * @brief Defines the maximum number of frames an animation sequence can have.
15 */
16#ifndef GAMA_MAX_SPRITE_ANIM_LENGTH
17#define GAMA_MAX_SPRITE_ANIM_LENGTH 10
18#endif
19
20/**
21 * @brief Structure representing a sprite animation sequence.
22 */
23typedef struct {
24 double interval; /**< Time interval in seconds between animation frames. */
25 int8_t anim[GAMA_MAX_SPRITE_ANIM_LENGTH]; /**< Array of frame indices in the
26 animation sequence. These are indices
27 into the sprite sheet's total frames. */
28 size_t length; /**< Number of frames in the animation sequence. */
30
31/**
32 * @brief Creates a sprite animation that sequentially browses through all available frames.
33 *
34 * This function creates a simple animation where each frame of the sprite sheet
35 * is played in sequential order from 0 to `n_sprites - 1`.
36 *
37 * @param n_sprites The total number of frames in the sprite sheet.
38 * @param interval The time interval in seconds between each frame.
39 * @return A new `gmSpriteAnim` instance.
40 */
41gmSpriteAnim gm_sprite_anim_browse(int n_sprites, double interval) {
42 gmSpriteAnim anim;
43 anim.interval = interval;
44 anim.length = n_sprites;
45 for (int i = 0; i < n_sprites; i++)
46 anim.anim[i] = i;
47
48 return anim;
49}
50
51/**
52 * @brief Creates a sprite animation based on a pattern string.
53 *
54 * Each character in the `pattern` string corresponds to a frame index,
55 * where 'a' maps to frame 0, 'b' to frame 1, and so on. This allows for
56 * custom animation sequences.
57 *
58 * @param interval The time interval in seconds between frames.
59 * @param pattern A string where each character represents a frame index (e.g., "abcba" for a looping animation).
60 * @return A new `gmSpriteAnim` instance.
61 */
62gmSpriteAnim gm_sprite_anim_create(double interval, const char *pattern) {
63 gmSpriteAnim anim;
64 int i;
65
66 for (i = 0; pattern[i] != '\0'; i++) {
67 anim.anim[i] = (int)pattern[i] - (int)'a';
68 }
69 anim.length = i;
70 anim.interval = interval;
71 return anim;
72}
73
74/**
75 * @brief Structure representing a sprite with animation capabilities.
76 *
77 * A sprite consists of an image (sprite sheet) and defines how to play
78 * an animation sequence from that sheet.
79 */
80typedef struct {
81 double _backlog_t; /**< Internal time accumulator for animation timing. */
82
83 gmSpriteAnim animation; /**< The animation sequence for the sprite. */
84
85 size_t n_frames; /**< Total number of frames in the sprite sheet. */
86 size_t animation_frame; /**< Current frame index in the `animation.anim` sequence. */
87 size_t _frame; /**< Current actual frame index from the sprite sheet to display. */
88
89 gmImage image; /**< The image containing the sprite sheet. */
90} gmSprite;
91
92/**
93 * @brief Creates a new sprite from an image with a specified number of frames.
94 *
95 * This function initializes a sprite from a `gmImage` that is assumed to be
96 * a sprite sheet. The `n_frames` parameter specifies how many equal-width
97 * frames are contained horizontally within the `image`.
98 *
99 * @param img The `gmImage` containing the sprite sheet.
100 * @param n_frames The number of animation frames horizontally arranged in the sprite sheet.
101 * @return A new `gmSprite` instance.
102 */
104 gmSprite s;
105 s._backlog_t = 0;
106 s.n_frames = n_frames;
107 s.animation = gm_sprite_anim_browse(n_frames, 0.1);
108 s.image = img;
109 s.animation_frame = 0;
110 s._frame = 0;
111 return s;
112}
113
114/**
115 * @brief Updates the sprite's animation state based on elapsed time.
116 * @param sprite Pointer to the sprite to update.
117 * @param dt Delta time since the last update.
118 */
119void gm_sprite_update_dt(gmSprite *sprite, double dt) {
120 sprite->_backlog_t += dt;
121 while (sprite->_backlog_t >= sprite->animation.interval) {
122 sprite->_backlog_t -= sprite->animation.interval;
123 sprite->animation_frame++;
124 sprite->animation_frame %= sprite->animation.length;
125 sprite->_frame = sprite->animation.anim[sprite->animation_frame];
126 }
127}
128
129/**
130 * @brief Updates the sprite's animation state using the global delta time.
131 * @param sprite Pointer to the sprite to update.
132 */
133static inline void gm_sprite_update(gmSprite *sprite) {
134 gm_sprite_update_dt(sprite, gm_dt());
135}
136
137/**
138 * @brief Draws the current frame of a sprite at the specified position and
139 * size.
140 *
141 * This function automatically calculates the correct slice of the sprite sheet
142 * to draw based on the current animation frame.
143 *
144 * @param sprite Pointer to the sprite to draw.
145 * @param x The x-coordinate of the center of the sprite.
146 * @param y The y-coordinate of the center of the sprite.
147 * @param width The width to draw the sprite.
148 * @param height The height to draw the sprite.
149 */
150void gm_sprite_draw(gmSprite *sprite, double x, double y, double width,
151 double height) {
152 size_t im_w = sprite->image.width / sprite->n_frames;
153 gm_image_draw_part(sprite->image, im_w * sprite->_frame, 0, im_w,
154 sprite->image.height, x, y, width, height);
155}
void gm_image_draw_part(gmImage i, int slice_x, int slice_y, int slice_width, int slice_height, double x, double y, double w, double h)
Draws a rectangular sub-region of an image.
Definition image.h:171
gmSpriteAnim gm_sprite_anim_browse(int n_sprites, double interval)
Creates a sprite animation that sequentially browses through all available frames.
Definition sprite.h:41
gmSpriteAnim gm_sprite_anim_create(double interval, const char *pattern)
Creates a sprite animation based on a pattern string.
Definition sprite.h:62
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.
Definition sprite.h:150
#define GAMA_MAX_SPRITE_ANIM_LENGTH
Defines the maximum number of frames an animation sequence can have.
Definition sprite.h:17
gmSprite gm_sprite_create(gmImage img, int n_frames)
Creates a new sprite from an image with a specified number of frames.
Definition sprite.h:103
void gm_sprite_update_dt(gmSprite *sprite, double dt)
Updates the sprite's animation state based on elapsed time.
Definition sprite.h:119
A handle to a GPU-managed image or texture.
Definition image.h:97
int width
Definition image.h:99
int height
Definition image.h:100
Structure representing a sprite animation sequence.
Definition sprite.h:23
int8_t anim[GAMA_MAX_SPRITE_ANIM_LENGTH]
Definition sprite.h:25
double interval
Definition sprite.h:24
size_t length
Definition sprite.h:28
Structure representing a sprite with animation capabilities.
Definition sprite.h:80
size_t n_frames
Definition sprite.h:85
size_t animation_frame
Definition sprite.h:86
gmImage image
Definition sprite.h:89
gmSpriteAnim animation
Definition sprite.h:83
size_t _frame
Definition sprite.h:87
double _backlog_t
Definition sprite.h:81