Gama C Library
Gama C API Documentation
Loading...
Searching...
No Matches
sprite.h
Go to the documentation of this file.
1#pragma once
2#include "image.h"
3#include <stdlib.h>
4#ifndef GAMA_MAX_SPRITE_ANIM_LENGTH
5#define GAMA_MAX_SPRITE_ANIM_LENGTH 10
6#endif
7
8/**
9 * @brief Structure representing a sprite animation sequence.
10 */
11typedef struct {
12 double interval; /**< Time interval between animation frames */
13 int anim[GAMA_MAX_SPRITE_ANIM_LENGTH]; /**< Array of frame indices in the animation sequence */
14 size_t length; /**< Number of frames in the animation */
16
17/**
18 * @brief Creates a sprite animation that sequentially browses through frames.
19 * @param n_sprites The number of sprites/frames in the animation.
20 * @param interval The time interval between frames.
21 * @return A new gmSpriteAnim instance.
22 */
23gmSpriteAnim gm_sprite_anim_browse(int n_sprites, double interval) {
24 gmSpriteAnim anim;
25 anim.interval = interval;
26 anim.length = n_sprites;
27 for (int i = 0; i < n_sprites; i++)
28 anim.anim[i] = i;
29
30 return anim;
31}
32
33/**
34 * @brief Creates a sprite animation based on a pattern string.
35 * @param interval The time interval between frames.
36 * @param pattern A string where each character represents a frame index (a=0, b=1, etc.).
37 * @return A new gmSpriteAnim instance.
38 */
39gmSpriteAnim gm_sprite_anim_create(double interval, const char *pattern) {
40 gmSpriteAnim anim;
41 int i;
42
43 for (i = 0; pattern[i] != '\0'; i++) {
44 anim.anim[i] = (int)pattern[i] - (int)'a';
45 }
46 anim.length = i;
47 anim.interval = interval;
48 return anim;
49}
50
51/**
52 * @brief Structure representing a sprite with animation capabilities.
53 */
54typedef struct {
55 double _backlog_t; /**< Internal time accumulator for animation timing */
56
57 gmSpriteAnim animation; /**< Animation sequence for the sprite */
58
59 size_t n_frames; /**< Total number of frames in the sprite sheet */
60 size_t animation_frame; /**< Current frame in the animation sequence */
61 size_t _frame; /**< Current actual frame to display */
62
63 gmImage image; /**< The image containing the sprite sheet */
64} gmSprite;
65
66/**
67 * @brief Creates a new sprite from an image with a specified number of frames.
68 * @param img The image containing the sprite sheet.
69 * @param n_frames The number of frames in the sprite sheet.
70 * @return A new gmSprite instance.
71 */
72gmSprite gm_sprite_create(gmImage img, int n_frames) {
73 gmSprite s;
74 s._backlog_t = 0;
75 s.n_frames = n_frames;
76 s.animation = gm_sprite_anim_browse(n_frames, 0.1);
77 s.image = img;
78 s.animation_frame = 0;
79 s._frame = 0;
80 return s;
81}
82
83/**
84 * @brief Updates the sprite's animation state based on elapsed time.
85 * @param sprite Pointer to the sprite to update.
86 * @param dt Delta time since the last update.
87 */
88void gm_sprite_update_dt(gmSprite *sprite, double dt) {
89 sprite->_backlog_t += dt;
90 while (sprite->_backlog_t >= sprite->animation.interval) {
91 sprite->_backlog_t -= sprite->animation.interval;
92 sprite->animation_frame++;
93 sprite->animation_frame %= sprite->animation.length;
94 sprite->_frame = sprite->animation.anim[sprite->animation_frame];
95 }
96}
97
98/**
99 * @brief Updates the sprite's animation state using the global delta time.
100 * @param sprite Pointer to the sprite to update.
101 */
102static inline void gm_sprite_update(gmSprite *sprite) {
103 gm_sprite_update_dt(sprite, gm_dt());
104}
105
106/**
107 * @brief Draws the current frame of a sprite at the specified position and size.
108 * @param sprite Pointer to the sprite to draw.
109 * @param x The x-coordinate to draw at.
110 * @param y The y-coordinate to draw at.
111 * @param width The width to draw the sprite.
112 * @param height The height to draw the sprite.
113 */
114void gm_sprite_draw(gmSprite *sprite, double x, double y, double width,
115 double height) {
116 size_t im_w = sprite->image.width / sprite->n_frames;
117 gm_image_draw_part(sprite->image, im_w * sprite->_frame, 0, im_w,
118 sprite->image.height, x, y, width, height);
119}
120
121/**
122 * @brief Creates a new sprite by loading an image from a file.
123 * @param path The file path to the sprite sheet image.
124 * @param n_frames The number of frames in the sprite sheet.
125 * @return A new gmSprite instance.
126 */
127gmSprite gm_sprite_open(const char *path, int n_frames) {
128 return gm_sprite_create(gm_image_open(path), n_frames);
129}
gmImage gm_image_open(const char *path)
Loads an image from a file path.
Definition image.h:20
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 part of an image at the specified position and size.
Definition image.h:50
gmSpriteAnim gm_sprite_anim_browse(int n_sprites, double interval)
Creates a sprite animation that sequentially browses through frames.
Definition sprite.h:23
gmSpriteAnim gm_sprite_anim_create(double interval, const char *pattern)
Creates a sprite animation based on a pattern string.
Definition sprite.h:39
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:114
gmSprite gm_sprite_open(const char *path, int n_frames)
Creates a new sprite by loading an image from a file.
Definition sprite.h:127
#define GAMA_MAX_SPRITE_ANIM_LENGTH
Definition sprite.h:5
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:72
void gm_sprite_update_dt(gmSprite *sprite, double dt)
Updates the sprite's animation state based on elapsed time.
Definition sprite.h:88
Structure representing an image with handle and dimensions.
Definition image.h:9
uint32_t width
Definition image.h:11
uint32_t height
Definition image.h:12
Structure representing a sprite animation sequence.
Definition sprite.h:11
double interval
Definition sprite.h:12
size_t length
Definition sprite.h:14
int anim[GAMA_MAX_SPRITE_ANIM_LENGTH]
Definition sprite.h:13
Structure representing a sprite with animation capabilities.
Definition sprite.h:54
size_t n_frames
Definition sprite.h:59
size_t animation_frame
Definition sprite.h:60
gmImage image
Definition sprite.h:63
gmSpriteAnim animation
Definition sprite.h:57
size_t _frame
Definition sprite.h:61
double _backlog_t
Definition sprite.h:55