Skip to content

Commit

Permalink
Test demo recording
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 committed Nov 3, 2024
1 parent 535a502 commit 82c98a9
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/tr1/game/input/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ typedef union INPUT_STATE {
uint64_t change_target : 1;
uint64_t toggle_ui : 1;
uint64_t toggle_photo_mode : 1;
uint64_t toggle_recording : 1;
};
} INPUT_STATE;

Expand Down
1 change: 1 addition & 0 deletions src/tr1/game/input/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ static bool M_Update(INPUT_STATE *const result, const INPUT_LAYOUT layout)
result->option = M_Key(layout, INPUT_ROLE_OPTION);
result->pause = M_Key(layout, INPUT_ROLE_PAUSE);
result->toggle_photo_mode = M_Key(layout, INPUT_ROLE_TOGGLE_PHOTO_MODE);
result->toggle_recording = KEY_DOWN(SDL_SCANCODE_F12);
result->camera_up = M_Key(layout, INPUT_ROLE_CAMERA_UP);
result->camera_down = M_Key(layout, INPUT_ROLE_CAMERA_DOWN);
result->camera_forward = M_Key(layout, INPUT_ROLE_CAMERA_FORWARD);
Expand Down
115 changes: 115 additions & 0 deletions src/tr1/game/phase/phase_game.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "game/phase/phase_game.h"

#include "game/camera.h"
#include "game/clock.h"
#include "game/console/common.h"
#include "game/effects.h"
#include "game/game.h"
#include "game/input.h"
Expand All @@ -13,6 +15,7 @@
#include "game/output.h"
#include "game/overlay.h"
#include "game/phase/phase_photo_mode.h"
#include "game/random.h"
#include "game/shell.h"
#include "game/sound.h"
#include "game/stats.h"
Expand All @@ -21,12 +24,27 @@
#include "global/types.h"
#include "global/vars.h"

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

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

#define DEMO_DIR "demos"
#define DEMO_EXT "dmo"

static struct {
bool enable_enhanced_look;
bool enable_tr2_swimming;
bool fix_bear_ai;
TARGET_LOCK_MODE target_mode;
int32_t fps;
} m_OldConfig;

static void M_Start(const void *args);
static void M_End(void);
Expand All @@ -46,6 +64,102 @@ static void M_End(void)
{
}

#define RECORD(val) \
do { \
uint32_t data = (uint32_t)(val); \
Vector_Add(m_RecordData, (void *)&data); \
} while (0)

static VECTOR *m_RecordData = NULL;

static bool M_IsRecording(void)
{
return m_RecordData != NULL;
}

static void m_UpdateRecording(void)
{
if (M_IsRecording()) {
if (g_InputDB.toggle_recording || m_RecordData->count == DEMO_COUNT_MAX
|| g_LaraItem->hit_points <= 0) {
RECORD(-1);

File_CreateDirectory(DEMO_DIR);

char date_time[20];
Clock_GetDateTime(date_time);

size_t out_size =
snprintf(NULL, 0, "%s-Level-%d", date_time, g_CurrentLevel) + 1;
char *filename = Memory_Alloc(out_size);
snprintf(
filename, out_size, "%s-Level-%d", date_time, g_CurrentLevel);

char *full_path = Memory_Alloc(
strlen(DEMO_DIR) + strlen(filename) + strlen(DEMO_EXT) + 6);
sprintf(full_path, "%s/%s.%s", DEMO_DIR, filename, DEMO_EXT);

MYFILE *const fp = File_Open(full_path, FILE_OPEN_WRITE);
File_WriteU16(fp, (uint16_t)(m_RecordData->count + 1));
for (int32_t i = 0; i < m_RecordData->count; i++) {
File_WriteU32(
fp, *(const uint32_t *)Vector_Get(m_RecordData, i));
}

Console_Log("Demo saved to %s", filename);

Memory_FreePointer(&filename);
Memory_FreePointer(&full_path);
File_Close(fp);

Vector_Free(m_RecordData);
m_RecordData = NULL;

g_Config.target_mode = m_OldConfig.target_mode;
g_Config.enable_enhanced_look = m_OldConfig.enable_enhanced_look;
g_Config.enable_tr2_swimming = m_OldConfig.enable_tr2_swimming;
g_Config.fix_bear_ai = m_OldConfig.fix_bear_ai;
g_Config.rendering.fps = m_OldConfig.fps;

} else {
RECORD(g_Input.any & 0xF01FFF);
}
} else if (g_InputDB.toggle_recording) {
if (g_Config.enable_tr2_jumping) {
Console_Log(
"TR2 jumping is enabled - please disable and restart the "
"level.");
return;
}

m_OldConfig.enable_enhanced_look = g_Config.enable_enhanced_look;
m_OldConfig.enable_tr2_swimming = g_Config.enable_tr2_swimming;
m_OldConfig.target_mode = g_Config.target_mode;
m_OldConfig.fix_bear_ai = g_Config.fix_bear_ai;
m_OldConfig.fps = g_Config.rendering.fps;
g_Config.enable_enhanced_look = false;
g_Config.enable_tr2_swimming = false;
g_Config.target_mode = TLM_FULL;
g_Config.fix_bear_ai = false;
g_Config.rendering.fps = 30; // not sure if needed

m_RecordData =
Vector_CreateAtCapacity(sizeof(uint32_t), DEMO_COUNT_MAX / 2);
RECORD(g_LaraItem->pos.x);
RECORD(g_LaraItem->pos.y);
RECORD(g_LaraItem->pos.z);
RECORD(g_LaraItem->rot.x);
RECORD(g_LaraItem->rot.y);
RECORD(g_LaraItem->rot.z);
RECORD(g_LaraItem->room_num);

Random_SeedDraw(0xD371F947);
Random_SeedControl(0xD371F947);

Console_Log("Recording!");
}
}

static PHASE_CONTROL M_Control(int32_t nframes)
{
Interpolation_Remember();
Expand All @@ -64,6 +178,7 @@ static PHASE_CONTROL M_Control(int32_t nframes)
Input_Update();
Shell_ProcessInput();
Game_ProcessInput();
m_UpdateRecording();

if (g_Lara.death_timer > DEATH_WAIT
|| (g_Lara.death_timer > DEATH_WAIT_MIN && g_Input.any
Expand Down

0 comments on commit 82c98a9

Please sign in to comment.