From 681fa2a5dc3faebd886a0d167ead9ec55278f5d0 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Mon, 2 Jan 2023 13:08:24 +0000 Subject: [PATCH] Reorganised the ObjectStore and AssetManager files --- include/ObjectStore.h | 4 - src/AssetManager.c | 221 ++++++++++++++++++++++-------------------- src/ObjectStore.c | 30 +++--- 3 files changed, 129 insertions(+), 126 deletions(-) diff --git a/include/ObjectStore.h b/include/ObjectStore.h index fae95f2..656ac98 100644 --- a/include/ObjectStore.h +++ b/include/ObjectStore.h @@ -49,10 +49,6 @@ void render_all_objects(ObjectStore *os); */ void destroy_object_store(ObjectStore **os); -#ifdef DEBUG_ENGINE -void print_objects(ObjectStore *os); -#endif // !DEBUG_ENGINE - #ifdef __cplusplus } #endif // !__cplusplus diff --git a/src/AssetManager.c b/src/AssetManager.c index ae46db0..f8b6781 100644 --- a/src/AssetManager.c +++ b/src/AssetManager.c @@ -45,116 +45,14 @@ struct asset_manager { IDNodeList hashed_path_ids[MAX_ASSET_COUNT]; }; -u64 hash_path(const char *path) { - /** - * Converts a string into a numerical index using a simple hashing algorithm - */ +u64 hash_path(const char *path); - u64 i = 0; +void insert_id_node(IDNodeList *node_list, AssetID id); - for (size_t j = 0; path[j]; ++j) { - i += (u64)path[j] * HASH_PRIME_01 % MAX_ASSET_COUNT; - } - - i = (i * HASH_PRIME_02) % MAX_ASSET_COUNT; - - return i; -} - -void insert_id_node(IDNodeList *node_list, AssetID id) { - if (node_list->node_count == 0) { - node_list->start_node = malloc(sizeof(IDNode)); - node_list->start_node->id = id; - node_list->start_node->next = NULL; - - node_list->node_count = 1; - - return; - } - - IDNode *last_node = node_list->start_node; - for (size_t i = 1; i < node_list->node_count; ++i) { - last_node = last_node->next; - } - - last_node->next = malloc(sizeof(IDNode)); - last_node->next->id = id; - last_node->next->next = NULL; - - ++(node_list->node_count); -} - -void remove_id_node(IDNodeList *node_list, AssetID id) { - if (node_list->node_count == 0) { - return; - } else if (node_list->node_count == 1) { - free(node_list->start_node); - node_list->start_node = NULL; - - node_list->node_count = 0; - - return; - } - - IDNode *node = NULL; - IDNode *prev = NULL; - IDNode *current = node_list->start_node; - for (size_t i = 0; i < node_list->node_count; ++i) { - if (current->id == id) { - node = current; - break; - } - - prev = current; - current = current->next; - } - - if (node) { - IDNode *next = node->next; - if (!prev) { - // Handle the case when there is no previous node, which means that node - // is actually the start node in the list. - free(node_list->start_node); - - node_list->start_node = next; - } else { - free(prev->next); - - prev->next = next; - } - - --(node_list->node_count); - } -} +void remove_id_node(IDNodeList *node_list, AssetID id); AssetID get_asset_id_from_id_node(AssetManager *am, IDNodeList *node_list, - const char *path) { - /** - * Checks to see if an asset with the same path exists and returns the ID of - * that asset if it exists, or an INVALID_ID otherwise - */ - - if (node_list->node_count == 0) { - return INVALID_ID; - } - - IDNode *id_node = node_list->start_node; - for (size_t i = 0; i < node_list->node_count; ++i) { - if (!id_node) { - break; - } - - Asset asset = am->assets[id_node->id]; - - if (strcmp(asset.path, path) == 0) { - return id_node->id; - } - - id_node = id_node->next; - } - - return INVALID_ID; -} + const char *path); AssetManager *create_asset_manager() { AssetManager *am = (AssetManager *)malloc(sizeof(AssetManager)); @@ -421,3 +319,114 @@ void destroy_asset_manager(AssetManager **am) { free(*am); *am = NULL; } + +u64 hash_path(const char *path) { + /** + * Converts a string into a numerical index using a simple hashing algorithm + */ + + u64 i = 0; + + for (size_t j = 0; path[j]; ++j) { + i += (u64)path[j] * HASH_PRIME_01 % MAX_ASSET_COUNT; + } + + i = (i * HASH_PRIME_02) % MAX_ASSET_COUNT; + + return i; +} + +void insert_id_node(IDNodeList *node_list, AssetID id) { + if (node_list->node_count == 0) { + node_list->start_node = malloc(sizeof(IDNode)); + node_list->start_node->id = id; + node_list->start_node->next = NULL; + + node_list->node_count = 1; + + return; + } + + IDNode *last_node = node_list->start_node; + for (size_t i = 1; i < node_list->node_count; ++i) { + last_node = last_node->next; + } + + last_node->next = malloc(sizeof(IDNode)); + last_node->next->id = id; + last_node->next->next = NULL; + + ++(node_list->node_count); +} + +void remove_id_node(IDNodeList *node_list, AssetID id) { + if (node_list->node_count == 0) { + return; + } else if (node_list->node_count == 1) { + free(node_list->start_node); + node_list->start_node = NULL; + + node_list->node_count = 0; + + return; + } + + IDNode *node = NULL; + IDNode *prev = NULL; + IDNode *current = node_list->start_node; + for (size_t i = 0; i < node_list->node_count; ++i) { + if (current->id == id) { + node = current; + break; + } + + prev = current; + current = current->next; + } + + if (node) { + IDNode *next = node->next; + if (!prev) { + // Handle the case when there is no previous node, which means that node + // is actually the start node in the list. + free(node_list->start_node); + + node_list->start_node = next; + } else { + free(prev->next); + + prev->next = next; + } + + --(node_list->node_count); + } +} + +AssetID get_asset_id_from_id_node(AssetManager *am, IDNodeList *node_list, + const char *path) { + /** + * Checks to see if an asset with the same path exists and returns the ID of + * that asset if it exists, or an INVALID_ID otherwise + */ + + if (node_list->node_count == 0) { + return INVALID_ID; + } + + IDNode *id_node = node_list->start_node; + for (size_t i = 0; i < node_list->node_count; ++i) { + if (!id_node) { + break; + } + + Asset asset = am->assets[id_node->id]; + + if (strcmp(asset.path, path) == 0) { + return id_node->id; + } + + id_node = id_node->next; + } + + return INVALID_ID; +} diff --git a/src/ObjectStore.c b/src/ObjectStore.c index c2445a9..4d3194c 100644 --- a/src/ObjectStore.c +++ b/src/ObjectStore.c @@ -23,6 +23,8 @@ struct object_store { RenderFuncPtr render_funcs[MAX_OBJ_COUNT]; }; +bool object_exists(ObjectStore *os, void *object); + ObjectStore *create_object_store() { ObjectStore *os = (ObjectStore *)malloc(sizeof(ObjectStore)); if (!os) { @@ -42,19 +44,6 @@ ObjectStore *create_object_store() { return os; } -bool object_exists(ObjectStore *os, void *object) { - // This is a crude implementation for checking that an object has already - // been added. - // TODO (Abdelrahman): Figure out if there is a better way of doing this - for (size_t i = 0; i < MAX_OBJ_COUNT; ++i) { - if (object == os->object_ptrs[i]) { - return true; - } - } - - return false; -} - ObjectID add_object_to_store(ObjectStore *os, void *object, EventHandlerPtr eh, UpdateFuncPtr uf, RenderFuncPtr rf) { if (!os || !object || object_exists(os, object) || @@ -164,6 +153,15 @@ void destroy_object_store(ObjectStore **os) { *os = NULL; } -#ifdef DEBUG_ENGINE -void print_objects(ObjectStore *os) {} -#endif /* DEBUG_ENGINE */ +bool object_exists(ObjectStore *os, void *object) { + // This is a crude implementation for checking that an object has already + // been added. + // TODO (Abdelrahman): Figure out if there is a better way of doing this + for (size_t i = 0; i < MAX_OBJ_COUNT; ++i) { + if (object == os->object_ptrs[i]) { + return true; + } + } + + return false; +}