gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Mode 0-2 question

#19805 - JWizard - Sun Apr 25, 2004 9:49 pm

Hey,

After playing around for a bit with modes 3-5 I decided to start using the tile modes. They seem pretty straightforward conceptually but I'm confused by what exactly the data should be.
As I understand it, the palette data contains all the colors used in the tiles as an array of 256 bytes (where each byte represents a different color, right ?).
Then, it seems to me that the tile data should be indices into the palette for each pixel and then the map data should be indices into the array of tiles.
However, after looking at some examples of C header files it seems to work differently. For instance I noticed some palettes with repeated values which according to my interpretation (where each value is one of the colors used) wouldn't make sense. Also, the tile data doesn't seem to be indices into the palette array.
If anyone could shed some light on this issue it would be greatly appreciated.

#19811 - sajiimori - Sun Apr 25, 2004 11:53 pm

Quote:

As I understand it, the palette data contains all the colors used in the tiles as an array of 256 bytes (where each byte represents a different color, right ?).

It's an array of 256 colors, but each color is 2 bytes (because GBA uses 15 bit BGR color encoding, and it rounds up to 16 bits with one wasted bit).
Quote:

Then, it seems to me that the tile data should be indices into the palette for each pixel and then the map data should be indices into the array of tiles.

Yes, but maps aren't always just arrays of tile indexes. For "text" backgrounds (i.e. not rot/scale), you can set additional bits for horizontal and vertical flipping, among other things.
Quote:

For instance I noticed some palettes with repeated values which according to my interpretation (where each value is one of the colors used) wouldn't make sense.

Palettes can have multiple copies of the same color.
Quote:

Also, the tile data doesn't seem to be indices into the palette array.

Maybe it just looks funny because of the way the code is formatted.

#19843 - Gopher - Mon Apr 26, 2004 5:26 pm

The palette is an array of 256 colors. Each color is a 16-bit value just like is used in the 16b bitmap mode. Tiles and sprites reference these colors. In 256-color modes, each pixel is represented by a one-byte index into the palette. In 16 color modes, each byte represents two 4-bit indices into one of the 16 sixteen-color palettes. (basicly the 256-color palette can be treated as an array of 16 16-color palettes instead)

There are several reasons palettes will have repeated entries; first, index 0 is always the "transparency" color in tiles and sprites, so color 0 (for 16 or 256-color data) will not be rendered at all. This color IS used, however, as the color of the "back plane", and will be seen if there is no image information to draw for a pixel. So this color will often be black, and black will be repeated somewhere else in the palette, since most tile or sprite sets will need a non-transparent black. If you are using 16 color palettes for sprite data, then viewed as a single 256 color palette there may appear to be many repeated colors, but this is because they are not being used as one palette, but as some number of smaller palettes, so the colors aren't really repeated in that sense.
In 16-color tiles, remember that 1 byte is 2 tiles. If the headers are listing in hex (as they should, imo) then for 16-color tiles each hex character will be one index; viewed as whole bytes into a 256-color palette, this probably won't make sense.
[/quote]
_________________
"Only two things are infinite: the universe, and human stupidity. The first is debatable." -Albert Einstein