GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
sprite.h File Reference

Sprite system for Gbalatro. More...

#include <maxmod.h>
#include <tonc.h>

Go to the source code of this file.

Data Structures

struct  Sprite
 Sprite struct for GBA hardware specifics. More...
 
struct  SpriteObject
 A sprite object is a sprite that is focusable and movable in animation. More...
 

Macros

Sprite system constants
#define CARD_SPRITE_SIZE_PX   32
 
#define MAX_AFFINES   32
 
#define MAX_SPRITES   128
 
#define MAX_SPRITE_OBJECTS   16
 
#define SPRITE_FOCUS_RAISE_PX   10
 
#define CARD_FOCUS_SFX_PITCH_OFFSET_RANGE   512
 
Sprite sizes in number of tiles
#define CARD_SPRITE_TILES   16
 
#define JOKER_SPRITE_TILES   16
 
#define BLIND_SPRITE_TILES   16
 
#define SKIP_TAG_SPRITE_TILES   4
 

Enumerations

enum  SpriteType {
  CARD_SPRITE , BLIND_TOKEN_SPRITE , SKIP_TAG_SPRITE , JOKER_SPRITE ,
  DECK_SPRITE , MAX_SPRITE_TYPE
}
 The different types of sprites in the game. More...
 

Functions

int sprite_get_tid (enum SpriteType sprite_type, s16 layer)
 Get the tile index of a certain SpriteType at a certain layer.
 
int sprite_get_starting_layer (enum SpriteType sprite_type)
 Get the starting layer of a certain type of sprite.
 
Sprite * sprite_new (u16 a0, u16 a1, u32 tid, u32 pb, s16 sprite_index)
 Allocate and retrieve a pointer to a valid Sprite.
 
void sprite_destroy (Sprite **sprite)
 Destroy Sprite.
 
s16 sprite_get_layer (Sprite *sprite)
 Get index of Sprite in the GBA object buffer.
 
bool sprite_get_dimensions (Sprite *sprite, int *width, int *height)
 Get a Sprite's width and height.
 
bool sprite_get_height (Sprite *sprite, int *height)
 Get a Sprites's height.
 
bool sprite_get_width (Sprite *sprite, int *width)
 Get a Sprite's width.
 
int sprite_get_pb (const Sprite *sprite)
 Get the palette bank of a Sprite.
 
void sprite_hide (Sprite *sprite)
 Hides the sprite by manipulating ATTR0_HIDE in OAM.
 
void sprite_unhide (Sprite *sprite)
 Unhides the sprite by manipulating ATTR0_HIDE in OAM. The sprite's ATTR0_MODE is maintained from the sprite's creation with sprite_new()
 
void sprite_init (void)
 Initialize GBAlatro sprite system.
 
void sprite_draw (void)
 Draw Sprites to screen, to be called once per frame.
 
void sprite_object_init (SpriteObject *sprite_object)
 Initialize a SpriteObject to a default state. Must be called only once per SpriteObject when it is created.
 
void sprite_object_destroy (SpriteObject *sprite_object)
 Destroy SpriteObject.
 
void sprite_object_set_sprite (SpriteObject *sprite_object, Sprite *sprite)
 Register a Sprite to an associated SpriteObject.
 
void sprite_object_hide (SpriteObject *sprite_object)
 Hides the SpriteObject by manipulating ATTR0_HIDE in OAM.
 
void sprite_object_unhide (SpriteObject *sprite_object)
 Unhides the SpriteObject by manipulating ATTR0_HIDE in OAM. The sprite's ATTR0_MODE is maintained from the sprite's creation with sprite_new()
 
void sprite_object_reset_transform (SpriteObject *sprite_object)
 Reset SpriteObject's transform back to default values.
 
IWRAM_CODE void sprite_object_update (SpriteObject *sprite_object)
 Update a SpriteObject, to be called once per frame per active SpriteObject.
 
void sprite_object_update_all (void)
 Update all SpriteObjects, to be called once per frame in the main update loop.
 
void sprite_object_shake (SpriteObject *sprite_object, mm_word sound_id)
 Shake SpriteObject on screen and play a sound.
 
void sprite_object_bounce (SpriteObject *sprite_object, FIXED strength)
 Make SpriteObject bounce by slightly increasing its size in an elastic way.
 
void sprite_object_sway (SpriteObject *sprite_object)
 Make SpriteObject sway by slightly rotating on screen.
 
void sprite_object_set_target (SpriteObject *sprite_object, BG_POINT to)
 Set a SpriteObject's target position so that is slides across the screen between from the current position to the new.
 
Sprite * sprite_object_get_sprite (SpriteObject *sprite_object)
 Get a SpriteObject's registered Sprite.
 
void sprite_object_set_focus (SpriteObject *sprite_object, bool focus)
 Set the focus for SpriteObject Raises the object by SPRITE_FOCUS_RAISE_PX.
 
bool sprite_object_get_dimensions (SpriteObject *sprite_object, int *width, int *height)
 Get the width and height of SpriteObject's registered Sprite.
 
bool sprite_object_get_height (SpriteObject *sprite_object, int *height)
 Get a SpriteObject's height.
 
bool sprite_object_get_width (SpriteObject *sprite_object, int *width)
 Get a SpriteObject's width.
 
bool sprite_object_is_focused (SpriteObject *sprite_object)
 Get the focused variable from a SpriteObject.
 
void sprite_object_print_text_under (SpriteObject *sprite_object, const char text[])
 Print the given string directly beneath a SpriteObject. This is used only for Cards for now.
 
void sprite_object_print_price_under (SpriteObject *sprite_object, int price)
 Print the price string directly beneath a SpriteObject. More specialized version of sprite_object_print_text_under, automatically formats the price to $d.
 
void sprite_object_erase_text_under (SpriteObject *sprite_object)
 Erase the text within the Rect directly beneath a SpriteObject. This is used only for Cards for now.
 
INLINE void sprite_position (Sprite *sprite, int x, int y)
 Set sprite position. Inlined for efficiency.
 
INLINE void sprite_object_position (SpriteObject *sprite_object, int x, int y)
 Set sprite_object position. Inlined for efficiency.
 

Detailed Description

Sprite system for Gbalatro.

Definition in file sprite.h.

Macro Definition Documentation

◆ BLIND_SPRITE_TILES

#define BLIND_SPRITE_TILES   16

Definition at line 31 of file sprite.h.

◆ CARD_FOCUS_SFX_PITCH_OFFSET_RANGE

#define CARD_FOCUS_SFX_PITCH_OFFSET_RANGE   512

Definition at line 21 of file sprite.h.

◆ CARD_SPRITE_SIZE_PX

#define CARD_SPRITE_SIZE_PX   32

Definition at line 16 of file sprite.h.

◆ CARD_SPRITE_TILES

#define CARD_SPRITE_TILES   16

Definition at line 29 of file sprite.h.

◆ JOKER_SPRITE_TILES

#define JOKER_SPRITE_TILES   16

Definition at line 30 of file sprite.h.

◆ MAX_AFFINES

#define MAX_AFFINES   32

Definition at line 17 of file sprite.h.

◆ MAX_SPRITE_OBJECTS

#define MAX_SPRITE_OBJECTS   16

Definition at line 19 of file sprite.h.

◆ MAX_SPRITES

#define MAX_SPRITES   128

Definition at line 18 of file sprite.h.

◆ SKIP_TAG_SPRITE_TILES

#define SKIP_TAG_SPRITE_TILES   4

Definition at line 32 of file sprite.h.

◆ SPRITE_FOCUS_RAISE_PX

#define SPRITE_FOCUS_RAISE_PX   10

Definition at line 20 of file sprite.h.

Enumeration Type Documentation

◆ SpriteType

enum SpriteType

The different types of sprites in the game.

Defining sprite types allows to easily rearrange, expand, and get the info about sprites

Definition at line 40 of file sprite.h.

Function Documentation

◆ sprite_destroy()

void sprite_destroy ( Sprite **  sprite)

Destroy Sprite.

Parameters
spritepointer to a pointer of Sprite to destroy. No action if NULL.

Definition at line 134 of file sprite.c.

◆ sprite_draw()

void sprite_draw ( void  )

Draw Sprites to screen, to be called once per frame.

Definition at line 210 of file sprite.c.

◆ sprite_get_dimensions()

bool sprite_get_dimensions ( Sprite *  sprite,
int *  width,
int *  height 
)

Get a Sprite's width and height.

Parameters
spritepointer to Sprite, cannot be NULL
widthpointer to variable to be set, cannot be NULL
heightpointer to variable to be set, cannot be NULL
Returns
true if successful, false if otherwise. Upon success, width and height contain valid data, otherwise, the variables are unchanged.

Definition at line 182 of file sprite.c.

◆ sprite_get_height()

bool sprite_get_height ( Sprite *  sprite,
int *  height 
)

Get a Sprites's height.

Parameters
spritepointer to Sprite, cannot be NULL
heightpointer to variable to be set, cannot be NULL
Returns
true is successful, false if otherwise. Upon success, height contains valid data, otherwise, the variable is unchanged.

Definition at line 173 of file sprite.c.

◆ sprite_get_layer()

s16 sprite_get_layer ( Sprite *  sprite)

Get index of Sprite in the GBA object buffer.

Parameters
spritepointer to Sprite, cannot be NULL
Returns
Index of sprite in object buffer if sprite is valid, otherwise UNDEFINED.

Definition at line 157 of file sprite.c.

◆ sprite_get_pb()

int sprite_get_pb ( const Sprite *  sprite)

Get the palette bank of a Sprite.

Parameters
spritepointer to extract associated palette bank. Cannot be NULL.
Returns
The palette bank of the Sprite if successful, otherwise return UNDEFINED.

Definition at line 215 of file sprite.c.

◆ sprite_get_starting_layer()

int sprite_get_starting_layer ( enum SpriteType  sprite_type)

Get the starting layer of a certain type of sprite.

Parameters
sprite_type
Returns
int

Definition at line 71 of file sprite.c.

◆ sprite_get_tid()

int sprite_get_tid ( enum SpriteType  sprite_type,
s16  layer 
)

Get the tile index of a certain SpriteType at a certain layer.

Parameters
sprite_type
layer
Returns
index in tiles memory where to put the sprite

Definition at line 66 of file sprite.c.

◆ sprite_get_width()

bool sprite_get_width ( Sprite *  sprite,
int *  width 
)

Get a Sprite's width.

Parameters
spritepointer to Sprite, cannot be NULL
widthpointer to variable to be set, cannot be NULL
Returns
true is successful, false if otherwise. Upon success, width contains valid data, otherwise, the variable is unchanged.

Definition at line 164 of file sprite.c.

◆ sprite_hide()

void sprite_hide ( Sprite *  sprite)

Hides the sprite by manipulating ATTR0_HIDE in OAM.

Parameters
spriteThe sprite to hide

Definition at line 222 of file sprite.c.

◆ sprite_init()

void sprite_init ( void  )

Initialize GBAlatro sprite system.

Definition at line 195 of file sprite.c.

◆ sprite_new()

Sprite * sprite_new ( u16  a0,
u16  a1,
u32  tid,
u32  pb,
s16  sprite_index 
)

Allocate and retrieve a pointer to a valid Sprite.

Parameters
a0attribute 0 of OBJ_ATTR
a1attribute 1 of OBJ_ATTR
tidbase tile index of sprite, part of attribute 2
pbPalette-bank
sprite_indexindex in memory
Returns
Valid Sprite if allocations are successful. Otherwise, return NULL.

Definition at line 77 of file sprite.c.

◆ sprite_object_bounce()

void sprite_object_bounce ( SpriteObject *  sprite_object,
FIXED  strength 
)

Make SpriteObject bounce by slightly increasing its size in an elastic way.

Parameters
strengthhow much does the sprite bounce, bigger values yield a bigger bounce.
SpriteObjectpointer to SpriteObject to make bounce. Cannot be NULL.

Definition at line 416 of file sprite.c.

◆ sprite_object_destroy()

void sprite_object_destroy ( SpriteObject *  sprite_object)

Destroy SpriteObject.

Destroy a SpriteObject by releasing its associated resources (e.g. the sprite). This invalidates the SpriteObject and it should not be used after destroyed, a new one should be created instead.

Parameters
sprite_objectpointer to a SpriteObject to destroy. Cannot be NULL.

Definition at line 248 of file sprite.c.

◆ sprite_object_erase_text_under()

void sprite_object_erase_text_under ( SpriteObject *  sprite_object)

Erase the text within the Rect directly beneath a SpriteObject. This is used only for Cards for now.

Parameters
sprite_objectvalid pointer to SpriteObject to check
See also
sprite_object_print_text_under

Definition at line 533 of file sprite.c.

◆ sprite_object_get_dimensions()

bool sprite_object_get_dimensions ( SpriteObject *  sprite_object,
int *  width,
int *  height 
)

Get the width and height of SpriteObject's registered Sprite.

Parameters
sprite_objectpointer to SpriteObject to get the dimensions of. Cannot be NULL.
widthpointer to variable to be set, cannot be NULL
heightpointer to variable to be set, cannot be NULL
Returns
true is successful, false if otherwise. Upon success, width and height contain valid data, otherwise, the variables are unchanged.

Definition at line 478 of file sprite.c.

◆ sprite_object_get_height()

bool sprite_object_get_height ( SpriteObject *  sprite_object,
int *  height 
)

Get a SpriteObject's height.

Parameters
sprite_objectpointer to SpriteObject to get the height of. Cannot be NULL.
heightpointer to variable to be set, cannot be NULL
Returns
true is successful, false if otherwise. Upon success, height contains valid data, otherwise, the variable is unchanged.

Definition at line 471 of file sprite.c.

◆ sprite_object_get_sprite()

Sprite * sprite_object_get_sprite ( SpriteObject *  sprite_object)

Get a SpriteObject's registered Sprite.

Parameters
sprite_objectpointer to SpriteObject's registered Sprite. Cannot be NULL.
Returns
Sprite pointer registered to sprite_object if successful, otherwise return NULL. May be successful and NULL if there is no Sprite registered to the SpriteObject.

Definition at line 439 of file sprite.c.

◆ sprite_object_get_width()

bool sprite_object_get_width ( SpriteObject *  sprite_object,
int *  width 
)

Get a SpriteObject's width.

Parameters
sprite_objectpointer to SpriteObject to get the width of. Cannot be NULL.
widthpointer to variable to be set, cannot be NULL
Returns
true is successful, false if otherwise. Upon success, width contains valid data, otherwise, the variable is unchanged.

Definition at line 464 of file sprite.c.

◆ sprite_object_hide()

void sprite_object_hide ( SpriteObject *  sprite_object)

Hides the SpriteObject by manipulating ATTR0_HIDE in OAM.

Parameters
sprite_objectThe SpriteObject to hide

Definition at line 264 of file sprite.c.

◆ sprite_object_init()

void sprite_object_init ( SpriteObject *  sprite_object)

Initialize a SpriteObject to a default state. Must be called only once per SpriteObject when it is created.

Parameters
sprite_object- The SpriteObject to initialize

Definition at line 237 of file sprite.c.

◆ sprite_object_is_focused()

bool sprite_object_is_focused ( SpriteObject *  sprite_object)

Get the focused variable from a SpriteObject.

Parameters
sprite_objectvalid pointer to SpriteObject to check
Returns
true if the SpriteObject is focused, false otherwise

Definition at line 485 of file sprite.c.

◆ sprite_object_position()

INLINE void sprite_object_position ( SpriteObject *  sprite_object,
int  x,
int  y 
)

Set sprite_object position. Inlined for efficiency.

Parameters
sprite_objectpoitner to a SpriteObject to adjust the position of. A NULL check is not performed, though the value cannot be NULL.
xhorizontal position in pixels
yvertical position in pixels

Definition at line 483 of file sprite.h.

◆ sprite_object_print_price_under()

void sprite_object_print_price_under ( SpriteObject *  sprite_object,
int  price 
)

Print the price string directly beneath a SpriteObject. More specialized version of sprite_object_print_text_under, automatically formats the price to $d.

Parameters
sprite_objectvalid pointer to SpriteObject to check
pricethe price of the card to be printed
See also
sprite_object_print_text_under

Definition at line 523 of file sprite.c.

◆ sprite_object_print_text_under()

void sprite_object_print_text_under ( SpriteObject *  sprite_object,
const char  text[] 
)

Print the given string directly beneath a SpriteObject. This is used only for Cards for now.

Parameters
sprite_objectvalid pointer to SpriteObject to check
textthe string to be printed below the sprite

Definition at line 514 of file sprite.c.

◆ sprite_object_reset_transform()

void sprite_object_reset_transform ( SpriteObject *  sprite_object)

Reset SpriteObject's transform back to default values.

Parameters
sprite_objectpointer to SpriteObject to reset transform. Cannot be NULL.

Definition at line 278 of file sprite.c.

◆ sprite_object_set_focus()

void sprite_object_set_focus ( SpriteObject *  sprite_object,
bool  focus 
)

Set the focus for SpriteObject Raises the object by SPRITE_FOCUS_RAISE_PX.

Note: This is currently unused by CardObject as their focus is handled in cards_in_hand_update_loop() but we may want to extract it from there and refactor them use this instead.

Parameters
sprite_objectpointer to SpriteObject to set the focus of. Cannot be NULL.
focustrue to focus, false to unfocus

Definition at line 446 of file sprite.c.

◆ sprite_object_set_sprite()

void sprite_object_set_sprite ( SpriteObject *  sprite_object,
Sprite *  sprite 
)

Register a Sprite to an associated SpriteObject.

Parameters
sprite_objectpointer to SpriteObject to associate Sprite with. Cannot be NULL.
spritepointer to Sprite to associate SpriteObject with. Cannot be NULL.

Definition at line 256 of file sprite.c.

◆ sprite_object_set_target()

void sprite_object_set_target ( SpriteObject *  sprite_object,
BG_POINT  to 
)

Set a SpriteObject's target position so that is slides across the screen between from the current position to the new.

Parameters
SpriteObjectpointer to SpriteObject to move. Cannot be NULL.
toscreen coordinates of the target position. Cannot be UNDEFINED.

Definition at line 429 of file sprite.c.

◆ sprite_object_shake()

void sprite_object_shake ( SpriteObject *  sprite_object,
mm_word  sound_id 
)

Shake SpriteObject on screen and play a sound.

Parameters
SpriteObjectpointer to SpriteObject to shake. Cannot be NULL.
sound_idID of sound from maxmod to play on executing shake. If UNDEFINED no sound will play.

Definition at line 403 of file sprite.c.

◆ sprite_object_sway()

void sprite_object_sway ( SpriteObject *  sprite_object)

Make SpriteObject sway by slightly rotating on screen.

Parameters
SpriteObjectpointer to SpriteObject to sway. Cannot be NULL.

Definition at line 423 of file sprite.c.

◆ sprite_object_unhide()

void sprite_object_unhide ( SpriteObject *  sprite_object)

Unhides the SpriteObject by manipulating ATTR0_HIDE in OAM. The sprite's ATTR0_MODE is maintained from the sprite's creation with sprite_new()

Parameters
sprite_objectThe SpriteObject to unhide

Definition at line 271 of file sprite.c.

◆ sprite_object_update()

IWRAM_CODE void sprite_object_update ( SpriteObject *  sprite_object)

Update a SpriteObject, to be called once per frame per active SpriteObject.

Parameters
sprite_objectpointer to SpriteObject to update. Cannot be NULL.

Definition at line 383 of file sprite.c.

◆ sprite_object_update_all()

void sprite_object_update_all ( void  )

Update all SpriteObjects, to be called once per frame in the main update loop.

TODO: try and put this function in IWRAM for performance purposes. Crashed the last time I tried.

Definition at line 391 of file sprite.c.

◆ sprite_position()

INLINE void sprite_position ( Sprite *  sprite,
int  x,
int  y 
)

Set sprite position. Inlined for efficiency.

Parameters
spritepoitner to Sprite to adjust the position of. A NULL check is not performed, though the value cannot be NULL.
xhorizontal position in pixels
yvertical position in pixels

Definition at line 466 of file sprite.h.

◆ sprite_unhide()

void sprite_unhide ( Sprite *  sprite)

Unhides the sprite by manipulating ATTR0_HIDE in OAM. The sprite's ATTR0_MODE is maintained from the sprite's creation with sprite_new()

Parameters
spriteThe sprite to unhide

Definition at line 229 of file sprite.c.