Skip to content

Commit

Permalink
game-flow: reduce repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Jan 25, 2025
1 parent a8fe16e commit cd62814
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 51 deletions.
49 changes: 17 additions & 32 deletions src/tr1/game/game_flow/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

#include <string.h>

#define DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(name) \
int32_t name( \
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, \
void *extra_data, void *user_arg)
typedef int32_t (*M_SEQUENCE_EVENT_HANDLER_FUNC)(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);

typedef struct {
GAME_FLOW_SEQUENCE_EVENT_TYPE event_type;
M_SEQUENCE_EVENT_HANDLER_FUNC handler_func;
Expand All @@ -31,28 +34,20 @@ typedef void (*M_LOAD_ARRAY_FUNC)(
JSON_OBJECT *source_elem, GAME_FLOW *gf, void *target_elem,
size_t target_elem_idx, void *user_arg);

static GAME_OBJECT_ID M_GetObjectFromJSONValue(const JSON_VALUE *value);

static void M_LoadArray(
JSON_OBJECT *obj, const char *key, size_t element_size,
M_LOAD_ARRAY_FUNC load_func, GAME_FLOW *gf, int32_t *count, void **elements,
void *user_arg);

static GAME_OBJECT_ID M_GetObjectFromJSONValue(const JSON_VALUE *value);
static bool M_IsLegacySequence(const char *type_str);
static int32_t M_HandleIntEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static int32_t M_HandlePictureEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static int32_t M_HandleTotalStatsEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static int32_t M_HandleAddItemEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static int32_t M_HandleMeshSwapEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleTotalStatsEvent);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleMeshSwapEvent);

static void M_LoadSettings(
JSON_OBJECT *obj, GAME_FLOW_LEVEL_SETTINGS *settings);
static void M_LoadLevelSequence(
Expand Down Expand Up @@ -167,9 +162,7 @@ static bool M_IsLegacySequence(const char *const type_str)
|| strcmp(type_str, "stop_cine") == 0;
}

static int32_t M_HandleIntEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent)
{
if (event != NULL) {
event->data =
Expand All @@ -178,9 +171,7 @@ static int32_t M_HandleIntEvent(
return 0;
}

static int32_t M_HandlePictureEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent)
{
const char *const path = JSON_ObjectGetString(event_obj, "path", NULL);
if (path == NULL) {
Expand All @@ -199,9 +190,7 @@ static int32_t M_HandlePictureEvent(
return sizeof(GAME_FLOW_DISPLAY_PICTURE_DATA) + strlen(path) + 1;
}

static int32_t M_HandleTotalStatsEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleTotalStatsEvent)
{
const char *const path =
JSON_ObjectGetString(event_obj, "background_path", NULL);
Expand All @@ -217,9 +206,7 @@ static int32_t M_HandleTotalStatsEvent(
return strlen(path) + 1;
}

static int32_t M_HandleAddItemEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent)
{
const GAME_OBJECT_ID object_id =
M_GetObjectFromJSONValue(JSON_ObjectGetValue(event_obj, "object_id"));
Expand All @@ -236,9 +223,7 @@ static int32_t M_HandleAddItemEvent(
return sizeof(GAME_FLOW_ADD_ITEM_DATA);
}

static int32_t M_HandleMeshSwapEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleMeshSwapEvent)
{
const GAME_OBJECT_ID object1_id =
M_GetObjectFromJSONValue(JSON_ObjectGetValue(event_obj, "object1_id"));
Expand Down
29 changes: 10 additions & 19 deletions src/tr2/game/game_flow/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
#include <libtrx/log.h>
#include <libtrx/memory.h>

#define DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(name) \
int32_t name( \
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, \
void *extra_data, void *user_arg)
typedef int32_t (*M_SEQUENCE_EVENT_HANDLER_FUNC)(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);

typedef struct {
GAME_FLOW_SEQUENCE_EVENT_TYPE event_type;
M_SEQUENCE_EVENT_HANDLER_FUNC handler_func;
Expand All @@ -37,15 +40,9 @@ static void M_LoadArray(
M_LOAD_ARRAY_FUNC load_func, GAME_FLOW *gf, int32_t *count, void **elements,
void *user_arg);

static int32_t M_HandleIntEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static int32_t M_HandlePictureEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static int32_t M_HandleAddItemEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
void *user_arg);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent);
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent);
static size_t M_LoadSequenceEvent(
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data);
static void M_LoadSequence(JSON_ARRAY *jarr, GAME_FLOW_SEQUENCE *sequence);
Expand Down Expand Up @@ -138,9 +135,7 @@ static GAME_FLOW_COMMAND M_LoadCommand(
return (GAME_FLOW_COMMAND) { .action = action, .param = param };
}

static int32_t M_HandleIntEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent)
{
if (event != NULL) {
event->data =
Expand All @@ -149,9 +144,7 @@ static int32_t M_HandleIntEvent(
return 0;
}

static int32_t M_HandlePictureEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent)
{
const char *const path = JSON_ObjectGetString(event_obj, "path", NULL);
if (path == NULL) {
Expand All @@ -170,9 +163,7 @@ static int32_t M_HandlePictureEvent(
return sizeof(GAME_FLOW_DISPLAY_PICTURE_DATA) + strlen(path) + 1;
}

static int32_t M_HandleAddItemEvent(
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
void *const extra_data, void *const user_arg)
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent)
{
const GAME_OBJECT_ID object_id =
M_GetObjectFromJSONValue(JSON_ObjectGetValue(event_obj, "object_id"));
Expand Down

0 comments on commit cd62814

Please sign in to comment.