Skip to content

Commit

Permalink
phase: allocate arguments on the heap
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Oct 5, 2024
1 parent d71e2dd commit 7ff8214
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 90 deletions.
8 changes: 6 additions & 2 deletions src/tr1/game/game/game_main_menu.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "game/phase/phase.h"
#include "game/phase/phase_inventory.h"
#include "global/types.h"

#include <stdint.h>
#include <libtrx/memory.h>

GAMEFLOW_COMMAND Game_MainMenu(void)
{
Phase_Set(PHASE_INVENTORY, (void *)(intptr_t)INV_TITLE_MODE);
PHASE_INVENTORY_ARGS *const args =
Memory_Alloc(sizeof(PHASE_INVENTORY_ARGS));
args->mode = INV_TITLE_MODE;
Phase_Set(PHASE_INVENTORY, args);
return Phase_Run();
}
49 changes: 24 additions & 25 deletions src/tr1/game/gameflow.c
Original file line number Diff line number Diff line change
Expand Up @@ -1089,10 +1089,10 @@ GameFlow_InterpretSequence(int32_t level_num, GAMEFLOW_LEVEL_TYPE level_type)

case GFS_START_CINE:
if (level_type != GFL_SAVED) {
PHASE_CUTSCENE_DATA phase_args = {
.level_num = (int32_t)(intptr_t)seq->data,
};
Phase_Set(PHASE_CUTSCENE, &phase_args);
PHASE_CUTSCENE_ARGS *const args =
Memory_Alloc(sizeof(PHASE_CUTSCENE_ARGS));
args->level_num = (int32_t)(intptr_t)seq->data;
Phase_Set(PHASE_CUTSCENE, args);
}
break;

Expand All @@ -1113,10 +1113,10 @@ GameFlow_InterpretSequence(int32_t level_num, GAMEFLOW_LEVEL_TYPE level_type)
break;

case GFS_LEVEL_STATS: {
PHASE_STATS_DATA phase_args = {
.level_num = (int32_t)(intptr_t)seq->data,
};
Phase_Set(PHASE_STATS, &phase_args);
PHASE_STATS_ARGS *const args =
Memory_Alloc(sizeof(PHASE_STATS_ARGS));
args->level_num = (int32_t)(intptr_t)seq->data;
Phase_Set(PHASE_STATS, args);
command = Phase_Run();
if (command.action != GF_CONTINUE_SEQUENCE) {
return command;
Expand All @@ -1127,14 +1127,13 @@ GameFlow_InterpretSequence(int32_t level_num, GAMEFLOW_LEVEL_TYPE level_type)
case GFS_TOTAL_STATS:
if (g_Config.enable_total_stats && level_type != GFL_SAVED) {
const GAMEFLOW_DISPLAY_PICTURE_DATA *data = seq->data;
PHASE_STATS_DATA phase_args = {
.level_num = level_num,
.background_path = data->path,
.total = true,
.level_type =
level_type == GFL_BONUS ? GFL_BONUS : GFL_NORMAL,
};
Phase_Set(PHASE_STATS, &phase_args);
PHASE_STATS_ARGS *const args =
Memory_Alloc(sizeof(PHASE_STATS_ARGS));
args->level_num = level_num;
args->background_path = data->path, args->total = true,
args->level_type =
level_type == GFL_BONUS ? GFL_BONUS : GFL_NORMAL;
Phase_Set(PHASE_STATS, args);
command = Phase_Run();
if (command.action != GF_CONTINUE_SEQUENCE) {
return command;
Expand All @@ -1158,11 +1157,11 @@ GameFlow_InterpretSequence(int32_t level_num, GAMEFLOW_LEVEL_TYPE level_type)
}

GAMEFLOW_DISPLAY_PICTURE_DATA *data = seq->data;
PHASE_PICTURE_DATA phase_arg = {
.path = data->path,
.display_time = data->display_time,
};
Phase_Set(PHASE_PICTURE, &phase_arg);
PHASE_PICTURE_ARGS *const args =
Memory_Alloc(sizeof(PHASE_PICTURE_ARGS));
args->path = data->path;
args->display_time = data->display_time;
Phase_Set(PHASE_PICTURE, args);
command = Phase_Run();
if (command.action != GF_CONTINUE_SEQUENCE) {
return command;
Expand Down Expand Up @@ -1319,10 +1318,10 @@ GameFlow_StorySoFar(int32_t level_num, int32_t savegame_level)
break;

case GFS_START_CINE: {
PHASE_CUTSCENE_DATA phase_args = {
.level_num = (int32_t)(intptr_t)seq->data,
};
Phase_Set(PHASE_CUTSCENE, &phase_args);
PHASE_CUTSCENE_ARGS *const args =
Memory_Alloc(sizeof(PHASE_CUTSCENE_ARGS));
args->level_num = (int32_t)(intptr_t)seq->data;
Phase_Set(PHASE_CUTSCENE, args);
break;
}

Expand Down
9 changes: 6 additions & 3 deletions src/tr1/game/inventory/inventory.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#include "game/inventory/inventory_vars.h"
#include "game/phase/phase.h"
#include "game/phase/phase_inventory.h"
#include "global/types.h"

#include <stdbool.h>
#include <stdint.h>
#include <libtrx/memory.h>

bool Inv_Display(const INV_MODE inv_mode)
{
if (inv_mode == INV_KEYS_MODE && !g_InvKeysObjects) {
return false;
}
Phase_Set(PHASE_INVENTORY, (void *)(intptr_t)inv_mode);
PHASE_INVENTORY_ARGS *const args =
Memory_Alloc(sizeof(PHASE_INVENTORY_ARGS));
args->mode = inv_mode;
Phase_Set(PHASE_INVENTORY, args);
return true;
}
22 changes: 12 additions & 10 deletions src/tr1/game/phase/phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "global/vars.h"

#include <libtrx/log.h>
#include <libtrx/memory.h>

#include <stdbool.h>
#include <stddef.h>
Expand All @@ -24,12 +25,12 @@ static PHASER *m_Phaser = NULL;

static bool m_Running = false;
static PHASE m_PhaseToSet = PHASE_NULL;
static void *m_PhaseToSetArg = NULL;
static const void *m_PhaseToSetArgs = NULL;

static PHASE_CONTROL M_Control(int32_t nframes);
static void M_Draw(void);
static int32_t M_Wait(void);
static void M_SetUnconditionally(const PHASE phase, void *arg);
static void M_SetUnconditionally(const PHASE phase, const void *args);

static PHASE_CONTROL M_Control(int32_t nframes)
{
Expand All @@ -55,7 +56,7 @@ static void M_Draw(void)
Output_EndScene();
}

static void M_SetUnconditionally(const PHASE phase, void *arg)
static void M_SetUnconditionally(const PHASE phase, const void *args)
{
if (m_Phaser && m_Phaser->end) {
m_Phaser->end();
Expand Down Expand Up @@ -100,8 +101,9 @@ static void M_SetUnconditionally(const PHASE phase, void *arg)
break;
}

if (m_Phaser && m_Phaser->start) {
m_Phaser->start(arg);
if (m_Phaser && m_Phaser->start != NULL) {
m_Phaser->start(args);
Memory_FreePointer(&args);
}

// set it at the end, so that the start callbacks can retrieve the old phase
Expand All @@ -115,16 +117,16 @@ PHASE Phase_Get(void)
return m_Phase;
}

void Phase_Set(const PHASE phase, void *arg)
void Phase_Set(const PHASE phase, const void *const args)
{
// changing the phase in the middle of rendering is asking for trouble,
// so instead we schedule to run the change on the next iteration
if (m_Running) {
m_PhaseToSet = phase;
m_PhaseToSetArg = arg;
m_PhaseToSetArgs = args;
return;
}
M_SetUnconditionally(phase, arg);
M_SetUnconditionally(phase, args);
}

static int32_t M_Wait(void)
Expand All @@ -151,9 +153,9 @@ GAMEFLOW_COMMAND Phase_Run(void)
Interpolation_SetRate(1.0);
M_Draw();

M_SetUnconditionally(m_PhaseToSet, m_PhaseToSetArg);
M_SetUnconditionally(m_PhaseToSet, m_PhaseToSetArgs);
m_PhaseToSet = PHASE_NULL;
m_PhaseToSetArg = NULL;
m_PhaseToSetArgs = NULL;
if (control.end) {
M_Draw();
break;
Expand Down
27 changes: 20 additions & 7 deletions src/tr1/game/phase/phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,27 @@ typedef enum {
PHASE_PHOTO_MODE,
} PHASE;

typedef void (*PHASER_START)(const void *args);
typedef void (*PHASER_END)(void);
typedef PHASE_CONTROL (*PHASER_CONTROL)(int32_t nframes);
typedef void (*PHASER_DRAW)(void);
typedef int32_t (*PHASER_WAIT)(void);

typedef struct {
void (*start)(void *arg);
void (*end)();
PHASE_CONTROL (*control)(int32_t nframes);
void (*draw)(void);
int32_t (*wait)(void);
PHASER_START start;
PHASER_END end;
PHASER_CONTROL control;
PHASER_DRAW draw;
PHASER_WAIT wait;
} PHASER;

PHASE Phase_Get(void);
void Phase_Set(PHASE phase, void *arg);
GAMEFLOW_COMMAND Phase_Run();

// Sets the next phase to run.
// args are passed to the subsequent PHASER->start callback.
// Note that they must be allocated on the heap and will be
// immediately freed by the phaser module upon completing the start
// routine.
void Phase_Set(PHASE phase, const void *args);

GAMEFLOW_COMMAND Phase_Run(void);
12 changes: 6 additions & 6 deletions src/tr1/game/phase/phase_cutscene.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
#include "global/types.h"
#include "global/vars.h"

#include <libtrx/memory.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

static void M_InitialiseHair(int32_t level_num);

static void M_Start(void *arg);
static void M_Start(const PHASE_CUTSCENE_ARGS *args);
static void M_End(void);
static PHASE_CONTROL M_Control(int32_t nframes);
static void M_Draw(void);
Expand Down Expand Up @@ -62,17 +63,16 @@ static void M_InitialiseHair(int32_t level_num)
g_LaraItem->required_anim_state = cut_anim->current_anim_state;
}

static void M_Start(void *arg)
static void M_Start(const PHASE_CUTSCENE_ARGS *const args)
{
Output_FadeReset();

const PHASE_CUTSCENE_DATA *data = (const PHASE_CUTSCENE_DATA *)arg;
if (!Level_Initialise(data->level_num)) {
if (!Level_Initialise(args->level_num)) {
return;
}
g_GameInfo.current_level_type = GFL_CUTSCENE;

M_InitialiseHair(data->level_num);
M_InitialiseHair(args->level_num);

for (int16_t room_num = 0; room_num < g_RoomCount; room_num++) {
if (g_RoomInfo[room_num].flipped_room >= 0) {
Expand Down Expand Up @@ -148,7 +148,7 @@ static void M_Draw(void)
}

PHASER g_CutscenePhaser = {
.start = M_Start,
.start = (PHASER_START)M_Start,
.end = M_End,
.control = M_Control,
.draw = M_Draw,
Expand Down
2 changes: 1 addition & 1 deletion src/tr1/game/phase/phase_cutscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

typedef struct {
int32_t level_num;
} PHASE_CUTSCENE_DATA;
} PHASE_CUTSCENE_ARGS;

extern PHASER g_CutscenePhaser;
7 changes: 4 additions & 3 deletions src/tr1/game/phase/phase_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "global/types.h"
#include "global/vars.h"

#include <libtrx/memory.h>
#include <libtrx/utils.h>

#include <assert.h>
Expand Down Expand Up @@ -54,7 +55,7 @@ static uint32_t *m_DemoPtr = NULL;
static bool M_ProcessInput(void);
static int32_t M_ChooseLevel(void);

static void M_Start(void *arg);
static void M_Start(const void *args);
static void M_End(void);
static PHASE_CONTROL M_Run(int32_t nframes);
static PHASE_CONTROL M_FadeOut(void);
Expand Down Expand Up @@ -116,7 +117,7 @@ static int32_t M_ChooseLevel(void)
return level_num;
}

static void M_Start(void *arg)
static void M_Start(const void *const args)
{
m_DemoLevel = M_ChooseLevel();

Expand Down Expand Up @@ -321,7 +322,7 @@ static void M_Draw(void)
}

PHASER g_DemoPhaser = {
.start = M_Start,
.start = (PHASER_START)M_Start,
.end = M_End,
.control = M_Control,
.draw = M_Draw,
Expand Down
5 changes: 3 additions & 2 deletions src/tr1/game/phase/phase_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
#include "global/types.h"
#include "global/vars.h"

#include <libtrx/memory.h>
#include <libtrx/utils.h>

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

static void M_Start(void *arg);
static void M_Start(const void *args);
static void M_End(void);
static PHASE_CONTROL M_Control(int32_t nframes);
static void M_Draw(void);

static void M_Start(void *arg)
static void M_Start(const void *const args)
{
Interpolation_Remember();
Stats_StartTimer();
Expand Down
Loading

0 comments on commit 7ff8214

Please sign in to comment.