Skip to content

Commit

Permalink
libtrx/anims/common: create TRX ANIM_BONE storage
Browse files Browse the repository at this point in the history
This eliminates separate g_AnimBones globals in both games and moves
storage to TRX.
  • Loading branch information
lahm86 committed Jan 4, 2025
1 parent 6e188dc commit 1d6017e
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 35 deletions.
15 changes: 15 additions & 0 deletions src/libtrx/game/anims/common.c
Original file line number Diff line number Diff line change
@@ -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];
}
13 changes: 13 additions & 0 deletions src/libtrx/game/level/common.c
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/libtrx/game/objects/common.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "game/objects/common.h"

#include "game/anims.h"
#include "game/gamebuf.h"

static OBJECT_MESH **m_MeshPointers = NULL;
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/game/anims.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "anims/common.h"
#include "anims/enum.h"
#include "anims/types.h"
#include "anims/util.h"
9 changes: 9 additions & 0 deletions src/libtrx/include/libtrx/game/anims/common.h
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/game/level/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion src/libtrx/include/libtrx/game/objects/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions src/libtrx/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 5 additions & 6 deletions src/tr1/game/inject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions src/tr1/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions src/tr1/game/objects/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
1 change: 0 additions & 1 deletion src/tr1/global/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/tr1/global/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <libtrx/engine/audio.h>
#include <libtrx/filesystem.h>
#include <libtrx/game/gamebuf.h>
#include <libtrx/game/level.h>
#include <libtrx/log.h>
#include <libtrx/memory.h>

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down
5 changes: 0 additions & 5 deletions src/tr2/game/objects/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
1 change: 0 additions & 1 deletion src/tr2/global/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/tr2/global/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1d6017e

Please sign in to comment.