Skip to main content

How to colorize Game Boy games — Sprites

· 9 min read

Hero image

In previous entries we finally got to a point in which our custom palettes are loaded and ready to be used. Now, it's time to find out which values to change to apply them to the graphics, and more specifically in this entry, to the sprites.

Metasprite data

Each Game Boy game stores metasprite data in a different way. In my experience, the most common way to store it is in a table that follows the same sequence as the OAM. That is, sequential 4-byte blocks that define the Y-position, X-position, tile number, and attributes of each sprite respectively.

A special byte/bit is commonly used to signify the end of the metasprite data, as the game needs a way to know where it ends. In some older games, structures with no attribute byte are a bit more common, as well as fixed size metasprites. In those cases, you would probably be forced to make major changes to the data structure and/or the metasprite printing routine to have complete freedom over the metasprites.

Kirby's Dream Land follows the aforementioned OAM sequence with a small caveat. Let's take a look at Kirby's metasprite (more on how to find this later):

Y positionX positionTile IndexAttributes
F8F80200
F8001201

This corresponds to the data in our OAM viewer:

Kirby's metasprite

If you have a keen eye, you'll notice the attributes byte is being used here, even though it's supposed to be CGB only. That's why in our current build Kirby only has half of his body colored. Of course, as anticipated before, this is because this game uses bit 0 as a flag to indicate the end of the metasprite.

While this is not uncommon, it's way more frequent to have a separate byte for this purpose, meaning that usually no major data or code changes are required.

Planning our approach

In short, we just learned that in order to have attributes freedom on any sprite, we have to find a way to signify the metasprite has ended in a different way.

Three potential approaches come to mind:

  1. Use bit 4 instead of bit 0 as the end flag
  2. Use bits 3-1 instead of bits 2-0 for the palettes
  3. Add an end byte to every metasprite

Approach #3 would require too much ROM and approach #2 would make palette assignment a bit less intuitive, so I'll stick with #1.

This presents a little problem, as we have to change not only the code but the data for all metasprites. Still, it's not a big deal, we'll just have to write a small script.

Finding our data

The general process of finding routines and data has been explained in a previous entry, but as a reminder, breakpoints are set in the debugger and the code is followed to higher levels until it is understood where it came from. A look at the complete disassembly of the ROM can also be useful.

In this case, we'll start by taking a look at the DMA register, which takes us to address $C000. After setting a write breakpoint, we can find Kirby's data comes from B:$58B9.

Next, we want the metasprite pointers so we can easily move around metasprites if needed. For this, we set a read breakpoint at Kirby's metasprite data (again, at B:58B9) and continue to set execution breakpoints a bit back in the routine until we find where it starts. This should take us to $18FF, which after a quick search in our disassembly we find it's executed from a jump at $2CE0.

There, we find the pointers are stored in WRAM at $D1C0, so one more write breakpoint is needed. We finally find the data we're looking for at $15F1, with the pointers being prefixed with a byte probably related to its animation.

As an exercise, I strongly recommend you get your favorite debugger and try to follow the same path I did. It's a great way to get familiar with the process.

Editing the data

At this point, the best thing we can do (and actually what I would normally do) is to completely reverse the metasprites format, turn all pointers into labels and defining anything that's necessary to freely expand the metasprites without having to move data around. However, as we can already achieve what we want be deallocating the metasprites that need to be expanded, I'll consider that as beyond the scope of this tutorial.

Instead, we will store the metasprite data in a format that allows us to easily modify it. For this, we'll have a separate asm file with the following format:

./sprites.asm
.BANK $0B SLOT 1
.ORGA $58B9
.SECTION "Kirby metasprite data" OVERWRITE
Kirby:
@000:
.db $F8,$F8,$02,$00
.db $F8,$00,$12,$10

@001:
.db $F8,$F8,$04,$00
.db $F8,$00,$14,$10

@002:
.db $F8,$F8,$06,$00
.db $F8,$00,$16,$10

; ...
.ENDS

You might have noticed that the end flag is now in bit 4 of the attributes byte, which needs to be changed for every single metasprite. Assuming we have the relevant data in a binary file, we can use a script like the following to extract it in the format we need:

./tools/extract-metasprite-data.js
const fs = require('fs');

function extractData(inputFilename, outputFilename) {
const inputFile = fs.openSync(inputFilename, 'r');
const outputFile = fs.openSync(outputFilename, 'w');

let byteCount = 0;
let iterationCount = 0;

fs.writeSync(outputFile, `@${iterationCount.toString().padStart(3, '0')}:\n.db `);
iterationCount += 1;

const buffer = Buffer.alloc(1);

while (true) {
const bytesRead = fs.readSync(inputFile, buffer, 0, 1);
if (bytesRead === 0) {
break;
}

byteCount += 1;
const hexValue = buffer[0].toString(16).toUpperCase().padStart(2, '0');

let modifiedValue;
if (byteCount % 4 === 0 && (parseInt(hexValue, 16) & 0x01)) { // Checks bit 0
modifiedValue = (parseInt(hexValue, 16) & 0xFE | 0x10).toString(16).toUpperCase().padStart(2, '0'); // Sets bit 4 and unset bit 0
} else {
modifiedValue = hexValue;
}

fs.writeSync(outputFile, `$${modifiedValue}`);

if (modifiedValue !== hexValue) {
fs.writeSync(outputFile, `\n\n@${iterationCount.toString().padStart(3, '0')}:`);
iterationCount += 1;
}

if (byteCount % 4 === 0) {
fs.writeSync(outputFile, '\n.db ');
} else {
fs.writeSync(outputFile, ',');
}
}

fs.closeSync(inputFile);
fs.closeSync(outputFile);
}

const inputFilename = 'data.bin';
const outputFilename = 'data.txt';

extractData(inputFilename, outputFilename);

Of course, we also need to update our code so the end flag is checked correctly. If we set a read breakpoint at the attribute byte, we can very easily find the code that checks for the end flag:

./references/kirby.asm @ $1929
; ...
inc hl
inc e
bit 0, a ; $192B: Checks if the metasprite has ended
jr z,@Loop
ld a,e
; ...

Not much to say here, all we need to do is to change it for bit 4, a:

.BANK 0 SLOT 0
.ORG $192B
.SECTION "Change Metasprite Flag" OVERWRITE
bit 4,a
.ENDS

We can now compile our ROM and see the results:

Game running after the sprite changes

While Kirby's sprites are now correctly colored, we can observe that the enemies and other metasprites are not displayed correctly. This is because they are not using the same metasprite data, so we'll have to find it and apply the same principles.

I'll leave that as an exercise for you, but if you need a hint, you can find all you need in the linked code down below.

And that's it! Believe it or not, we already have everything we need to color all the sprites in the game — it's just a matter of having the appropriate palette set loaded at the right time and assigning the correct palette to each sprite. While we could have done this dynamically by loading palettes as enemies spawn, for most games this is an unnecessary overkill.

After asigning a couple of palettes, our current build should look like this:

Game running after the sprite changes

In following posts we will be dealing with the backgrounds so that we can start to have a more complete final look.

Check out the code so far in GitHub