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

level: read animated textures in TRX #2400

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
2 changes: 1 addition & 1 deletion src/libtrx/include/libtrx/game/enum_map.def
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_BOXES, "Boxes")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_OVERLAPS, "Overlaps")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_GROUND_ZONE, "Ground zones")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_FLY_ZONE, "Fly zones")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_ANIMATING_TEXTURE_RANGES, "Animating texture ranges")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_ANIMATED_TEXTURE_RANGES, "Animated texture ranges")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_CINEMATIC_FRAMES, "Cinematic frames")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_DEMO_BUFFER, "Demo buffer")
ENUM_MAP_DEFINE(GAME_BUFFER, GBUF_CREATURE_DATA, "Creature data")
Expand Down
2 changes: 1 addition & 1 deletion src/libtrx/include/libtrx/game/game_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef enum {
GBUF_OVERLAPS,
GBUF_GROUND_ZONE,
GBUF_FLY_ZONE,
GBUF_ANIMATING_TEXTURE_RANGES,
GBUF_ANIMATED_TEXTURE_RANGES,
GBUF_CINEMATIC_FRAMES,
GBUF_CREATURE_DATA,
GBUF_CREATURE_LOT,
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
6 changes: 6 additions & 0 deletions src/libtrx/include/libtrx/game/output/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ typedef struct {
int16_t y1;
} SPRITE_TEXTURE;

typedef struct ANIMATED_TEXTURE_RANGE {
int16_t num_textures;
int16_t *textures;
struct ANIMATED_TEXTURE_RANGE *next_range;
} ANIMATED_TEXTURE_RANGE;

typedef struct {
float r;
float g;
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;
42 changes: 7 additions & 35 deletions src/tr1/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,49 +607,21 @@ static void M_LoadBoxes(VFILE *file)
Benchmark_End(benchmark, NULL);
}

static void M_LoadAnimatedTextures(VFILE *file)
static void M_LoadAnimatedTextures(VFILE *const file)
{
BENCHMARK *const benchmark = Benchmark_Start();
m_LevelInfo.anim_texture_range_count = VFile_ReadS32(file);
size_t end_position = VFile_GetPos(file)
+ m_LevelInfo.anim_texture_range_count * sizeof(int16_t);
const int32_t data_size = VFile_ReadS32(file);
const size_t end_position =
VFile_GetPos(file) + data_size * sizeof(int16_t);

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

g_AnimTextureRanges = GameBuf_Alloc(
sizeof(TEXTURE_RANGE) * num_ranges, GBUF_ANIMATING_TEXTURE_RANGES);
for (int32_t i = 0; i < num_ranges; i++) {
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_ANIMATING_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.
const int32_t skip_length = end_position - VFile_GetPos(file);
if (skip_length > 0) {
VFile_Skip(file, skip_length);
}
VFile_SetPos(file, end_position);
Benchmark_End(benchmark, NULL);
}
}

static void M_LoadItems(VFILE *file)
{
Expand Down
4 changes: 2 additions & 2 deletions src/tr1/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -1081,8 +1081,8 @@ void Output_AnimateTextures(const int32_t num_frames)
m_WibbleOffset = (m_WibbleOffset + num_frames) % WIBBLE_SIZE;
m_AnimatedTexturesOffset += num_frames;
while (m_AnimatedTexturesOffset > 5) {
const TEXTURE_RANGE *range = g_AnimTextureRanges;
while (range) {
const ANIMATED_TEXTURE_RANGE *range = g_AnimTextureRanges;
while (range != NULL) {
int32_t i = 0;
const OBJECT_TEXTURE temp = g_ObjectTextures[range->textures[i]];
for (; i < range->num_textures - 1; i++) {
Expand Down
7 changes: 0 additions & 7 deletions src/tr1/global/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ typedef struct {
int16_t v;
} PHD_VBUF;

typedef struct TEXTURE_RANGE {
int16_t num_textures;
int16_t *textures;
struct TEXTURE_RANGE *next_range;
} TEXTURE_RANGE;

typedef struct {
SECTOR *sector;
SECTOR old_sector;
Expand Down Expand Up @@ -362,7 +356,6 @@ typedef struct {
int32_t texture_page_count;
uint8_t *texture_palette_page_ptrs;
RGBA_8888 *texture_rgb_page_ptrs;
int32_t anim_texture_range_count;
int32_t item_count;
int32_t sprite_info_count;
int32_t overlap_count;
Expand Down
2 changes: 1 addition & 1 deletion src/tr1/global/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ uint16_t *g_Overlap = NULL;
int16_t *g_GroundZone[2] = { NULL };
int16_t *g_GroundZone2[2] = { NULL };
int16_t *g_FlyZone[2] = { NULL };
TEXTURE_RANGE *g_AnimTextureRanges = NULL;
ANIMATED_TEXTURE_RANGE *g_AnimTextureRanges = NULL;
int16_t g_NumCineFrames = 0;
int16_t g_CineFrame = -1;
CINE_CAMERA *g_CineCamera = NULL;
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 TEXTURE_RANGE *g_AnimTextureRanges;
extern int16_t g_NumCineFrames;
extern int16_t g_CineFrame;
extern CINE_CAMERA *g_CineCamera;
Expand Down
14 changes: 10 additions & 4 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,16 @@ static void M_LoadBoxes(VFILE *const file)
static void M_LoadAnimatedTextures(VFILE *const file)
{
BENCHMARK *const benchmark = Benchmark_Start();
const int32_t num_ranges = VFile_ReadS32(file);
g_AnimTextureRanges = GameBuf_Alloc(
sizeof(int16_t) * num_ranges, GBUF_ANIMATING_TEXTURE_RANGES);
VFile_Read(file, g_AnimTextureRanges, sizeof(int16_t) * num_ranges);
const int32_t data_size = VFile_ReadS32(file);
const size_t end_position =
VFile_GetPos(file) + data_size * sizeof(int16_t);

const int16_t num_ranges = VFile_ReadS16(file);
LOG_INFO("%d animated texture ranges", num_ranges);
Output_InitialiseAnimatedTextures(num_ranges);
Level_ReadAnimatedTextureRanges(num_ranges, file);

VFile_SetPos(file, end_position);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm isn't this function identical to TR1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is, we also have others more or less identical like objects and static meshes. But TR1 currently has the extra injection counts in some others; I'm hoping eventually we can get everything common shifted to the TRX reader. So with that difference, I was thinking it'd be cleaner for now to keep the similar base private functions in the two level modules and just move the common "intensive" reading out to TRX.
But equally, I'm happy to shift the identical ones over now if that's preferred overall.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No it's OK to keep them around, whichever is more comfortable for you 😌

Benchmark_End(benchmark, NULL);
}

Expand Down
17 changes: 9 additions & 8 deletions src/tr2/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -746,15 +746,16 @@ void Output_DoAnimateTextures(const int32_t ticks)
{
m_TickComp += ticks;
while (m_TickComp > TICKS_PER_FRAME * 5) {
const int16_t *ptr = g_AnimTextureRanges;
int16_t i = *(ptr++);
for (; i > 0; --i, ++ptr) {
int16_t j = *(ptr++);
const OBJECT_TEXTURE temp = g_ObjectTextures[*ptr];
for (; j > 0; --j, ++ptr) {
g_ObjectTextures[ptr[0]] = g_ObjectTextures[ptr[1]];
const ANIMATED_TEXTURE_RANGE *range = g_AnimTextureRanges;
while (range != NULL) {
int32_t i = 0;
const OBJECT_TEXTURE temp = g_ObjectTextures[range->textures[i]];
for (; i < range->num_textures - 1; i++) {
g_ObjectTextures[range->textures[i]] =
g_ObjectTextures[range->textures[i + 1]];
}
g_ObjectTextures[*ptr] = temp;
g_ObjectTextures[range->textures[i]] = temp;
range = range->next_range;
}
m_TickComp -= TICKS_PER_FRAME * 5;
}
Expand Down
2 changes: 1 addition & 1 deletion src/tr2/global/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ int32_t g_TexturePageCount;
int32_t g_ObjectTextureCount;
uint8_t g_LabTextureUVFlag[MAX_OBJECT_TEXTURES];
int32_t g_NumCameras;
int16_t *g_AnimTextureRanges = NULL;
ANIMATED_TEXTURE_RANGE *g_AnimTextureRanges = NULL;
uint32_t *g_DemoData = NULL;
char g_LevelFileName[256];
uint16_t g_MusicTrackFlags[64];
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 int16_t *g_AnimTextureRanges;
extern uint32_t *g_DemoData;
extern char g_LevelFileName[256];
extern uint16_t g_MusicTrackFlags[64];
Expand Down
Loading