23#define SPRING_DAMP_NUMERATOR 179
24#define SPRING_DAMP_DENOM_SHIFT 8
25#define SPRING_DAMP_ROUNDING (1 << (SPRING_DAMP_DENOM_SHIFT - 1))
27OBJ_ATTR obj_buffer[MAX_SPRITES];
28OBJ_AFFINE* obj_aff_buffer = (OBJ_AFFINE*)obj_buffer;
30static Sprite* free_sprites[MAX_SPRITES] = {NULL};
31static bool free_affines[MAX_AFFINES] = {
false};
43 if (!free_sprites[sprite_index])
45 free_sprites[sprite_index] = sprite;
55 int aff_index = MAX_AFFINES;
57 for (
int i = 0; i < MAX_AFFINES; i++)
61 free_affines[i] =
true;
67 if (aff_index == MAX_AFFINES)
73 a1 = a1 | ATTR1_AFF_ID(aff_index);
75 sprite->
obj = &obj_buffer[sprite_index];
76 sprite->
aff = &obj_aff_buffer[aff_index];
77 obj_set_attr(sprite->
obj, a0, a1, ATTR2_PALBANK(pb) | tid);
78 obj_aff_identity(&obj_aff_buffer[aff_index]);
82 sprite->
obj = &obj_buffer[sprite_index];
83 obj_set_attr(sprite->
obj, a0, a1, ATTR2_PALBANK(pb) | tid);
86 sprite->
idx = sprite_index;
88 sprite->
mode = a0 & ATTR0_MODE_MASK;
98 obj_hide((*sprite)->obj);
100 if ((*sprite)->aff != NULL)
102 free_affines[(*sprite)->aff - obj_aff_buffer] =
false;
105 free_sprites[(*sprite)->idx] = NULL;
107 POOL_FREE(
Sprite, *sprite);
120 return (s16)(sprite->
obj - obj_buffer);
128 *width = obj_get_width(sprite->
obj);
137 *height = obj_get_height(sprite->
obj);
147 const u8* size = obj_get_size(sprite->
obj);
156 oam_init(obj_buffer, MAX_SPRITES);
161 oam_copy(oam_mem, obj_buffer, MAX_SPRITES);
168 return (sprite->
obj->attr2 & ATTR2_PALBANK_MASK) >> ATTR2_PALBANK_SHIFT;
175 obj_hide(sprite->
obj);
182 obj_unhide(sprite->
obj, sprite->
mode);
190 sprite_object->
sprite = NULL;
192 sprite_object->
focused =
false;
210 sprite_object->
sprite = sprite;
232 sprite_object->
vx = 0;
233 sprite_object->vy = 0;
234 sprite_object->
tscale = FIX_ONE;
235 sprite_object->
scale = FIX_ONE;
236 sprite_object->
vscale = 0;
246static inline bool sprite_object_has_velocity(
const SpriteObject* sprite_object)
248 return sprite_object->
vx != 0 || sprite_object->vy != 0 || sprite_object->
vscale != 0 ||
252static inline bool sprite_object_at_target(
const SpriteObject* s)
257static inline bool is_sprite_object_static(
const SpriteObject* sprite_object)
259 return !sprite_object_has_velocity(sprite_object) && sprite_object_at_target(sprite_object);
262static inline IWRAM_CODE
void update_sprite_position(
SpriteObject* sprite_object)
264 sprite_object->
vx += ((sprite_object->
tx - sprite_object->
x) * g_game_vars.game_speed) / 8;
265 sprite_object->vy += ((sprite_object->ty - sprite_object->y) * g_game_vars.game_speed) / 8;
273 const FIXED epsilon = (FIX_ONE >> 6);
276 if (abs(sprite_object->
vx) < epsilon && abs(sprite_object->vy) < epsilon)
278 sprite_object->
vx = 0;
279 sprite_object->vy = 0;
281 sprite_object->
x = sprite_object->
tx;
282 sprite_object->y = sprite_object->ty;
286 sprite_object->
vx = (sprite_object->
vx * SPRING_DAMP_NUMERATOR + SPRING_DAMP_ROUNDING) >>
287 SPRING_DAMP_DENOM_SHIFT;
288 sprite_object->vy = (sprite_object->vy * SPRING_DAMP_NUMERATOR + SPRING_DAMP_ROUNDING) >>
289 SPRING_DAMP_DENOM_SHIFT;
291 sprite_object->
x += sprite_object->
vx;
292 sprite_object->y += sprite_object->vy;
296 if (abs(sprite_object->
vscale) < epsilon)
298 sprite_object->
vscale = 0;
304 (sprite_object->
vscale * SPRING_DAMP_NUMERATOR + SPRING_DAMP_ROUNDING) >>
305 SPRING_DAMP_DENOM_SHIFT;
310 if (abs(sprite_object->
vrotation) < epsilon)
318 (sprite_object->
vrotation * SPRING_DAMP_NUMERATOR + SPRING_DAMP_ROUNDING) >>
319 SPRING_DAMP_DENOM_SHIFT;
326 sprite_object->
scale,
327 sprite_object->
scale,
334 if (!is_sprite_object_static(sprite_object))
335 update_sprite_position(sprite_object);
354 sprite_object->
vscale = float2fx(0.3f);
355 sprite_object->
vrotation = float2fx(8.0f);
357 if (sound_id == UNDEFINED)
367 return sprite_object->
sprite;
374 if (sprite_object->
focused == focus)
378 sprite_object->
focused = focus;
385 sprite_object->ty = sprite_object->ty + int2fx((focus ? -1 : 1) * SPRITE_FOCUS_RAISE_PX);
415static Rect sprite_object_get_text_rect_under(
SpriteObject* sprite_object)
426 height = CARD_SPRITE_SIZE;
427 width = CARD_SPRITE_SIZE;
430 ret_rect.left = fx2int(sprite_object->
tx);
431 ret_rect.top = fx2int(sprite_object->ty) + height +
TILE_SIZE;
432 ret_rect.right = ret_rect.left + width;
442 Rect text_rect = sprite_object_get_text_rect_under(sprite_object);
444 tte_printf(
"#{P:%d,%d; cx:0x%X000}%s", text_rect.left, text_rect.top, TTE_YELLOW_PB, text);
452 char price_str_buff[INT_MAX_DIGITS + 2];
453 snprintf(price_str_buff,
sizeof(price_str_buff),
"$%d", price);
461 Rect text_rect = sprite_object_get_text_rect_under(sprite_object);
464 text_rect.bottom = text_rect.bottom + SPRITE_FOCUS_RAISE_PX;
Utilities for using maxmod to play sound effects.
#define MM_BASE_PITCH_RATE
The default pitch rate for the sound effect played.
void play_sfx(mm_word id, mm_word rate, mm_byte volume)
Play a sound effect, wrapper for mmEffectEx() See https://maxmod.org/ref/functions/mm_sound_effect....
#define SFX_DEFAULT_VOLUME
Default volume for sound effects.
Game global game variables struct definition.
Graphic utility functions.
void update_text_rect_to_center_str(Rect *rect, const char *str, enum ScreenHorzDir bias_direction)
Updates a rect so a string is centered within it.
#define TILE_SIZE
Tile size in pixels, both height and width as tiles are square.
#define TTE_CHAR_SIZE
By default TTE characters occupy a single tile.
void tte_erase_rect_wrapper(Rect rect)
A wrapper for tte_erase_rect that would use the rect struct.
The core structure for items in the shop and inventory. Provides a common API for the shop and invent...
void * list_itr_next(ListItr *itr)
bool list_remove_data(List *list, void *data)
#define LIST_DEFAULT
Default list declaration for empty lists.
void list_push_back(List *list, void *data)
ListItr list_itr_create(List *list)
Interface to interact with the mgba logger.
Common functions to handle RNG manipulation. Using this interface has three goals:
u32 rng_get_u32(enum RngSequence key)
Get the next "randomly" generated number in the sequence corresponding to the given type.
Sprite system for Gbalatro.
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_is_focused(SpriteObject *sprite_object)
Get the focused variable from a SpriteObject.
bool sprite_get_width(Sprite *sprite, int *width)
Get a Sprite's width.
INLINE void sprite_position(Sprite *sprite, int x, int y)
Set sprite position. Inlined for efficiency.
void sprite_object_unhide(SpriteObject *sprite_object)
Unhides the SpriteObject by manipulating ATTR0_HIDE in OAM. The sprite's ATTR0_MODE is maintained fro...
void sprite_object_set_focus(SpriteObject *sprite_object, bool focus)
Set the focus for SpriteObject Raises the object by SPRITE_FOCUS_RAISE_PX.
void sprite_object_init(SpriteObject *sprite_object)
Initialize a SpriteObject to a default state. Must be called only once per SpriteObject when it is cr...
void sprite_object_destroy(SpriteObject *sprite_object)
Destroy SpriteObject.
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_pri...
s16 sprite_get_layer(Sprite *sprite)
Get index of Sprite in the GBA object buffer.
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_init(void)
Initialize GBAlatro sprite system.
void sprite_object_reset_transform(SpriteObject *sprite_object)
Reset SpriteObject's transform back to default values.
int sprite_get_pb(const Sprite *sprite)
Get the palette bank of a Sprite.
bool sprite_object_get_width(SpriteObject *sprite_object, int *width)
Get a SpriteObject's width.
void sprite_object_set_sprite(SpriteObject *sprite_object, Sprite *sprite)
Register a Sprite to an associated SpriteObject.
void sprite_object_update_all(void)
Update all SpriteObjects, to be called once per frame in the main update loop.
bool sprite_get_dimensions(Sprite *sprite, int *width, int *height)
Get a Sprite's width and height.
bool sprite_object_get_height(SpriteObject *sprite_object, int *height)
Get a SpriteObject's height.
INLINE void sprite_object_position(SpriteObject *sprite_object, int x, int y)
Set sprite_object position. Inlined for efficiency.
void sprite_object_shake(SpriteObject *sprite_object, mm_word sound_id)
Shake SpriteObject on screen and play a sound.
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_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.
bool sprite_get_height(Sprite *sprite, int *height)
Get a Sprites's height.
void sprite_destroy(Sprite **sprite)
Destroy Sprite.
Sprite * sprite_object_get_sprite(SpriteObject *sprite_object)
Get a SpriteObject's registered 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 ...
void sprite_draw(void)
Draw Sprites to screen, to be called once per frame.
IWRAM_CODE void sprite_object_update(SpriteObject *sprite_object)
Update a SpriteObject, to be called once per frame per active SpriteObject.
void sprite_object_hide(SpriteObject *sprite_object)
Hides the SpriteObject by manipulating ATTR0_HIDE in OAM.
A sprite object is a sprite that is focusable and movable in animation.
bool focused
Focused status (card specific, raise and lower card)
FIXED vscale
Scale velocity AKA the rate of change of scaling ops.
FIXED rotation
Actual rotation, in units for tonc's obj_aff_rotscale
FIXED trotation
Target rotation.
FIXED scale
Current Scale, in units for tonc's obj_aff_rotscale
Sprite * sprite
Sprite configuration info.
FIXED tscale
Target Scale.
FIXED vrotation
Rotation velocity.
Sprite struct for GBA hardware specifics.
OBJ_AFFINE * aff
GBA sprite affine matrices registers info.
s16 idx
Sprite index in memory managed by GBAlatro.
u16 mode
The mode of the sprite (regular, affine, etc.), set when the sprite is created corresponds to A0 & AT...
OBJ_ATTR * obj
GBA sprite attribute registers info (A0-A2)
Utilities relating around number string representation and protected arithmatic helper functions.
#define GBAL_RETURN_IF_NULL_RET(param, ret_val)
Checks if param is equal to NULL and prints error message and returns in case it is....
#define GBAL_RETURN_IF_NULL_VOID(param)
Checks if param is NULL and prints error message and returns in case it is. Useful for checking argum...