-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
game-string: manage game strings in libtrx
- Loading branch information
Showing
8 changed files
with
108 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,15 @@ | ||
#include "game_string.h" | ||
|
||
#include <libtrx/memory.h> | ||
#include <libtrx/utils.h> | ||
#include <libtrx/game/game_string.h> | ||
|
||
#include <assert.h> | ||
#include <stddef.h> | ||
#include <string.h> | ||
|
||
typedef struct ENUM_NAME_MAP { | ||
const char *str; | ||
const GAME_STRING_ID val; | ||
} ENUM_NAME_MAP; | ||
|
||
static ENUM_NAME_MAP m_EnumNameMap[]; | ||
static char *m_StringMap[GS_NUMBER_OF] = { 0 }; | ||
#undef GS_DEFINE | ||
#define GS_DEFINE(id, str) str, | ||
static const char *m_DefaultStringMap[GS_NUMBER_OF] = { | ||
#include "game/game_string.def" | ||
}; | ||
|
||
#undef GS_DEFINE | ||
#define GS_DEFINE(id, str) \ | ||
{ \ | ||
QUOTE(id), \ | ||
GS_##id, \ | ||
}, | ||
static ENUM_NAME_MAP m_EnumNameMap[] = { | ||
#include "game/game_string.def" | ||
{ NULL, 0 }, | ||
}; | ||
|
||
void GameString_Set(GAME_STRING_ID id, const char *value) | ||
{ | ||
assert(id >= 0); | ||
assert(id < GS_NUMBER_OF); | ||
Memory_FreePointer(&m_StringMap[id]); | ||
m_StringMap[id] = Memory_DupStr(value); | ||
} | ||
|
||
const char *GameString_Get(GAME_STRING_ID id) | ||
void GameString_Init(void) | ||
{ | ||
return m_StringMap[id] != NULL ? (const char *)m_StringMap[id] | ||
: m_DefaultStringMap[id]; | ||
// IWYU pragma: begin_keep | ||
#include "game_string.def" | ||
// IWYU pragma: end_keep | ||
} | ||
|
||
GAME_STRING_ID GameString_IDFromEnum(const char *const str) | ||
void GameString_Shutdown(void) | ||
{ | ||
const ENUM_NAME_MAP *current = &m_EnumNameMap[0]; | ||
while (current->str) { | ||
if (!strcmp(str, current->str)) { | ||
return current->val; | ||
} | ||
current++; | ||
} | ||
return GS_INVALID; | ||
GameString_Clear(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,6 @@ | ||
#pragma once | ||
|
||
#define GS(id) GameString_Get(GS_##id) | ||
#include <libtrx/game/game_string.h> | ||
|
||
#undef GS_DEFINE | ||
#define GS_DEFINE(id, str) GS_##id, | ||
typedef enum GAME_STRING_ID { | ||
GS_INVALID = -1, | ||
#include "game/game_string.def" | ||
GS_NUMBER_OF, | ||
} GAME_STRING_ID; | ||
|
||
void GameString_Set(GAME_STRING_ID id, const char *value); | ||
const char *GameString_Get(GAME_STRING_ID id); | ||
GAME_STRING_ID GameString_IDFromEnum(const char *str); | ||
void GameString_Init(void); | ||
void GameString_Shutdown(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule libtrx
updated
4 files
+15 −0 | include/libtrx/game/game_string.h | |
+5 −1 | meson.build | |
+57 −0 | src/game/game_string.c | |
+11 −0 | subprojects/uthash.wrap |