diff --git a/src/libtrx/game/anims/common.c b/src/libtrx/game/anims/common.c new file mode 100644 index 0000000000..f8daa5f708 --- /dev/null +++ b/src/libtrx/game/anims/common.c @@ -0,0 +1,15 @@ +#include "game/anims/common.h" + +#include "game/gamebuf.h" + +static ANIM_BONE *m_Bones = NULL; + +void Anim_InitialiseBones(const int32_t num_bones) +{ + m_Bones = GameBuf_Alloc(sizeof(ANIM_BONE) * num_bones, GBUF_ANIM_BONES); +} + +ANIM_BONE *Anim_GetBone(const int32_t bone_idx) +{ + return &m_Bones[bone_idx]; +} diff --git a/src/libtrx/game/level/common.c b/src/libtrx/game/level/common.c index 11e2547da7..31c0d0461c 100644 --- a/src/libtrx/game/level/common.c +++ b/src/libtrx/game/level/common.c @@ -1,6 +1,7 @@ #include "game/level/common.h" #include "debug.h" +#include "game/anims.h" #include "game/gamebuf.h" #include "game/inject.h" #include "game/objects/common.h" @@ -212,3 +213,15 @@ void Level_ReadObjectMeshes( Vector_Free(unique_indices); } + +void Level_ReadAnimBones( + const int32_t base_idx, const int32_t num_bones, VFILE *const file) +{ + for (int32_t i = 0; i < num_bones; i++) { + ANIM_BONE *const bone = Anim_GetBone(base_idx + i); + bone->flags = VFile_ReadS32(file); + bone->pos.x = VFile_ReadS32(file); + bone->pos.y = VFile_ReadS32(file); + bone->pos.z = VFile_ReadS32(file); + } +} diff --git a/src/libtrx/game/objects/common.c b/src/libtrx/game/objects/common.c index 31f055b2bb..ea35537232 100644 --- a/src/libtrx/game/objects/common.c +++ b/src/libtrx/game/objects/common.c @@ -1,5 +1,6 @@ #include "game/objects/common.h" +#include "game/anims.h" #include "game/gamebuf.h" static OBJECT_MESH **m_MeshPointers = NULL; @@ -96,3 +97,8 @@ void Object_SwapMesh( m_MeshPointers[obj2->mesh_idx + mesh_num]; m_MeshPointers[obj2->mesh_idx + mesh_num] = temp; } + +ANIM_BONE *Object_GetBone(const OBJECT *const object, const int32_t bone_idx) +{ + return Anim_GetBone(object->bone_idx + bone_idx); +} diff --git a/src/libtrx/include/libtrx/game/anims.h b/src/libtrx/include/libtrx/game/anims.h index 02578caa46..8c13ea196d 100644 --- a/src/libtrx/include/libtrx/game/anims.h +++ b/src/libtrx/include/libtrx/game/anims.h @@ -1,5 +1,6 @@ #pragma once +#include "anims/common.h" #include "anims/enum.h" #include "anims/types.h" #include "anims/util.h" diff --git a/src/libtrx/include/libtrx/game/anims/common.h b/src/libtrx/include/libtrx/game/anims/common.h new file mode 100644 index 0000000000..adc0b004c2 --- /dev/null +++ b/src/libtrx/include/libtrx/game/anims/common.h @@ -0,0 +1,9 @@ +#pragma once + +#include "./types.h" + +#define BONE_SIZE 4 + +void Anim_InitialiseBones(int32_t num_bones); + +ANIM_BONE *Anim_GetBone(int32_t bone_idx); diff --git a/src/libtrx/include/libtrx/game/level/common.h b/src/libtrx/include/libtrx/game/level/common.h index cdfe35ac1d..b27af6fb66 100644 --- a/src/libtrx/include/libtrx/game/level/common.h +++ b/src/libtrx/include/libtrx/game/level/common.h @@ -5,3 +5,4 @@ void Level_ReadRoomMesh(int32_t room_num, VFILE *file); void Level_ReadObjectMeshes( int32_t num_indices, const int32_t *indices, VFILE *file); +void Level_ReadAnimBones(int32_t base_idx, int32_t num_bones, VFILE *file); diff --git a/src/libtrx/include/libtrx/game/objects/common.h b/src/libtrx/include/libtrx/game/objects/common.h index d8dfb1d6e6..bbf61fd937 100644 --- a/src/libtrx/include/libtrx/game/objects/common.h +++ b/src/libtrx/include/libtrx/game/objects/common.h @@ -34,6 +34,6 @@ OBJECT_MESH *Object_GetMesh(int32_t index); void Object_SwapMesh( GAME_OBJECT_ID object1_id, GAME_OBJECT_ID object2_id, int32_t mesh_num); -extern ANIM_BONE *Object_GetBone(const OBJECT *object, int32_t bone_idx); +ANIM_BONE *Object_GetBone(const OBJECT *object, int32_t bone_idx); extern void Object_DrawMesh(int32_t mesh_idx, int32_t clip, bool interpolated); diff --git a/src/libtrx/meson.build b/src/libtrx/meson.build index 140a8a0c2c..644b23558f 100644 --- a/src/libtrx/meson.build +++ b/src/libtrx/meson.build @@ -70,6 +70,7 @@ sources = [ 'enum_map.c', 'event_manager.c', 'filesystem.c', + 'game/anims/common.c', 'game/clock/common.c', 'game/clock/timer.c', 'game/clock/turbo.c', diff --git a/src/tr1/game/inject.c b/src/tr1/game/inject.c index 35d71fed6b..53f5d01043 100644 --- a/src/tr1/game/inject.c +++ b/src/tr1/game/inject.c @@ -260,7 +260,7 @@ static void M_LoadFromFile(INJECTION *injection, const char *filename) info->anim_change_count = VFile_ReadS32(fp); info->anim_range_count = VFile_ReadS32(fp); info->anim_cmd_count = VFile_ReadS32(fp); - info->anim_bone_count = VFile_ReadS32(fp); + info->anim_bone_count = VFile_ReadS32(fp) / BONE_SIZE; info->anim_frame_data_count = VFile_ReadS32(fp); info->anim_count = VFile_ReadS32(fp); info->object_count = VFile_ReadS32(fp); @@ -351,7 +351,7 @@ static void M_LoadFromFile(INJECTION *injection, const char *filename) VFile_Skip(fp, info->anim_change_count * 6); VFile_Skip(fp, info->anim_range_count * 8); VFile_Skip(fp, info->anim_cmd_count * 2); - VFile_Skip(fp, info->anim_bone_count * 4); + VFile_Skip(fp, info->anim_bone_count * 4 * BONE_SIZE); info->anim_frame_count = 0; info->anim_frame_mesh_rot_count = 0; @@ -554,9 +554,8 @@ static void M_AnimData(INJECTION *injection, LEVEL_INFO *level_info) VFile_Read( fp, g_AnimCommands + level_info->anim_command_count, sizeof(int16_t) * inj_info->anim_cmd_count); - VFile_Read( - fp, g_AnimBones + level_info->anim_bone_count, - sizeof(int32_t) * inj_info->anim_bone_count); + Level_ReadAnimBones( + level_info->anim_bone_count, inj_info->anim_bone_count, fp); const size_t frame_data_start = VFile_GetPos(fp); VFile_Skip(fp, inj_info->anim_frame_data_count * sizeof(int16_t)); const size_t frame_data_end = VFile_GetPos(fp); @@ -717,7 +716,7 @@ static void M_ObjectData( const int16_t num_meshes = VFile_ReadS16(fp); const int16_t mesh_idx = VFile_ReadS16(fp); - const int32_t bone_idx = VFile_ReadS32(fp); + const int32_t bone_idx = VFile_ReadS32(fp) / BONE_SIZE; // When mesh data has been omitted from the injection, this indicates // that we wish to retain what's already defined so to avoid duplicate diff --git a/src/tr1/game/level.c b/src/tr1/game/level.c index 9a5cafac5e..b4f4669540 100644 --- a/src/tr1/game/level.c +++ b/src/tr1/game/level.c @@ -375,17 +375,14 @@ static void M_LoadAnimCommands(VFILE *file) Benchmark_End(benchmark, NULL); } -static void M_LoadAnimBones(VFILE *file) +static void M_LoadAnimBones(VFILE *const file) { BENCHMARK *const benchmark = Benchmark_Start(); - m_LevelInfo.anim_bone_count = VFile_ReadS32(file); + m_LevelInfo.anim_bone_count = VFile_ReadS32(file) / BONE_SIZE; LOG_INFO("%d anim bones", m_LevelInfo.anim_bone_count); - g_AnimBones = GameBuf_Alloc( - sizeof(int32_t) - * (m_LevelInfo.anim_bone_count + m_InjectionInfo->anim_bone_count), - GBUF_ANIM_BONES); - VFile_Read( - file, g_AnimBones, sizeof(int32_t) * m_LevelInfo.anim_bone_count); + Anim_InitialiseBones( + m_LevelInfo.anim_bone_count + m_InjectionInfo->anim_bone_count); + Level_ReadAnimBones(0, m_LevelInfo.anim_bone_count, file); Benchmark_End(benchmark, NULL); } @@ -478,7 +475,7 @@ static void M_LoadObjects(VFILE *file) object->nmeshes = VFile_ReadS16(file); object->mesh_idx = VFile_ReadS16(file); - object->bone_idx = VFile_ReadS32(file); + object->bone_idx = VFile_ReadS32(file) / BONE_SIZE; const int32_t frame_offset = VFile_ReadS32(file); object->anim_idx = VFile_ReadS16(file); diff --git a/src/tr1/game/objects/common.c b/src/tr1/game/objects/common.c index 9364ade59d..b55736cdc3 100644 --- a/src/tr1/game/objects/common.c +++ b/src/tr1/game/objects/common.c @@ -431,8 +431,3 @@ void Object_DrawMesh( Output_DrawObjectMesh(mesh, clip); } } - -ANIM_BONE *Object_GetBone(const OBJECT *const object, const int32_t bone_idx) -{ - return (ANIM_BONE *)&g_AnimBones[object->bone_idx + bone_idx * 4]; -} diff --git a/src/tr1/global/vars.c b/src/tr1/global/vars.c index 8974ff431f..d0bfc80d0b 100644 --- a/src/tr1/global/vars.c +++ b/src/tr1/global/vars.c @@ -55,7 +55,6 @@ ANIM_CHANGE *g_AnimChanges = NULL; ANIM_RANGE *g_AnimRanges = NULL; TEXTURE_RANGE *g_AnimTextureRanges = NULL; int16_t *g_AnimCommands = NULL; -int32_t *g_AnimBones = NULL; ANIM_FRAME *g_AnimFrames = NULL; int32_t *g_AnimFrameMeshRots = NULL; int16_t g_NumCineFrames = 0; diff --git a/src/tr1/global/vars.h b/src/tr1/global/vars.h index d9a28a47ec..7c897d6fd1 100644 --- a/src/tr1/global/vars.h +++ b/src/tr1/global/vars.h @@ -63,7 +63,6 @@ extern ANIM_CHANGE *g_AnimChanges; extern ANIM_RANGE *g_AnimRanges; extern TEXTURE_RANGE *g_AnimTextureRanges; extern int16_t *g_AnimCommands; -extern int32_t *g_AnimBones; extern ANIM_FRAME *g_AnimFrames; extern int32_t *g_AnimFrameMeshRots; extern int16_t g_NumCineFrames; diff --git a/src/tr2/game/level.c b/src/tr2/game/level.c index 6b7910780c..9ae73d5d12 100644 --- a/src/tr2/game/level.c +++ b/src/tr2/game/level.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -310,11 +311,10 @@ static void M_LoadAnimCommands(VFILE *const file) static void M_LoadAnimBones(VFILE *const file) { BENCHMARK *const benchmark = Benchmark_Start(); - const int32_t num_anim_bones = VFile_ReadS32(file); + const int32_t num_anim_bones = VFile_ReadS32(file) / BONE_SIZE; LOG_INFO("anim bones: %d", num_anim_bones); - g_AnimBones = - GameBuf_Alloc(sizeof(int32_t) * num_anim_bones, GBUF_ANIM_BONES); - VFile_Read(file, g_AnimBones, sizeof(int32_t) * num_anim_bones); + Anim_InitialiseBones(num_anim_bones); + Level_ReadAnimBones(0, num_anim_bones, file); Benchmark_End(benchmark, NULL); } @@ -341,7 +341,7 @@ static void M_LoadObjects(VFILE *const file) OBJECT *const object = &g_Objects[object_id]; object->mesh_count = VFile_ReadS16(file); object->mesh_idx = VFile_ReadS16(file); - object->bone_idx = VFile_ReadS32(file); + object->bone_idx = VFile_ReadS32(file) / BONE_SIZE; const int32_t frame_idx = VFile_ReadS32(file); object->frame_base = ((int16_t *)g_AnimFrames) + frame_idx / 2; object->anim_idx = VFile_ReadS16(file); diff --git a/src/tr2/game/objects/common.c b/src/tr2/game/objects/common.c index 99eb87941b..13134bf562 100644 --- a/src/tr2/game/objects/common.c +++ b/src/tr2/game/objects/common.c @@ -277,8 +277,3 @@ BOUNDS_16 Object_GetBoundingBox( Matrix_Pop(); return new_bounds; } - -ANIM_BONE *Object_GetBone(const OBJECT *const object, const int32_t bone_idx) -{ - return (ANIM_BONE *)&g_AnimBones[object->bone_idx + bone_idx * 4]; -} diff --git a/src/tr2/global/vars.c b/src/tr2/global/vars.c index 401ef82431..b00bb22ed9 100644 --- a/src/tr2/global/vars.c +++ b/src/tr2/global/vars.c @@ -100,7 +100,6 @@ int32_t g_SoundEffectCount; OBJECT g_Objects[265] = { 0 }; int16_t **g_Meshes = NULL; ANIM *g_Anims = NULL; -int32_t *g_AnimBones = NULL; int32_t g_RoomCount; ROOM *g_Rooms = NULL; int32_t g_FlipStatus; diff --git a/src/tr2/global/vars.h b/src/tr2/global/vars.h index 17b4095b87..0e21171d2d 100644 --- a/src/tr2/global/vars.h +++ b/src/tr2/global/vars.h @@ -98,7 +98,6 @@ extern int32_t g_SoundEffectCount; extern OBJECT g_Objects[265]; extern int16_t **g_Meshes; extern ANIM *g_Anims; -extern int32_t *g_AnimBones; extern int32_t g_RoomCount; extern ROOM *g_Rooms; extern int32_t g_FlipStatus;