Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
game-string: introduce uthash-powered game string table
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 17, 2024
1 parent 8ad35cc commit 85e2556
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
15 changes: 15 additions & 0 deletions include/libtrx/game/game_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

#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);
6 changes: 5 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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)
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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'
Expand Down
56 changes: 56 additions & 0 deletions src/game/game_string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "game/game_string.h"

#include "memory.h"
#include <uthash.h>

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);
}
}
11 changes: 11 additions & 0 deletions subprojects/uthash.wrap
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 85e2556

Please sign in to comment.