Added aliases with sizes for the integer and floating-point numbers
This commit is contained in:
1 parent
45f6021f82
commit
5e6211c2a9
14 files changed
+146
-136
No files matched your search
+17
-18
@@ -5,11 +5,10 @@
|
||||
#include "SDL_mixer.h"
|
||||
#include "SDL_render.h"
|
||||
#include "SDL_ttf.h"
|
||||
#include "aliases.h"
|
||||
#include "constants.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -33,27 +32,27 @@ struct id_node {
|
||||
|
||||
typedef struct id_node_list {
|
||||
IDNode *start_node;
|
||||
size_t node_count;
|
||||
u64 node_count;
|
||||
} IDNodeList;
|
||||
|
||||
struct asset_manager {
|
||||
size_t count;
|
||||
size_t deleted_count;
|
||||
u64 count;
|
||||
u64 deleted_count;
|
||||
AssetID reusable_ids[MAX_ASSET_COUNT];
|
||||
Asset assets[MAX_ASSET_COUNT];
|
||||
size_t user_counter[MAX_ASSET_COUNT];
|
||||
u64 user_counter[MAX_ASSET_COUNT];
|
||||
IDNodeList hashed_path_ids[MAX_ASSET_COUNT];
|
||||
};
|
||||
|
||||
size_t hash_path(const char *path) {
|
||||
u64 hash_path(const char *path) {
|
||||
/**
|
||||
* Converts a string into a numerical index using a simple hashing algorithm
|
||||
*/
|
||||
|
||||
size_t i = 0;
|
||||
u64 i = 0;
|
||||
|
||||
for (size_t j = 0; path[j]; ++j) {
|
||||
i += (size_t)path[j] * HASH_PRIME_01 % MAX_ASSET_COUNT;
|
||||
for (u64 j = 0; path[j]; ++j) {
|
||||
i += (u64)path[j] * HASH_PRIME_01 % MAX_ASSET_COUNT;
|
||||
}
|
||||
|
||||
i = (i * HASH_PRIME_02) % MAX_ASSET_COUNT;
|
||||
@@ -73,7 +72,7 @@ void insert_id_node(IDNodeList *node_list, AssetID id) {
|
||||
}
|
||||
|
||||
IDNode *last_node = node_list->start_node;
|
||||
for (size_t i = 1; i < node_list->node_count; ++i) {
|
||||
for (u64 i = 1; i < node_list->node_count; ++i) {
|
||||
last_node = last_node->next;
|
||||
}
|
||||
|
||||
@@ -99,7 +98,7 @@ void remove_id_node(IDNodeList *node_list, AssetID id) {
|
||||
IDNode *node = NULL;
|
||||
IDNode *prev = NULL;
|
||||
IDNode *current = node_list->start_node;
|
||||
for (size_t i = 0; i < node_list->node_count; ++i) {
|
||||
for (u64 i = 0; i < node_list->node_count; ++i) {
|
||||
if (current->id == id) {
|
||||
node = current;
|
||||
break;
|
||||
@@ -139,7 +138,7 @@ AssetID get_asset_id_from_id_node(AssetManager *am, IDNodeList *node_list,
|
||||
}
|
||||
|
||||
IDNode *id_node = node_list->start_node;
|
||||
for (size_t i = 0; i < node_list->node_count; ++i) {
|
||||
for (u64 i = 0; i < node_list->node_count; ++i) {
|
||||
if (!id_node) {
|
||||
break;
|
||||
}
|
||||
@@ -178,7 +177,7 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
|
||||
return INVALID_ID;
|
||||
}
|
||||
|
||||
size_t hash_index = hash_path(path);
|
||||
u64 hash_index = hash_path(path);
|
||||
|
||||
AssetID existing_id =
|
||||
get_asset_id_from_id_node(am, &(am->hashed_path_ids[hash_index]), path);
|
||||
@@ -255,7 +254,7 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
|
||||
AssetID id;
|
||||
|
||||
if (am->deleted_count > 0) {
|
||||
id = (AssetID)reuse_id((int32_t *)am->reusable_ids, am->deleted_count);
|
||||
id = (AssetID)reuse_id((i32 *)am->reusable_ids, am->deleted_count);
|
||||
am->deleted_count -= 1;
|
||||
am->count += 1;
|
||||
} else {
|
||||
@@ -308,7 +307,7 @@ void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
size_t hash_index = hash_path(am->assets[id].path);
|
||||
u64 hash_index = hash_path(am->assets[id].path);
|
||||
remove_id_node(&(am->hashed_path_ids[hash_index]), id);
|
||||
|
||||
free(am->assets[id].path);
|
||||
@@ -380,14 +379,14 @@ void destroy_asset_manager(AssetManager **am) {
|
||||
// TODO (Abdelrahman): Figure out if there is a better way to remove all of
|
||||
// the existing assets without having to loop for the maximum number of
|
||||
// assets allowed.
|
||||
for (size_t i = 0; i < MAX_ASSET_COUNT; ++i) {
|
||||
for (u64 i = 0; i < MAX_ASSET_COUNT; ++i) {
|
||||
if (!(*am)->assets[i].path) {
|
||||
continue;
|
||||
}
|
||||
|
||||
(*am)->user_counter[i] = 0;
|
||||
|
||||
size_t hash_index = hash_path((*am)->assets[i].path);
|
||||
u64 hash_index = hash_path((*am)->assets[i].path);
|
||||
remove_id_node(&((*am)->hashed_path_ids[hash_index]), i);
|
||||
|
||||
free((*am)->assets[i].path);
|
||||
|
||||
Reference in new issue
Block a user