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 > Extended Background Palettes

#139402 - Corsix - Wed Sep 05, 2007 9:57 pm

I am trying to move from using the default BG palette to using an extended BG palette, but I am having some difficulty.
The dualis debugging type things show that the map data contains the correct tile indexes, and that the tiles contain the correct palette indexes, and that the palette contains the correct colours, however the video output window shows pure black, suggesting that I did not link the tiles to the extended palette.

I'm using background 3, mode 0, main 2D core.

Standard palette code (which works nicely):
Code:
videoSetMode   (MODE_0_2D | DISPLAY_SPR_1D_LAYOUT | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_EXT_PALETTE);
...
/* A -> main bg */ vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
...
SetBmpPaletteTo( BG_PALETTE, pBmp);

When I change those sections to the following code, the background appears black:
Code:
videoSetMode   (MODE_0_2D | DISPLAY_SPR_1D_LAYOUT | DISPLAY_SPR_ACTIVE | DISPLAY_BG_EXT_PALETTE | DISPLAY_SPR_EXT_PALETTE);
...
/* A -> main bg */ vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
/* F -> main bg extpal */vramSetBankF(VRAM_F_BG_EXT_PALETTE);
...
#define EXT_PALETTE(BANK, NUM) ( reinterpret_cast<_ext_palette*>(BANK)[0][NUM] )
VRAM_F_CR = VRAM_ENABLE | VRAM_F_LCD;
SetBmpPaletteTo( EXT_PALETTE(VRAM_F, 0), pBmp);
VRAM_F_CR = VRAM_ENABLE | VRAM_F_BG_EXT_PALETTE;


What am I forgetting to do?

Edit: If I use BG0 instead of BG3, the extended palette code works, so I'm obviously not understanding something about BG numbers and extended palettes.

#139412 - mml - Wed Sep 05, 2007 11:33 pm

It's not that thing where the high bit of a 16 bit colour is an alpha bit, and you must set it to 1 for it to not be completely transparent, is it? I don't remember when this applies, but it's the first thing that comes to mind...

#139422 - Cydrak - Thu Sep 06, 2007 1:12 am

typedef u16 _ext_palette[16][256];

This is 16 x 256 = 4096 colors, but GBAtek says there are four of them, one for each layer. So BG3 will try to use the one at 3*8 = 24 KB, which is past the end of the bank. This is probably why you're seeing BG0 works.

My guess (I haven't tried) is it may help to map F at 16 KB. This should supply colors for BG2/3 instead of 0/1. I didn't see any constants in libnds, so you'd have to define your own:
Code:
enum {
    VRAM_MAIN_BG_EXT_PALETTE_0_1 = 4 | VRAM_OFFSET(0),  // the default
    VRAM_MAIN_BG_EXT_PALETTE_2_3 = 4 | VRAM_OFFSET(1)
};
VRAM_F_CR = VRAM_ENABLE | VRAM_F_LCD;
SetBmpPaletteTo( EXT_PALETTE(VRAM_F, 1), pBmp);
VRAM_F_CR = VRAM_ENABLE | VRAM_MAIN_BG_EXT_PALETTE_2_3;


If this is the case, there's not much use for the default--BGs 0/1 can be set to use palettes 2/3 respectively (using BG_PALETTE_SLOTn), whereas the reverse isn't true.

#139446 - Corsix - Thu Sep 06, 2007 8:00 am

I've extended the EXT_PALETTE macro to specify a slot:
Code:
#define EXT_PALETTE(BANK, SLOT, NUM) ( reinterpret_cast<_ext_palette*>(BANK)[SLOT][NUM] )


With F mapped as the main background extension palette, main BG3 uses F slot 1.
With H mapped as the sub background extension palette, sub BG3 uses H slot 3.
With I mapped as the sub sprite extension palette, sub sprites use I slot 0.

There seem to be some constants for swapping between background palette slots (or at least between 0/2 and 1/3):
Code:
#define BG_PALETTE_SLOT0 0
#define BG_PALETTE_SLOT1 0
#define BG_PALETTE_SLOT2 BIT(13)
#define BG_PALETTE_SLOT3 BIT(13)
Sprites don't seem to have any such options, so I assume they always use slot 0.