Reorganised the ObjectStore and AssetManager files
This commit is contained in:
1 parent
24a86ba6e3
commit
681fa2a5dc
3 files changed
+129
-126
No files matched your search
@@ -49,10 +49,6 @@ void render_all_objects(ObjectStore *os);
|
|||||||
*/
|
*/
|
||||||
void destroy_object_store(ObjectStore **os);
|
void destroy_object_store(ObjectStore **os);
|
||||||
|
|
||||||
#ifdef DEBUG_ENGINE
|
|
||||||
void print_objects(ObjectStore *os);
|
|
||||||
#endif // !DEBUG_ENGINE
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif // !__cplusplus
|
#endif // !__cplusplus
|
||||||
|
|||||||
+115
-106
@@ -45,116 +45,14 @@ struct asset_manager {
|
|||||||
IDNodeList hashed_path_ids[MAX_ASSET_COUNT];
|
IDNodeList hashed_path_ids[MAX_ASSET_COUNT];
|
||||||
};
|
};
|
||||||
|
|
||||||
u64 hash_path(const char *path) {
|
u64 hash_path(const char *path);
|
||||||
/**
|
|
||||||
* Converts a string into a numerical index using a simple hashing algorithm
|
|
||||||
*/
|
|
||||||
|
|
||||||
u64 i = 0;
|
void insert_id_node(IDNodeList *node_list, AssetID id);
|
||||||
|
|
||||||
for (size_t j = 0; path[j]; ++j) {
|
void remove_id_node(IDNodeList *node_list, AssetID id);
|
||||||
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,
|
AssetID get_asset_id_from_id_node(AssetManager *am, IDNodeList *node_list,
|
||||||
const char *path) {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
AssetManager *create_asset_manager() {
|
AssetManager *create_asset_manager() {
|
||||||
AssetManager *am = (AssetManager *)malloc(sizeof(AssetManager));
|
AssetManager *am = (AssetManager *)malloc(sizeof(AssetManager));
|
||||||
@@ -421,3 +319,114 @@ void destroy_asset_manager(AssetManager **am) {
|
|||||||
free(*am);
|
free(*am);
|
||||||
*am = NULL;
|
*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;
|
||||||
|
}
|
||||||
+14
-16
@@ -23,6 +23,8 @@ struct object_store {
|
|||||||
RenderFuncPtr render_funcs[MAX_OBJ_COUNT];
|
RenderFuncPtr render_funcs[MAX_OBJ_COUNT];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool object_exists(ObjectStore *os, void *object);
|
||||||
|
|
||||||
ObjectStore *create_object_store() {
|
ObjectStore *create_object_store() {
|
||||||
ObjectStore *os = (ObjectStore *)malloc(sizeof(ObjectStore));
|
ObjectStore *os = (ObjectStore *)malloc(sizeof(ObjectStore));
|
||||||
if (!os) {
|
if (!os) {
|
||||||
@@ -42,19 +44,6 @@ ObjectStore *create_object_store() {
|
|||||||
return os;
|
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,
|
ObjectID add_object_to_store(ObjectStore *os, void *object, EventHandlerPtr eh,
|
||||||
UpdateFuncPtr uf, RenderFuncPtr rf) {
|
UpdateFuncPtr uf, RenderFuncPtr rf) {
|
||||||
if (!os || !object || object_exists(os, object) ||
|
if (!os || !object || object_exists(os, object) ||
|
||||||
@@ -164,6 +153,15 @@ void destroy_object_store(ObjectStore **os) {
|
|||||||
*os = NULL;
|
*os = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_ENGINE
|
bool object_exists(ObjectStore *os, void *object) {
|
||||||
void print_objects(ObjectStore *os) {}
|
// This is a crude implementation for checking that an object has already
|
||||||
#endif /* DEBUG_ENGINE */
|
// 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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user