Refactored list code to be generic void* list

This commit is contained in:
MeirGavish
2025-08-15 20:03:17 +03:00
parent d1677e82d2
commit 2a4f045db6
3 changed files with 51 additions and 35 deletions
+14 -14
View File
@@ -3,23 +3,23 @@
#include <stdbool.h> #include <stdbool.h>
/* Defining an int list for simplicity typedef struct List
* If another list is needed, a generic void*
* list should be defined....
*/
typedef struct IntList
{ {
int* _array; void** _array;
int size; int size;
int allocated_size; int allocated_size;
} IntList; } List;
IntList *int_list_new(int init_size); List *list_new(int init_size);
void int_list_destroy(IntList **list); void list_destroy(List **list);
bool int_list_append(IntList *list, int value); bool list_append(List *list, void *value);
bool int_list_remove_by_idx(IntList *list, int index); bool list_remove_by_idx(List *list, int index);
int int_list_get(IntList *list, int index); void* list_get(List *list, int index);
int int_list_get_size(IntList *list); int list_get_size(List *list);
bool int_list_remove_by_value(IntList *list, int value); 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 #endif
+9 -9
View File
@@ -174,7 +174,7 @@ int get_jokers_top(void) {
return jokers_top; return jokers_top;
} }
IntList* jokers_available_to_shop; // List of joker IDs List* jokers_available_to_shop; // List of joker IDs
// Consts // Consts
@@ -1009,8 +1009,8 @@ void game_set_state(enum GameState new_game_state)
void jokers_available_to_shop_init() void jokers_available_to_shop_init()
{ {
int num_defined_jokers = get_joker_registry_size(); int num_defined_jokers = get_joker_registry_size();
jokers_available_to_shop = int_list_new(num_defined_jokers); jokers_available_to_shop = list_new(num_defined_jokers);
for (int i = 0; i < num_defined_jokers; i++) for (intptr_t i = 0; i < num_defined_jokers; i++)
{ {
// Add all joker IDs sequentially // Add all joker IDs sequentially
int_list_append(jokers_available_to_shop, i); 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[]) static void game_shop_create_items(JokerObject *shop_jokers[])
{ {
tte_erase_rect_wrapper(SHOP_PRICES_TEXT_RECT); 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 // No jokers to create
return; return;
@@ -2160,9 +2160,9 @@ static void game_shop_create_items(JokerObject *shop_jokers[])
for (int i = 0; i < MAX_SHOP_JOKERS; i++) for (int i = 0; i < MAX_SHOP_JOKERS; i++)
{ {
// TODO: weight the random choice by joker rarity // TODO: weight the random choice by joker rarity
int joker_idx = random() % int_list_get_size(jokers_available_to_shop); int joker_idx = random() % list_get_size(jokers_available_to_shop);
int joker_id = int_list_get(jokers_available_to_shop, joker_idx); intptr_t joker_id = int_list_get(jokers_available_to_shop, joker_idx);
int_list_remove_by_idx(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] = joker_object_new(joker_new(joker_id));
shop_jokers[i]->sprite_object->x = int2fx(120 + i * CARD_SPRITE_SIZE); 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) 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 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) if (shop_jokers[i] != NULL)
{ {
// Make the joker available back to shop // 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 joker_object_destroy(&shop_jokers[i]); // Destroy the joker objects
} }
+28 -12
View File
@@ -3,10 +3,10 @@
#include "list.h" #include "list.h"
#include "util.h" #include "util.h"
IntList *int_list_new(int init_size) { List *list_new(int init_size) {
IntList *list = (IntList *)malloc(sizeof(IntList)); List *list = (List *)malloc(sizeof(List));
if (list == NULL) return NULL; if (list == NULL) return NULL;
list->_array = (int *)malloc(sizeof(int) * init_size); list->_array = (void **)malloc(sizeof(void*) * init_size);
if (!list->_array) if (!list->_array)
{ {
free(list); free(list);
@@ -17,7 +17,7 @@ IntList *int_list_new(int init_size) {
return list; return list;
} }
void int_list_destroy(IntList **list) { void list_destroy(List **list) {
if (list == NULL || *list == NULL) if (list == NULL || *list == NULL)
return; return;
{ {
@@ -28,22 +28,28 @@ void int_list_destroy(IntList **list) {
*list = NULL; *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) if (list->size >= list->allocated_size)
{ {
int new_size = list->allocated_size * 2; 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) if (new_arr == NULL)
return false; return false;
list->_array = new_arr; list->_array = new_arr;
list->allocated_size = new_size; list->allocated_size = new_size;
} }
list->_array[list->size++] = value; list->_array[list->size++] = value;
return true; 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) if (index < 0 || index >= list->size)
return false; return false;
for (int i = index; i < list->size - 1; ++i) 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; 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++) for (int i = 0; i < list->size; i++)
{ {
if (list->_array[i] == value) if (list->_array[i] == value)
{ {
return int_list_remove_by_idx(list, i); return list_remove_by_idx(list, i);
} }
} }
return false; 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) if (index < 0 || index >= list->size)
return 0; return NULL;
return list->_array[index]; 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) if (list == NULL)
{ {