Skip to content

Commit fe49300

Browse files
committed
game-flow: reduce repetition
1 parent e04f955 commit fe49300

File tree

2 files changed

+27
-51
lines changed

2 files changed

+27
-51
lines changed

src/tr1/game/game_flow/reader.c

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
#include <string.h>
1919

20+
#define DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(name) \
21+
int32_t name( \
22+
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, \
23+
void *extra_data, void *user_arg)
2024
typedef int32_t (*M_SEQUENCE_EVENT_HANDLER_FUNC)(
2125
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
2226
void *user_arg);
23-
2427
typedef struct {
2528
GAME_FLOW_SEQUENCE_EVENT_TYPE event_type;
2629
M_SEQUENCE_EVENT_HANDLER_FUNC handler_func;
@@ -31,28 +34,20 @@ typedef void (*M_LOAD_ARRAY_FUNC)(
3134
JSON_OBJECT *source_elem, GAME_FLOW *gf, void *target_elem,
3235
size_t target_elem_idx, void *user_arg);
3336

37+
static GAME_OBJECT_ID M_GetObjectFromJSONValue(const JSON_VALUE *value);
38+
3439
static void M_LoadArray(
3540
JSON_OBJECT *obj, const char *key, size_t element_size,
3641
M_LOAD_ARRAY_FUNC load_func, GAME_FLOW *gf, int32_t *count, void **elements,
3742
void *user_arg);
3843

39-
static GAME_OBJECT_ID M_GetObjectFromJSONValue(const JSON_VALUE *value);
4044
static bool M_IsLegacySequence(const char *type_str);
41-
static int32_t M_HandleIntEvent(
42-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
43-
void *user_arg);
44-
static int32_t M_HandlePictureEvent(
45-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
46-
void *user_arg);
47-
static int32_t M_HandleTotalStatsEvent(
48-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
49-
void *user_arg);
50-
static int32_t M_HandleAddItemEvent(
51-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
52-
void *user_arg);
53-
static int32_t M_HandleMeshSwapEvent(
54-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
55-
void *user_arg);
45+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent);
46+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent);
47+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleTotalStatsEvent);
48+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent);
49+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleMeshSwapEvent);
50+
5651
static void M_LoadSettings(
5752
JSON_OBJECT *obj, GAME_FLOW_LEVEL_SETTINGS *settings);
5853
static void M_LoadLevelSequence(
@@ -167,9 +162,7 @@ static bool M_IsLegacySequence(const char *const type_str)
167162
|| strcmp(type_str, "stop_cine") == 0;
168163
}
169164

170-
static int32_t M_HandleIntEvent(
171-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
172-
void *const extra_data, void *const user_arg)
165+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent)
173166
{
174167
if (event != NULL) {
175168
event->data =
@@ -178,9 +171,7 @@ static int32_t M_HandleIntEvent(
178171
return 0;
179172
}
180173

181-
static int32_t M_HandlePictureEvent(
182-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
183-
void *const extra_data, void *const user_arg)
174+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent)
184175
{
185176
const char *const path = JSON_ObjectGetString(event_obj, "path", NULL);
186177
if (path == NULL) {
@@ -199,9 +190,7 @@ static int32_t M_HandlePictureEvent(
199190
return sizeof(GAME_FLOW_DISPLAY_PICTURE_DATA) + strlen(path) + 1;
200191
}
201192

202-
static int32_t M_HandleTotalStatsEvent(
203-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
204-
void *const extra_data, void *const user_arg)
193+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleTotalStatsEvent)
205194
{
206195
const char *const path =
207196
JSON_ObjectGetString(event_obj, "background_path", NULL);
@@ -217,9 +206,7 @@ static int32_t M_HandleTotalStatsEvent(
217206
return strlen(path) + 1;
218207
}
219208

220-
static int32_t M_HandleAddItemEvent(
221-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
222-
void *const extra_data, void *const user_arg)
209+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent)
223210
{
224211
const GAME_OBJECT_ID object_id =
225212
M_GetObjectFromJSONValue(JSON_ObjectGetValue(event_obj, "object_id"));
@@ -236,9 +223,7 @@ static int32_t M_HandleAddItemEvent(
236223
return sizeof(GAME_FLOW_ADD_ITEM_DATA);
237224
}
238225

239-
static int32_t M_HandleMeshSwapEvent(
240-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
241-
void *const extra_data, void *const user_arg)
226+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleMeshSwapEvent)
242227
{
243228
const GAME_OBJECT_ID object1_id =
244229
M_GetObjectFromJSONValue(JSON_ObjectGetValue(event_obj, "object1_id"));

src/tr2/game/game_flow/reader.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
#include <libtrx/log.h>
1515
#include <libtrx/memory.h>
1616

17+
#define DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(name) \
18+
int32_t name( \
19+
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, \
20+
void *extra_data, void *user_arg)
1721
typedef int32_t (*M_SEQUENCE_EVENT_HANDLER_FUNC)(
1822
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
1923
void *user_arg);
20-
2124
typedef struct {
2225
GAME_FLOW_SEQUENCE_EVENT_TYPE event_type;
2326
M_SEQUENCE_EVENT_HANDLER_FUNC handler_func;
@@ -37,15 +40,9 @@ static void M_LoadArray(
3740
M_LOAD_ARRAY_FUNC load_func, GAME_FLOW *gf, int32_t *count, void **elements,
3841
void *user_arg);
3942

40-
static int32_t M_HandleIntEvent(
41-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
42-
void *user_arg);
43-
static int32_t M_HandlePictureEvent(
44-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
45-
void *user_arg);
46-
static int32_t M_HandleAddItemEvent(
47-
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data,
48-
void *user_arg);
43+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent);
44+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent);
45+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent);
4946
static size_t M_LoadSequenceEvent(
5047
JSON_OBJECT *event_obj, GAME_FLOW_SEQUENCE_EVENT *event, void *extra_data);
5148
static void M_LoadSequence(JSON_ARRAY *jarr, GAME_FLOW_SEQUENCE *sequence);
@@ -138,9 +135,7 @@ static GAME_FLOW_COMMAND M_LoadCommand(
138135
return (GAME_FLOW_COMMAND) { .action = action, .param = param };
139136
}
140137

141-
static int32_t M_HandleIntEvent(
142-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
143-
void *const extra_data, void *const user_arg)
138+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleIntEvent)
144139
{
145140
if (event != NULL) {
146141
event->data =
@@ -149,9 +144,7 @@ static int32_t M_HandleIntEvent(
149144
return 0;
150145
}
151146

152-
static int32_t M_HandlePictureEvent(
153-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
154-
void *const extra_data, void *const user_arg)
147+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandlePictureEvent)
155148
{
156149
const char *const path = JSON_ObjectGetString(event_obj, "path", NULL);
157150
if (path == NULL) {
@@ -170,9 +163,7 @@ static int32_t M_HandlePictureEvent(
170163
return sizeof(GAME_FLOW_DISPLAY_PICTURE_DATA) + strlen(path) + 1;
171164
}
172165

173-
static int32_t M_HandleAddItemEvent(
174-
JSON_OBJECT *const event_obj, GAME_FLOW_SEQUENCE_EVENT *const event,
175-
void *const extra_data, void *const user_arg)
166+
static DECLARE_SEQUENCE_EVENT_HANDLER_FUNC(M_HandleAddItemEvent)
176167
{
177168
const GAME_OBJECT_ID object_id =
178169
M_GetObjectFromJSONValue(JSON_ObjectGetValue(event_obj, "object_id"));

0 commit comments

Comments
 (0)