Skip to content

Commit

Permalink
game-string: manage game strings in libtrx
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Sep 17, 2024
1 parent 8526818 commit 75a432c
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 152 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ Release/
subprojects/packagecache/
subprojects/dwarfstack-*/
subprojects/dwarfstack.wrap
subprojects/uthash-*/
subprojects/uthash.wrap
57 changes: 7 additions & 50 deletions src/game/game_string.c
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();
}
15 changes: 3 additions & 12 deletions src/game/game_string.h
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);
11 changes: 6 additions & 5 deletions src/game/gameflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,14 @@ static bool GameFlow_LoadScriptGameStrings(struct json_object_s *obj)

struct json_object_element_s *strings_elem = strings_obj->start;
while (strings_elem) {
const GAME_STRING_ID key =
GameString_IDFromEnum(strings_elem->name->string);
const char *const key = strings_elem->name->string;
struct json_string_s *value = json_value_as_string(strings_elem->value);
if (!value || !value->string || key < 0 || key >= GS_NUMBER_OF) {
LOG_ERROR("invalid string key %s", strings_elem->name->string);
if (!GameString_IsKnown(key)) {
LOG_ERROR("invalid game string key: %s", key);
} else if (!value || value->string == NULL) {
LOG_ERROR("invalid game string value: %s", key);
} else {
GameString_Set(key, value->string);
GameString_Define(key, value->string);
}
strings_elem = strings_elem->next;
}
Expand Down
144 changes: 72 additions & 72 deletions src/game/option/option_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,86 +106,86 @@ static MENU m_ControlMenu = {
};

static const LAYOUT_NUM_GS_MAP m_LayoutMap[] = {
{ INPUT_LAYOUT_DEFAULT, GS_CONTROL_DEFAULT_KEYS },
{ INPUT_LAYOUT_CUSTOM_1, GS_CONTROL_CUSTOM_1 },
{ INPUT_LAYOUT_CUSTOM_2, GS_CONTROL_CUSTOM_2 },
{ INPUT_LAYOUT_CUSTOM_3, GS_CONTROL_CUSTOM_3 },
{ INPUT_LAYOUT_DEFAULT, GS_ID(CONTROL_DEFAULT_KEYS) },
{ INPUT_LAYOUT_CUSTOM_1, GS_ID(CONTROL_CUSTOM_1) },
{ INPUT_LAYOUT_CUSTOM_2, GS_ID(CONTROL_CUSTOM_2) },
{ INPUT_LAYOUT_CUSTOM_3, GS_ID(CONTROL_CUSTOM_3) },
};

static const TEXT_COLUMN_PLACEMENT CtrlTextPlacementNormal[] = {
{ INPUT_ROLE_UP, GS_KEYMAP_RUN, false },
{ INPUT_ROLE_DOWN, GS_KEYMAP_BACK, false },
{ INPUT_ROLE_LEFT, GS_KEYMAP_LEFT, false },
{ INPUT_ROLE_RIGHT, GS_KEYMAP_RIGHT, false },
{ INPUT_ROLE_DRAW, GS_KEYMAP_DRAW_WEAPON, false },
{ INPUT_ROLE_ACTION, GS_KEYMAP_ACTION, false },
{ INPUT_ROLE_JUMP, GS_KEYMAP_JUMP, false },
{ INPUT_ROLE_ROLL, GS_KEYMAP_ROLL, false },
{ INPUT_ROLE_LOOK, GS_KEYMAP_LOOK, false },
{ INPUT_ROLE_SLOW, GS_KEYMAP_WALK, false },
{ INPUT_ROLE_STEP_L, GS_KEYMAP_STEP_LEFT, true },
{ INPUT_ROLE_STEP_R, GS_KEYMAP_STEP_RIGHT, true },
{ INPUT_ROLE_OPTION, GS_KEYMAP_INVENTORY, false },
{ INPUT_ROLE_PAUSE, GS_KEYMAP_PAUSE, true },
{ INPUT_ROLE_CHANGE_TARGET, GS_KEYMAP_CHANGE_TARGET, true },
{ INPUT_ROLE_CAMERA_UP, GS_KEYMAP_CAMERA_UP, true },
{ INPUT_ROLE_CAMERA_DOWN, GS_KEYMAP_CAMERA_DOWN, true },
{ INPUT_ROLE_CAMERA_LEFT, GS_KEYMAP_CAMERA_LEFT, true },
{ INPUT_ROLE_CAMERA_RIGHT, GS_KEYMAP_CAMERA_RIGHT, true },
{ INPUT_ROLE_CAMERA_RESET, GS_KEYMAP_CAMERA_RESET, true },
{ INPUT_ROLE_EQUIP_PISTOLS, GS_KEYMAP_EQUIP_PISTOLS, true },
{ INPUT_ROLE_EQUIP_SHOTGUN, GS_KEYMAP_EQUIP_SHOTGUN, true },
{ INPUT_ROLE_EQUIP_MAGNUMS, GS_KEYMAP_EQUIP_MAGNUMS, true },
{ INPUT_ROLE_EQUIP_UZIS, GS_KEYMAP_EQUIP_UZIS, true },
{ INPUT_ROLE_USE_SMALL_MEDI, GS_KEYMAP_USE_SMALL_MEDI, true },
{ INPUT_ROLE_USE_BIG_MEDI, GS_KEYMAP_USE_BIG_MEDI, true },
{ INPUT_ROLE_SAVE, GS_KEYMAP_SAVE, true },
{ INPUT_ROLE_LOAD, GS_KEYMAP_LOAD, true },
{ INPUT_ROLE_FPS, GS_KEYMAP_FPS, true },
{ INPUT_ROLE_BILINEAR, GS_KEYMAP_BILINEAR, true },
{ INPUT_ROLE_ENTER_CONSOLE, GS_KEYMAP_ENTER_CONSOLE, true },
{ INPUT_ROLE_UP, GS_ID(KEYMAP_RUN), false },
{ INPUT_ROLE_DOWN, GS_ID(KEYMAP_BACK), false },
{ INPUT_ROLE_LEFT, GS_ID(KEYMAP_LEFT), false },
{ INPUT_ROLE_RIGHT, GS_ID(KEYMAP_RIGHT), false },
{ INPUT_ROLE_DRAW, GS_ID(KEYMAP_DRAW_WEAPON), false },
{ INPUT_ROLE_ACTION, GS_ID(KEYMAP_ACTION), false },
{ INPUT_ROLE_JUMP, GS_ID(KEYMAP_JUMP), false },
{ INPUT_ROLE_ROLL, GS_ID(KEYMAP_ROLL), false },
{ INPUT_ROLE_LOOK, GS_ID(KEYMAP_LOOK), false },
{ INPUT_ROLE_SLOW, GS_ID(KEYMAP_WALK), false },
{ INPUT_ROLE_STEP_L, GS_ID(KEYMAP_STEP_LEFT), true },
{ INPUT_ROLE_STEP_R, GS_ID(KEYMAP_STEP_RIGHT), true },
{ INPUT_ROLE_OPTION, GS_ID(KEYMAP_INVENTORY), false },
{ INPUT_ROLE_PAUSE, GS_ID(KEYMAP_PAUSE), true },
{ INPUT_ROLE_CHANGE_TARGET, GS_ID(KEYMAP_CHANGE_TARGET), true },
{ INPUT_ROLE_CAMERA_UP, GS_ID(KEYMAP_CAMERA_UP), true },
{ INPUT_ROLE_CAMERA_DOWN, GS_ID(KEYMAP_CAMERA_DOWN), true },
{ INPUT_ROLE_CAMERA_LEFT, GS_ID(KEYMAP_CAMERA_LEFT), true },
{ INPUT_ROLE_CAMERA_RIGHT, GS_ID(KEYMAP_CAMERA_RIGHT), true },
{ INPUT_ROLE_CAMERA_RESET, GS_ID(KEYMAP_CAMERA_RESET), true },
{ INPUT_ROLE_EQUIP_PISTOLS, GS_ID(KEYMAP_EQUIP_PISTOLS), true },
{ INPUT_ROLE_EQUIP_SHOTGUN, GS_ID(KEYMAP_EQUIP_SHOTGUN), true },
{ INPUT_ROLE_EQUIP_MAGNUMS, GS_ID(KEYMAP_EQUIP_MAGNUMS), true },
{ INPUT_ROLE_EQUIP_UZIS, GS_ID(KEYMAP_EQUIP_UZIS), true },
{ INPUT_ROLE_USE_SMALL_MEDI, GS_ID(KEYMAP_USE_SMALL_MEDI), true },
{ INPUT_ROLE_USE_BIG_MEDI, GS_ID(KEYMAP_USE_BIG_MEDI), true },
{ INPUT_ROLE_SAVE, GS_ID(KEYMAP_SAVE), true },
{ INPUT_ROLE_LOAD, GS_ID(KEYMAP_LOAD), true },
{ INPUT_ROLE_FPS, GS_ID(KEYMAP_FPS), true },
{ INPUT_ROLE_BILINEAR, GS_ID(KEYMAP_BILINEAR), true },
{ INPUT_ROLE_ENTER_CONSOLE, GS_ID(KEYMAP_ENTER_CONSOLE), true },
// end
{ COL_END, -1, false },
{ COL_END, NULL, false },
};

static const TEXT_COLUMN_PLACEMENT CtrlTextPlacementCheats[] = {
{ INPUT_ROLE_UP, GS_KEYMAP_RUN, false },
{ INPUT_ROLE_DOWN, GS_KEYMAP_BACK, false },
{ INPUT_ROLE_LEFT, GS_KEYMAP_LEFT, false },
{ INPUT_ROLE_RIGHT, GS_KEYMAP_RIGHT, false },
{ INPUT_ROLE_DRAW, GS_KEYMAP_DRAW_WEAPON, false },
{ INPUT_ROLE_ACTION, GS_KEYMAP_ACTION, false },
{ INPUT_ROLE_JUMP, GS_KEYMAP_JUMP, false },
{ INPUT_ROLE_ROLL, GS_KEYMAP_ROLL, false },
{ INPUT_ROLE_LOOK, GS_KEYMAP_LOOK, false },
{ INPUT_ROLE_SLOW, GS_KEYMAP_WALK, false },
{ INPUT_ROLE_STEP_L, GS_KEYMAP_STEP_LEFT, true },
{ INPUT_ROLE_STEP_R, GS_KEYMAP_STEP_RIGHT, true },
{ INPUT_ROLE_OPTION, GS_KEYMAP_INVENTORY, false },
{ INPUT_ROLE_PAUSE, GS_KEYMAP_PAUSE, true },
{ INPUT_ROLE_CHANGE_TARGET, GS_KEYMAP_CHANGE_TARGET, true },
{ INPUT_ROLE_CAMERA_UP, GS_KEYMAP_CAMERA_UP, true },
{ INPUT_ROLE_CAMERA_DOWN, GS_KEYMAP_CAMERA_DOWN, true },
{ INPUT_ROLE_CAMERA_LEFT, GS_KEYMAP_CAMERA_LEFT, true },
{ INPUT_ROLE_CAMERA_RIGHT, GS_KEYMAP_CAMERA_RIGHT, true },
{ INPUT_ROLE_CAMERA_RESET, GS_KEYMAP_CAMERA_RESET, true },
{ INPUT_ROLE_EQUIP_PISTOLS, GS_KEYMAP_EQUIP_PISTOLS, true },
{ INPUT_ROLE_EQUIP_SHOTGUN, GS_KEYMAP_EQUIP_SHOTGUN, true },
{ INPUT_ROLE_EQUIP_MAGNUMS, GS_KEYMAP_EQUIP_MAGNUMS, true },
{ INPUT_ROLE_EQUIP_UZIS, GS_KEYMAP_EQUIP_UZIS, true },
{ INPUT_ROLE_USE_SMALL_MEDI, GS_KEYMAP_USE_SMALL_MEDI, true },
{ INPUT_ROLE_USE_BIG_MEDI, GS_KEYMAP_USE_BIG_MEDI, true },
{ INPUT_ROLE_SAVE, GS_KEYMAP_SAVE, true },
{ INPUT_ROLE_LOAD, GS_KEYMAP_LOAD, true },
{ INPUT_ROLE_FPS, GS_KEYMAP_FPS, true },
{ INPUT_ROLE_BILINEAR, GS_KEYMAP_BILINEAR, true },
{ INPUT_ROLE_FLY_CHEAT, GS_KEYMAP_FLY_CHEAT, true },
{ INPUT_ROLE_ITEM_CHEAT, GS_KEYMAP_ITEM_CHEAT, true },
{ INPUT_ROLE_LEVEL_SKIP_CHEAT, GS_KEYMAP_LEVEL_SKIP_CHEAT, true },
{ INPUT_ROLE_TURBO_CHEAT, GS_KEYMAP_TURBO_CHEAT, true },
{ INPUT_ROLE_ENTER_CONSOLE, GS_KEYMAP_ENTER_CONSOLE, true },
{ INPUT_ROLE_UP, GS_ID(KEYMAP_RUN), false },
{ INPUT_ROLE_DOWN, GS_ID(KEYMAP_BACK), false },
{ INPUT_ROLE_LEFT, GS_ID(KEYMAP_LEFT), false },
{ INPUT_ROLE_RIGHT, GS_ID(KEYMAP_RIGHT), false },
{ INPUT_ROLE_DRAW, GS_ID(KEYMAP_DRAW_WEAPON), false },
{ INPUT_ROLE_ACTION, GS_ID(KEYMAP_ACTION), false },
{ INPUT_ROLE_JUMP, GS_ID(KEYMAP_JUMP), false },
{ INPUT_ROLE_ROLL, GS_ID(KEYMAP_ROLL), false },
{ INPUT_ROLE_LOOK, GS_ID(KEYMAP_LOOK), false },
{ INPUT_ROLE_SLOW, GS_ID(KEYMAP_WALK), false },
{ INPUT_ROLE_STEP_L, GS_ID(KEYMAP_STEP_LEFT), true },
{ INPUT_ROLE_STEP_R, GS_ID(KEYMAP_STEP_RIGHT), true },
{ INPUT_ROLE_OPTION, GS_ID(KEYMAP_INVENTORY), false },
{ INPUT_ROLE_PAUSE, GS_ID(KEYMAP_PAUSE), true },
{ INPUT_ROLE_CHANGE_TARGET, GS_ID(KEYMAP_CHANGE_TARGET), true },
{ INPUT_ROLE_CAMERA_UP, GS_ID(KEYMAP_CAMERA_UP), true },
{ INPUT_ROLE_CAMERA_DOWN, GS_ID(KEYMAP_CAMERA_DOWN), true },
{ INPUT_ROLE_CAMERA_LEFT, GS_ID(KEYMAP_CAMERA_LEFT), true },
{ INPUT_ROLE_CAMERA_RIGHT, GS_ID(KEYMAP_CAMERA_RIGHT), true },
{ INPUT_ROLE_CAMERA_RESET, GS_ID(KEYMAP_CAMERA_RESET), true },
{ INPUT_ROLE_EQUIP_PISTOLS, GS_ID(KEYMAP_EQUIP_PISTOLS), true },
{ INPUT_ROLE_EQUIP_SHOTGUN, GS_ID(KEYMAP_EQUIP_SHOTGUN), true },
{ INPUT_ROLE_EQUIP_MAGNUMS, GS_ID(KEYMAP_EQUIP_MAGNUMS), true },
{ INPUT_ROLE_EQUIP_UZIS, GS_ID(KEYMAP_EQUIP_UZIS), true },
{ INPUT_ROLE_USE_SMALL_MEDI, GS_ID(KEYMAP_USE_SMALL_MEDI), true },
{ INPUT_ROLE_USE_BIG_MEDI, GS_ID(KEYMAP_USE_BIG_MEDI), true },
{ INPUT_ROLE_SAVE, GS_ID(KEYMAP_SAVE), true },
{ INPUT_ROLE_LOAD, GS_ID(KEYMAP_LOAD), true },
{ INPUT_ROLE_FPS, GS_ID(KEYMAP_FPS), true },
{ INPUT_ROLE_BILINEAR, GS_ID(KEYMAP_BILINEAR), true },
{ INPUT_ROLE_FLY_CHEAT, GS_ID(KEYMAP_FLY_CHEAT), true },
{ INPUT_ROLE_ITEM_CHEAT, GS_ID(KEYMAP_ITEM_CHEAT), true },
{ INPUT_ROLE_LEVEL_SKIP_CHEAT, GS_ID(KEYMAP_LEVEL_SKIP_CHEAT), true },
{ INPUT_ROLE_TURBO_CHEAT, GS_ID(KEYMAP_TURBO_CHEAT), true },
{ INPUT_ROLE_ENTER_CONSOLE, GS_ID(KEYMAP_ENTER_CONSOLE), true },
// end
{ COL_END, -1, false },
{ COL_END, NULL, false },
};

static void Option_Control_InitMenu(void);
Expand Down
27 changes: 15 additions & 12 deletions src/game/option/option_graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,21 @@ typedef struct GRAPHICS_MENU {
} GRAPHICS_MENU;

static const GRAPHICS_OPTION_ROW m_GfxOptionRows[] = {
{ OPTION_FPS, GS_DETAIL_FPS, GS_DETAIL_DECIMAL_FMT },
{ OPTION_TEXTURE_FILTER, GS_DETAIL_TEXTURE_FILTER, GS_MISC_OFF },
{ OPTION_FBO_FILTER, GS_DETAIL_FBO_FILTER, GS_MISC_OFF },
{ OPTION_VSYNC, GS_DETAIL_VSYNC, GS_MISC_ON },
{ OPTION_BRIGHTNESS, GS_DETAIL_BRIGHTNESS, GS_DETAIL_FLOAT_FMT },
{ OPTION_UI_TEXT_SCALE, GS_DETAIL_UI_TEXT_SCALE, GS_DETAIL_FLOAT_FMT },
{ OPTION_UI_BAR_SCALE, GS_DETAIL_UI_BAR_SCALE, GS_DETAIL_FLOAT_FMT },
{ OPTION_RENDER_MODE, GS_DETAIL_RENDER_MODE, GS_DETAIL_STRING_FMT },
{ OPTION_RESOLUTION, GS_DETAIL_RESOLUTION, GS_DETAIL_RESOLUTION_FMT },
{ OPTION_PERSPECTIVE, GS_DETAIL_PERSPECTIVE, GS_MISC_ON },
{ OPTION_PRETTY_PIXELS, GS_DETAIL_PRETTY_PIXELS, GS_MISC_ON },
{ OPTION_REFLECTIONS, GS_DETAIL_REFLECTIONS, GS_MISC_ON },
{ OPTION_FPS, GS_ID(DETAIL_FPS), GS_ID(DETAIL_DECIMAL_FMT) },
{ OPTION_TEXTURE_FILTER, GS_ID(DETAIL_TEXTURE_FILTER), GS_ID(MISC_OFF) },
{ OPTION_FBO_FILTER, GS_ID(DETAIL_FBO_FILTER), GS_ID(MISC_OFF) },
{ OPTION_VSYNC, GS_ID(DETAIL_VSYNC), GS_ID(MISC_ON) },
{ OPTION_BRIGHTNESS, GS_ID(DETAIL_BRIGHTNESS), GS_ID(DETAIL_FLOAT_FMT) },
{ OPTION_UI_TEXT_SCALE, GS_ID(DETAIL_UI_TEXT_SCALE),
GS_ID(DETAIL_FLOAT_FMT) },
{ OPTION_UI_BAR_SCALE, GS_ID(DETAIL_UI_BAR_SCALE),
GS_ID(DETAIL_FLOAT_FMT) },
{ OPTION_RENDER_MODE, GS_ID(DETAIL_RENDER_MODE), GS_ID(DETAIL_STRING_FMT) },
{ OPTION_RESOLUTION, GS_ID(DETAIL_RESOLUTION),
GS_ID(DETAIL_RESOLUTION_FMT) },
{ OPTION_PERSPECTIVE, GS_ID(DETAIL_PERSPECTIVE), GS_ID(MISC_ON) },
{ OPTION_PRETTY_PIXELS, GS_ID(DETAIL_PRETTY_PIXELS), GS_ID(MISC_ON) },
{ OPTION_REFLECTIONS, GS_ID(DETAIL_REFLECTIONS), GS_ID(MISC_ON) },
// end
{ OPTION_NUMBER_OF, 0, 0 },
};
Expand Down
2 changes: 2 additions & 0 deletions src/game/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ void Shell_Shutdown(void)

void Shell_Main(void)
{
GameString_Init();
Config_Read();

const char *gameflow_path = m_TR1XGameflowPath;
Expand Down Expand Up @@ -269,6 +270,7 @@ void Shell_Main(void)
}

Config_Write();
GameString_Shutdown();
}

void Shell_ExitSystem(const char *message)
Expand Down
2 changes: 1 addition & 1 deletion subprojects/libtrx

0 comments on commit 75a432c

Please sign in to comment.