This page looks best with JavaScript enabled

GameBoy Dev06: Fadeout/Fadein effect

 ·  🎃 kr0m

It is recommended to read the previous articles to better understand the current one:


The GameBoy’s color palette is composed of 4 colors. To alter them, we only need to change the values of the BGP_REG register.

The bits of the BGP_REG register follow an order. Depending on the bits we alter, we will be changing the color that represents black/dark gray/gray/transparent:

bits 0-1: Transparente
bits 2-3: Gris
bits 4-5: Gris oscuro
bits 6-7: Negro

In each of the above slots, we must define a value according to the desired representation color. Colors are indicated by their binary values:

11: Negro
10: Gris oscuro
01: Gris
00: Transparente

The default palette is: 11 10 01 00

This indicates that to represent black, black color is used, for dark gray, dark gray, for gray, gray, and for transparent, transparent.

For example, if we wanted gray to be displayed as black, the value would be: 11 10 11 00, which is quite easy to understand.

Therefore, to achieve a fadeout effect, we will change the color representations to black, and for the fadein effect, the opposite process will be used.

vi 06.c
#include <gb/gb.h>
#include <stdio.h>
#include "CyberPunkTiles.c"
#include "CyberPunkMap.c"

UINT8 i;

void fadeout(){
    for(i=0; i<4; i++){
        switch(i){
            case 0:
                // 11 10 01 00
                BGP_REG = 0xE4;
                break;
            case 1:
                // 11 11 10 01
                BGP_REG = 0xF9;
                break;
            case 2:
                // 11 11 11 10
                BGP_REG = 0xFE;
                break;
            case 3:
                // 11 11 11 11
                BGP_REG = 0xFF;	
                break;						
        }
        delay(110);
    }
}

void fadein(){
    for(i=0; i<4; i++){
        switch(i){
            case 0:
                // 11 11 11 11
                BGP_REG = 0xFF;	
                break;
            case 1:
                // 11 11 11 10
                BGP_REG = 0xFE;
                break;
            case 2:
                // 11 11 10 01
                BGP_REG = 0xF9;
                break;
            case 3:
                // 11 10 01 00
                BGP_REG = 0xE4;
                break;					
        }
        delay(110);
    }
}

void main(){
    // grep TILESET_TILE_COUNT CyberPunkTiles.h
    set_bkg_data(0, 106, TILESET);

    // grep TILEMAP_ CyberPunkMap.h
    set_bkg_tiles(0, 0, 20, 18, TILEMAP);

    SHOW_BKG;
    DISPLAY_ON;

    while(1){
        waitpad(J_START);
        fadeout();

        waitpad(J_START);
        fadein();

        delay(100);
    }
}

We compile:

~/GBDEV/gbdk/bin/lcc 06.c -o 06.gb

We load the ROM into the emulator, and every time we press Start, we will see the effect:

If you liked the article, you can treat me to a RedBull here