LibGame  v0.4.0
The LG Game Engine - Copyright (C) 2024 ETMSoftware
lg_sprites.c File Reference

Functions

LG_Sprite lg_sprite (const char *file_name[], LG_Color_u lg_color_u_key, int w, int h)
 
int lg_sprite_draw (LG_Sprite *sprite)
 
void lg_sprite_free_tex (LG_Sprite *sprite)
 
void lg_sprite_set_cur_frame (LG_Sprite *sprite, int frame)
 
int lg_sprite_get_cur_frame (LG_Sprite *sprite)
 
LG_Sprite lg_clone_sprite (LG_Sprite *sprite)
 
int lg_sprite_is_inside_rectangle (LG_Sprite *sprite, Rec2Di *rect)
 
int lg_sprite_is_inside_rectangle_set (LG_Sprite *sprite, Rec2Di **rect)
 
int lg_sprite_is_outside_rectangle (LG_Sprite *sprite, Rec2Di *rect)
 
int lg_sprite_is_outside_rectangle_set (LG_Sprite *sprite, Rec2Di **rect)
 
int lg_sprites_collide (LG_Sprite *sprite1, LG_Sprite *sprite2)
 
int lg_sprite_centers_collide_by_x_y (LG_Sprite *sprite1, LG_Sprite *sprite2, int x, int y)
 
int lg_sprite_centers_collide_by_k (LG_Sprite *sprite1, LG_Sprite *sprite2, int k)
 
void lg_save_sprite_position (LG_Sprite *sprite)
 
void lg_restore_sprite_position (LG_Sprite *sprite)
 
void lg_sprite_info (LG_Sprite *sprite)
 
void lg_rqdraw_init (Rq_SDraw_Array *draw_list)
 
int lg_rqdraw_add_sprite (LG_Sprite *sprite, Rq_SDraw_Array *draw_list)
 

Detailed Description

=== Sprites structs and funcs ===

LG_Sprite are only graphical objects at the moment - next thing to do is adding some kind of "behaviour" and making frames usage less tedious.

Another option is to create a new struct ("character" or whatnot) whose first member is a sprite.

Function Documentation

◆ lg_sprite()

LG_Sprite lg_sprite ( const char *  file_name[],
LG_Color_u  lg_color_u_key,
int  w,
int  h 
)

Create a new sprite (LG_Sprite)

=== ALL IMAGES MUST BE SQUARE (ie width = height) ===

All images are scaled to w, h (or first image dims if -1, -1)

Scaling may degrade image quality, so original dims should be prefered

From the SDL2 doc: "The color key defines a pixel value that will be treated as transparent in a blit"

Then we use a texture atlas

Parameters
file_nameA NULL terminated array of image file paths
lg_color_u_keyThe color key to use
wSprite width
hSprite height
Returns
A new LG_Sprite, with id set to 0 -> OK / -1 -> invalid sprite (TODO: need to find sth better)

◆ lg_sprite_draw()

int lg_sprite_draw ( LG_Sprite sprite)

Draw a sprite

Parameters
spriteA LG_Sprite instance
Returns
LG_OK if OK

◆ lg_sprite_free_tex()

void lg_sprite_free_tex ( LG_Sprite sprite)

Free sprite's atlas texture

WARNING: freeing the original sprite atlas texture will invalidate all cloned sprites atlas textures, so you should make sure to keep the original sprite around until done with all cloned sprites

Parameters
spriteA LG_Sprite instance
Returns
LG_OK if OK

◆ lg_sprite_set_cur_frame()

void lg_sprite_set_cur_frame ( LG_Sprite sprite,
int  frame 
)

Bla bla (self-explanatory)

◆ lg_sprite_get_cur_frame()

int lg_sprite_get_cur_frame ( LG_Sprite sprite)

Bla bla (self-explanatory)

◆ lg_clone_sprite()

LG_Sprite lg_clone_sprite ( LG_Sprite sprite)

Create a new sprite which will use the original sprite texture atlas

WARNING: freeing the original sprite atlas texture will invalidate all cloned sprites atlas textures, so you should make sure to keep the original sprite around until done with all cloned sprites

Parameters
spriteA LG_Sprite
Returns
A new LG_Sprite

◆ lg_sprite_is_inside_rectangle()

int lg_sprite_is_inside_rectangle ( LG_Sprite sprite,
Rec2Di rect 
)

Bla bla (self-explanatory)

◆ lg_sprite_is_inside_rectangle_set()

int lg_sprite_is_inside_rectangle_set ( LG_Sprite sprite,
Rec2Di **  rect 
)

Bla bla (self-explanatory)

◆ lg_sprite_is_outside_rectangle()

int lg_sprite_is_outside_rectangle ( LG_Sprite sprite,
Rec2Di rect 
)

Bla bla (self-explanatory)

◆ lg_sprite_is_outside_rectangle_set()

int lg_sprite_is_outside_rectangle_set ( LG_Sprite sprite,
Rec2Di **  rect 
)

Bla bla (self-explanatory)

◆ lg_sprites_collide()

int lg_sprites_collide ( LG_Sprite sprite1,
LG_Sprite sprite2 
)

Test if sprites (squares) overlap

Parameters
sprite1A LG_Sprite
sprite1A LG_Sprite
Returns
TRUE if sprites overlap, FALSE otherwise

◆ lg_sprite_centers_collide_by_x_y()

int lg_sprite_centers_collide_by_x_y ( LG_Sprite sprite1,
LG_Sprite sprite2,
int  x,
int  y 
)

Test centers distance (< x && < y)

Parameters
sprite1A LG_Sprite
sprite1A LG_Sprite
xSprite centers distance threshold along x
ySprite centers distance threshold along y
Returns
TRUE if sprite centers collide, FALSE otherwise

◆ lg_sprite_centers_collide_by_k()

int lg_sprite_centers_collide_by_k ( LG_Sprite sprite1,
LG_Sprite sprite2,
int  k 
)

Test centers distance (< k)

Parameters
sprite1A LG_Sprite
sprite1A LG_Sprite
kSprite centers distance threshold
Returns
TRUE if sprite centers collide, FALSE otherwise

◆ lg_save_sprite_position()

void lg_save_sprite_position ( LG_Sprite sprite)

Bla bla (self-explanatory)

◆ lg_restore_sprite_position()

void lg_restore_sprite_position ( LG_Sprite sprite)

Bla bla (self-explanatory)

◆ lg_sprite_info()

void lg_sprite_info ( LG_Sprite sprite)

Bla bla (self-explanatory)

◆ lg_rqdraw_init()

void lg_rqdraw_init ( Rq_SDraw_Array draw_list)

Reset a Rq_SDraw_Array, ie an array of LG_Sprite instances to be drawed in batch

Parameters
draw_listA pointer to a Rq_SDraw_Array

◆ lg_rqdraw_add_sprite()

int lg_rqdraw_add_sprite ( LG_Sprite sprite,
Rq_SDraw_Array draw_list 
)

Add a LG_Sprite instances at the end of a Rq_SDraw_Array, ie an array of LG_Sprite instances to be drawed in batch

Must call lg_rqdraw_init() at start of loop

Array has no duplicates and ends with first NULL element met

// Rq_SDraw_Array stands for Requested Sprite Draw Array
// Array of sprites to be drawed in batch - use linked list instead ?
typedef struct {
LG_Sprite *sprite[LG_SPRITE_N_DRAWS_MAX];
Parameters
draw_listA pointer to a Rq_SDraw_Array
spriteA pointer to a LG_Sprite
Returns
LG_OK if OK
Rq_SDraw_Array
Definition: lg_sprites.h:62
LG_Sprite
Definition: lg_sprites.h:12