Fixed bug where TOD was updated too frequently.

This commit is contained in:
Ariel Antonitis 2021-04-24 01:17:39 -04:00
parent dbc4cb8293
commit 4454a6397b
4 changed files with 6 additions and 76 deletions

View file

@ -141,6 +141,7 @@ void CleanupOverworldWindowsAndTilemaps(void);
bool32 IsUpdateLinkStateCBActive(void); bool32 IsUpdateLinkStateCBActive(void);
void CB1_Overworld(void); void CB1_Overworld(void);
void CB2_OverworldBasic(void); void CB2_OverworldBasic(void);
u8 UpdateTimeOfDay(void);
void UpdatePalettesWithTime(u32); void UpdatePalettesWithTime(u32);
void CB2_Overworld(void); void CB2_Overworld(void);
void SetMainCallback1(void (*cb)(void)); void SetMainCallback1(void (*cb)(void));

View file

@ -88,7 +88,6 @@ void BlendPalettesUnfaded(u32, u8, u16);
void BlendPalettesGradually(u32 selectedPalettes, s8 delay, u8 coeff, u8 coeffTarget, u16 color, u8 priority, u8 id); void BlendPalettesGradually(u32 selectedPalettes, s8 delay, u8 coeff, u8 coeffTarget, u16 color, u8 priority, u8 id);
void TimeBlendPalette(u16 palOffset, u32 coeff, u32 blendColor); void TimeBlendPalette(u16 palOffset, u32 coeff, u32 blendColor);
void TintPalette_RGB_Copy(u16 palOffset, u32 blendColor); void TintPalette_RGB_Copy(u16 palOffset, u32 blendColor);
void TimeBlendPalettes(u32 palettes, u32 coeff, u32 blendColor);
void TimeMixPalettes(u32, u16 *, u16 *, struct BlendSettings *, struct BlendSettings *, u16); void TimeMixPalettes(u32, u16 *, u16 *, struct BlendSettings *, struct BlendSettings *, u16);
void TintPalette_GrayScale(u16 *palette, u16 count); void TintPalette_GrayScale(u16 *palette, u16 count);
void TintPalette_GrayScale2(u16 *palette, u16 count); void TintPalette_GrayScale2(u16 *palette, u16 count);

View file

@ -186,7 +186,7 @@ static u16 (*sPlayerKeyInterceptCallback)(u32);
static bool8 sReceivingFromLink; static bool8 sReceivingFromLink;
static u8 sRfuKeepAliveTimer; static u8 sRfuKeepAliveTimer;
static u8 timeCounter; static u16 sTimeUpdateCounter; // playTimeVBlanks will eventually overflow, so this is used to update TOD
static struct TimeBlendSettings currentTimeBlend; static struct TimeBlendSettings currentTimeBlend;
// IWRAM common // IWRAM common
@ -1542,7 +1542,7 @@ void UpdatePalettesWithTime(u32 palettes) {
} }
} }
u8 UpdateSpritePaletteWithTime(u8 paletteNum) { // TODO: Optimize this u8 UpdateSpritePaletteWithTime(u8 paletteNum) {
UpdatePalettesWithTime(1 << (paletteNum + 16)); UpdatePalettesWithTime(1 << (paletteNum + 16));
return paletteNum; return paletteNum;
} }
@ -1559,13 +1559,13 @@ static void OverworldBasic(void)
UpdateTilesetAnimations(); UpdateTilesetAnimations();
DoScheduledBgTilemapCopiesToVram(); DoScheduledBgTilemapCopiesToVram();
// Every minute if no palette fade is active, update TOD blending as needed // Every minute if no palette fade is active, update TOD blending as needed
if (!(gPaletteFade.active || (timeCounter++ % 3600))) { if (!(gPaletteFade.active || (++sTimeUpdateCounter % 3600))) {
struct TimeBlendSettings cachedBlend = { struct TimeBlendSettings cachedBlend = {
.time0 = currentTimeBlend.time0, .time0 = currentTimeBlend.time0,
.time1 = currentTimeBlend.time1, .time1 = currentTimeBlend.time1,
.weight = currentTimeBlend.weight, .weight = currentTimeBlend.weight,
}; };
timeCounter = 0; sTimeUpdateCounter = 0;
UpdateTimeOfDay(); UpdateTimeOfDay();
if (cachedBlend.time0 != currentTimeBlend.time0 if (cachedBlend.time0 != currentTimeBlend.time0
|| cachedBlend.time1 != currentTimeBlend.time1 || cachedBlend.time1 != currentTimeBlend.time1

View file

@ -1084,78 +1084,8 @@ void TimeBlendPalette(u16 palOffset, u32 coeff, u32 blendColor) {
} }
} }
void TimeBlendPalettes(u32 palettes, u32 coeff, u32 blendColor) {
s32 newR, newG, newB, defR, defG, defB, altR, altG, altB;
u16 * src;
u16 * dst;
u32 defaultBlendColor = DEFAULT_LIGHT_COLOR;
if (!palettes)
return;
coeff *= 2;
newR = (blendColor << 27) >> 27;
newG = (blendColor << 22) >> 27;
newB = (blendColor << 17) >> 27;
defR = (defaultBlendColor << 27) >> 27;
defG = (defaultBlendColor << 22) >> 27;
defB = (defaultBlendColor << 17) >> 27;
src = gPlttBufferUnfaded;
dst = gPlttBufferFaded;
do {
if (palettes & 1) {
u16 *srcEnd = src + 16;
u16 altBlendIndices = *dst++ = *src++; // 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 = src[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 (src != srcEnd) {
u32 srcColor = *src;
s32 r = (srcColor << 27) >> 27;
s32 g = (srcColor << 22) >> 27;
s32 b = (srcColor << 17) >> 27;
if (altBlendIndices & 1) {
if (altBlendColor) { // Use alternate blend color
*dst = ((r + (((altR - r) * coeff) >> 5)) << 0)
| ((g + (((altG - g) * coeff) >> 5)) << 5)
| ((b + (((altB - b) * coeff) >> 5)) << 10);
} else { // Use default blend color
*dst = ((r + (((defR - r) * coeff) >> 5)) << 0)
| ((g + (((defG - g) * coeff) >> 5)) << 5)
| ((b + (((defB - b) * coeff) >> 5)) << 10);
}
} else { // Use provided blend color
*dst = ((r + (((newR - r) * coeff) >> 5)) << 0)
| ((g + (((newG - g) * coeff) >> 5)) << 5)
| ((b + (((newB - b) * coeff) >> 5)) << 10);
}
src++;
dst++;
altBlendIndices >>= 1;
}
} else {
src += 16;
dst += 16;
}
palettes >>= 1;
} while (palettes);
}
// Blends a weighted average of two blend parameters // Blends a weighted average of two blend parameters
// TODO: Should pointers be marked as const? // Parameters can be either blended (as in BlendPalettes) or tinted (as in TintPaletteRGB_Copy)
void TimeMixPalettes(u32 palettes, u16 *src, u16 *dst, struct BlendSettings *blend0, struct BlendSettings *blend1, u16 weight0) { void TimeMixPalettes(u32 palettes, u16 *src, u16 *dst, struct BlendSettings *blend0, struct BlendSettings *blend1, u16 weight0) {
s32 r0, g0, b0, r1, g1, b1, defR, defG, defB, altR, altG, altB; s32 r0, g0, b0, r1, g1, b1, defR, defG, defB, altR, altG, altB;
u32 color0, coeff0, color1, coeff1; u32 color0, coeff0, color1, coeff1;