remove usage of extern for joker_registry. Add array size macro to util.h

This commit is contained in:
theturtlemafia
2025-07-28 14:24:33 -04:00
parent 5c58b092d7
commit ed1204db74
5 changed files with 24 additions and 8 deletions
+2 -2
View File
@@ -60,8 +60,8 @@ typedef struct {
u8 base_value;
JokerEffectFunc effect;
} JokerInfo;
extern const JokerInfo joker_registry[];
extern const size_t joker_registry_size;
const JokerInfo* get_joker_registry_entry(int joker_id);
size_t get_joker_registry_size(void);
void joker_init();
+2
View File
@@ -35,4 +35,6 @@ static inline int get_digits_even(int n)
#define UNDEFINED -1
#define NUM_ELEM_IN_ARR(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif // UTIL_H
+1 -1
View File
@@ -1825,7 +1825,7 @@ static void game_shop_create_items(JokerObject *shop_jokers[], bool first_time)
joker_object_destroy(&shop_jokers[i]); // Destroy the joker object if it exists
}
u8 joker_id = random() % joker_registry_size;
u8 joker_id = random() % get_joker_registry_size();
shop_jokers[i] = joker_object_new(joker_new(joker_id));
shop_jokers[i]->sprite_object->x = int2fx(120 + i * 32);
+6 -4
View File
@@ -38,10 +38,10 @@ void joker_init()
Joker *joker_new(u8 id)
{
if (id >= joker_registry_size) return NULL;
if (id >= get_joker_registry_size()) return NULL;
Joker *joker = malloc(sizeof(Joker));
const JokerInfo *jinfo = &joker_registry[id];
const JokerInfo *jinfo = get_joker_registry_entry(id);
joker->id = id;
joker->modifier = BASE_EDITION; // TODO: Make this random later
@@ -61,8 +61,10 @@ void joker_destroy(Joker **joker)
JokerEffect joker_get_score_effect(Joker *joker, Card *scored_card)
{
if (joker->id >= joker_registry_size) return (JokerEffect){0};
return joker_registry[joker->id].effect(joker, scored_card);
const JokerInfo *jinfo = get_joker_registry_entry(joker->id);
if (!jinfo) return (JokerEffect){0};
return jinfo->effect(joker, scored_card);
}
// JokerObject methods
+13 -1
View File
@@ -1,4 +1,5 @@
#include "joker.h"
#include "util.h"
static JokerEffect default_joker_effect(Joker *joker, Card *scored_card) {
@@ -38,4 +39,15 @@ const JokerInfo joker_registry[] = {
{ COMMON_JOKER, 5, gluttonous_joker_effect },
};
const size_t joker_registry_size = sizeof(joker_registry) / sizeof(joker_registry[0]);
static const size_t joker_registry_size = NUM_ELEM_IN_ARR(joker_registry);
const JokerInfo* get_joker_registry_entry(int joker_id) {
if (joker_id < 0 || (size_t)joker_id >= joker_registry_size) {
return NULL;
}
return &joker_registry[joker_id];
}
size_t get_joker_registry_size(void) {
return joker_registry_size;
}