Skip to content

Instantly share code, notes, and snippets.

@bashaus
Created June 13, 2015 13:33
Show Gist options
  • Save bashaus/8ea7b760b54ad19590b4 to your computer and use it in GitHub Desktop.
Save bashaus/8ea7b760b54ad19590b4 to your computer and use it in GitHub Desktop.
Game Boy Color fade script
#include <gb/gb.h>
#include "fade.h"
UWORD FadePalette[32] = {
RGB(30,0,0), RGB(0,30,0), RGB(0,0,30), RGB(0,0,0)
};
void Fade(UWORD *org_p, FadeDirection direction, FadeColor color, UWORD speed)
{
UBYTE i,x;
UWORD bit_mask;
bit_mask = (direction == FadeDirectionFrom)
? 0x0421 /* 0000010000100001 */
: 0x4210; /* 0100001000010000 */
for (i = 0; i < 5; i++) { /* 5 bits per color */
for (x = 0; x < 32; x++) {
if (direction == FadeDirectionFrom && color == FadeColorBlack) {
/* fade from black */
FadePalette[x] = org_p[x] & bit_mask;
} else if (direction == FadeDirectionFrom && color == FadeColorWhite) {
/* fade from white */
FadePalette[x] = org_p[x] | (0x7FFF & ~bit_mask);
} else if (direction == FadeDirectionTo && color == FadeColorBlack) {
/* fade to black */
FadePalette[x] = org_p[x] & ~bit_mask;
} else if (direction == FadeDirectionTo && color == FadeColorWhite) {
/* fade to white */
FadePalette[x] = org_p[x] | bit_mask;
}
}
bit_mask |= (direction == FadeDirectionFrom)
? (bit_mask << 1)
: (bit_mask >> 1);
set_bkg_palette(0,8, &FadePalette[0]);
delay(speed);
}
}
#ifndef _GBX_FADE_H
#define _GBX_FADE_H
typedef enum {
FadeDirectionFrom,
FadeDirectionTo
} FadeDirection;
typedef enum {
FadeColorBlack,
FadeColorWhite
} FadeColor;
void Fade(UWORD *org_p, FadeDirection direction, FadeColor color, UWORD speed);
#endif
@bashaus
Copy link
Author

bashaus commented Jun 13, 2015

Example usage:

#include <gb/gb.h>
#include "fade.h"

void main() {
    // Define your target palette
    const UWORD TargetPalette[] = {
        RGB(30, 30, 30), RGB(20, 20, 20), RGB(10, 10, 10), RGB(0 , 0 , 0 ),
        RGB(0 , 0 , 0 ), RGB(10, 10, 10), RGB(20, 20, 20), RGB(30, 30, 30),
    };

    // Fade from Black to Palette
    Fade(TargetPalette, FadeDirectionFrom, FadeColorBlack, 300);

    // Fade from White to Palette
    Fade(TargetPalette, FadeDirectionFrom, FadeColorWhite, 300);

    // Fade from Palette to White
    Fade(TargetPalette, FadeDirectionTo, FadeColorWhite, 300);

    // Fade from Palette to Black
    Fade(TargetPalette, FadeDirectionTo, FadeColorBlack, 300);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment