From 2a4f045db63c0d20cb53463230f8814ab926575e Mon Sep 17 00:00:00 2001 From: MeirGavish Date: Fri, 15 Aug 2025 20:03:17 +0300 Subject: [PATCH] Refactored list code to be generic void* list --- include/list.h | 28 ++++++++++++++-------------- source/game.c | 18 +++++++++--------- source/int_list.c | 40 ++++++++++++++++++++++++++++------------ 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/include/list.h b/include/list.h index 64341f7..fd1c366 100644 --- a/include/list.h +++ b/include/list.h @@ -3,23 +3,23 @@ #include -/* Defining an int list for simplicity - * If another list is needed, a generic void* - * list should be defined.... - */ -typedef struct IntList +typedef struct List { - int* _array; + void** _array; int size; int allocated_size; -} IntList; +} List; -IntList *int_list_new(int init_size); -void int_list_destroy(IntList **list); -bool int_list_append(IntList *list, int value); -bool int_list_remove_by_idx(IntList *list, int index); -int int_list_get(IntList *list, int index); -int int_list_get_size(IntList *list); -bool int_list_remove_by_value(IntList *list, int value); +List *list_new(int init_size); +void list_destroy(List **list); +bool list_append(List *list, void *value); +bool list_remove_by_idx(List *list, int index); +void* list_get(List *list, int index); +int list_get_size(List *list); +bool list_remove_by_value(List *list, void *value); + +bool int_list_append(List *list, intptr_t value); +intptr_t int_list_get(List *list, int index); +bool int_list_remove_by_value(List *list, intptr_t value); #endif diff --git a/source/game.c b/source/game.c index 0cf06a2..6adc82b 100644 --- a/source/game.c +++ b/source/game.c @@ -174,7 +174,7 @@ int get_jokers_top(void) { return jokers_top; } -IntList* jokers_available_to_shop; // List of joker IDs +List* jokers_available_to_shop; // List of joker IDs // Consts @@ -1009,8 +1009,8 @@ void game_set_state(enum GameState new_game_state) void jokers_available_to_shop_init() { int num_defined_jokers = get_joker_registry_size(); - jokers_available_to_shop = int_list_new(num_defined_jokers); - for (int i = 0; i < num_defined_jokers; i++) + jokers_available_to_shop = list_new(num_defined_jokers); + for (intptr_t i = 0; i < num_defined_jokers; i++) { // Add all joker IDs sequentially int_list_append(jokers_available_to_shop, i); @@ -2151,7 +2151,7 @@ static int reroll_cost = REROLL_BASE_COST; static void game_shop_create_items(JokerObject *shop_jokers[]) { tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT); - if (int_list_get_size(jokers_available_to_shop) == 0) + if (list_get_size(jokers_available_to_shop) == 0) { // No jokers to create return; @@ -2160,9 +2160,9 @@ static void game_shop_create_items(JokerObject *shop_jokers[]) for (int i = 0; i < MAX_SHOP_JOKERS; i++) { // TODO: weight the random choice by joker rarity - int joker_idx = random() % int_list_get_size(jokers_available_to_shop); - int joker_id = int_list_get(jokers_available_to_shop, joker_idx); - int_list_remove_by_idx(jokers_available_to_shop, joker_idx); + int joker_idx = random() % list_get_size(jokers_available_to_shop); + intptr_t joker_id = int_list_get(jokers_available_to_shop, joker_idx); + list_remove_by_idx(jokers_available_to_shop, joker_idx); shop_jokers[i] = joker_object_new(joker_new(joker_id)); shop_jokers[i]->sprite_object->x = int2fx(120 + i * CARD_SPRITE_SIZE); @@ -2220,7 +2220,7 @@ static void game_shop_reroll(JokerObject** shop_jokers, int *reroll_cost) { if (shop_jokers[i] != NULL) { - int_list_append(jokers_available_to_shop, shop_jokers[i]->joker->id); + int_list_append(jokers_available_to_shop, (intptr_t)shop_jokers[i]->joker->id); joker_object_destroy(&shop_jokers[i]); // Destroy the joker object if it exists } } @@ -2482,7 +2482,7 @@ void game_shop() if (shop_jokers[i] != NULL) { // Make the joker available back to shop - int_list_append(jokers_available_to_shop, shop_jokers[i]->joker->id); + int_list_append(jokers_available_to_shop, (intptr_t)shop_jokers[i]->joker->id); } joker_object_destroy(&shop_jokers[i]); // Destroy the joker objects } diff --git a/source/int_list.c b/source/int_list.c index 0b65f3d..1a39dc4 100644 --- a/source/int_list.c +++ b/source/int_list.c @@ -3,10 +3,10 @@ #include "list.h" #include "util.h" -IntList *int_list_new(int init_size) { - IntList *list = (IntList *)malloc(sizeof(IntList)); +List *list_new(int init_size) { + List *list = (List *)malloc(sizeof(List)); if (list == NULL) return NULL; - list->_array = (int *)malloc(sizeof(int) * init_size); + list->_array = (void **)malloc(sizeof(void*) * init_size); if (!list->_array) { free(list); @@ -17,7 +17,7 @@ IntList *int_list_new(int init_size) { return list; } -void int_list_destroy(IntList **list) { +void list_destroy(List **list) { if (list == NULL || *list == NULL) return; { @@ -28,22 +28,28 @@ void int_list_destroy(IntList **list) { *list = NULL; } -bool int_list_append(IntList *list, int value) +bool int_list_append(List *list, intptr_t value) +{ + return list_append(list, (void*)value); +} + +bool list_append(List *list, void *value) { if (list->size >= list->allocated_size) { int new_size = list->allocated_size * 2; - int *new_arr = (int *)realloc(list->_array, sizeof(int) * new_size); + void **new_arr = (void **)realloc(list->_array, sizeof(void*) * new_size); if (new_arr == NULL) return false; list->_array = new_arr; list->allocated_size = new_size; } + list->_array[list->size++] = value; return true; } -bool int_list_remove_by_idx(IntList *list, int index) { +bool list_remove_by_idx(List *list, int index) { if (index < 0 || index >= list->size) return false; for (int i = index; i < list->size - 1; ++i) @@ -54,27 +60,37 @@ bool int_list_remove_by_idx(IntList *list, int index) { return true; } -bool int_list_remove_by_value(IntList *list, int value) +bool list_remove_by_value(List *list, void* value) { for (int i = 0; i < list->size; i++) { if (list->_array[i] == value) { - return int_list_remove_by_idx(list, i); + return list_remove_by_idx(list, i); } } return false; } -int int_list_get(IntList *list, int index) +bool int_list_remove_by_value(List *list, intptr_t value) +{ + return list_remove_by_value(list, (void*)value); +} + +void* list_get(List *list, int index) { if (index < 0 || index >= list->size) - return 0; + return NULL; return list->_array[index]; } -int int_list_get_size(IntList *list) +intptr_t int_list_get(List *list, int index) +{ + return (intptr_t)list_get(list, index); +} + +int list_get_size(List *list) { if (list == NULL) {