Added foreach loop macro for hashmaps

dev
shariq 9 years ago
parent c7f3d480ea
commit ff51f3f990
  1. 2
      build/linux/makefile
  2. 69
      include/SDL2/SDL_config_linux.h
  3. BIN
      libs/debug/linux64_gcc/libSDL2.a
  4. BIN
      libs/debug/linux64_gcc/libopenal.a
  5. BIN
      libs/release/linux64_gcc/libSDL2.a
  6. BIN
      libs/release/linux64_gcc/libopenal.a
  7. 2
      orgfile.org
  8. 17
      src/editor.c
  9. 32
      src/game.c
  10. 64
      src/hashmap.c
  11. 48
      src/hashmap.h
  12. 3
      src/variant.c
  13. 2
      src/variant.h

@ -11,7 +11,7 @@ OBJS_DEBUG = $(patsubst %.c,.debug/%.o,$(SRCS))
CFLAGS = -Wall -I$(INCLUDE_DIR) -DUSE_GLAD CFLAGS = -Wall -I$(INCLUDE_DIR) -DUSE_GLAD
CFLAGS_DEBUG = -g -DGL_DEBUG_CONTEXT -DAL_DEBUG -std=c99 CFLAGS_DEBUG = -g -DGL_DEBUG_CONTEXT -DAL_DEBUG -std=c99
CFLAGS_RELEASE = -O3 -ffast-math CFLAGS_RELEASE = -O3 -ffast-math
LFLAGS = -lSDL2 -lpthread -ldl -lm -lopenal -lsndio -lGL LFLAGS = -lSDL2 -lpthread -ldl -lm -lopenal
all: release all: release

@ -47,7 +47,7 @@
/* #undef HAVE_DSOUND_H */ /* #undef HAVE_DSOUND_H */
/* #undef HAVE_DINPUT_H */ /* #undef HAVE_DINPUT_H */
/* #undef HAVE_XAUDIO2_H */ /* #undef HAVE_XAUDIO2_H */
#define HAVE_XINPUT_H 1 /* #undef HAVE_XINPUT_H */
/* #undef HAVE_DXGI_H */ /* #undef HAVE_DXGI_H */
/* Comment this if you want to build without any C library requirements */ /* Comment this if you want to build without any C library requirements */
@ -73,10 +73,10 @@
#define HAVE_SIGNAL_H 1 #define HAVE_SIGNAL_H 1
/* #undef HAVE_ALTIVEC_H */ /* #undef HAVE_ALTIVEC_H */
/* #undef HAVE_PTHREAD_NP_H */ /* #undef HAVE_PTHREAD_NP_H */
#define HAVE_LIBUDEV_H 1 /* #undef HAVE_LIBUDEV_H */
#define HAVE_DBUS_DBUS_H 1 /* #undef HAVE_DBUS_DBUS_H */
#define HAVE_IBUS_IBUS_H 1 /* #undef HAVE_IBUS_IBUS_H */
#define HAVE_FCITX_FRONTEND_H 1 /* #undef HAVE_FCITX_FRONTEND_H */
/* C library functions */ /* C library functions */
#define HAVE_MALLOC 1 #define HAVE_MALLOC 1
@ -178,14 +178,14 @@
/* Allow disabling of core subsystems */ /* Allow disabling of core subsystems */
/* #undef SDL_ATOMIC_DISABLED */ /* #undef SDL_ATOMIC_DISABLED */
/* #undef SDL_AUDIO_DISABLED */ #define SDL_AUDIO_DISABLED 1
/* #undef SDL_CPUINFO_DISABLED */ /* #undef SDL_CPUINFO_DISABLED */
/* #undef SDL_EVENTS_DISABLED */ /* #undef SDL_EVENTS_DISABLED */
/* #undef SDL_FILE_DISABLED */ /* #undef SDL_FILE_DISABLED */
/* #undef SDL_JOYSTICK_DISABLED */ /* #undef SDL_JOYSTICK_DISABLED */
/* #undef SDL_HAPTIC_DISABLED */ /* #undef SDL_HAPTIC_DISABLED */
/* #undef SDL_LOADSO_DISABLED */ /* #undef SDL_LOADSO_DISABLED */
/* #undef SDL_RENDER_DISABLED */ #define SDL_RENDER_DISABLED 1
/* #undef SDL_THREADS_DISABLED */ /* #undef SDL_THREADS_DISABLED */
/* #undef SDL_TIMERS_DISABLED */ /* #undef SDL_TIMERS_DISABLED */
/* #undef SDL_VIDEO_DISABLED */ /* #undef SDL_VIDEO_DISABLED */
@ -194,26 +194,26 @@
/* Enable various audio drivers */ /* Enable various audio drivers */
/* #undef SDL_AUDIO_DRIVER_ANDROID */ /* #undef SDL_AUDIO_DRIVER_ANDROID */
#define SDL_AUDIO_DRIVER_ALSA 1 /* #undef SDL_AUDIO_DRIVER_ALSA */
#define SDL_AUDIO_DRIVER_ALSA_DYNAMIC "libasound.so.2" /* #undef SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
/* #undef SDL_AUDIO_DRIVER_ARTS */ /* #undef SDL_AUDIO_DRIVER_ARTS */
/* #undef SDL_AUDIO_DRIVER_ARTS_DYNAMIC */ /* #undef SDL_AUDIO_DRIVER_ARTS_DYNAMIC */
#define SDL_AUDIO_DRIVER_PULSEAUDIO 1 /* #undef SDL_AUDIO_DRIVER_PULSEAUDIO */
#define SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC "libpulse-simple.so.0" /* #undef SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC */
/* #undef SDL_AUDIO_DRIVER_HAIKU */ /* #undef SDL_AUDIO_DRIVER_HAIKU */
/* #undef SDL_AUDIO_DRIVER_BSD */ /* #undef SDL_AUDIO_DRIVER_BSD */
/* #undef SDL_AUDIO_DRIVER_COREAUDIO */ /* #undef SDL_AUDIO_DRIVER_COREAUDIO */
#define SDL_AUDIO_DRIVER_DISK 1 /* #undef SDL_AUDIO_DRIVER_DISK */
#define SDL_AUDIO_DRIVER_DUMMY 1 /* #undef SDL_AUDIO_DRIVER_DUMMY */
/* #undef SDL_AUDIO_DRIVER_XAUDIO2 */ /* #undef SDL_AUDIO_DRIVER_XAUDIO2 */
/* #undef SDL_AUDIO_DRIVER_DSOUND */ /* #undef SDL_AUDIO_DRIVER_DSOUND */
#define SDL_AUDIO_DRIVER_ESD 1 /* #undef SDL_AUDIO_DRIVER_ESD */
#define SDL_AUDIO_DRIVER_ESD_DYNAMIC "libesd.so.0" /* #undef SDL_AUDIO_DRIVER_ESD_DYNAMIC */
#define SDL_AUDIO_DRIVER_NAS 1 /* #undef SDL_AUDIO_DRIVER_NAS */
#define SDL_AUDIO_DRIVER_NAS_DYNAMIC "libaudio.so.2" /* #undef SDL_AUDIO_DRIVER_NAS_DYNAMIC */
/* #undef SDL_AUDIO_DRIVER_SNDIO */ /* #undef SDL_AUDIO_DRIVER_SNDIO */
/* #undef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC */ /* #undef SDL_AUDIO_DRIVER_SNDIO_DYNAMIC */
#define SDL_AUDIO_DRIVER_OSS 1 /* #undef SDL_AUDIO_DRIVER_OSS */
/* #undef SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H */ /* #undef SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H */
/* #undef SDL_AUDIO_DRIVER_PAUDIO */ /* #undef SDL_AUDIO_DRIVER_PAUDIO */
/* #undef SDL_AUDIO_DRIVER_QSA */ /* #undef SDL_AUDIO_DRIVER_QSA */
@ -291,19 +291,19 @@
#define SDL_VIDEO_DRIVER_X11 1 #define SDL_VIDEO_DRIVER_X11 1
#define SDL_VIDEO_DRIVER_X11_DYNAMIC "libX11.so.6" #define SDL_VIDEO_DRIVER_X11_DYNAMIC "libX11.so.6"
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "libXext.so.6" #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT "libXext.so.6"
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR "libXcursor.so.1" /* #undef SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR */
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA "libXinerama.so.1" /* #undef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINERAMA */
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 "libXi.so.6" /* #undef SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 */
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR "libXrandr.so.2" /* #undef SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR */
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS "libXss.so.1" /* #undef SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS */
#define SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "libXxf86vm.so.1" #define SDL_VIDEO_DRIVER_X11_DYNAMIC_XVIDMODE "libXxf86vm.so.1"
#define SDL_VIDEO_DRIVER_X11_XCURSOR 1 /* #undef SDL_VIDEO_DRIVER_X11_XCURSOR */
/* #undef SDL_VIDEO_DRIVER_X11_XDBE */ /* #undef SDL_VIDEO_DRIVER_X11_XDBE */
#define SDL_VIDEO_DRIVER_X11_XINERAMA 1 /* #undef SDL_VIDEO_DRIVER_X11_XINERAMA */
#define SDL_VIDEO_DRIVER_X11_XINPUT2 1 /* #undef SDL_VIDEO_DRIVER_X11_XINPUT2 */
#define SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH 1 /* #undef SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_MULTITOUCH */
#define SDL_VIDEO_DRIVER_X11_XRANDR 1 /* #undef SDL_VIDEO_DRIVER_X11_XRANDR */
#define SDL_VIDEO_DRIVER_X11_XSCRNSAVER 1 /* #undef SDL_VIDEO_DRIVER_X11_XSCRNSAVER */
#define SDL_VIDEO_DRIVER_X11_XSHAPE 1 #define SDL_VIDEO_DRIVER_X11_XSHAPE 1
#define SDL_VIDEO_DRIVER_X11_XVIDMODE 1 #define SDL_VIDEO_DRIVER_X11_XVIDMODE 1
#define SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS 1 #define SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS 1
@ -313,19 +313,19 @@
/* #undef SDL_VIDEO_RENDER_D3D */ /* #undef SDL_VIDEO_RENDER_D3D */
/* #undef SDL_VIDEO_RENDER_D3D11 */ /* #undef SDL_VIDEO_RENDER_D3D11 */
#define SDL_VIDEO_RENDER_OGL 1 #define SDL_VIDEO_RENDER_OGL 1
#define SDL_VIDEO_RENDER_OGL_ES 1 /* #undef SDL_VIDEO_RENDER_OGL_ES */
#define SDL_VIDEO_RENDER_OGL_ES2 1 /* #undef SDL_VIDEO_RENDER_OGL_ES2 */
/* #undef SDL_VIDEO_RENDER_DIRECTFB */ /* #undef SDL_VIDEO_RENDER_DIRECTFB */
/* Enable OpenGL support */ /* Enable OpenGL support */
#define SDL_VIDEO_OPENGL 1 #define SDL_VIDEO_OPENGL 1
#define SDL_VIDEO_OPENGL_ES 1 /* #undef SDL_VIDEO_OPENGL_ES */
#define SDL_VIDEO_OPENGL_ES2 1 /* #undef SDL_VIDEO_OPENGL_ES2 */
/* #undef SDL_VIDEO_OPENGL_BGL */ /* #undef SDL_VIDEO_OPENGL_BGL */
/* #undef SDL_VIDEO_OPENGL_CGL */ /* #undef SDL_VIDEO_OPENGL_CGL */
#define SDL_VIDEO_OPENGL_GLX 1 #define SDL_VIDEO_OPENGL_GLX 1
/* #undef SDL_VIDEO_OPENGL_WGL */ /* #undef SDL_VIDEO_OPENGL_WGL */
#define SDL_VIDEO_OPENGL_EGL 1 /* #undef SDL_VIDEO_OPENGL_EGL */
/* #undef SDL_VIDEO_OPENGL_OSMESA */ /* #undef SDL_VIDEO_OPENGL_OSMESA */
/* #undef SDL_VIDEO_OPENGL_OSMESA_DYNAMIC */ /* #undef SDL_VIDEO_OPENGL_OSMESA_DYNAMIC */
@ -412,4 +412,5 @@ typedef unsigned int uintptr_t;
# endif /* !_STDINT_H_ && !HAVE_STDINT_H */ # endif /* !_STDINT_H_ && !HAVE_STDINT_H */
#endif /* __WIN32__ */ #endif /* __WIN32__ */
#endif #endif

Binary file not shown.

@ -95,6 +95,7 @@ msaa_levels: 8
** DONE Mesh/Model ** DONE Mesh/Model
- State "DONE" from "TODO" [2015-10-13 Tue 19:38] - State "DONE" from "TODO" [2015-10-13 Tue 19:38]
** TODO Add modifiers to input maps to enable combinations for example, c-x, m-k etc ** TODO Add modifiers to input maps to enable combinations for example, c-x, m-k etc
** TODO Only allocate hashmap bucket when required
** DONE Heirarchical Transforms ** DONE Heirarchical Transforms
** DONE Materials with textures ** DONE Materials with textures
- State "DONE" from "TODO" [2015-10-15 Thu 21:21] - State "DONE" from "TODO" [2015-10-15 Thu 21:21]
@ -171,6 +172,7 @@ x Font atlas proper cleanup
** TODO Do input maps really need to be queried by their string names? ** TODO Do input maps really need to be queried by their string names?
** TODO Reloading all the things! (textures/shaders/models/settings/entities etc) ** TODO Reloading all the things! (textures/shaders/models/settings/entities etc)
** TODO Separate Debug/Editor camera from the active camera in the scene that can be switched to at any time ** TODO Separate Debug/Editor camera from the active camera in the scene that can be switched to at any time
** TODO Use hashmaps for debugvar slots in editor
** DONE Live data views in editor ** DONE Live data views in editor
- State "DONE" from "TODO" [2017-03-22 Wed 02:14] - State "DONE" from "TODO" [2017-03-22 Wed 02:14]
** DONE Camera resize on window reisze ** DONE Camera resize on window reisze

@ -21,6 +21,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <string.h>
struct Editor_State struct Editor_State
{ {
@ -250,6 +251,7 @@ void editor_update(float dt)
/* Debug Vars Window */ /* Debug Vars Window */
if(editor_state.debug_vars_window) if(editor_state.debug_vars_window)
{ {
static char variant_str[MAX_VARIANT_STR_LEN] = {'\0'};
if(nk_begin_titled(context, "Debug_Variables_Window", "Debug Variables", nk_rect(20, 20, 300, 300), default_window_flags)) if(nk_begin_titled(context, "Debug_Variables_Window", "Debug Variables", nk_rect(20, 20, 300, 300), default_window_flags))
{ {
nk_layout_row_static(context, 250, 250, 2); nk_layout_row_static(context, 250, 250, 2);
@ -261,18 +263,9 @@ void editor_update(float dt)
if(debug_var->data.type == VT_NONE) continue; if(debug_var->data.type == VT_NONE) continue;
nk_layout_row_dynamic(context, 20, 2); nk_layout_row_dynamic(context, 20, 2);
nk_label(context, debug_var->name, NK_TEXT_ALIGN_LEFT); nk_label(context, debug_var->name, NK_TEXT_ALIGN_LEFT);
struct Variant* var_data = &debug_var->data; variant_to_str(&debug_var->data, variant_str, MAX_VARIANT_STR_LEN);
switch(debug_var->data.type) nk_label(context, variant_str, NK_TEXT_ALIGN_RIGHT);
{ memset(variant_str, '\0', MAX_VARIANT_STR_LEN);
case VT_INT: nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "%d", var_data->val_int); break;
case VT_FLOAT: nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "%.4f", var_data->val_float); break;
case VT_DOUBLE: nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "%d", var_data->val_double); break;
case VT_VEC2: nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "(%.2f, %.2f)", var_data->val_vec2.x, var_data->val_vec2.y); break;
case VT_VEC3: nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "(%.2f, %.2f, %.2f)", var_data->val_vec3.x, var_data->val_vec3.y, var_data->val_vec3.z); break;
case VT_VEC4: nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "(%.2f, %.2f, %.2f, %.2f)", var_data->val_vec4.x, var_data->val_vec4.y, var_data->val_vec4.z, var_data->val_vec4.w); break;
case VT_QUAT: nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "(%.2f, %.2f, %.2f, %.2f)", var_data->val_quat.x, var_data->val_quat.y, var_data->val_quat.z, var_data->val_quat.w); break;
default: nk_label(context, "Unsupported Value type", NK_TEXT_ALIGN_RIGHT); break;
};
} }
nk_group_end(context); nk_group_end(context);
} }

@ -215,22 +215,32 @@ void scene_setup(void)
/* 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(); */ struct Hashmap* cvars = config_vars_get();
/* hashmap_int_set(cvars, "My_Int", 20); */ hashmap_int_set(cvars, "My_Int", 20);
/* hashmap_str_set(cvars, "My_String", "This is my string"); */ hashmap_str_set(cvars, "My_String", "This is my string");
/* hashmap_float_set(cvars, "Some_FLOAT", 42.222f); */ hashmap_float_set(cvars, "Some_FLOAT", 42.222f);
/* hashmap_double_set(cvars, "Some_Double", 99.999); */ hashmap_double_set(cvars, "Some_Double", 99.999);
/* hashmap_bool_set(cvars, "The_Truth", 0); */ hashmap_bool_set(cvars, "The_Truth", 0);
char* key = NULL;
struct Variant* value = NULL;
char variant_str[256];
HASHMAP_FOREACH(cvars, key, value)
{
variant_to_str(value, variant_str, 256);
log_message("VAL :(%s) : (%s)", key, variant_str);
memset(variant_str, '\0', 256);
}
/* hashmap_float_set(cvars, "Some_FLOAT", 99.3f); */ /* hashmap_float_set(cvars, "Some_FLOAT", 99.3f); */
/* hashmap_debug_print(cvars); */ /* hashmap_debug_print(cvars); */
/* log_message("The value of Some_FLOAT is : %f", hashmap_float_get(cvars, "Some_FLOAT")); */ /* log_message("The value of Some_FLOAT is : %f", hashmap_float_get(cvars, "Some_FLOAT")); */
/* } */ /* } */
struct Variant variant; /* struct Variant variant; */
variant_from_str(&variant, "3.333333333333333333333", VT_DOUBLE); /* variant_from_str(&variant, "3.333333333333333333333", VT_DOUBLE); */
log_message("Variant val : %lf", variant.val_double); /* log_message("Variant val : %lf", variant.val_double); */
log_message("Variant type : %s", variant.type == VT_DOUBLE ? "expected" : "not what's expected"); /* log_message("Variant type : %s", variant.type == VT_DOUBLE ? "expected" : "not what's expected"); */
} }

@ -1,13 +1,12 @@
#include "hashmap.h" #include "hashmap.h"
#include "array.h"
#include "variant.h" #include "variant.h"
#include "log.h" #include "log.h"
#include "string_utils.h" #include "string_utils.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h>
#define HASH_MAP_NUM_BUCKETS 10
#define HASH_MAX_KEY_LEN 128 #define HASH_MAX_KEY_LEN 128
struct Hashmap_Entry struct Hashmap_Entry
@ -19,6 +18,7 @@ struct Hashmap_Entry
struct Hashmap struct Hashmap
{ {
struct Hashmap_Entry* buckets[HASH_MAP_NUM_BUCKETS]; struct Hashmap_Entry* buckets[HASH_MAP_NUM_BUCKETS];
int iter_bucket, iter_index;
}; };
static unsigned int hashmap_generate_hash(const char* key); static unsigned int hashmap_generate_hash(const char* key);
@ -58,6 +58,8 @@ struct Hashmap* hashmap_new(void)
return NULL; return NULL;
for(int i = 0; i < HASH_MAP_NUM_BUCKETS; i++) for(int i = 0; i < HASH_MAP_NUM_BUCKETS; i++)
hashmap->buckets[i] = array_new(struct Hashmap_Entry); hashmap->buckets[i] = array_new(struct Hashmap_Entry);
hashmap->iter_bucket = 0;
hashmap->iter_index = -1;
return hashmap; return hashmap;
} }
@ -90,7 +92,7 @@ void hashmap_value_set(struct Hashmap* hashmap, const char* key, const struct Va
variant_copy(&new_entry->value, value); variant_copy(&new_entry->value, value);
} }
const struct Variant* hashmap_value_get(struct Hashmap* hashmap, const char* key) const struct Variant* hashmap_value_get(const struct Hashmap* hashmap, const char* key)
{ {
if(!hashmap || !key) return NULL; if(!hashmap || !key) return NULL;
struct Variant* value = NULL; struct Variant* value = NULL;
@ -189,73 +191,73 @@ void hashmap_ptr_set(struct Hashmap* hashmap, const char* key, void* value)
variant_assign_ptr(&new_entry->value, value); variant_assign_ptr(&new_entry->value, value);
} }
float hashmap_float_get(struct Hashmap* hashmap, const char* key) float hashmap_float_get(const 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_float; return variant->val_float;
} }
int hashmap_int_get(struct Hashmap* hashmap, const char* key) int hashmap_int_get(const 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_int; return variant->val_int;
} }
double hashmap_double_get(struct Hashmap* hashmap, const char* key) double hashmap_double_get(const 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_double; return variant->val_double;
} }
int hashmap_get_bool(struct Hashmap* hashmap, const char* key) int hashmap_get_bool(const 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_bool; return variant->val_bool;
} }
vec2 hashmap_vec2_get(struct Hashmap* hashmap, const char* key) vec2 hashmap_vec2_get(const 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_vec2; return variant->val_vec2;
} }
vec3 hashmap_vec3_get(struct Hashmap* hashmap, const char* key) vec3 hashmap_vec3_get(const 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_vec3; return variant->val_vec3;
} }
vec4 hashmap_vec4_get(struct Hashmap* hashmap, const char* key) vec4 hashmap_vec4_get(const 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_vec4; return variant->val_vec4;
} }
quat hashmap_quat_get(struct Hashmap* hashmap, const char* key) quat hashmap_quat_get(const 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_quat; return variant->val_quat;
} }
const mat4* hashmap_mat4_get(struct Hashmap* hashmap, const char* key) const mat4* hashmap_mat4_get(const 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_mat4; return variant->val_mat4;
} }
const char* hashmap_str_get(struct Hashmap* hashmap, const char* key) const char* hashmap_str_get(const 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_str; return variant->val_str;
} }
void* hashmap_ptr_get(struct Hashmap* hashmap, const char* key) void* hashmap_ptr_get(const 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) void hashmap_debug_print(const struct Hashmap* hashmap)
{ {
if(!hashmap) return; if(!hashmap) return;
static char str[128]; static char str[128];
@ -268,10 +270,40 @@ void hashmap_debug_print(struct Hashmap* hashmap)
{ {
struct Hashmap_Entry* entry = &hashmap->buckets[i][j]; struct Hashmap_Entry* entry = &hashmap->buckets[i][j];
const struct Variant* value = &entry->value; const struct Variant* value = &entry->value;
log_message("Key : %s", entry->key); const char* key = entry->key;
log_message("Key : %s", key);
variant_to_str(value, str, 128); variant_to_str(value, str, 128);
log_message("Value : %s", str); log_message("Value : %s", str);
memset(str, '\0', 128); memset(str, '\0', 128);
} }
} }
} }
void hashmap_iter_begin(struct Hashmap* hashmap)
{
assert(hashmap);
hashmap->iter_bucket = 0;
hashmap->iter_index = -1;
}
int hashmap_iter_next(struct Hashmap* hashmap, char** key, struct Variant** value)
{
assert(hashmap);
for(;hashmap->iter_bucket < HASH_MAP_NUM_BUCKETS; hashmap->iter_bucket++)
{
if(hashmap->buckets[hashmap->iter_bucket])
{
if(++hashmap->iter_index < array_len(hashmap->buckets[hashmap->iter_bucket]))
{
*key = hashmap->buckets[hashmap->iter_bucket][hashmap->iter_index].key;
*value = &hashmap->buckets[hashmap->iter_bucket][hashmap->iter_index].value;
return 1;
}
else
{
hashmap->iter_index = -1;
}
}
}
return 0;
}

@ -2,6 +2,9 @@
#define HASHMAP_H #define HASHMAP_H
#include "linmath.h" #include "linmath.h"
#include "array.h"
#define HASH_MAP_NUM_BUCKETS 10
struct Hashmap; struct Hashmap;
struct Variant; struct Variant;
@ -10,12 +13,12 @@ struct Hashmap* hashmap_new(void);
void hashmap_free(struct Hashmap* hashmap); void hashmap_free(struct Hashmap* hashmap);
void hashmap_value_remove(struct Hashmap* hashmap, const char* key); void hashmap_value_remove(struct Hashmap* hashmap, const char* key);
void hashmap_value_set(struct Hashmap* hashmap, const char* key, const struct Variant* value); void hashmap_value_set(struct Hashmap* hashmap, const char* key, const struct Variant* value);
const struct Variant* hashmap_value_get(struct Hashmap* hashmap, const char* key); const struct Variant* hashmap_value_get(const struct Hashmap* hashmap, const char* key);
void hashmap_float_set(struct Hashmap* hashmap, const char* key, float value); void hashmap_float_set(struct Hashmap* hashmap, const char* key, const float value);
void hashmap_int_set(struct Hashmap* hashmap, const char* key, int value); void hashmap_int_set(struct Hashmap* hashmap, const char* key, const int value);
void hashmap_double_set(struct Hashmap* hashmap, const char* key, double value); void hashmap_double_set(struct Hashmap* hashmap, const char* key, const double value);
void hashmap_bool_set(struct Hashmap* hashmap, const char* key, int value); void hashmap_bool_set(struct Hashmap* hashmap, const char* key, const int value);
void hashmap_vec2_set(struct Hashmap* hashmap, const char* key, const vec2* value); void hashmap_vec2_set(struct Hashmap* hashmap, const char* key, const vec2* value);
void hashmap_vec3_set(struct Hashmap* hashmap, const char* key, const vec3* value); void hashmap_vec3_set(struct Hashmap* hashmap, const char* key, const vec3* value);
void hashmap_vec4_set(struct Hashmap* hashmap, const char* key, const vec4* value); void hashmap_vec4_set(struct Hashmap* hashmap, const char* key, const vec4* value);
@ -24,18 +27,27 @@ void hashmap_mat4_set(struct Hashmap* hashmap, const char* key, const
void hashmap_str_set(struct Hashmap* hashmap, const char* key, const char* value); void hashmap_str_set(struct Hashmap* hashmap, const char* key, const char* value);
void hashmap_ptr_set(struct Hashmap* hashmap, const char* key, void* value); void hashmap_ptr_set(struct Hashmap* hashmap, const char* key, void* value);
float hashmap_float_get(struct Hashmap* hashmap, const char* key); float hashmap_float_get(const struct Hashmap* hashmap, const char* key);
int hashmap_int_get(struct Hashmap* hashmap, const char* key); int hashmap_int_get(const struct Hashmap* hashmap, const char* key);
double hashmap_double_get(struct Hashmap* hashmap, const char* key); double hashmap_double_get(const struct Hashmap* hashmap, const char* key);
int hashmap_get_bool(struct Hashmap* hashmap, const char* key); int hashmap_get_bool(const struct Hashmap* hashmap, const char* key);
vec2 hashmap_vec2_get(struct Hashmap* hashmap, const char* key); vec2 hashmap_vec2_get(const struct Hashmap* hashmap, const char* key);
vec3 hashmap_vec3_get(struct Hashmap* hashmap, const char* key); vec3 hashmap_vec3_get(const struct Hashmap* hashmap, const char* key);
vec4 hashmap_vec4_get(struct Hashmap* hashmap, const char* key); vec4 hashmap_vec4_get(const struct Hashmap* hashmap, const char* key);
quat hashmap_quat_get(struct Hashmap* hashmap, const char* key); quat hashmap_quat_get(const struct Hashmap* hashmap, const char* key);
const mat4* hashmap_mat4_get(struct Hashmap* hashmap, const char* key); const mat4* hashmap_mat4_get(const struct Hashmap* hashmap, const char* key);
const char* hashmap_str_get(struct Hashmap* hashmap, const char* key); const char* hashmap_str_get(const struct Hashmap* hashmap, const char* key);
void* hashmap_ptr_get(struct Hashmap* hashmap, const char* key); void* hashmap_ptr_get(const struct Hashmap* hashmap, const char* key);
void hashmap_debug_print(struct Hashmap* hashmap); void hashmap_debug_print(const struct Hashmap* hashmap);
/* Only used during iteration */
void hashmap_iter_begin(struct Hashmap* hashmap);
int hashmap_iter_next(struct Hashmap* hashmap, char** key, struct Variant** value);
#define HASHMAP_FOREACH(hashmap, key, value) \
hashmap_iter_begin(hashmap); \
while(hashmap_iter_next(hashmap, &key, &value)) \
#endif #endif

@ -7,8 +7,6 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#define MAX_VARIANT_STR_LEN 1024
void variant_init_empty(struct Variant* variant) void variant_init_empty(struct Variant* variant)
{ {
variant->type = VT_NONE; variant->type = VT_NONE;
@ -148,6 +146,7 @@ void variant_copy(struct Variant* to, const struct Variant* from)
void variant_to_str(const struct Variant* variant, char* str, int len) void variant_to_str(const struct Variant* variant, char* str, int len)
{ {
if(!variant) return;
switch(variant->type) switch(variant->type)
{ {
case VT_BOOL: snprintf(str, len, "%s", variant->val_bool ? "true" : "false"); break; case VT_BOOL: snprintf(str, len, "%s", variant->val_bool ? "true" : "false"); break;

@ -3,6 +3,8 @@
#include "linmath.h" #include "linmath.h"
#define MAX_VARIANT_STR_LEN 1024
enum Variant_Type enum Variant_Type
{ {
VT_NONE = 0, VT_NONE = 0,

Loading…
Cancel
Save