Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 committed Jan 27, 2025
1 parent b0872c2 commit d502460
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 60 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 @@ -295,3 +296,12 @@ void Output_AddDynamicLight(
light->shade.value_1 = intensity;
light->falloff.value_1 = falloff;
}

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);
}
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 @@ -44,3 +44,5 @@ void Output_LightRoom(ROOM *room);

void Output_ResetDynamicLights(void);
void Output_AddDynamicLight(XYZ_32 pos, int32_t intensity, int32_t falloff);

void Output_InitialiseAnimatedTextures(int32_t num_ranges);
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;
39 changes: 5 additions & 34 deletions src/tr1/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,46 +610,17 @@ static void M_LoadBoxes(VFILE *file)
static void M_LoadAnimatedTextures(VFILE *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);
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
6 changes: 0 additions & 6 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
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
13 changes: 9 additions & 4 deletions src/tr2/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,15 @@ 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);
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);
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

0 comments on commit d502460

Please sign in to comment.