Skip to content

Instantly share code, notes, and snippets.

@willbritton
Last active April 8, 2023 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbritton/6f2218094561e758bc4a5870bd1661d5 to your computer and use it in GitHub Desktop.
Save willbritton/6f2218094561e758bc4a5870bd1661d5 to your computer and use it in GitHub Desktop.
#define COLOR_SUBTRACT(c,r) (((((r)&0x3)>=((c)&0x3))?0:(((c)&0x3)-((r)&0x3)))|((((r)&0xC)>=((c)&0xC))?0:(((c)&0xC)-((r)&0xC)))|((((r)&0x30)>=((c)&0x30))?0:(((c)&0x30)-((r)&0x30))))
void _SMS_loadBGPaletteafterColorSubtraction (const void *palette, const unsigned char subtraction_color) {
unsigned char i;
SMS_setNextBGColoratIndex(0);
// precalculate the first four colors so that we tie up the CPU long enough to miss the bottom border
unsigned char new_colors[4];
for (i=0;i<4;i++)
new_colors[i] = COLOR_SUBTRACT(((unsigned char *)(palette))[i],subtraction_color);
for (i=0;i<4;i++)
SMS_setColor(new_colors[i]);
for (i=4;i<16;i++)
SMS_setColor(COLOR_SUBTRACT(((unsigned char *)(palette))[i],subtraction_color));
}
void _SMS_loadSpritePaletteafterColorSubtraction (const void *palette, const unsigned char subtraction_color) {
unsigned char i;
SMS_setNextSpriteColoratIndex(0);
// precalculate the first four colors so that we tie up the CPU long enough to miss the bottom border
unsigned char new_colors[4];
for (i=0;i<4;i++)
new_colors[i] = COLOR_SUBTRACT(((unsigned char *)(palette))[i],subtraction_color);
for (i=0;i<4;i++)
SMS_setColor(new_colors[i]);
for (i=4;i<16;i++)
SMS_setColor(COLOR_SUBTRACT(((unsigned char *)(palette))[i],subtraction_color));
}
void fade_in(const void *bg_palette, const void *sprite_palette, unsigned int sprite_palette_bank)
{
SMS_displayOff();
for (int i = sizeof(color_reduction) - 1; i >= 0; i--)
{
SMS_waitForVBlank();
if (bg_palette != NULL)
_SMS_loadBGPaletteafterColorSubtraction(bg_palette, color_reduction[i]);
SMS_waitForVBlank();
if (sprite_palette != NULL)
{
SMS_saveROMBank();
SMS_mapROMBank(sprite_palette_bank);
_SMS_loadSpritePaletteafterColorSubtraction(sprite_palette, color_reduction[i]);
SMS_restoreROMBank();
}
if (i == sizeof(color_reduction) - 1)
SMS_displayOn();
}
}
void fade_out(const void *bg_palette, const void *sprite_palette, unsigned int sprite_palette_bank)
{
for (int i = 0; i < sizeof(color_reduction); i++)
{
SMS_waitForVBlank();
if (bg_palette != NULL)
_SMS_loadBGPaletteafterColorSubtraction(bg_palette, color_reduction[i]);
SMS_waitForVBlank();
if (sprite_palette != NULL)
{
SMS_saveROMBank();
SMS_mapROMBank(sprite_palette_bank);
_SMS_loadSpritePaletteafterColorSubtraction(sprite_palette, color_reduction[i]);
SMS_restoreROMBank();
}
}
SMS_displayOff();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment