Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libtrx/anims/common: move ANIMs to TRX #2230

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/libtrx/game/anims/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

#include "game/gamebuf.h"

ANIM *g_Anims = NULL;
static ANIM *m_Anims = NULL;
static ANIM_BONE *m_Bones = NULL;

void Anim_InitialiseAnims(const int32_t num_anims)
{
m_Anims = GameBuf_Alloc(sizeof(ANIM) * num_anims, GBUF_ANIMS);
}

void Anim_InitialiseBones(const int32_t num_bones)
{
m_Bones = GameBuf_Alloc(sizeof(ANIM_BONE) * num_bones, GBUF_ANIM_BONES);
}

ANIM *Anim_GetAnim(const int32_t anim_idx)
{
return &g_Anims[anim_idx];
return &m_Anims[anim_idx];
}

ANIM_BONE *Anim_GetBone(const int32_t bone_idx)
Expand Down
35 changes: 35 additions & 0 deletions src/libtrx/game/level/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,41 @@ void Level_ReadObjectMeshes(
Vector_Free(unique_indices);
}

void Level_ReadAnims(
const int32_t base_idx, const int32_t num_anims, VFILE *const file,
int32_t **frame_pointers)
{
for (int32_t i = 0; i < num_anims; i++) {
ANIM *const anim = Anim_GetAnim(base_idx + i);
#if TR_VERSION == 1
anim->frame_ofs = VFile_ReadU32(file);
const int16_t interpolation = VFile_ReadS16(file);
ASSERT(interpolation <= 0xFF);
anim->interpolation = interpolation & 0xFF;
anim->frame_size = 0;
#else
Copy link
Collaborator

@walkawayy walkawayy Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these else consistent with all later TRs? Is it worth explicitly saying #if TR_VERSION == 2 to set up for potentially a TR3X or later that is different?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TR3 uses the same format as TR2. Arguably, TR1 is the same in terms of struct size, it's just that frame size was introduced in TR2 to deal with the way rotations are packed.
TR4 and 5 have extra (unrelated) properties (lateral speed and acceleration IIRC).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK – we'll have to go through every single if TR_VERSION when thinking to support more games anyway.

const int32_t frame_idx = VFile_ReadS32(file);
if (frame_pointers != NULL) {
(*frame_pointers)[i] = frame_idx;
}
anim->frame_ptr = NULL; // filled later by the animation frame loader
anim->interpolation = VFile_ReadU8(file);
anim->frame_size = VFile_ReadU8(file);
#endif
anim->current_anim_state = VFile_ReadS16(file);
anim->velocity = VFile_ReadS32(file);
anim->acceleration = VFile_ReadS32(file);
anim->frame_base = VFile_ReadS16(file);
anim->frame_end = VFile_ReadS16(file);
anim->jump_anim_num = VFile_ReadS16(file);
anim->jump_frame_num = VFile_ReadS16(file);
anim->num_changes = VFile_ReadS16(file);
anim->change_idx = VFile_ReadS16(file);
anim->num_commands = VFile_ReadS16(file);
anim->command_idx = VFile_ReadS16(file);
}
}

void Level_ReadAnimBones(
const int32_t base_idx, const int32_t num_bones, VFILE *const file)
{
Expand Down
3 changes: 1 addition & 2 deletions src/libtrx/include/libtrx/game/anims/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

#include "./types.h"

extern ANIM *g_Anims;

void Anim_InitialiseAnims(int32_t num_anims);
void Anim_InitialiseBones(int32_t num_bones);

ANIM *Anim_GetAnim(int32_t anim_idx);
Expand Down
2 changes: 2 additions & 0 deletions src/libtrx/include/libtrx/game/level/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
void Level_ReadRoomMesh(int32_t room_num, VFILE *file);
void Level_ReadObjectMeshes(
int32_t num_indices, const int32_t *indices, VFILE *file);
void Level_ReadAnims(
int32_t base_idx, int32_t num_anims, VFILE *file, int32_t **frame_pointers);
void Level_ReadAnimBones(int32_t base_idx, int32_t num_bones, VFILE *file);
18 changes: 1 addition & 17 deletions src/tr1/game/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,26 +584,10 @@ static void M_AnimData(INJECTION *injection, LEVEL_INFO *level_info)
}
ASSERT(VFile_GetPos(fp) == frame_data_end);

Level_ReadAnims(level_info->anim_count, inj_info->anim_count, fp, NULL);
for (int32_t i = 0; i < inj_info->anim_count; i++) {
ANIM *const anim = Anim_GetAnim(level_info->anim_count + i);

anim->frame_ofs = VFile_ReadU32(fp);
const int16_t interpolation = VFile_ReadS16(fp);
ASSERT(interpolation <= 0xFF);
anim->interpolation = interpolation & 0xFF;
anim->frame_size = 0;
anim->current_anim_state = VFile_ReadS16(fp);
anim->velocity = VFile_ReadS32(fp);
anim->acceleration = VFile_ReadS32(fp);
anim->frame_base = VFile_ReadS16(fp);
anim->frame_end = VFile_ReadS16(fp);
anim->jump_anim_num = VFile_ReadS16(fp);
anim->jump_frame_num = VFile_ReadS16(fp);
anim->num_changes = VFile_ReadS16(fp);
anim->change_idx = VFile_ReadS16(fp);
anim->num_commands = VFile_ReadS16(fp);
anim->command_idx = VFile_ReadS16(fp);

// Re-align to the level.
anim->jump_anim_num += level_info->anim_count;
bool found = false;
Expand Down
25 changes: 2 additions & 23 deletions src/tr1/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,29 +295,8 @@ static void M_LoadAnims(VFILE *file)
BENCHMARK *const benchmark = Benchmark_Start();
m_LevelInfo.anim_count = VFile_ReadS32(file);
LOG_INFO("%d anims", m_LevelInfo.anim_count);
g_Anims = GameBuf_Alloc(
sizeof(ANIM) * (m_LevelInfo.anim_count + m_InjectionInfo->anim_count),
GBUF_ANIMS);
for (int32_t i = 0; i < m_LevelInfo.anim_count; i++) {
ANIM *const anim = Anim_GetAnim(i);

anim->frame_ofs = VFile_ReadU32(file);
const int16_t interpolation = VFile_ReadS16(file);
ASSERT(interpolation <= 0xFF);
anim->interpolation = interpolation & 0xFF;
anim->frame_size = 0;
anim->current_anim_state = VFile_ReadS16(file);
anim->velocity = VFile_ReadS32(file);
anim->acceleration = VFile_ReadS32(file);
anim->frame_base = VFile_ReadS16(file);
anim->frame_end = VFile_ReadS16(file);
anim->jump_anim_num = VFile_ReadS16(file);
anim->jump_frame_num = VFile_ReadS16(file);
anim->num_changes = VFile_ReadS16(file);
anim->change_idx = VFile_ReadS16(file);
anim->num_commands = VFile_ReadS16(file);
anim->command_idx = VFile_ReadS16(file);
}
Anim_InitialiseAnims(m_LevelInfo.anim_count + m_InjectionInfo->anim_count);
Level_ReadAnims(0, m_LevelInfo.anim_count, file, NULL);
Benchmark_End(benchmark, NULL);
}

Expand Down
25 changes: 2 additions & 23 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,32 +234,11 @@ static int32_t M_LoadAnims(VFILE *const file, int32_t **frame_pointers)
BENCHMARK *const benchmark = Benchmark_Start();
const int32_t num_anims = VFile_ReadS32(file);
LOG_INFO("anims: %d", num_anims);
g_Anims = GameBuf_Alloc(sizeof(ANIM) * num_anims, GBUF_ANIMS);
if (frame_pointers != NULL) {
*frame_pointers = Memory_Alloc(sizeof(int32_t) * num_anims);
}

for (int32_t i = 0; i < num_anims; i++) {
ANIM *const anim = Anim_GetAnim(i);
const int32_t frame_idx = VFile_ReadS32(file);
if (frame_pointers != NULL) {
(*frame_pointers)[i] = frame_idx;
}
anim->frame_ptr = NULL; // filled later by the animation frame loader
anim->interpolation = VFile_ReadU8(file);
anim->frame_size = VFile_ReadU8(file);
anim->current_anim_state = VFile_ReadS16(file);
anim->velocity = VFile_ReadS32(file);
anim->acceleration = VFile_ReadS32(file);
anim->frame_base = VFile_ReadS16(file);
anim->frame_end = VFile_ReadS16(file);
anim->jump_anim_num = VFile_ReadS16(file);
anim->jump_frame_num = VFile_ReadS16(file);
anim->num_changes = VFile_ReadS16(file);
anim->change_idx = VFile_ReadS16(file);
anim->num_commands = VFile_ReadS16(file);
anim->command_idx = VFile_ReadS16(file);
}
Anim_InitialiseAnims(num_anims);
Level_ReadAnims(0, num_anims, file, frame_pointers);
Benchmark_End(benchmark, NULL);
return num_anims;
}
Expand Down