diff --git a/orgfile.org b/orgfile.org index 33f5948..521d10a 100644 --- a/orgfile.org +++ b/orgfile.org @@ -136,6 +136,7 @@ x Font atlas proper cleanup - Decoupled event handling of gui and input if possible - Custom rendering for gui ** TODO Allow passsing base path as commandline argument? +** TODO Variant -> String conversion procedure. Use in editor for debug var slots ** DONE Add strings and booleans to variant types - State "DONE" from "TODO" [2017-03-29 Wed 00:23] ** DONE Fix Key release not being reported diff --git a/src/game.c b/src/game.c index 0545c5c..444a00e 100644 --- a/src/game.c +++ b/src/game.c @@ -26,6 +26,8 @@ #include "gui.h" #include "sound.h" #include "editor.h" +#include "config_vars.h" +#include "hashmap.h" #define UNUSED(a) (void)a #define MIN(a,b) ((a) < (b) ? (a) : (b)) @@ -68,6 +70,7 @@ void game_init(struct Window* window) editor_init(); model_init(); entity_init(); + config_vars_init(); scene_init(); /* Debug scene setup */ @@ -210,6 +213,15 @@ void scene_setup(void) /* struct Entity* sun = scene_add_new("Sun", NULL); */ /* struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR); */ /* sun_light->intensity = 0.8f; */ + + struct Hashmap* cvars = config_vars_get(); + hashmap_int_set(cvars, "My_Int", 20); + hashmap_str_set(cvars, "My_String", "This is my string"); + hashmap_float_set(cvars, "Some_FLOAT", 42.222f); + hashmap_double_set(cvars, "Some_Double", 99.999); + hashmap_bool_set(cvars, "The_Truth", 0); + + hashmap_debug_print(cvars); } void debug(float dt) @@ -1599,6 +1611,7 @@ void game_cleanup(void) { editor_cleanup(); scene_cleanup(); + config_vars_cleanup(); entity_cleanup(); model_cleanup(); material_cleanup(); diff --git a/src/hashmap.c b/src/hashmap.c index e430ebd..ef40482 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,6 +1,7 @@ #include "hashmap.h" #include "array.h" #include "variant.h" +#include "log.h" #include "string_utils.h" #include @@ -20,12 +21,12 @@ struct Hashmap struct Hashmap_Entry* buckets[HASH_MAP_NUM_BUCKETS]; }; -static int hashmap_generate_hash(const char* key); +static unsigned int hashmap_generate_hash(const char* key); static struct Hashmap_Entry* hashmap_entry_new(struct Hashmap* hashmap, const char* key); static struct Hashmap_Entry* hashmap_entry_new(struct Hashmap* hashmap, const char* key) { - int index = hashmap_generate_hash(key); + unsigned int index = hashmap_generate_hash(key); struct Hashmap_Entry* new_entry = NULL; for(int i = 0; i < array_len(hashmap->buckets[index]); i++) /* Look for duplicates and over-write if found */ { @@ -41,10 +42,10 @@ static struct Hashmap_Entry* hashmap_entry_new(struct Hashmap* hashmap, const ch return new_entry; } -int hashmap_generate_hash(const char* key) +unsigned int hashmap_generate_hash(const char* key) { - int index = -1; - const int multiplier = 51; + unsigned int index = 0; + const int multiplier = 51; for(int i = 0; i < (int)strlen(key); i++) index = index * multiplier + key[i]; return index % HASH_MAP_NUM_BUCKETS; @@ -65,7 +66,7 @@ void hashmap_free(struct Hashmap* hashmap) if(!hashmap) return; for(int i = 0; i < HASH_MAP_NUM_BUCKETS; i++) { - for(int j = 0; j < array_len(hashmap->buckets[j]); j++) + for(int j = 0; j < array_len(hashmap->buckets[i]); j++) { struct Hashmap_Entry* entry = &hashmap->buckets[i][j]; if(entry->key) @@ -75,6 +76,8 @@ void hashmap_free(struct Hashmap* hashmap) } variant_free(&entry->value); } + array_free(hashmap->buckets[i]); + hashmap->buckets[i] = NULL; } free(hashmap); hashmap = NULL; @@ -91,7 +94,7 @@ const struct Variant* hashmap_value_get(struct Hashmap* hashmap, const char* key { if(!hashmap || !key) return NULL; struct Variant* value = NULL; - int index = hashmap_generate_hash(key); + unsigned int index = hashmap_generate_hash(key); for(int i = 0; i < array_len(hashmap->buckets[index]); i++) { if(strncmp(key, hashmap->buckets[index][i].key, HASH_MAX_KEY_LEN) == 0) @@ -106,7 +109,7 @@ const struct Variant* hashmap_value_get(struct Hashmap* hashmap, const char* key void hashmap_value_remove(struct Hashmap* hashmap, const char* key) { if(!hashmap || !key) return; - int index = hashmap_generate_hash(key); + unsigned int index = hashmap_generate_hash(key); int index_to_remove = -1; for(int i = 0; i < array_len(hashmap->buckets[index]); i++) { @@ -251,3 +254,24 @@ void* hashmap_ptr_get(struct Hashmap* hashmap, const char* key) const struct Variant* variant = hashmap_value_get(hashmap, key); return variant->val_voidptr; } + +void hashmap_debug_print(struct Hashmap* hashmap) +{ + if(!hashmap) return; + static char str[128]; + memset(str, '\0', 128); + for(int i = 0; i < HASH_MAP_NUM_BUCKETS; i++) + { + log_message("Bucket : %d", i); + log_message("Bucket len : %d", array_len(hashmap->buckets[i])); + for(int j = 0; j < array_len(hashmap->buckets[i]); j++) + { + struct Hashmap_Entry* entry = &hashmap->buckets[i][j]; + const struct Variant* value = &entry->value; + log_message("Key : %s", entry->key); + variant_to_str(value, str, 128); + log_message("Value : %s", str); + memset(str, '\0', 128); + } + } +} diff --git a/src/hashmap.h b/src/hashmap.h index 23a6da7..4ba4671 100644 --- a/src/hashmap.h +++ b/src/hashmap.h @@ -36,4 +36,6 @@ const mat4* hashmap_mat4_get(struct Hashmap* hashmap, const char* key); const char* hashmap_str_get(struct Hashmap* hashmap, const char* key); void* hashmap_ptr_get(struct Hashmap* hashmap, const char* key); +void hashmap_debug_print(struct Hashmap* hashmap); + #endif diff --git a/src/variant.c b/src/variant.c index 83826d2..d05de67 100644 --- a/src/variant.c +++ b/src/variant.c @@ -3,6 +3,7 @@ #include "string_utils.h" #include +#include void variant_init_empty(struct Variant* variant) { @@ -140,3 +141,21 @@ void variant_copy(struct Variant* to, const struct Variant* from) break; } } + +void variant_to_str(const struct Variant* variant, char* str, int len) +{ + switch(variant->type) + { + case VT_BOOL: snprintf(str, len, "%s", variant->val_bool ? "true" : "false"); break; + case VT_INT: snprintf(str, len, "%d", variant->val_int); break; + case VT_FLOAT: snprintf(str, len, "%.4f", variant->val_float); break; + case VT_DOUBLE: snprintf(str, len, "%.4f", variant->val_double); break; + case VT_VEC2: snprintf(str, len, "%.3f, %.3f", variant->val_vec2.x, variant->val_vec2.y); break; + case VT_VEC3: snprintf(str, len, "%.3f, %.3f, %.3f", variant->val_vec3.x, variant->val_vec3.y, variant->val_vec3.z); break; + case VT_VEC4: snprintf(str, len, "%.3f, %.3f, %.3f, %.3f", variant->val_vec4.x, variant->val_vec4.y, variant->val_vec4.z, variant->val_vec4.w); break; + case VT_QUAT: snprintf(str, len, "%.3f, %.3f, %.3f, %.3f", variant->val_quat.x, variant->val_quat.y, variant->val_quat.z, variant->val_quat.w); break; + case VT_STR: snprintf(str, len, "%s", variant->val_str); break; + case VT_NONE: snprintf(str, len, "%s", "NONE"); break; + default: snprintf(str, len, "Unsupported Variant type"); break; + } +} diff --git a/src/variant.h b/src/variant.h index e3e75de..7ceb3be 100644 --- a/src/variant.h +++ b/src/variant.h @@ -53,5 +53,6 @@ void variant_assign_mat4(struct Variant* variant, const mat4* source); void variant_assign_ptr(struct Variant* variant, void* source); void variant_copy(struct Variant* to, const struct Variant* from); void variant_free(struct Variant* variant); +void variant_to_str(const struct Variant* variant, char* str, int len); #endif