From 93ab4451fc155bc1e841d3daafc4c85099dfb292 Mon Sep 17 00:00:00 2001 From: Ariel Antonitis Date: Mon, 19 Apr 2021 23:50:50 -0400 Subject: [PATCH] Testing combined averaging function. --- include/palette.h | 1 + src/overworld.c | 25 ++++----------- src/palette.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 19 deletions(-) diff --git a/include/palette.h b/include/palette.h index 071af1c12c..346091c64a 100644 --- a/include/palette.h +++ b/include/palette.h @@ -80,6 +80,7 @@ void AveragePalettes(u16 *palette0, u16* palette1, u16* dest, u16 weight); void TimeBlendPalette(u16 palOffset, u32 coeff, u32 blendColor); void TintPalette_RGB_Copy(u16 palOffset, u32 blendColor); void TimeBlendPalettes(u32 palettes, u32 coeff, u32 blendColor); +void TimeMixPalettes(u32 palettes, u32 coeff0, u32 color0, u32 coeff1, u32 color1, u16 weight); void TintPalette_GrayScale(u16 *palette, u16 count); void TintPalette_GrayScale2(u16 *palette, u16 count); void TintPalette_SepiaTone(u16 *palette, u16 count); diff --git a/src/overworld.c b/src/overworld.c index aa66280fc2..8ccdf412bb 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -1535,25 +1535,12 @@ void UpdatePalettesWithTime(u32 palettes) { palettes &= ~0xE000; // Don't blend tile palettes [13,15] if (!palettes) return; - for (i = 0; palettes; i+=16) { - if (palettes & 1) { - if (sTimeOfDayBlendVars[currentTimeBlend.time0].isTint) - TintPalette_RGB_Copy(i, sTimeOfDayBlendVars[currentTimeBlend.time0].blendColor); - else - TimeBlendPalette(i, sTimeOfDayBlendVars[currentTimeBlend.time0].coeff, sTimeOfDayBlendVars[currentTimeBlend.time0].blendColor); - if (currentTimeBlend.weight == 256) { - palettes >>= 1; - continue; - } - CpuFastCopy(&gPlttBufferFaded[i], tempPaletteBuffer, 32); - if (sTimeOfDayBlendVars[currentTimeBlend.time1].isTint) - TintPalette_RGB_Copy(i, sTimeOfDayBlendVars[currentTimeBlend.time1].blendColor); - else - TimeBlendPalette(i, sTimeOfDayBlendVars[currentTimeBlend.time1].coeff, sTimeOfDayBlendVars[currentTimeBlend.time1].blendColor); - AveragePalettes(tempPaletteBuffer, &gPlttBufferFaded[i], &gPlttBufferFaded[i], currentTimeBlend.weight); - } - palettes >>= 1; - } + TimeMixPalettes(palettes, + sTimeOfDayBlendVars[currentTimeBlend.time0].coeff, + sTimeOfDayBlendVars[currentTimeBlend.time0].blendColor, + sTimeOfDayBlendVars[currentTimeBlend.time1].coeff, + sTimeOfDayBlendVars[currentTimeBlend.time1].blendColor, + currentTimeBlend.weight); } } diff --git a/src/palette.c b/src/palette.c index 36dfdfffa3..c2029adf01 100644 --- a/src/palette.c +++ b/src/palette.c @@ -1131,6 +1131,83 @@ void TimeBlendPalettes(u32 palettes, u32 coeff, u32 blendColor) { } while (palettes); } +// Blends a weighted average of two blend parameters +void TimeMixPalettes(u32 palettes, u32 coeff0, u32 color0, u32 coeff1, u32 color1, u16 weight) { + s32 r0, g0, b0, r1, g1, b1, defR, defG, defB, altR, altG, altB; + u16 * palDataSrc; + u16 * palDataDst; + u32 defaultColor = DEFAULT_LIGHT_COLOR; + + if (!palettes) + return; + + // TODO: Check coeff for tint + coeff0 *= 2; + coeff1 *= 2; + r0 = (color0 << 27) >> 27; + g0 = (color0 << 22) >> 27; + b0 = (color0 << 17) >> 27; + r1 = (color1 << 27) >> 27; + g1 = (color1 << 22) >> 27; + b1 = (color1 << 17) >> 27; + defR = (defaultColor << 27) >> 27; + defG = (defaultColor << 22) >> 27; + defB = (defaultColor << 17) >> 27; + palDataSrc = gPlttBufferUnfaded; + palDataDst = gPlttBufferFaded; + + do { + if (palettes & 1) { + u16 *palDataSrcEnd = palDataSrc + 16; + u16 altBlendIndices = *palDataDst++ = *palDataSrc++; // color 0 is copied through + u32 altBlendColor; + if (altBlendIndices >> 15) { // High bit set; bitmask of which colors to alt-blend + // Note that bit 0 of altBlendIndices specifies color 1 + altBlendColor = palDataSrc[14]; // color 15 + if (altBlendColor >> 15) { // Set alternate blend color + altR = (altBlendColor << 27) >> 27; + altG = (altBlendColor << 22) >> 27; + altB = (altBlendColor << 17) >> 27; + } else { + altBlendColor = 0; + } + } else { + altBlendIndices = 0; + } + while (palDataSrc != palDataSrcEnd) { + u32 palDataSrcColor = *palDataSrc; + s32 r = (palDataSrcColor << 27) >> 27; + s32 g = (palDataSrcColor << 22) >> 27; + s32 b = (palDataSrcColor << 17) >> 27; + + if (altBlendIndices & 1) { // No need to average; colors will be the same + if (altBlendColor) { // Use alternate blend color + *palDataDst = ((r + (((altR - r) * coeff0) >> 5)) << 0) + | ((g + (((altG - g) * coeff0) >> 5)) << 5) + | ((b + (((altB - b) * coeff0) >> 5)) << 10); + } else { // Use default blend color + *palDataDst = ((r + (((defR - r) * coeff0) >> 5)) << 0) + | ((g + (((defG - g) * coeff0) >> 5)) << 5) + | ((b + (((defB - b) * coeff0) >> 5)) << 10); + } + } else { // Use provided blend colors + r = (weight*(r + (((r0 - r) * coeff0) >> 5)) + (256-weight)*(r + (((r1 - r) * coeff1) >> 5))) >> 8; + g = (weight*(g + (((g0 - g) * coeff0) >> 5)) + (256-weight)*(g + (((g1 - g) * coeff1) >> 5))) >> 8; + b = (weight*(b + (((b0 - b) * coeff0) >> 5)) + (256-weight)*(b + (((b1 - b) * coeff1) >> 5))) >> 8; + *palDataDst = RGB2(r, g, b); + } + palDataSrc++; + palDataDst++; + altBlendIndices >>= 1; + } + } else { + palDataSrc += 16; + palDataDst += 16; + } + palettes >>= 1; + } while (palettes); +} + void BlendPalettesUnfaded(u32 selectedPalettes, u8 coeff, u16 color) { void *src = gPlttBufferUnfaded;