From 147e101df516cb9eeee81bbbe5e763bbd8e2637b Mon Sep 17 00:00:00 2001 From: Ariel Antonitis Date: Sun, 25 Apr 2021 17:00:07 -0400 Subject: [PATCH] More testing of fog blending. --- include/field_weather.h | 2 +- src/event_object_movement.c | 6 ++--- src/faraway_island.c | 2 +- src/field_effect.c | 3 +-- src/field_effect_helpers.c | 6 ++--- src/field_weather.c | 53 ++++++++++++++++++++++--------------- src/field_weather_effect.c | 7 +++++ src/overworld.c | 31 +++++++++------------- 8 files changed, 61 insertions(+), 49 deletions(-) diff --git a/include/field_weather.h b/include/field_weather.h index c462707f9d..ce1a91c4c7 100644 --- a/include/field_weather.h +++ b/include/field_weather.h @@ -151,7 +151,7 @@ void ApplyWeatherGammaShiftIfIdle(s8 gammaIndex); void sub_80ABC7C(u8 gammaIndex, u8 gammaTargetIndex, u8 gammaStepDelay); void FadeScreen(u8 mode, s8 delay); bool8 IsWeatherNotFadingIn(void); -void UpdateSpritePaletteWithWeather(u8 spritePaletteIndex); +void UpdateSpritePaletteWithWeather(u8 spritePaletteIndex, bool8 allowFog); void ApplyWeatherGammaShiftToPal(u8 paletteIndex); void LoadCustomWeatherSpritePalette(const u16 *palette); void ResetDroughtWeatherPaletteLoading(void); diff --git a/src/event_object_movement.c b/src/event_object_movement.c index 635659fb03..76fc26d055 100644 --- a/src/event_object_movement.c +++ b/src/event_object_movement.c @@ -1684,7 +1684,7 @@ static void FollowerSetGraphics(struct ObjectEvent *objectEvent, u16 species, u8 if (palette[0] & 0x4000) // If color 15 is blended, use it as the alternate color palette[15] |= 0x8000; } - UpdateSpritePaletteWithTime(sprite->oam.paletteNum); + UpdateSpritePaletteWithWeather(sprite->oam.paletteNum, FALSE); } else sprite->oam.paletteNum = IndexOfSpritePaletteTag(spritePalette->tag); // Tag is always present } @@ -2186,7 +2186,7 @@ static u8 UpdateSpritePalette(const struct SpritePalette * spritePalette, struct sprite->inUse = TRUE; if (IndexOfSpritePaletteTag(spritePalette->tag) == 0xFF) { sprite->oam.paletteNum = LoadSpritePalette(spritePalette); - UpdateSpritePaletteWithTime(sprite->oam.paletteNum); + UpdateSpritePaletteWithWeather(sprite->oam.paletteNum, FALSE); } else { sprite->oam.paletteNum = LoadSpritePalette(spritePalette); } @@ -2443,7 +2443,7 @@ static u8 LoadSpritePaletteIfTagExists(const struct SpritePalette *spritePalette if (IndexOfSpritePaletteTag(spritePalette->tag) != 0xFF) return 0xFF; paletteNum = LoadSpritePalette(spritePalette); - UpdateSpritePaletteWithTime(paletteNum); + UpdateSpritePaletteWithWeather(paletteNum, FALSE); return paletteNum; } diff --git a/src/faraway_island.c b/src/faraway_island.c index bc08146523..28c5bcf787 100755 --- a/src/faraway_island.c +++ b/src/faraway_island.c @@ -393,7 +393,7 @@ void SetMewAboveGrass(void) gSprites[mew->spriteId].subpriority = 1; LoadSpritePalette(&gSpritePalette_GeneralFieldEffect1); - UpdateSpritePaletteWithWeather(IndexOfSpritePaletteTag(gSpritePalette_GeneralFieldEffect1.tag)); + UpdateSpritePaletteWithWeather(IndexOfSpritePaletteTag(gSpritePalette_GeneralFieldEffect1.tag), FALSE); x = mew->currentCoords.x; y = mew->currentCoords.y; diff --git a/src/field_effect.c b/src/field_effect.c index ad6bdd747a..4ef5ea4970 100644 --- a/src/field_effect.c +++ b/src/field_effect.c @@ -770,8 +770,7 @@ void FieldEffectScript_LoadFadedPalette(u8 **script) { struct SpritePalette *palette = (struct SpritePalette *)FieldEffectScript_ReadWord(script); LoadSpritePalette(palette); - UpdateSpritePaletteWithTime(IndexOfSpritePaletteTag(palette->tag)); // Ensure field effects are blended - UpdateSpritePaletteWithWeather(IndexOfSpritePaletteTag(palette->tag)); + UpdateSpritePaletteWithWeather(IndexOfSpritePaletteTag(palette->tag), FALSE); (*script) += 4; } diff --git a/src/field_effect_helpers.c b/src/field_effect_helpers.c index 8c3cf5bb1c..389a3dce63 100755 --- a/src/field_effect_helpers.c +++ b/src/field_effect_helpers.c @@ -151,7 +151,7 @@ static void LoadObjectRegularReflectionPalette(struct ObjectEvent *objectEvent, ApplyIceFilter(mainSprite->oam.paletteNum, filteredData); } paletteNum = LoadSpritePalette(&filteredPalette); - UpdateSpritePaletteWithWeather(paletteNum); + UpdateSpritePaletteWithWeather(paletteNum, TRUE); } sprite->oam.paletteNum = paletteNum; sprite->oam.objMode = 1; // Alpha blending @@ -170,7 +170,7 @@ static void LoadObjectHighBridgeReflectionPalette(struct ObjectEvent *objectEven blueData[i] = 0x55c9; } sprite->oam.paletteNum = LoadSpritePalette(&bluePalette); - UpdateSpritePaletteWithWeather(sprite->oam.paletteNum); + UpdateSpritePaletteWithWeather(sprite->oam.paletteNum, TRUE); } static void UpdateObjectReflectionSprite(struct Sprite *reflectionSprite) @@ -203,7 +203,7 @@ static void UpdateObjectReflectionSprite(struct Sprite *reflectionSprite) ApplyIceFilter(mainSprite->oam.paletteNum, filteredData); } paletteNum = LoadSpritePalette(&filteredPalette); - UpdateSpritePaletteWithWeather(paletteNum); + UpdateSpritePaletteWithWeather(paletteNum, TRUE); } reflectionSprite->oam.paletteNum = paletteNum; } diff --git a/src/field_weather.c b/src/field_weather.c index d4444a098d..aad85dfc61 100644 --- a/src/field_weather.c +++ b/src/field_weather.c @@ -536,13 +536,16 @@ static void ApplyGammaShift(u8 startPalIndex, u8 numPalettes, s8 gammaIndex) } else { - u32 palettes = 0; - u32 mask = 1 << startPalIndex; - u8 i; - CpuFastCopy(gPlttBufferUnfaded + startPalIndex * 16, gPlttBufferFaded + startPalIndex * 16, numPalettes * 16 * sizeof(u16)); - for (i = 0; i < numPalettes; i++, mask <<= 1) - palettes |= mask; - UpdatePalettesWithTime(palettes); + if (MapHasNaturalLight(gMapHeader.mapType)) { // Time-blend + u32 palettes = 0; + u32 mask = 1 << startPalIndex; + u8 i; + for (i = 0; i < numPalettes; i++, mask <<= 1) + palettes |= mask; + UpdatePalettesWithTime(palettes); + } else { // copy + CpuFastCopy(gPlttBufferUnfaded + startPalIndex * 16, gPlttBufferFaded + startPalIndex * 16, numPalettes * 16 * sizeof(u16)); + } } } @@ -653,7 +656,7 @@ static void ApplyDroughtGammaShiftWithBlend(s8 gammaIndex, u8 blendCoeff, u16 bl } } -static void ApplyFogBlend(u8 blendCoeff, u16 blendColor) // TODO: How does this interact with time +static void ApplyFogBlend(u8 blendCoeff, u16 blendColor) // TODO: Optimize this with time blending { struct RGBColor color; u8 rBlend; @@ -661,7 +664,11 @@ static void ApplyFogBlend(u8 blendCoeff, u16 blendColor) // TODO: How does this u8 bBlend; u16 curPalIndex; - BlendPalette(0, 256, blendCoeff, blendColor); + // First blend all palettes with time + CpuFastCopy(gPlttBufferUnfaded, gPlttBufferFaded, 0x400); + UpdatePalettesWithTime(PALETTES_ALL); + // Then blend tile palettes [0, 12] faded->faded + BlendPalettesFine(0x1FFF, gPlttBufferFaded, gPlttBufferFaded, blendCoeff, blendColor); color = *(struct RGBColor *)&blendColor; rBlend = color.r; gBlend = color.g; @@ -676,7 +683,7 @@ static void ApplyFogBlend(u8 blendCoeff, u16 blendColor) // TODO: How does this while (palOffset < palEnd) { - struct RGBColor color = *(struct RGBColor *)&gPlttBufferUnfaded[palOffset]; + struct RGBColor color = *(struct RGBColor *)&gPlttBufferFaded[palOffset]; u8 r = color.r; u8 g = color.g; u8 b = color.b; @@ -695,7 +702,7 @@ static void ApplyFogBlend(u8 blendCoeff, u16 blendColor) // TODO: How does this } else { - BlendPalette(curPalIndex * 16, 16, blendCoeff, blendColor); + BlendPalettesFine(1, gPlttBufferFaded + curPalIndex * 16, gPlttBufferFaded + curPalIndex * 16, blendCoeff, blendColor); } } } @@ -799,10 +806,10 @@ void FadeScreen(u8 mode, s8 delay) else { gWeatherPtr->fadeDestColor = fadeColor; + UpdateTimeOfDay(); if (useWeatherPal) 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], @@ -826,7 +833,7 @@ bool8 IsWeatherNotFadingIn(void) return (gWeatherPtr->palProcessingState != WEATHER_PAL_STATE_SCREEN_FADING_IN); } -void UpdateSpritePaletteWithWeather(u8 spritePaletteIndex) +void UpdateSpritePaletteWithWeather(u8 spritePaletteIndex, bool8 allowFog) { u16 paletteIndex = 16 + spritePaletteIndex; u16 i; @@ -850,17 +857,22 @@ void UpdateSpritePaletteWithWeather(u8 spritePaletteIndex) // WEATHER_PAL_STATE_CHANGING_WEATHER // WEATHER_PAL_STATE_CHANGING_IDLE default: - if (gWeatherPtr->currWeather != WEATHER_FOG_HORIZONTAL) - { - ApplyGammaShift(paletteIndex, 1, gWeatherPtr->gammaIndex); - } - else - { + if (gWeatherPtr->currWeather != WEATHER_FOG_HORIZONTAL) { + if (gWeatherPtr->gammaIndex) + ApplyGammaShift(paletteIndex, 1, gWeatherPtr->gammaIndex); + else + UpdateSpritePaletteWithTime(spritePaletteIndex); + } else { // In horizontal fog, only specific palettes should be fog-blended + if (allowFog) { paletteIndex *= 16; // First blend with time CpuFastCopy(gPlttBufferUnfaded + paletteIndex, gPlttBufferFaded + paletteIndex, 32); UpdateSpritePaletteWithTime(spritePaletteIndex); + // Then blend faded->faded BlendPalettesFine(1 << (spritePaletteIndex + 16), gPlttBufferFaded + paletteIndex, gPlttBufferFaded + paletteIndex, 12, RGB(28, 31, 28)); + } else { // Otherwise, just time-blend the palette + UpdateSpritePaletteWithTime(spritePaletteIndex); + } } break; } @@ -883,8 +895,7 @@ static bool8 IsFirstFrameOfWeatherFadeIn(void) void LoadCustomWeatherSpritePalette(const u16 *palette) { LoadPalette(palette, 0x100 + gWeatherPtr->weatherPicSpritePalIndex * 16, 32); - UpdateSpritePaletteWithTime(gWeatherPtr->weatherPicSpritePalIndex); - UpdateSpritePaletteWithWeather(gWeatherPtr->weatherPicSpritePalIndex); + UpdateSpritePaletteWithWeather(gWeatherPtr->weatherPicSpritePalIndex, TRUE); } static void LoadDroughtWeatherPalette(u8 *gammaIndexPtr, u8 *a1) diff --git a/src/field_weather_effect.c b/src/field_weather_effect.c index bda62fcf06..e22f04b741 100644 --- a/src/field_weather_effect.c +++ b/src/field_weather_effect.c @@ -1380,10 +1380,17 @@ static void DestroyFogHorizontalSprites(void); // Updates just the color of shadows to match special weather blending static u8 UpdateShadowColor(u16 color) { u8 paletteNum = IndexOfSpritePaletteTag(TAG_WEATHER_START); + u16 tempBuffer[16]; + u16 blendedColor; if (paletteNum != 0xFF) { u16 index = (paletteNum+16)*16+SHADOW_COLOR_INDEX; gPlttBufferUnfaded[index] = gPlttBufferFaded[index] = color; + // Copy to temporary buffer, blend, and keep just the shadow color index + CpuFastCopy(&gPlttBufferFaded[index-SHADOW_COLOR_INDEX], tempBuffer, 32); UpdateSpritePaletteWithTime(paletteNum); + blendedColor = gPlttBufferFaded[index]; + CpuFastCopy(tempBuffer, &gPlttBufferFaded[index-SHADOW_COLOR_INDEX], 32); + gPlttBufferFaded[index] = blendedColor; } return paletteNum; } diff --git a/src/overworld.c b/src/overworld.c index 5e4aa1a32f..a36e2ed551 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -821,7 +821,7 @@ void LoadMapFromCameraTransition(u8 mapGroup, u8 mapNum) CopySecondaryTilesetToVramUsingHeap(gMapHeader.mapLayout); LoadSecondaryTilesetPalette(gMapHeader.mapLayout); - for (paletteIndex = 6; paletteIndex < 13; paletteIndex++) + for (paletteIndex = 6; paletteIndex < 13; paletteIndex++) // TODO: Optimize gamma shifts ApplyWeatherGammaShiftToPal(paletteIndex); InitSecondaryTilesetAnimation(); @@ -829,7 +829,6 @@ void LoadMapFromCameraTransition(u8 mapGroup, u8 mapNum) RoamerMove(); DoCurrentWeather(); ResetFieldTasksArgs(); - UpdatePalettesWithTime(PALETTES_ALL); RunOnResumeMapScript(); if (gMapHeader.regionMapSectionId != MAPSEC_BATTLE_FRONTIER @@ -1512,19 +1511,6 @@ bool8 MapHasNaturalLight(u8 mapType) { // Whether a map type is naturally lit/ou || 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 - UpdateTimeOfDay(); - if (MapHasNaturalLight(gMapHeader.mapType)) { - ResetPaletteFade(); - BeginTimeOfDayPaletteFade(PALETTES_ALL, 0, 16, 0, - (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time0], - (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time1], - currentTimeBlend.weight, 0); - } -} - void UpdatePalettesWithTime(u32 palettes) { if (MapHasNaturalLight(gMapHeader.mapType)) { u16 i; @@ -1546,7 +1532,18 @@ void UpdatePalettesWithTime(u32 palettes) { } u8 UpdateSpritePaletteWithTime(u8 paletteNum) { - UpdatePalettesWithTime(1 << (paletteNum + 16)); + if (MapHasNaturalLight(gMapHeader.mapType)) { + u16 offset; + if (GetSpritePaletteTagByPaletteNum(paletteNum) >> 15) + return paletteNum; + offset = (paletteNum + 16) * 16; + TimeMixPalettes(1, + gPlttBufferUnfaded + offset, + gPlttBufferFaded + offset, + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time0], + (struct BlendSettings *)&gTimeOfDayBlend[currentTimeBlend.time1], + currentTimeBlend.weight); + } return paletteNum; } @@ -1688,7 +1685,6 @@ static void CB2_LoadMap2(void) DoMapLoadLoop(&gMain.state); SetFieldVBlankCallback(); SetMainCallback1(CB1_Overworld); - // FadePalettesWithTime(); SetMainCallback2(CB2_Overworld); } @@ -1745,7 +1741,6 @@ static void CB2_ReturnToFieldLocal(void) if (ReturnToFieldLocal(&gMain.state)) { SetFieldVBlankCallback(); - // FadePalettesWithTime(); SetMainCallback2(CB2_Overworld); } }