Overview
The sprite system manages animated and static images in Cub3D. Sprites are used for walls, billboards, UI elements, and character animations. They support frame-based animation with configurable timing and looping.s_sprite Structure
Core structure for sprite animation.Fields
Linked list of
t_ftm_image* frames. Each node contains one frame of the animation.Current frame index in the animation sequence (0-based)
Timestamp of the last frame update (milliseconds)
Milliseconds between frame updates. Set to 0 for static sprites.
If true, animation plays backward (decrements index instead of incrementing)
Whether the animation is currently playing. Set to false when non-looping animation finishes.
If true, animation loops infinitely. If false, stops at the last frame.
Core Functions
sprite_new()
Creates a new sprite with the given frames and animation timing.Linked list of
t_ftm_image* pointers representing animation framesMilliseconds between frame changes. Use 0 for static images.
t_sprite, or NULL on allocation failure
Behavior:
- Allocates and initializes sprite structure
- Sets
runningto true by default - Sets
indexto 0 - Initializes
updated_atto current time
src/utils/sprites/sprites0.c:47
get_sprite_image()
Retrieves the current frame of an animated or static sprite.The sprite to get the current frame from. Can be NULL.
- Current frame image (
t_ftm_image*) if sprite is valid - Placeholder image if sprite is NULL
- NULL if the current index is out of bounds
- Returns placeholder if sprite is NULL
- Checks if enough time has elapsed since last update
- If
update_delay> 0 andrunningis true:- Calculates how many frames to advance based on elapsed time
- Updates
index(forward ifreversedis false, backward if true) - Handles looping or stopping based on
loopflag - Updates
updated_attimestamp
- Returns the image at the current index
src/utils/sprites/sprites1.c:36
get_sprite3d_image()
Retrieves the appropriate sprite from a 3D sprite array based on viewing angle.Array of 360 sprite pointers (one for each degree of viewing angle)
Viewing angle in degrees (0-359). Automatically normalized.
src/utils/sprites/sprites1.c:53
init_sprite()
Initializes an existing sprite structure.Sprite structure to initialize
List of image frames
Animation frame delay in milliseconds
src/utils/sprites/sprites0.c:26
sprite_soft_copy()
Creates a shallow copy of a sprite, resetting animation state.Pointer to destination sprite pointer (allocated if NULL)
Source sprite to copy from
- Allocates dst if NULL
- Copies all fields from src
- Resets
indexto 0 - Sets
runningto true - Resets
updated_atto current time - Note: Images list is shared (not deep copied)
src/utils/sprites/sprites0.c:33
Memory Management
free_sprite()
Frees sprite and its image list.Pointer to
t_sprite to freesrc/utils/sprites/sprites0.c:20
clear_sprite()
Clears sprite’s image list without freeing the sprite itself.Pointer to
t_sprite to clearsrc/utils/sprites/sprites0.c:15
Animation Example
Creating and using an animated sprite:3D Sprite Arrays
3D sprites (sprite3d) are used for directional billboards:Billboard Sprites
Billboards use sprites for rendering:Door Animation
Doors use animated sprites:Frames per second for door opening/closing animations. Defined in
cub3d.h:87.door_sprite- Closed door textureopening_sprite- Animation of door openingdoor_sides_sprite- Side textures when door is open
Related Functions
get_entity_sprite()
Gets the appropriate sprite for an entity based on direction.Related Constants
Path to placeholder image shown when sprite is NULL. Defined in
cub3d.h:53.Path to loading screen image. Defined in
cub3d.h:54.