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.

DS development > DS tile background layer and alpha / transparency

#50045 - DShappy - Thu Aug 04, 2005 7:53 pm

Hey guys, here is my problem...

I'm creating a font system for use on the DS's BG0 using tiles. Everything is up and running smoothly except for transparency / alpha. I've read that index 0 in the palette is the "clear color" / "key color" along with others saying you have to set the 16th pad bit of the 16bit color value to on (1). I've tried bothways (and may others) but can't seems to get it working :(

Here is my code:

///////////////////////////////////////////////////////////////
// Graphics Init
///////////////////////////////////////////////////////////////

const int char_base = 0;
const int screen_base = 20;

DISPLAY_CR = MODE_0_2D | DISPLAY_BG0_ACTIVE;
SUB_DISPLAY_CR = MODE_0_2D | DISPLAY_BG0_ACTIVE;

SUB_BG0_CR = BG_256_COLOR | BG_TILE_BASE(char_base) | BG_MAP_BASE(screen_base);

BG0_CR = BG_256_COLOR | BG_TILE_BASE(char_base) | BG_MAP_BASE(screen_base) ;

vramSetBankA(VRAM_A_MAIN_BG);
vramSetBankC(VRAM_C_SUB_BG);

///////////////////////////////////////////////////////////////
// Tile/Palette Init
///////////////////////////////////////////////////////////////

u16* sub_tile = (u16*)CHAR_BASE_BLOCK_SUB(char_base);
u16* sub_map = (u16*)SCREEN_BASE_BLOCK_SUB(screen_base);
u16* tile = (u16*)CHAR_BASE_BLOCK(char_base);
u16* map = (u16*)SCREEN_BASE_BLOCK(screen_base);

cNDSFile tmp;
files.LoadFile( "font1b8x8.SPI", &tmp );

for(i = 0,j=0; i < tmp.length; i+=2,j++)
{
sub_tile[j] = (tmp.data[i + 1] << 8) | tmp.data[i];
}

for(i = (tmp.length / 2); i < ((tmp.length / 2) + 32); ++i)
{
sub_tile[i] = 0x0202;
}

BG_PALETTE_SUB[0] = RGB15(31, 0, 31) | (1 << 15); // Key color ???
BG_PALETTE_SUB[1] = RGB15(0, 0, 0);
BG_PALETTE_SUB[2] = RGB15(0, 0, 31);

for(j = 0; j < (tmp.length / 64); ++j)
{
sub_map[j] = j;
}

for(j = (tmp.length / 64); j < 768; ++j)
{
sub_map[j] = (tmp.length / 64);
}
///////////////////////////////////////////////////////////////

Any help would be way cool! :)

#50049 - dovoto - Thu Aug 04, 2005 8:42 pm

For 256 color backgrounds(sprites) color 0 is not rendered except as a backdrop. What this means is that if you want part a tile to be tranperent then you must draw the transperent using the first palette color in your palette.

It does not work as a color key.

It is similar for 16 color modes with the exception that the first index of each palette is not rendered.

For 16 bit color modes (other than framebuffer mode) the high bit is the alpha (transperency) bit.

I am not certain if this applies to 256 color textures.
_________________
www.drunkencoders.com

#50051 - DShappy - Thu Aug 04, 2005 8:54 pm

Thanks for the reply but unfortunately that's not working either. :( My file which is the array of indices into the palette make reference to the key color (aka index 0) in the palette. Basicly the actual font references index 1 (RGB(0,0,0) for a black font) and the rest of the image (the back drop) is magenta (RGB(31,0,31)) which is placed in palette index 0. So if index 0 is skipped (not drawn) then I should never see the color magent since that color is only in palette position 0. Unfortunately I do see magenta :(

#50057 - dovoto - Thu Aug 04, 2005 9:56 pm

The color stored in palette index 0 will be used to render the backdrop (what is seen if there is nothing rendered to that particular location). Because of this, if you only have one layer it might appear as if it transperent pixels were being drawn.

Just in case there is a confusion in terminology:

The color in palette index 0 is not used as a color key/mask of any sort.

say you had the bitmap for the character 'A' as such:

char a[64] ={
0,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,0,1,0,0,1,0,0,
0,1,1,1,1,1,1,0,
0,1,0,0,0,0,1,0,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
};

When the DS renders this tile to the screen it will not render the 0s regardless of the color of palette index 0 (neither the palette you have used to draw your character nor the one you stuck into the DS can cause 0 to be drawn). The 1's will be rendered in what ever color you have stuck at palette location 1 in the DS.

This is how the DS works so either you are seeing the backdrop (which seems unlikely) or you dont actualy have 0s in for the pixels you wish to be transperent.



Hope this was a bit clearer answer.
_________________
www.drunkencoders.com

#50060 - DShappy - Thu Aug 04, 2005 10:43 pm

Yeah, That's how the font image is stored (palette index 1 for the pixels making up a letter and palette index 0 for the pixels that lay in between the letters).

It is interesting that you said "...if you only have one layer it might appear as if it transperent pixels were being drawn."

I do only have one layer being drawn to, BG0. Seems like this is the issue since my font image is only made up of 1's & 0's for indices and the entire image is being drawn.

I'll give it a try thanks for the tip. ;)