From 65e616342b8ea414dd175e0fee116f7aaf7105eb Mon Sep 17 00:00:00 2001 From: Ariel Antonitis Date: Sat, 24 Apr 2021 03:24:14 -0400 Subject: [PATCH] Changed time blending to work with FadeScreen. --- common_syms/overworld.txt | 1 + include/overworld.h | 5 +++++ include/palette.h | 2 +- src/field_weather.c | 16 +++++++++++++--- src/overworld.c | 30 ++++++++++++++++-------------- src/palette.c | 9 ++++----- 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/common_syms/overworld.txt b/common_syms/overworld.txt index 4833362098..cc92cee1e2 100644 --- a/common_syms/overworld.txt +++ b/common_syms/overworld.txt @@ -7,3 +7,4 @@ gFieldCallback2 gLocalLinkPlayerId gFieldLinkPlayerCount gTimeOfDay +currentTimeBlend diff --git a/include/overworld.h b/include/overworld.h index 58cf5b16af..2a1353b7bc 100644 --- a/include/overworld.h +++ b/include/overworld.h @@ -62,11 +62,15 @@ extern void (*gFieldCallback)(void); extern bool8 (*gFieldCallback2)(void); extern u8 gLocalLinkPlayerId; extern u8 gFieldLinkPlayerCount; + extern u8 gTimeOfDay; +extern struct TimeBlendSettings currentTimeBlend; // Exported ROM declarations extern const struct UCoords32 gDirectionToVectors[]; +extern const struct BlendSettings gTimeOfDayBlend[]; + void DoWhiteOut(void); void Overworld_ResetStateAfterFly(void); void Overworld_ResetStateAfterTeleport(void); @@ -142,6 +146,7 @@ bool32 IsUpdateLinkStateCBActive(void); void CB1_Overworld(void); void CB2_OverworldBasic(void); u8 UpdateTimeOfDay(void); +bool8 MapHasNaturalLight(u8 mapType); void UpdatePalettesWithTime(u32); void CB2_Overworld(void); void SetMainCallback1(void (*cb)(void)); diff --git a/include/palette.h b/include/palette.h index 3b0368aca4..201e853800 100644 --- a/include/palette.h +++ b/include/palette.h @@ -70,7 +70,7 @@ u8 UpdatePaletteFade(void); void ResetPaletteFade(void); void ReadPlttIntoBuffers(void); bool8 BeginNormalPaletteFade(u32, s8, u8, u8, u16); -bool8 BeginTimeOfDayPaletteFade(u32, s8, u8, u8, struct BlendSettings *, struct BlendSettings *, u16); +bool8 BeginTimeOfDayPaletteFade(u32, s8, u8, u8, struct BlendSettings *, struct BlendSettings *, u16, u16); bool8 unref_sub_8073D3C(u32, u8, u8, u8, u16); void unref_sub_8073D84(u8, u32 *); void ResetPaletteStructByUid(u16); diff --git a/src/field_weather.c b/src/field_weather.c index 089fcf9894..bbc7506e32 100644 --- a/src/field_weather.c +++ b/src/field_weather.c @@ -472,6 +472,7 @@ static void ApplyGammaShift(u8 startPalIndex, u8 numPalettes, s8 gammaIndex) curPalIndex = startPalIndex; // Loop through the speficied palette range and apply necessary gamma shifts to the colors. + // TODO: Optimize this to work with time blending while (curPalIndex < numPalettes) { CpuFastCopy(gPlttBufferUnfaded + palOffset, gPlttBufferFaded + palOffset, 16 * sizeof(u16)); @@ -647,7 +648,7 @@ static void ApplyDroughtGammaShiftWithBlend(s8 gammaIndex, u8 blendCoeff, u16 bl } } -static void ApplyFogBlend(u8 blendCoeff, u16 blendColor) +static void ApplyFogBlend(u8 blendCoeff, u16 blendColor) // TODO: How does this interact with time { struct RGBColor color; u8 rBlend; @@ -794,9 +795,18 @@ void FadeScreen(u8 mode, s8 delay) { gWeatherPtr->fadeDestColor = fadeColor; if (useWeatherPal) - gWeatherPtr->fadeScreenCounter = 0; - else + gWeatherPtr->fadeScreenCounter = 0; // Triggers gamma-shift-based fade-in + else { + UpdateTimeOfDay(); + if (MapHasNaturalLight(gMapHeader.mapType)) { + BeginTimeOfDayPaletteFade(PALETTES_ALL, delay, 16, 0, + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time0], + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time1], + currentTimeBlend.weight, fadeColor); + } else { BeginNormalPaletteFade(PALETTES_ALL, delay, 16, 0, fadeColor); + } + } gWeatherPtr->palProcessingState = WEATHER_PAL_STATE_SCREEN_FADING_IN; gWeatherPtr->fadeInFirstFrame = TRUE; diff --git a/src/overworld.c b/src/overworld.c index 99d8294947..5e4aa1a32f 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -187,7 +187,7 @@ static bool8 sReceivingFromLink; static u8 sRfuKeepAliveTimer; static u16 sTimeUpdateCounter; // playTimeVBlanks will eventually overflow, so this is used to update TOD -static struct TimeBlendSettings currentTimeBlend; + // IWRAM common u16 *gBGTilemapBuffers1; @@ -200,6 +200,7 @@ u8 gLocalLinkPlayerId; // This is our player id in a multiplayer mode. u8 gFieldLinkPlayerCount; u8 gTimeOfDay; +struct TimeBlendSettings currentTimeBlend; // EWRAM vars EWRAM_DATA static u8 sObjectEventLoadFlag = 0; @@ -828,7 +829,7 @@ void LoadMapFromCameraTransition(u8 mapGroup, u8 mapNum) RoamerMove(); DoCurrentWeather(); ResetFieldTasksArgs(); - UpdatePalettesWithTime(0xFFFFFFFF); + UpdatePalettesWithTime(PALETTES_ALL); RunOnResumeMapScript(); if (gMapHeader.regionMapSectionId != MAPSEC_BATTLE_FRONTIER @@ -1462,7 +1463,7 @@ void CB1_Overworld(void) -static const struct BlendSettings sTimeOfDayBlendVars[] = +const struct BlendSettings gTimeOfDayBlend[] = { [TIME_OF_DAY_NIGHT] = {.coeff = 10, .blendColor = 0x1400}, [TIME_OF_DAY_TWILIGHT] = {.coeff = 4, .blendColor = 0x56dc, .isTint = TRUE}, @@ -1506,20 +1507,21 @@ u8 UpdateTimeOfDay(void) { } } -static bool8 MapHasNaturalLight(u8 mapType) { // Whether a map type is naturally lit/outside +bool8 MapHasNaturalLight(u8 mapType) { // Whether a map type is naturally lit/outside return mapType == MAP_TYPE_TOWN || mapType == MAP_TYPE_CITY || mapType == MAP_TYPE_ROUTE || mapType == MAP_TYPE_OCEAN_ROUTE; } // TODO: Rewrite palette fading to work with FadeScreen +// Currently, this cancels the "Normal" palette fade started by FadeScreen static bool8 FadePalettesWithTime(void) { // Only used to fade back in - gTimeOfDay = UpdateTimeOfDay(); + UpdateTimeOfDay(); if (MapHasNaturalLight(gMapHeader.mapType)) { ResetPaletteFade(); - BeginTimeOfDayPaletteFade(0xFFFFFFFF, 0, 16, 0, - (struct BlendSettings *)&sTimeOfDayBlendVars[currentTimeBlend.time0], - (struct BlendSettings *)&sTimeOfDayBlendVars[currentTimeBlend.time1], - currentTimeBlend.weight); + BeginTimeOfDayPaletteFade(PALETTES_ALL, 0, 16, 0, + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time0], + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time1], + currentTimeBlend.weight, 0); } } @@ -1537,8 +1539,8 @@ void UpdatePalettesWithTime(u32 palettes) { TimeMixPalettes(palettes, gPlttBufferUnfaded, gPlttBufferFaded, - (struct BlendSettings *)&sTimeOfDayBlendVars[currentTimeBlend.time0], - (struct BlendSettings *)&sTimeOfDayBlendVars[currentTimeBlend.time1], + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time0], + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time1], currentTimeBlend.weight); } } @@ -1571,7 +1573,7 @@ static void OverworldBasic(void) if (cachedBlend.time0 != currentTimeBlend.time0 || cachedBlend.time1 != currentTimeBlend.time1 || cachedBlend.weight != currentTimeBlend.weight) - UpdatePalettesWithTime(0xFFFFFFFF); + UpdatePalettesWithTime(PALETTES_ALL); } } @@ -1686,7 +1688,7 @@ static void CB2_LoadMap2(void) DoMapLoadLoop(&gMain.state); SetFieldVBlankCallback(); SetMainCallback1(CB1_Overworld); - FadePalettesWithTime(); + // FadePalettesWithTime(); SetMainCallback2(CB2_Overworld); } @@ -1743,7 +1745,7 @@ static void CB2_ReturnToFieldLocal(void) if (ReturnToFieldLocal(&gMain.state)) { SetFieldVBlankCallback(); - FadePalettesWithTime(); + // FadePalettesWithTime(); SetMainCallback2(CB2_Overworld); } } diff --git a/src/palette.c b/src/palette.c index 8b08eb9e48..b947f464c3 100644 --- a/src/palette.c +++ b/src/palette.c @@ -204,8 +204,7 @@ bool8 BeginNormalPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targe } // Like normal palette fade but respects sprite/tile palettes immune to time of day fading -// Blend color here is always assumed to be 0 (black). -bool8 BeginTimeOfDayPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targetY, struct BlendSettings *bld0, struct BlendSettings *bld1, u16 weight) +bool8 BeginTimeOfDayPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 targetY, struct BlendSettings *bld0, struct BlendSettings *bld1, u16 weight, u16 color) { u8 temp; @@ -231,7 +230,7 @@ bool8 BeginTimeOfDayPaletteFade(u32 selectedPalettes, s8 delay, u8 startY, u8 ta gPaletteFade.active = 1; gPaletteFade.mode = TIME_OF_DAY_FADE; - gPaletteFade.blendColor = 0; + gPaletteFade.blendColor = color; gPaletteFade.bld0 = bld0; gPaletteFade.bld1 = bld1; gPaletteFade.weight = weight; @@ -522,7 +521,7 @@ static u8 UpdateTimeOfDayPaletteFade(void) u16 * dst1 = dst; while (copyPalettes) { if (copyPalettes & 1) - CpuFastCopy(src1, dst1, 64); + CpuFastCopy(src1, dst1, 32); copyPalettes >>= 1; src1 += 16; dst1 += 16; @@ -530,7 +529,7 @@ static u8 UpdateTimeOfDayPaletteFade(void) } // Then, blend from faded->faded with native BlendPalettes - BlendPalettesFine(selectedPalettes, dst, dst, gPaletteFade.y, 0); + BlendPalettesFine(selectedPalettes, dst, dst, gPaletteFade.y, gPaletteFade.blendColor); gPaletteFade.objPaletteToggle ^= 1;