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   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
 

Functions

Spritesprite_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.
 
Spritesprite_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

◆ 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

#define CARD_SPRITE_SIZE   32

Definition at line 16 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.

◆ SPRITE_FOCUS_RAISE_PX

#define SPRITE_FOCUS_RAISE_PX   10

Definition at line 20 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 93 of file sprite.c.

◆ sprite_draw()

void sprite_draw ( void  )

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

Definition at line 159 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 141 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 132 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 116 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 164 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 123 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 171 of file sprite.c.

◆ sprite_init()

void sprite_init ( void  )

Initialize GBAlatro sprite system.

Definition at line 154 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 36 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 197 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 457 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 402 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 395 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 363 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 388 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 213 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 186 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 409 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 417 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 447 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 438 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 227 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 370 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 205 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 350 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 220 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 332 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 340 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 400 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 178 of file sprite.c.