diff --git a/include/libtrx/game/game_string.h b/include/libtrx/game/game_string.h new file mode 100644 index 0000000..308a6a3 --- /dev/null +++ b/include/libtrx/game/game_string.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +#define GS_DEFINE(id, value) GameString_Define(#id, value); +#define GS(id) GameString_Get(#id) +#define GS_ID(id) (#id) + +typedef const char *GAME_STRING_ID; + +void GameString_Define(const char *key, const char *value); +bool GameString_IsKnown(const char *key); +const char *GameString_Get(const char *key); +void GameString_Clear(void); diff --git a/meson.build b/meson.build index ea4f8b4..66ead3e 100644 --- a/meson.build +++ b/meson.build @@ -7,11 +7,11 @@ project( ], ) -fs = import('fs') staticdeps = get_option('staticdeps') tr_version = get_option('tr_version') gfx_gl_default_backend = get_option('gfx_gl_default_backend') +fs = import('fs') c_compiler = meson.get_compiler('c') relative_dir = fs.relative_to(meson.current_source_dir(), meson.global_build_root()) build_opts = [ @@ -33,6 +33,8 @@ if host_machine.system() == 'darwin' staticdeps = false endif +uthash = subproject('uthash', default_options: ['warning_level=0']) + null_dep = dependency('', required: false) dep_avcodec = dependency('libavcodec', static: staticdeps) dep_avformat = dependency('libavformat', static: staticdeps) @@ -64,6 +66,7 @@ sources = [ 'src/enum_str.c', 'src/filesystem.c', 'src/game/items.c', + 'src/game/game_string.c', 'src/gfx/2d/2d_renderer.c', 'src/gfx/2d/2d_surface.c', 'src/gfx/3d/3d_renderer.c', @@ -102,6 +105,7 @@ dependencies = [ dep_swscale, dep_zlib, dep_opengl, + uthash.get_variable('uthash_dep'), ] if dep_backtrace.found() and host_machine.system() == 'linux' diff --git a/src/game/game_string.c b/src/game/game_string.c new file mode 100644 index 0000000..ce18f1a --- /dev/null +++ b/src/game/game_string.c @@ -0,0 +1,57 @@ +#include "game/game_string.h" + +#include "memory.h" + +#include + +typedef struct { + char *key; + char *value; + UT_hash_handle hh; +} STRING_TABLE_ENTRY; + +static STRING_TABLE_ENTRY *m_StringTable = NULL; + +void GameString_Define(const char *key, const char *value) +{ + STRING_TABLE_ENTRY *entry; + + HASH_FIND_STR(m_StringTable, key, entry); + if (entry == NULL) { + entry = (STRING_TABLE_ENTRY *)Memory_Alloc(sizeof(STRING_TABLE_ENTRY)); + entry->key = Memory_DupStr(key); + entry->value = Memory_DupStr(value); + HASH_ADD_KEYPTR( + hh, m_StringTable, entry->key, strlen(entry->key), entry); + } else { + Memory_Free(entry->value); + entry->value = Memory_DupStr(value); + } +} + +bool GameString_IsKnown(const char *key) +{ + STRING_TABLE_ENTRY *entry; + HASH_FIND_STR(m_StringTable, key, entry); + return entry != NULL; +} + +const char *GameString_Get(const char *key) +{ + STRING_TABLE_ENTRY *entry; + HASH_FIND_STR(m_StringTable, key, entry); + return entry ? entry->value : NULL; +} + +void GameString_Clear(void) +{ + STRING_TABLE_ENTRY *entry, *tmp; + + HASH_ITER(hh, m_StringTable, entry, tmp) + { + HASH_DEL(m_StringTable, entry); + Memory_Free(entry->key); + Memory_Free(entry->value); + Memory_Free(entry); + } +} diff --git a/subprojects/uthash.wrap b/subprojects/uthash.wrap new file mode 100644 index 0000000..68a51ea --- /dev/null +++ b/subprojects/uthash.wrap @@ -0,0 +1,11 @@ +[wrap-file] +directory = uthash-2.3.0 +source_url = https://github.com/troydhanson/uthash/archive/v2.3.0.tar.gz +source_filename = uthash-2.3.0.tar.gz +source_hash = e10382ab75518bad8319eb922ad04f907cb20cccb451a3aa980c9d005e661acc +patch_filename = uthash_2.3.0-1_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/uthash_2.3.0-1/get_patch +patch_hash = d0b7cf9788c3735ee6a08bb649c58be1f5fad4b95e50e7681cbdf44882da9d7e + +[provide] +uthash = uthash_dep