From 517d40c294901ed27840b1964b86ade32b9dafbf Mon Sep 17 00:00:00 2001 From: Rickey Fehr Date: Sun, 19 Oct 2025 15:52:36 -0700 Subject: [PATCH] Cleanup for PR, return old sprite tracker --- source/joker.c | 3 ++- source/pool.c | 13 ++++++++++--- source/sprite.c | 33 +++++++++++++++------------------ tests/pool/pool_test.c | 12 +++++++----- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/source/joker.c b/source/joker.c index 63b71bd..c0d1b7e 100644 --- a/source/joker.c +++ b/source/joker.c @@ -15,6 +15,7 @@ #define JOKER_SCORE_TEXT_Y 48 #define NUM_JOKERS_PER_SPRITESHEET 2 +#define MAX_DEFINABLE_JOKERS 150 static const unsigned int *joker_gfxTiles[] = { @@ -50,7 +51,7 @@ static bool used_layers[MAX_JOKER_OBJECTS] = {false}; // Track used layers for j // Maps the spritesheet index to the palette bank index allocated to it. // Spritesheets that were not allocated are -static int joker_spritesheet_pb_map[MAX_SPRITES]; +static int joker_spritesheet_pb_map[(MAX_DEFINABLE_JOKERS + 1) / NUM_JOKERS_PER_SPRITESHEET]; static int joker_pb_num_sprite_users[JOKER_LAST_PB - JOKER_BASE_PB + 1] = { 0 }; static int get_num_spritesheets() diff --git a/source/pool.c b/source/pool.c index 0b526a6..8e59da0 100644 --- a/source/pool.c +++ b/source/pool.c @@ -1,11 +1,18 @@ #include "pool.h" +#include "util.h" void pool_bm_clear_idx(PoolBitmap *bm, int idx) { // Divide by 32 to get the word index - uint32_t i = idx >> 5; + uint32_t i = idx / 32; // Get last 5-bits, same as a modulo (% 32) operation on positive numbers - uint32_t b = idx & 0x1F; + uint32_t b = idx % 32; + + // Below are the "fast" forms of the above operations, respectively. + // These are more efficient, but removed for readability + // See: https://github.com/cellos51/balatro-gba/pull/132#discussion_r2365966071 + //uint32_t i = idx >> 5; + //uint32_t b = idx & 0x1F; bm->w[i] &= ~((uint32_t)1 << b); } @@ -32,7 +39,7 @@ int pool_bm_get_free_idx(PoolBitmap *bm) } } - return -1; + return UNDEFINED; } diff --git a/source/sprite.c b/source/sprite.c index 355a973..db2b210 100644 --- a/source/sprite.c +++ b/source/sprite.c @@ -9,34 +9,34 @@ #include #include +#define MAX_SPRITES 128 #define MAX_AFFINES 32 #define SPRITE_FOCUS_RAISE_PX 10 OBJ_ATTR obj_buffer[MAX_SPRITES]; OBJ_AFFINE *obj_aff_buffer = (OBJ_AFFINE*)obj_buffer; -typedef struct -{ - bool used; - Sprite* p_sprite; -} _UsedSprite; - +static Sprite *free_sprites[MAX_SPRITES] = {NULL}; static bool free_affines[MAX_AFFINES] = {false}; -static _UsedSprite free_sprites[MAX_SPRITES] = {{0}}; // Sprite methods Sprite *sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, int sprite_index) { - if(free_sprites[sprite_index].used) - { - return free_sprites[sprite_index].p_sprite; - } - Sprite* sprite = POOL_GET(Sprite); sprite->obj = NULL; sprite->aff = NULL; + if(!free_sprites[sprite_index]) + { + free_sprites[sprite_index] = sprite; + } + else + { + POOL_FREE(Sprite, sprite); + return NULL; + } + if (a0 & ATTR0_AFF) { int aff_index = MAX_AFFINES; @@ -70,13 +70,11 @@ Sprite *sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, int sprite_index) obj_set_attr(sprite->obj, a0, a1, ATTR2_PALBANK(pb) | tid); } - free_sprites[sprite_index].used = true; - free_sprites[sprite_index].p_sprite = sprite; sprite->idx = sprite_index; + return sprite; } - void sprite_destroy(Sprite **sprite) { if (*sprite == NULL) return; @@ -88,8 +86,7 @@ void sprite_destroy(Sprite **sprite) free_affines[(*sprite)->aff - obj_aff_buffer] = false; } - free_sprites[(*sprite)->idx].used = false; - free_sprites[(*sprite)->idx].p_sprite = NULL; + free_sprites[(*sprite)->idx] = NULL; POOL_FREE(Sprite, *sprite); @@ -98,7 +95,7 @@ void sprite_destroy(Sprite **sprite) int sprite_get_layer(Sprite *sprite) { - if (sprite == NULL || sprite->obj == NULL) return -1; + if (sprite == NULL || sprite->obj == NULL) return UNDEFINED; return sprite->obj - obj_buffer; } diff --git a/tests/pool/pool_test.c b/tests/pool/pool_test.c index 66abeed..398d7ee 100644 --- a/tests/pool/pool_test.c +++ b/tests/pool/pool_test.c @@ -1,4 +1,6 @@ #include "pool.h" + +#include "util.h" #include "test_structures.h" #include @@ -93,17 +95,17 @@ int main(void) { // Test it twice to make sure empty works, kinda hacky. printf("Testing Pool Fill and Empty 1x.\n"); - if(!test_fill_and_empty()) return -1; + if(!test_fill_and_empty()) return UNDEFINED; printf("Testing Pool Fill and Empty 2x.\n"); - if(!test_fill_and_empty()) return -1; + if(!test_fill_and_empty()) return UNDEFINED; // Similarly here, verify that fill, random num removal, refill, and empty // by refilling and emptying again printf("Testing Pool Fill, Partial Empty, Refill, Empty.\n"); - if(!test_fill_and_remove_at_random_and_refill_and_empty()) return -1; + if(!test_fill_and_remove_at_random_and_refill_and_empty()) return UNDEFINED; printf("Testing Pool Fill and Empty.\n"); - if(!test_fill_and_empty()) return -1; + if(!test_fill_and_empty()) return UNDEFINED; printf("---------------------------------------------------------\n"); printf("Pool Tests Passed\n"); @@ -139,7 +141,7 @@ int main(void) printf("\n"); t1 = get_time(); - if(!test_fill_and_empty()) return -1; + if(!test_fill_and_empty()) return UNDEFINED; t2 = get_time(); printf("Fill and Empty Pool %d times:\n\t", TEST_SIZE); print_time_diff(t1, t2);