From 4b0c3b367a94576411a9bec58b914e0b8133b4b1 Mon Sep 17 00:00:00 2001 From: lahm86 <33758420+lahm86@users.noreply.github.com> Date: Sun, 14 Apr 2024 11:44:13 +0100 Subject: [PATCH] level: structure animated textures This is an internal change to place animated textures into structures for improved readability. --- src/game/level.c | 31 +++++++++++++++++++++++++------ src/game/output.c | 23 +++++++++-------------- src/global/types.h | 6 ++++++ src/global/vars.c | 2 +- src/global/vars.h | 2 +- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/game/level.c b/src/game/level.c index 2aabc97b1..bdf0966b4 100644 --- a/src/game/level.c +++ b/src/game/level.c @@ -720,14 +720,33 @@ static bool Level_LoadBoxes(MYFILE *fp) static bool Level_LoadAnimatedTextures(MYFILE *fp) { + int16_t num_ranges; File_Read(&m_LevelInfo.anim_texture_range_count, sizeof(int32_t), 1, fp); - LOG_INFO("%d animated textures", m_LevelInfo.anim_texture_range_count); + File_Read(&num_ranges, sizeof(int16_t), 1, fp); + LOG_INFO("%d animated texture ranges", num_ranges); + if (!num_ranges) { + g_AnimTextureRanges = NULL; + return true; + } + g_AnimTextureRanges = GameBuf_Alloc( - sizeof(int16_t) * m_LevelInfo.anim_texture_range_count, - GBUF_ANIMATING_TEXTURE_RANGES); - File_Read( - g_AnimTextureRanges, sizeof(int16_t), - m_LevelInfo.anim_texture_range_count, fp); + 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. + File_Read(&range->num_textures, sizeof(int16_t), 1, fp); + range->num_textures++; + + range->textures = GameBuf_Alloc( + sizeof(int16_t) * range->num_textures, + GBUF_ANIMATING_TEXTURE_RANGES); + File_Read(range->textures, sizeof(int16_t), range->num_textures, fp); + } + return true; } diff --git a/src/game/output.c b/src/game/output.c index 98598577d..1f05b2026 100644 --- a/src/game/output.c +++ b/src/game/output.c @@ -928,21 +928,16 @@ void Output_AnimateTextures(void) return; } - if (g_AnimTextureRanges) { - const int16_t *ptr = g_AnimTextureRanges; - int16_t i = *ptr++; - while (i > 0) { - int16_t j = *ptr++; - const PHD_TEXTURE temp = g_PhdTextureInfo[*ptr]; - while (j > 0) { - g_PhdTextureInfo[ptr[0]] = g_PhdTextureInfo[ptr[1]]; - j--; - ptr++; - } - g_PhdTextureInfo[*ptr] = temp; - i--; - ptr++; + const TEXTURE_RANGE *range = g_AnimTextureRanges; + while (range) { + int32_t i = 0; + const PHD_TEXTURE temp = g_PhdTextureInfo[range->textures[i]]; + for (; i < range->num_textures - 1; i++) { + g_PhdTextureInfo[range->textures[i]] = + g_PhdTextureInfo[range->textures[i + 1]]; } + g_PhdTextureInfo[range->textures[i]] = temp; + range = range->next_range; } for (int32_t i = 0; i < STATIC_NUMBER_OF; i++) { diff --git a/src/global/types.h b/src/global/types.h index 627377a8b..8dc60ad77 100644 --- a/src/global/types.h +++ b/src/global/types.h @@ -1142,6 +1142,12 @@ typedef struct PHD_SPRITE { int16_t y2; } PHD_SPRITE; +typedef struct TEXTURE_RANGE { + int16_t num_textures; + int16_t *textures; + struct TEXTURE_RANGE *next_range; +} TEXTURE_RANGE; + typedef struct DOOR_INFO { int16_t room_num; struct { diff --git a/src/global/vars.c b/src/global/vars.c index 5105d62d1..bcc217d20 100644 --- a/src/global/vars.c +++ b/src/global/vars.c @@ -79,7 +79,7 @@ int16_t *g_FlyZone[2] = { NULL }; ANIM_STRUCT *g_Anims = NULL; ANIM_CHANGE_STRUCT *g_AnimChanges = NULL; ANIM_RANGE_STRUCT *g_AnimRanges = NULL; -int16_t *g_AnimTextureRanges = NULL; +TEXTURE_RANGE *g_AnimTextureRanges = NULL; int16_t *g_AnimCommands = NULL; int32_t *g_AnimBones = NULL; FRAME_INFO *g_AnimFrames = NULL; diff --git a/src/global/vars.h b/src/global/vars.h index 301cebb8a..a830526ba 100644 --- a/src/global/vars.h +++ b/src/global/vars.h @@ -67,7 +67,7 @@ extern int16_t *g_FlyZone[2]; extern ANIM_STRUCT *g_Anims; extern ANIM_CHANGE_STRUCT *g_AnimChanges; extern ANIM_RANGE_STRUCT *g_AnimRanges; -extern int16_t *g_AnimTextureRanges; +extern TEXTURE_RANGE *g_AnimTextureRanges; extern int16_t *g_AnimCommands; extern int32_t *g_AnimBones; extern FRAME_INFO *g_AnimFrames;