From 9ac8d0de25b6a1ca647bfcd4b911e375f6c2c704 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Mon, 5 Aug 2024 03:41:55 +0200 Subject: [PATCH] Optimize/get rid of unused palette vars/functions (#4841) --- include/palette.h | 30 ++-- src/battle_anim_mons.c | 6 - src/fldeff_sweetscent.c | 5 +- src/palette.c | 325 +++++----------------------------------- src/scrcmd.c | 5 +- 5 files changed, 60 insertions(+), 311 deletions(-) diff --git a/include/palette.h b/include/palette.h index 15c92cc2a7..13c06b0078 100644 --- a/include/palette.h +++ b/include/palette.h @@ -54,33 +54,31 @@ struct PaletteFadeControl extern struct PaletteFadeControl gPaletteFade; extern u32 gPlttBufferTransferPending; -extern u8 ALIGNED(4) gPaletteDecompressionBuffer[]; extern u16 ALIGNED(4) gPlttBufferUnfaded[PLTT_BUFFER_SIZE]; extern u16 ALIGNED(4) gPlttBufferFaded[PLTT_BUFFER_SIZE]; -void LoadCompressedPalette(const u32 *src, u16 offset, u16 size); -void LoadPalette(const void *src, u16 offset, u16 size); -void FillPalette(u16 value, u16 offset, u16 size); +void LoadCompressedPalette(const u32 *src, u32 offset, u32 size); +void LoadPalette(const void *src, u32 offset, u32 size); +void FillPalette(u32 value, u32 offset, u32 size); void TransferPlttBuffer(void); -u8 UpdatePaletteFade(void); +u32 UpdatePaletteFade(void); void ResetPaletteFade(void); -bool8 BeginNormalPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targetY, u16 blendColor); -void PaletteStruct_ResetById(u16 id); +bool32 BeginNormalPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targetY, u32 blendColor); void ResetPaletteFadeControl(void); void InvertPlttBuffer(u32 selectedPalettes); void TintPlttBuffer(u32 selectedPalettes, s8 r, s8 g, s8 b); void UnfadePlttBuffer(u32 selectedPalettes); -void BeginFastPaletteFade(u8 submode); -void BeginHardwarePaletteFade(u8 blendCnt, u8 delay, u8 y, u8 targetY, u8 shouldResetBlendRegisters); -void BlendPalettes(u32 selectedPalettes, u8 coeff, u16 color); -void BlendPalettesUnfaded(u32 selectedPalettes, u8 coeff, u16 color); +void BeginFastPaletteFade(u32 submode); +void BeginHardwarePaletteFade(u32 blendCnt, u32 delay, u32 y, u32 targetY, u32 shouldResetBlendRegisters); +void BlendPalettes(u32 selectedPalettes, u8 coeff, u32 color); +void BlendPalettesUnfaded(u32 selectedPalettes, u8 coeff, u32 color); void BlendPalettesGradually(u32 selectedPalettes, s8 delay, u8 coeff, u8 coeffTarget, u16 color, u8 priority, u8 id); -void TintPalette_GrayScale(u16 *palette, u16 count); -void TintPalette_GrayScale2(u16 *palette, u16 count); -void TintPalette_SepiaTone(u16 *palette, u16 count); -void TintPalette_CustomTone(u16 *palette, u16 count, u16 rTone, u16 gTone, u16 bTone); +void TintPalette_GrayScale(u16 *palette, u32 count); +void TintPalette_GrayScale2(u16 *palette, u32 count); +void TintPalette_SepiaTone(u16 *palette, u32 count); +void TintPalette_CustomTone(u16 *palette, u32 count, u16 rTone, u16 gTone, u16 bTone); -static inline void SetBackdropFromColor(u16 color) +static inline void SetBackdropFromColor(u32 color) { FillPalette(color, 0, PLTT_SIZEOF(1)); } diff --git a/src/battle_anim_mons.c b/src/battle_anim_mons.c index 542a179d6a..73c1e99448 100644 --- a/src/battle_anim_mons.c +++ b/src/battle_anim_mons.c @@ -663,12 +663,6 @@ static void UNUSED TranslateSpriteToBattleAttackerPos(struct Sprite *sprite) #undef sStartY #undef sTargetY -static void UNUSED EndUnkPaletteAnim(struct Sprite *sprite) -{ - PaletteStruct_ResetById(sprite->data[5]); - DestroySpriteAndMatrix(sprite); -} - void RunStoredCallbackWhenAffineAnimEnds(struct Sprite *sprite) { if (sprite->affineAnimEnded) diff --git a/src/fldeff_sweetscent.c b/src/fldeff_sweetscent.c index a28b6fee61..a5b2740458 100644 --- a/src/fldeff_sweetscent.c +++ b/src/fldeff_sweetscent.c @@ -1,4 +1,5 @@ #include "global.h" +#include "decompress.h" #include "event_data.h" #include "event_scripts.h" #include "field_effect.h" @@ -52,7 +53,7 @@ void StartSweetScentFieldEffect(void) u32 palettes = ~(1 << (gSprites[GetPlayerAvatarSpriteId()].oam.paletteNum + 16) | (1 << 13) | (1 << 14) | (1 << 15)); PlaySE(SE_M_SWEET_SCENT); - CpuFastCopy(gPlttBufferUnfaded, gPaletteDecompressionBuffer, PLTT_SIZE); + CpuFastCopy(gPlttBufferUnfaded, gDecompressionBuffer, PLTT_SIZE); CpuFastCopy(gPlttBufferFaded, gPlttBufferUnfaded, PLTT_SIZE); BeginNormalPaletteFade(palettes, 4, 0, 8, RGB_RED); taskId = CreateTask(TrySweetScentEncounter, 0); @@ -91,7 +92,7 @@ static void FailSweetScentEncounter(u8 taskId) { if (!gPaletteFade.active) { - CpuFastCopy(gPaletteDecompressionBuffer, gPlttBufferUnfaded, PLTT_SIZE); + CpuFastCopy(gDecompressionBuffer, gPlttBufferUnfaded, PLTT_SIZE); SetWeatherPalStateIdle(); ScriptContext_SetupScript(EventScript_FailSweetScent); DestroyTask(taskId); diff --git a/src/palette.c b/src/palette.c index fccff062a8..2526479ca8 100644 --- a/src/palette.c +++ b/src/palette.c @@ -13,62 +13,20 @@ enum HARDWARE_FADE, }; -// These are structs for some unused palette system. -// The full functionality of this system is unknown. - -#define NUM_PALETTE_STRUCTS 16 - -struct PaletteStructTemplate -{ - u16 id; - u16 *src; - bool16 pst_field_8_0:1; - u16 unused:9; - u16 size:5; - u8 time1; - u8 srcCount:5; - u8 state:3; - u8 time2; -}; - -struct PaletteStruct -{ - const struct PaletteStructTemplate *template; - bool32 active:1; - bool32 flag:1; - u32 baseDestOffset:9; - u32 destOffset:10; - u32 srcIndex:7; - u8 countdown1; - u8 countdown2; -}; - -static void PaletteStruct_Copy(struct PaletteStruct *, u32 *); -static void PaletteStruct_Blend(struct PaletteStruct *, u32 *); -static void PaletteStruct_TryEnd(struct PaletteStruct *); -static void PaletteStruct_Reset(u8); -static u8 PaletteStruct_GetPalNum(u16); -static u8 UpdateNormalPaletteFade(void); -static void BeginFastPaletteFadeInternal(u8); -static u8 UpdateFastPaletteFade(void); -static u8 UpdateHardwarePaletteFade(void); +static u32 UpdateNormalPaletteFade(void); +static void BeginFastPaletteFadeInternal(u32); +static u32 UpdateFastPaletteFade(void); +static u32 UpdateHardwarePaletteFade(void); static void UpdateBlendRegisters(void); -static bool8 IsSoftwarePaletteFadeFinishing(void); +static bool32 IsSoftwarePaletteFadeFinishing(void); static void Task_BlendPalettesGradually(u8 taskId); // palette buffers require alignment with agbcc because // unaligned word reads are issued in BlendPalette otherwise ALIGNED(4) EWRAM_DATA u16 gPlttBufferUnfaded[PLTT_BUFFER_SIZE] = {0}; ALIGNED(4) EWRAM_DATA u16 gPlttBufferFaded[PLTT_BUFFER_SIZE] = {0}; -static EWRAM_DATA struct PaletteStruct sPaletteStructs[NUM_PALETTE_STRUCTS] = {0}; EWRAM_DATA struct PaletteFadeControl gPaletteFade = {0}; static EWRAM_DATA u32 sPlttBufferTransferPending = 0; -EWRAM_DATA u8 ALIGNED(2) gPaletteDecompressionBuffer[PLTT_SIZE] = {0}; - -static const struct PaletteStructTemplate sDummyPaletteStructTemplate = { - .id = 0xFFFF, - .state = 1 -}; static const u8 sRoundedDownGrayscaleMap[] = { 0, 0, 0, 0, 0, @@ -80,20 +38,20 @@ static const u8 sRoundedDownGrayscaleMap[] = { 31, 31 }; -void LoadCompressedPalette(const u32 *src, u16 offset, u16 size) +void LoadCompressedPalette(const u32 *src, u32 offset, u32 size) { - LZDecompressWram(src, gPaletteDecompressionBuffer); - CpuCopy16(gPaletteDecompressionBuffer, &gPlttBufferUnfaded[offset], size); - CpuCopy16(gPaletteDecompressionBuffer, &gPlttBufferFaded[offset], size); + LZDecompressWram(src, gDecompressionBuffer); + CpuCopy16(gDecompressionBuffer, &gPlttBufferUnfaded[offset], size); + CpuCopy16(gDecompressionBuffer, &gPlttBufferFaded[offset], size); } -void LoadPalette(const void *src, u16 offset, u16 size) +void LoadPalette(const void *src, u32 offset, u32 size) { CpuCopy16(src, &gPlttBufferUnfaded[offset], size); CpuCopy16(src, &gPlttBufferFaded[offset], size); } -void FillPalette(u16 value, u16 offset, u16 size) +void FillPalette(u32 value, u32 offset, u32 size) { CpuFill16(value, &gPlttBufferUnfaded[offset], size); CpuFill16(value, &gPlttBufferFaded[offset], size); @@ -112,10 +70,9 @@ void TransferPlttBuffer(void) } } -u8 UpdatePaletteFade(void) +u32 UpdatePaletteFade(void) { - u8 result; - u8 dummy = 0; + u32 result; if (sPlttBufferTransferPending) return PALETTE_FADE_STATUS_LOADING; @@ -127,37 +84,19 @@ u8 UpdatePaletteFade(void) else result = UpdateHardwarePaletteFade(); - sPlttBufferTransferPending = gPaletteFade.multipurpose1 | dummy; + sPlttBufferTransferPending = gPaletteFade.multipurpose1; return result; } void ResetPaletteFade(void) { - u8 i; - - for (i = 0; i < NUM_PALETTE_STRUCTS; i++) - PaletteStruct_Reset(i); - ResetPaletteFadeControl(); } -static void ReadPlttIntoBuffers(void) -{ - u16 i; - u16 *pltt = (u16 *)PLTT; - - for (i = 0; i < PLTT_BUFFER_SIZE; i++) - { - gPlttBufferUnfaded[i] = pltt[i]; - gPlttBufferFaded[i] = pltt[i]; - } -} - -bool8 BeginNormalPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targetY, u16 blendColor) +bool32 BeginNormalPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targetY, u32 blendColor) { u8 temp; - u16 color = blendColor; if (gPaletteFade.active) { @@ -178,7 +117,7 @@ bool8 BeginNormalPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targe gPaletteFade_delay = delay; gPaletteFade.y = startY; gPaletteFade.targetY = targetY; - gPaletteFade.blendColor = color; + gPaletteFade.blendColor = blendColor; gPaletteFade.active = TRUE; gPaletteFade.mode = NORMAL_FADE; @@ -200,165 +139,6 @@ bool8 BeginNormalPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targe } } -static bool8 UNUSED BeginPlttFade(u32 selectedPalettes, u8 delay, u8 startY, u8 targetY, u16 blendColor) -{ - ReadPlttIntoBuffers(); - return BeginNormalPaletteFade(selectedPalettes, delay, startY, targetY, blendColor); -} - -static void UNUSED PaletteStruct_Run(u8 a1, u32 *unkFlags) -{ - u8 i; - - for (i = 0; i < NUM_PALETTE_STRUCTS; i++) - { - struct PaletteStruct *palstruct = &sPaletteStructs[i]; - if (palstruct->active) - { - if (palstruct->template->pst_field_8_0 == a1) - { - if (palstruct->srcIndex == palstruct->template->srcCount) - { - PaletteStruct_TryEnd(palstruct); - if (!palstruct->active) - continue; - } - if (palstruct->countdown1 == 0) - PaletteStruct_Copy(palstruct, unkFlags); - else - palstruct->countdown1--; - - PaletteStruct_Blend(palstruct, unkFlags); - } - } - } -} - -static void PaletteStruct_Copy(struct PaletteStruct *palStruct, u32 *unkFlags) -{ - s32 srcIndex; - s32 srcCount; - u8 i = 0; - u16 srcOffset = palStruct->srcIndex * palStruct->template->size; - - if (!palStruct->template->pst_field_8_0) - { - while (i < palStruct->template->size) - { - gPlttBufferUnfaded[palStruct->destOffset] = palStruct->template->src[srcOffset]; - gPlttBufferFaded[palStruct->destOffset] = palStruct->template->src[srcOffset]; - i++; - palStruct->destOffset++; - srcOffset++; - } - } - else - { - while (i < palStruct->template->size) - { - gPlttBufferFaded[palStruct->destOffset] = palStruct->template->src[srcOffset]; - i++; - palStruct->destOffset++; - srcOffset++; - } - } - - palStruct->destOffset = palStruct->baseDestOffset; - palStruct->countdown1 = palStruct->template->time1; - palStruct->srcIndex++; - - srcIndex = palStruct->srcIndex; - srcCount = palStruct->template->srcCount; - - if (srcIndex >= srcCount) - { - if (palStruct->countdown2) - palStruct->countdown2--; - palStruct->srcIndex = 0; - } - - *unkFlags |= 1 << (palStruct->baseDestOffset >> 4); -} - -static void PaletteStruct_Blend(struct PaletteStruct *palStruct, u32 *unkFlags) -{ - if (gPaletteFade.active && ((1 << (palStruct->baseDestOffset >> 4)) & gPaletteFade_selectedPalettes)) - { - if (!palStruct->template->pst_field_8_0) - { - if (gPaletteFade.delayCounter != gPaletteFade_delay) - { - BlendPalette( - palStruct->baseDestOffset, - palStruct->template->size, - gPaletteFade.y, - gPaletteFade.blendColor); - } - } - else - { - if (!gPaletteFade.delayCounter) - { - if (palStruct->countdown1 != palStruct->template->time1) - { - u32 srcOffset = palStruct->srcIndex * palStruct->template->size; - u8 i; - - for (i = 0; i < palStruct->template->size; i++) - gPlttBufferFaded[palStruct->baseDestOffset + i] = palStruct->template->src[srcOffset + i]; - } - } - } - } -} - -static void PaletteStruct_TryEnd(struct PaletteStruct *pal) -{ - if (pal->countdown2 == 0) - { - s32 state = pal->template->state; - - if (state == 0) - { - pal->srcIndex = 0; - pal->countdown1 = pal->template->time1; - pal->countdown2 = pal->template->time2; - pal->destOffset = pal->baseDestOffset; - } - else - { - if (state < 0) - return; - if (state > 2) - return; - PaletteStruct_ResetById(pal->template->id); - } - } - else - { - pal->countdown2--; - } -} - -void PaletteStruct_ResetById(u16 id) -{ - u8 paletteNum = PaletteStruct_GetPalNum(id); - if (paletteNum != NUM_PALETTE_STRUCTS) - PaletteStruct_Reset(paletteNum); -} - -static void PaletteStruct_Reset(u8 paletteNum) -{ - sPaletteStructs[paletteNum].template = &sDummyPaletteStructTemplate; - sPaletteStructs[paletteNum].active = FALSE; - sPaletteStructs[paletteNum].baseDestOffset = 0; - sPaletteStructs[paletteNum].destOffset = 0; - sPaletteStructs[paletteNum].srcIndex = 0; - sPaletteStructs[paletteNum].flag = 0; - sPaletteStructs[paletteNum].countdown1 = 0; - sPaletteStructs[paletteNum].countdown2 = 0; -} - void ResetPaletteFadeControl(void) { gPaletteFade.multipurpose1 = 0; @@ -379,32 +159,7 @@ void ResetPaletteFadeControl(void) gPaletteFade.deltaY = 2; } -static void UNUSED PaletteStruct_SetUnusedFlag(u16 id) -{ - u8 paletteNum = PaletteStruct_GetPalNum(id); - if (paletteNum != NUM_PALETTE_STRUCTS) - sPaletteStructs[paletteNum].flag = TRUE; -} - -static void UNUSED PaletteStruct_ClearUnusedFlag(u16 id) -{ - u8 paletteNum = PaletteStruct_GetPalNum(id); - if (paletteNum != NUM_PALETTE_STRUCTS) - sPaletteStructs[paletteNum].flag = FALSE; -} - -static u8 PaletteStruct_GetPalNum(u16 id) -{ - u8 i; - - for (i = 0; i < NUM_PALETTE_STRUCTS; i++) - if (sPaletteStructs[i].template->id == id) - return i; - - return NUM_PALETTE_STRUCTS; -} - -static u8 UpdateNormalPaletteFade(void) +static u32 UpdateNormalPaletteFade(void) { u16 paletteOffset; u16 selectedPalettes; @@ -498,7 +253,7 @@ void InvertPlttBuffer(u32 selectedPalettes) { if (selectedPalettes & 1) { - u8 i; + u32 i; for (i = 0; i < 16; i++) gPlttBufferFaded[paletteOffset + i] = ~gPlttBufferFaded[paletteOffset + i]; } @@ -515,7 +270,7 @@ void TintPlttBuffer(u32 selectedPalettes, s8 r, s8 g, s8 b) { if (selectedPalettes & 1) { - u8 i; + u32 i; for (i = 0; i < 16; i++) { struct PlttData *data = (struct PlttData *)&gPlttBufferFaded[paletteOffset + i]; @@ -546,13 +301,13 @@ void UnfadePlttBuffer(u32 selectedPalettes) } } -void BeginFastPaletteFade(u8 submode) +void BeginFastPaletteFade(u32 submode) { gPaletteFade.deltaY = 2; BeginFastPaletteFadeInternal(submode); } -static void BeginFastPaletteFadeInternal(u8 submode) +static void BeginFastPaletteFadeInternal(u32 submode) { gPaletteFade.y = 31; gPaletteFade_submode = submode & 0x3F; @@ -568,9 +323,9 @@ static void BeginFastPaletteFadeInternal(u8 submode) UpdatePaletteFade(); } -static u8 UpdateFastPaletteFade(void) +static u32 UpdateFastPaletteFade(void) { - u16 i; + u32 i; u16 paletteOffsetStart; u16 paletteOffsetEnd; s8 r0; @@ -726,7 +481,7 @@ static u8 UpdateFastPaletteFade(void) return gPaletteFade.active ? PALETTE_FADE_STATUS_ACTIVE : PALETTE_FADE_STATUS_DONE; } -void BeginHardwarePaletteFade(u8 blendCnt, u8 delay, u8 y, u8 targetY, u8 shouldResetBlendRegisters) +void BeginHardwarePaletteFade(u32 blendCnt, u32 delay, u32 y, u32 targetY, u32 shouldResetBlendRegisters) { gPaletteFade_blendCnt = blendCnt; gPaletteFade.delayCounter = delay; @@ -744,7 +499,7 @@ void BeginHardwarePaletteFade(u8 blendCnt, u8 delay, u8 y, u8 targetY, u8 should gPaletteFade.yDec = 1; } -static u8 UpdateHardwarePaletteFade(void) +static u32 UpdateHardwarePaletteFade(void) { if (!gPaletteFade.active) return PALETTE_FADE_STATUS_DONE; @@ -805,7 +560,7 @@ static void UpdateBlendRegisters(void) } } -static bool8 IsSoftwarePaletteFadeFinishing(void) +static bool32 IsSoftwarePaletteFadeFinishing(void) { if (gPaletteFade.softwareFadeFinishing) { @@ -828,7 +583,7 @@ static bool8 IsSoftwarePaletteFadeFinishing(void) } } -void BlendPalettes(u32 selectedPalettes, u8 coeff, u16 color) +void BlendPalettes(u32 selectedPalettes, u8 coeff, u32 color) { u16 paletteOffset; @@ -840,7 +595,7 @@ void BlendPalettes(u32 selectedPalettes, u8 coeff, u16 color) } } -void BlendPalettesUnfaded(u32 selectedPalettes, u8 coeff, u16 color) +void BlendPalettesUnfaded(u32 selectedPalettes, u8 coeff, u32 color) { void *src = gPlttBufferUnfaded; void *dest = gPlttBufferFaded; @@ -848,10 +603,10 @@ void BlendPalettesUnfaded(u32 selectedPalettes, u8 coeff, u16 color) BlendPalettes(selectedPalettes, coeff, color); } -void TintPalette_GrayScale(u16 *palette, u16 count) +void TintPalette_GrayScale(u16 *palette, u32 count) { - s32 r, g, b, i; - u32 gray; + s32 r, g, b; + u32 i, gray; for (i = 0; i < count; i++) { @@ -865,10 +620,10 @@ void TintPalette_GrayScale(u16 *palette, u16 count) } } -void TintPalette_GrayScale2(u16 *palette, u16 count) +void TintPalette_GrayScale2(u16 *palette, u32 count) { - s32 r, g, b, i; - u32 gray; + s32 r, g, b; + u32 i, gray; for (i = 0; i < count; i++) { @@ -887,10 +642,10 @@ void TintPalette_GrayScale2(u16 *palette, u16 count) } } -void TintPalette_SepiaTone(u16 *palette, u16 count) +void TintPalette_SepiaTone(u16 *palette, u32 count) { - s32 r, g, b, i; - u32 gray; + s32 r, g, b; + u32 i, gray; for (i = 0; i < count; i++) { @@ -911,10 +666,10 @@ void TintPalette_SepiaTone(u16 *palette, u16 count) } } -void TintPalette_CustomTone(u16 *palette, u16 count, u16 rTone, u16 gTone, u16 bTone) +void TintPalette_CustomTone(u16 *palette, u32 count, u16 rTone, u16 gTone, u16 bTone) { - s32 r, g, b, i; - u32 gray; + s32 r, g, b; + u32 i, gray; for (i = 0; i < count; i++) { diff --git a/src/scrcmd.c b/src/scrcmd.c index b642f41ede..4d391e0140 100644 --- a/src/scrcmd.c +++ b/src/scrcmd.c @@ -8,6 +8,7 @@ #include "contest_util.h" #include "contest_painting.h" #include "data.h" +#include "decompress.h" #include "decoration.h" #include "decoration_inventory.h" #include "event_data.h" @@ -656,12 +657,12 @@ bool8 ScrCmd_fadescreenswapbuffers(struct ScriptContext *ctx) case FADE_TO_BLACK: case FADE_TO_WHITE: default: - CpuCopy32(gPlttBufferUnfaded, gPaletteDecompressionBuffer, PLTT_SIZE); + CpuCopy32(gPlttBufferUnfaded, gDecompressionBuffer, PLTT_SIZE); FadeScreen(mode, 0); break; case FADE_FROM_BLACK: case FADE_FROM_WHITE: - CpuCopy32(gPaletteDecompressionBuffer, gPlttBufferUnfaded, PLTT_SIZE); + CpuCopy32(gDecompressionBuffer, gPlttBufferUnfaded, PLTT_SIZE); FadeScreen(mode, 0); break; }