Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made BuildColorMaps redundant by using static tables #5289

Merged
merged 14 commits into from
Sep 10, 2024
Merged
2 changes: 0 additions & 2 deletions include/field_weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ struct Weather
struct Sprite *sandstormSprites2[NUM_SWIRL_SANDSTORM_SPRITES];
} s2;
} sprites;
u8 darkenedContrastColorMaps[NUM_WEATHER_COLOR_MAPS][32];
u8 contrastColorMaps[NUM_WEATHER_COLOR_MAPS][32];
s8 colorMapIndex;
s8 targetColorMapIndex;
u8 colorMapStepDelay;
Expand Down
145 changes: 58 additions & 87 deletions src/field_weather.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "util.h"
#include "event_object_movement.h"
#include "field_weather.h"
#include "fieldmap.h"
#include "main.h"
#include "menu.h"
#include "palette.h"
Expand Down Expand Up @@ -44,7 +45,6 @@ struct WeatherCallbacks

// This file's functions.
static bool8 LightenSpritePaletteInFog(u8);
static void BuildColorMaps(void);
static void UpdateWeatherColorMap(void);
static void ApplyColorMap(u8 startPalIndex, u8 numPalettes, s8 colorMapIndex);
static void ApplyColorMapWithBlend(u8 startPalIndex, u8 numPalettes, s8 colorMapIndex, u8 blendCoeff, u32 blendColor);
Expand All @@ -66,6 +66,52 @@ EWRAM_DATA static u8 ALIGNED(2) sFieldEffectPaletteColorMapTypes[32] = {0};

static const u8 *sPaletteColorMapTypes;

static const u8 sDarkenedContrastColorMaps[NUM_WEATHER_COLOR_MAPS][32] =
{
{0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29},
{0, 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25, 26, 27},
{0, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 13, 14, 15, 16, 17, 17, 18, 19, 20, 21, 21, 22, 23, 24, 25},
{0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 11, 11, 12, 13, 14, 14, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 23, 24, 24, 25},
{1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 12, 13, 13, 14, 15, 15, 16, 17, 18, 18, 19, 20, 20, 21, 22, 23, 23, 24, 25, 25},
{1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 13, 14, 15, 15, 16, 17, 17, 18, 19, 19, 20, 20, 21, 22, 22, 23, 24, 24, 25, 26},
{1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 15, 15, 16, 16, 17, 18, 18, 19, 19, 20, 21, 21, 22, 22, 23, 24, 24, 25, 26, 26},
{1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 16, 16, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 23, 23, 24, 24, 25, 25, 26, 27},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 19, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 27, 27},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 20, 20, 21, 21, 22, 22, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 27, 27, 27, 28, 28},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 23, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 28, 28, 28},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 29, 29, 29},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30},
{1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9, 10, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31}
};

static const u8 sContrastColorMaps[NUM_WEATHER_COLOR_MAPS][32] =
{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 14, 15, 16, 17, 17, 18, 19, 20, 21, 22, 23, 24, 24, 25, 26, 27, 28, 29, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 18, 19, 20, 21, 22, 22, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 19, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 28, 28, 29, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 24, 25, 26, 26, 27, 28, 28, 29, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 19, 19, 20, 21, 21, 22, 22, 23, 24, 24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 20, 20, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 27, 27, 28, 28, 29, 29, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 22, 23, 23, 24, 24, 24, 25, 25, 26, 26, 27, 27, 27, 28, 28, 29, 29, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 23, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 26, 26, 26, 27, 27, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 27, 27, 27, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 29, 29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31}
};

// The drought weather effect uses a precalculated color lookup table. Presumably this
// is because the underlying color shift calculation is slow.
static const u16 sDroughtWeatherColors[][0x1000] = {
Expand Down Expand Up @@ -158,7 +204,9 @@ void StartWeather(void)
{
u8 index = AllocSpritePalette(PALTAG_WEATHER);
CpuCopy32(gFogPalette, &gPlttBufferUnfaded[OBJ_PLTT_ID(index)], PLTT_SIZE_4BPP);
BuildColorMaps();

sPaletteColorMapTypes = sBasePaletteColorMapTypes;

gWeatherPtr->contrastColorMapSpritePalIndex = index;
gWeatherPtr->weatherPicSpritePalIndex = AllocSpritePalette(PALTAG_WEATHER_2);
gWeatherPtr->rainSpriteCount = 0;
Expand Down Expand Up @@ -265,83 +313,6 @@ static u8 None_Finish(void)
return 0;
}

// Builds two tables that contain color maps, used for directly transforming
// palette colors in weather effects. The colors maps are a spectrum of
// brightness + contrast mappings. By transitioning between the maps, weather
// effects like lightning are created.
// It's unclear why the two tables aren't declared as const arrays, since
// this function always builds the same two tables.
static void BuildColorMaps(void)
{
u16 i;
u8 (*colorMaps)[32];
u16 colorVal;
u16 curBrightness;
u16 brightnessDelta;
u16 colorMapIndex;
u16 baseBrightness;
s16 diff;

sPaletteColorMapTypes = sBasePaletteColorMapTypes;
for (i = 0; i < 2; i++)
{
if (i == 0)
colorMaps = gWeatherPtr->darkenedContrastColorMaps;
else
colorMaps = gWeatherPtr->contrastColorMaps;

for (colorVal = 0; colorVal < 32; colorVal++)
{
curBrightness = colorVal << 8;
if (i == 0)
brightnessDelta = (colorVal << 8) / 16;
else
brightnessDelta = 0;

// First three color mappings are simple brightness modifiers which are
// progressively darker, according to brightnessDelta.
for (colorMapIndex = 0; colorMapIndex < 3; colorMapIndex++)
{
curBrightness -= brightnessDelta;
colorMaps[colorMapIndex][colorVal] = curBrightness >> 8;
}

baseBrightness = curBrightness;
brightnessDelta = (0x1f00 - curBrightness) / (NUM_WEATHER_COLOR_MAPS - 3);
if (colorVal < 12)
{
// For shadows (color values < 12), the remaining color mappings are
// brightness modifiers, which are increased at a significantly lower rate
// than the midtones and highlights (color values >= 12). This creates a
// high contrast effect, used in the thunderstorm weather.
for (; colorMapIndex < NUM_WEATHER_COLOR_MAPS; colorMapIndex++)
{
curBrightness += brightnessDelta;
diff = curBrightness - baseBrightness;
if (diff > 0)
curBrightness -= diff / 2;
colorMaps[colorMapIndex][colorVal] = curBrightness >> 8;
if (colorMaps[colorMapIndex][colorVal] > 31)
colorMaps[colorMapIndex][colorVal] = 31;
}
}
else
{
// For midtones and highlights (color values >= 12), the remaining
// color mappings are simple brightness modifiers which are
// progressively brighter, hitting exactly 31 at the last mapping.
for (; colorMapIndex < NUM_WEATHER_COLOR_MAPS; colorMapIndex++)
{
curBrightness += brightnessDelta;
colorMaps[colorMapIndex][colorVal] = curBrightness >> 8;
if (colorMaps[colorMapIndex][colorVal] > 31)
colorMaps[colorMapIndex][colorVal] = 31;
}
}
}
}
}

// When the weather is changing, it gradually updates the palettes
// towards the desired color map.
static void UpdateWeatherColorMap(void)
Expand Down Expand Up @@ -463,8 +434,8 @@ static void ApplyColorMap(u8 startPalIndex, u8 numPalettes, s8 colorMapIndex)
{
u16 curPalIndex;
u16 palOffset;
u8 *colorMap;
u16 i;
const u8 *colorMap;
u32 i;

if (colorMapIndex > 0)
{
Expand All @@ -487,9 +458,9 @@ static void ApplyColorMap(u8 startPalIndex, u8 numPalettes, s8 colorMapIndex)
u8 r, g, b;

if (sPaletteColorMapTypes[curPalIndex] == COLOR_MAP_CONTRAST || curPalIndex - 16 == gWeatherPtr->contrastColorMapSpritePalIndex)
colorMap = gWeatherPtr->contrastColorMaps[colorMapIndex];
colorMap = sContrastColorMaps[colorMapIndex];
else
colorMap = gWeatherPtr->darkenedContrastColorMaps[colorMapIndex];
colorMap = sDarkenedContrastColorMaps[colorMapIndex];

for (i = 0; i < 16; i++)
{
Expand Down Expand Up @@ -544,7 +515,7 @@ static void ApplyColorMapWithBlend(u8 startPalIndex, u8 numPalettes, s8 colorMap
{
u16 palOffset;
u16 curPalIndex;
u16 i;
u32 i;
struct RGBColor color = *(struct RGBColor *)&blendColor;
u8 rBlend = color.r;
u8 gBlend = color.g;
Expand All @@ -565,12 +536,12 @@ static void ApplyColorMapWithBlend(u8 startPalIndex, u8 numPalettes, s8 colorMap
}
else
{
u8 *colorMap;
const u8 *colorMap;

if (sPaletteColorMapTypes[curPalIndex] == COLOR_MAP_DARK_CONTRAST)
colorMap = gWeatherPtr->darkenedContrastColorMaps[colorMapIndex];
colorMap = sDarkenedContrastColorMaps[colorMapIndex];
else
colorMap = gWeatherPtr->contrastColorMaps[colorMapIndex];
colorMap = sContrastColorMaps[colorMapIndex];

for (i = 0; i < 16; i++)
{
Expand Down
Loading