Added conversion to string for variant

dev
Shariq Shah 9 years ago
parent a17cb85b92
commit b1f20862f2
  1. 1
      orgfile.org
  2. 13
      src/game.c
  3. 38
      src/hashmap.c
  4. 2
      src/hashmap.h
  5. 19
      src/variant.c
  6. 1
      src/variant.h

@ -136,6 +136,7 @@ x Font atlas proper cleanup
- Decoupled event handling of gui and input if possible - Decoupled event handling of gui and input if possible
- Custom rendering for gui - Custom rendering for gui
** TODO Allow passsing base path as commandline argument? ** 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 ** DONE Add strings and booleans to variant types
- State "DONE" from "TODO" [2017-03-29 Wed 00:23] - State "DONE" from "TODO" [2017-03-29 Wed 00:23]
** DONE Fix Key release not being reported ** DONE Fix Key release not being reported

@ -26,6 +26,8 @@
#include "gui.h" #include "gui.h"
#include "sound.h" #include "sound.h"
#include "editor.h" #include "editor.h"
#include "config_vars.h"
#include "hashmap.h"
#define UNUSED(a) (void)a #define UNUSED(a) (void)a
#define MIN(a,b) ((a) < (b) ? (a) : (b)) #define MIN(a,b) ((a) < (b) ? (a) : (b))
@ -68,6 +70,7 @@ void game_init(struct Window* window)
editor_init(); editor_init();
model_init(); model_init();
entity_init(); entity_init();
config_vars_init();
scene_init(); scene_init();
/* Debug scene setup */ /* Debug scene setup */
@ -210,6 +213,15 @@ void scene_setup(void)
/* struct Entity* sun = scene_add_new("Sun", NULL); */ /* struct Entity* sun = scene_add_new("Sun", NULL); */
/* struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR); */ /* struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR); */
/* sun_light->intensity = 0.8f; */ /* 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) void debug(float dt)
@ -1599,6 +1611,7 @@ void game_cleanup(void)
{ {
editor_cleanup(); editor_cleanup();
scene_cleanup(); scene_cleanup();
config_vars_cleanup();
entity_cleanup(); entity_cleanup();
model_cleanup(); model_cleanup();
material_cleanup(); material_cleanup();

@ -1,6 +1,7 @@
#include "hashmap.h" #include "hashmap.h"
#include "array.h" #include "array.h"
#include "variant.h" #include "variant.h"
#include "log.h"
#include "string_utils.h" #include "string_utils.h"
#include <stdlib.h> #include <stdlib.h>
@ -20,12 +21,12 @@ struct Hashmap
struct Hashmap_Entry* buckets[HASH_MAP_NUM_BUCKETS]; 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);
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; 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 */ for(int i = 0; i < array_len(hashmap->buckets[index]); i++) /* Look for duplicates and over-write if found */
{ {
@ -41,9 +42,9 @@ static struct Hashmap_Entry* hashmap_entry_new(struct Hashmap* hashmap, const ch
return new_entry; return new_entry;
} }
int hashmap_generate_hash(const char* key) unsigned int hashmap_generate_hash(const char* key)
{ {
int index = -1; unsigned int index = 0;
const int multiplier = 51; const int multiplier = 51;
for(int i = 0; i < (int)strlen(key); i++) for(int i = 0; i < (int)strlen(key); i++)
index = index * multiplier + key[i]; index = index * multiplier + key[i];
@ -65,7 +66,7 @@ void hashmap_free(struct Hashmap* hashmap)
if(!hashmap) return; if(!hashmap) return;
for(int i = 0; i < HASH_MAP_NUM_BUCKETS; i++) 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]; struct Hashmap_Entry* entry = &hashmap->buckets[i][j];
if(entry->key) if(entry->key)
@ -75,6 +76,8 @@ void hashmap_free(struct Hashmap* hashmap)
} }
variant_free(&entry->value); variant_free(&entry->value);
} }
array_free(hashmap->buckets[i]);
hashmap->buckets[i] = NULL;
} }
free(hashmap); free(hashmap);
hashmap = NULL; hashmap = NULL;
@ -91,7 +94,7 @@ const struct Variant* hashmap_value_get(struct Hashmap* hashmap, const char* key
{ {
if(!hashmap || !key) return NULL; if(!hashmap || !key) return NULL;
struct Variant* value = 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++) for(int i = 0; i < array_len(hashmap->buckets[index]); i++)
{ {
if(strncmp(key, hashmap->buckets[index][i].key, HASH_MAX_KEY_LEN) == 0) 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) void hashmap_value_remove(struct Hashmap* hashmap, const char* key)
{ {
if(!hashmap || !key) return; if(!hashmap || !key) return;
int index = hashmap_generate_hash(key); unsigned int index = hashmap_generate_hash(key);
int index_to_remove = -1; int index_to_remove = -1;
for(int i = 0; i < array_len(hashmap->buckets[index]); i++) 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); const struct Variant* variant = hashmap_value_get(hashmap, key);
return variant->val_voidptr; 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);
}
}
}

@ -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); const char* hashmap_str_get(struct Hashmap* hashmap, const char* key);
void* hashmap_ptr_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 #endif

@ -3,6 +3,7 @@
#include "string_utils.h" #include "string_utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
void variant_init_empty(struct Variant* variant) void variant_init_empty(struct Variant* variant)
{ {
@ -140,3 +141,21 @@ void variant_copy(struct Variant* to, const struct Variant* from)
break; 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;
}
}

@ -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_assign_ptr(struct Variant* variant, void* source);
void variant_copy(struct Variant* to, const struct Variant* from); void variant_copy(struct Variant* to, const struct Variant* from);
void variant_free(struct Variant* variant); void variant_free(struct Variant* variant);
void variant_to_str(const struct Variant* variant, char* str, int len);
#endif #endif

Loading…
Cancel
Save