Skip to content

Commit

Permalink
level: read animated textures in TRX
Browse files Browse the repository at this point in the history
This shifts animated texture reading to TRX.
  • Loading branch information
lahm86 committed Jan 27, 2025
1 parent 7ba9bac commit 8bcf42c
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 59 deletions.
19 changes: 19 additions & 0 deletions src/libtrx/game/level/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,22 @@ void Level_ReadSpriteSequences(const int32_t num_sequences, VFILE *const file)
}
}
}

void Level_ReadAnimatedTextureRanges(
const int32_t num_ranges, VFILE *const file)
{
for (int32_t i = 0; i < num_ranges; i++) {
ANIMATED_TEXTURE_RANGE *const range = &g_AnimTextureRanges[i];
range->next_range =
i == num_ranges - 1 ? NULL : &g_AnimTextureRanges[i + 1];

// Level data is tied to the original logic in Output_AnimateTextures
// and hence stores one less than the actual count here.
range->num_textures = VFile_ReadS16(file) + 1;
range->textures = GameBuf_Alloc(
sizeof(int16_t) * range->num_textures,
GBUF_ANIMATED_TEXTURE_RANGES);
VFile_Read(
file, range->textures, sizeof(int16_t) * range->num_textures);
}
}
10 changes: 10 additions & 0 deletions src/libtrx/game/output.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "game/output.h"

#include "game/const.h"
#include "game/game_buf.h"
#include "game/matrix.h"
#include "utils.h"

Expand Down Expand Up @@ -147,6 +148,15 @@ void Output_CalculateLight(const XYZ_32 pos, const int16_t room_num)
Output_SetLightDivider(global_divider);
}

void Output_InitialiseAnimatedTextures(const int32_t num_ranges)
{
g_AnimTextureRanges = num_ranges == 0
? NULL
: GameBuf_Alloc(
sizeof(ANIMATED_TEXTURE_RANGE) * num_ranges,
GBUF_ANIMATED_TEXTURE_RANGES);
}

void Output_CalculateStaticLight(const int16_t adder)
{
// TODO: use m_LsAdder
Expand Down
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 @@ -22,3 +22,4 @@ void Level_ReadObjectTextures(
void Level_ReadSpriteTextures(
int32_t base_idx, int16_t base_page_idx, int32_t num_textures, VFILE *file);
void Level_ReadSpriteSequences(int32_t num_sequences, VFILE *file);
void Level_ReadAnimatedTextureRanges(int32_t num_ranges, VFILE *file);
2 changes: 2 additions & 0 deletions src/libtrx/include/libtrx/game/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern int32_t Output_CalcFogShade(int32_t depth);
extern int32_t Output_GetRoomLightShade(ROOM_LIGHT_MODE mode);
extern void Output_LightRoomVertices(const ROOM *room);

void Output_InitialiseAnimatedTextures(int32_t num_ranges);

void Output_CalculateLight(XYZ_32 pos, int16_t room_num);
void Output_CalculateStaticLight(int16_t adder);
void Output_CalculateStaticMeshLight(XYZ_32 pos, SHADE shade, const ROOM *room);
Expand Down
2 changes: 2 additions & 0 deletions src/libtrx/include/libtrx/game/output/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
#include "./const.h"
#include "./types.h"

// TODO: change to output.c module scope
extern OBJECT_TEXTURE g_ObjectTextures[MAX_OBJECT_TEXTURES];
extern SPRITE_TEXTURE g_SpriteTextures[MAX_SPRITE_TEXTURES];
extern ANIMATED_TEXTURE_RANGE *g_AnimTextureRanges;
30 changes: 2 additions & 28 deletions src/tr1/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,38 +615,12 @@ static void M_LoadAnimatedTextures(VFILE *file)

const int16_t num_ranges = VFile_ReadS16(file);
LOG_INFO("%d animated texture ranges", num_ranges);
if (num_ranges == 0) {
g_AnimTextureRanges = NULL;
goto cleanup;
}

g_AnimTextureRanges = GameBuf_Alloc(
sizeof(ANIMATED_TEXTURE_RANGE) * num_ranges,
GBUF_ANIMATED_TEXTURE_RANGES);
for (int32_t i = 0; i < num_ranges; i++) {
ANIMATED_TEXTURE_RANGE *range = &g_AnimTextureRanges[i];
range->next_range =
i == num_ranges - 1 ? NULL : &g_AnimTextureRanges[i + 1];

// Level data is tied to the original logic in Output_AnimateTextures
// and hence stores one less than the actual count here.
range->num_textures = VFile_ReadS16(file);
range->num_textures++;

range->textures = GameBuf_Alloc(
sizeof(int16_t) * range->num_textures,
GBUF_ANIMATED_TEXTURE_RANGES);
VFile_Read(
file, range->textures, sizeof(int16_t) * range->num_textures);
}
Output_InitialiseAnimatedTextures(num_ranges);
Level_ReadAnimatedTextureRanges(num_ranges, file);

cleanup: {
// Ensure to read everything intended by the level compiler, even if it
// does not wholly contain accurate texture data.
VFile_SetPos(file, end_position);
Benchmark_End(benchmark, NULL);
}
}

static void M_LoadItems(VFILE *file)
{
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 @@ -52,7 +52,6 @@ extern uint16_t *g_Overlap;
extern int16_t *g_GroundZone[2];
extern int16_t *g_GroundZone2[2];
extern int16_t *g_FlyZone[2];
extern ANIMATED_TEXTURE_RANGE *g_AnimTextureRanges;
extern int16_t g_NumCineFrames;
extern int16_t g_CineFrame;
extern CINE_CAMERA *g_CineCamera;
Expand Down
32 changes: 3 additions & 29 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,38 +546,12 @@ static void M_LoadAnimatedTextures(VFILE *file)

const int16_t num_ranges = VFile_ReadS16(file);
LOG_INFO("%d animated texture ranges", num_ranges);
if (num_ranges == 0) {
g_AnimTextureRanges = NULL;
goto cleanup;
}

g_AnimTextureRanges = GameBuf_Alloc(
sizeof(ANIMATED_TEXTURE_RANGE) * num_ranges,
GBUF_ANIMATED_TEXTURE_RANGES);
for (int32_t i = 0; i < num_ranges; i++) {
ANIMATED_TEXTURE_RANGE *range = &g_AnimTextureRanges[i];
range->next_range =
i == num_ranges - 1 ? NULL : &g_AnimTextureRanges[i + 1];

// Level data is tied to the original logic in Output_AnimateTextures
// and hence stores one less than the actual count here.
range->num_textures = VFile_ReadS16(file);
range->num_textures++;

range->textures = GameBuf_Alloc(
sizeof(int16_t) * range->num_textures,
GBUF_ANIMATED_TEXTURE_RANGES);
VFile_Read(
file, range->textures, sizeof(int16_t) * range->num_textures);
}

cleanup: {
// Ensure to read everything intended by the level compiler, even if it
// does not wholly contain accurate texture data.
Output_InitialiseAnimatedTextures(num_ranges);
Level_ReadAnimatedTextureRanges(num_ranges, file);

VFile_SetPos(file, end_position);
Benchmark_End(benchmark, NULL);
}
}

static void M_LoadCinematic(VFILE *const file)
{
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 @@ -114,7 +114,6 @@ extern int32_t g_TexturePageCount;
extern int32_t g_ObjectTextureCount;
extern uint8_t g_LabTextureUVFlag[MAX_OBJECT_TEXTURES];
extern int32_t g_NumCameras;
extern ANIMATED_TEXTURE_RANGE *g_AnimTextureRanges;
extern uint32_t *g_DemoData;
extern char g_LevelFileName[256];
extern uint16_t g_MusicTrackFlags[64];
Expand Down

0 comments on commit 8bcf42c

Please sign in to comment.