diff --git a/src/libtrx/game/level/common.c b/src/libtrx/game/level/common.c index 0edc1bb2d..845d99403 100644 --- a/src/libtrx/game/level/common.c +++ b/src/libtrx/game/level/common.c @@ -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); + } +} diff --git a/src/libtrx/game/output.c b/src/libtrx/game/output.c index ba2c8f49f..3cef626ec 100644 --- a/src/libtrx/game/output.c +++ b/src/libtrx/game/output.c @@ -1,6 +1,7 @@ #include "game/output.h" #include "game/const.h" +#include "game/game_buf.h" #include "game/matrix.h" #include "utils.h" @@ -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 diff --git a/src/libtrx/include/libtrx/game/level/common.h b/src/libtrx/include/libtrx/game/level/common.h index 72df382f0..3e363297e 100644 --- a/src/libtrx/include/libtrx/game/level/common.h +++ b/src/libtrx/include/libtrx/game/level/common.h @@ -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); diff --git a/src/libtrx/include/libtrx/game/output.h b/src/libtrx/include/libtrx/game/output.h index e24be199e..b4bb3736e 100644 --- a/src/libtrx/include/libtrx/game/output.h +++ b/src/libtrx/include/libtrx/game/output.h @@ -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); diff --git a/src/libtrx/include/libtrx/game/output/vars.h b/src/libtrx/include/libtrx/game/output/vars.h index ceaa2075c..2fc657069 100644 --- a/src/libtrx/include/libtrx/game/output/vars.h +++ b/src/libtrx/include/libtrx/game/output/vars.h @@ -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; diff --git a/src/tr1/game/level.c b/src/tr1/game/level.c index ffae963b5..fa33a2a6c 100644 --- a/src/tr1/game/level.c +++ b/src/tr1/game/level.c @@ -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) { diff --git a/src/tr1/global/vars.h b/src/tr1/global/vars.h index 22b58d78c..e6811e539 100644 --- a/src/tr1/global/vars.h +++ b/src/tr1/global/vars.h @@ -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; diff --git a/src/tr2/game/level.c b/src/tr2/game/level.c index 1c4350ced..08af0a6d3 100644 --- a/src/tr2/game/level.c +++ b/src/tr2/game/level.c @@ -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) { diff --git a/src/tr2/global/vars.h b/src/tr2/global/vars.h index f378647a0..b1d065e4d 100644 --- a/src/tr2/global/vars.h +++ b/src/tr2/global/vars.h @@ -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];