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 > How to control content of color palettes

#171446 - MichaelMossman - Thu Nov 19, 2009 8:14 pm

There's a lot of help about how to choose and select 16-colour palettes but I can't find any reference about how to control exactly what goes into each palette.

Are these pre-determined ? If so, how do I choose the selection I want ?

Or are they able to be defined by program ? If so, how ?

Finally, on this subject, what is the actual format of each palette (i.e How do I know which colour is in which slot ? ).

#171449 - Drovor - Thu Nov 19, 2009 9:02 pm

I think its just an area of memory that you need to initialize. This is a snippet which sets a palette that I got here

Code:

   videoSetModeSub(MODE_5_2D);
   vramSetBankH(VRAM_H_SUB_BG);
   vramSetBankI(VRAM_I_SUB_BG_0x06208000);
   int SubPtr = bgInitSub(2, BgType_Bmp8, BgSize_B8_256x256, 0, 0);
   for(int i=0; i < 256; i++)
   {
      BG_PALETTE_SUB[i] = RGB15(i & 0x1F, i & 0x1F, i & 0x1F);
   }
   u16 *SubData = bgGetGfxPtr(SubPtr);
   for(int y=0; y < 128; y++)
   {
      for(int x=0; x < 192 / 2; x++)
         SubData[x + y * 192 / 2] = ((x * 2) & 0xFF) | (((x * 2 + 1) << 8) & 0xFF00);
   }

#171450 - Dwedit - Thu Nov 19, 2009 9:06 pm

There are standard palettes, and extended palettes. I'll just be talking about standard palettes here.

There are four 256 color palettes, first one for backgrounds, then one for sprites, then another background and sprite palette for the secondary screen. Each entry in the palette is 15-bit rgb (like xBBBBBGGGGGRRRRR). The most significant bit is not used in the palette. You can also think of it as Red, Green, and Blue being in the range of 0-31, and the formula for a color is Blue*1024 + Green*32 + Red.

The standard palette for background is located in memory at address 0x05000000, and the sprite palette is 512 bytes later. Add 1024 to an address to get the palette for the secondary screen.

LibNDS defines the arrays BG_PALETTE, BG_PALETTE_SUB, SPRITE_PALETTE, and SPRITE_PALETTE_SUB for your convenience, as well as macros to calculate RGB values. So you'd just use code like BG_PALETTE[4] = RGB15(15,31,4);
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#171455 - headspin - Fri Nov 20, 2009 2:06 am

If you use grit to convert graphics you can just dmaCopy() the generated palette to VRAM.

Eg.
Code:
dmaCopy(MyImagePal, BG_PALETTE, MyImagePalLen);

_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#171459 - sverx - Fri Nov 20, 2009 10:20 am

MichaelMossman wrote:
There's a lot of help about how to choose and select 16-colour palettes but I can't find any reference about how to control exactly what goes into each palette.


If you use 16 colors tiled background and/or 16 colors sprites, then you should know that you'll be using only the first 16 colors of each palette (BG, SPRITE, SUB_BG, SUB_SPRITE) if you do not set the extended palette flag(s) when setting the video mode.

But if you want to use more palettes, you do set these flags and these arrays will be 16 palettes of 16 colors each, and you'll choose which palette to use for a sprite, for instance, with ATTR2_PALETTE(n).

I'm not sure about BGs, I think all of them will share the same 16 palettes, in contrast of the 16x256 colors palettes PER BG that you get using additional VRAM banks.

(sorry for my bad English in this post, sometimes it's really hard for me to explain properly...)

#171460 - Dwedit - Fri Nov 20, 2009 7:48 pm

Um, what? You don't need extended palettes to select which 16 color palette a BG tile or sprite uses.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#171490 - sverx - Mon Nov 23, 2009 12:43 pm

Dwedit wrote:
Um, what? You don't need extended palettes to select which 16 color palette a BG tile or sprite uses.


Oh... you mean I don't need to activate them (the flag in the video mode) or you just mean I don't need to allocate a VRAM bank?

#171493 - SteveH - Mon Nov 23, 2009 1:21 pm

sverx wrote:
Dwedit wrote:
Um, what? You don't need extended palettes to select which 16 color palette a BG tile or sprite uses.


Oh... you mean I don't need to activate them (the flag in the video mode) or you just mean I don't need to allocate a VRAM bank?


sverx if your using 16 colour spites or backgrounds then you have 16 palettes using the standard 256 palette tables.

For tiles, the top 4 bits of the u16 decides what palette to use
For sprites IIRC there's an option for the palette number (0-15).

Palette 0 is 0-15 of the palette table
Palette 1 is 16-31 of the palette table
Palette 2 is 32-48 of the palette table
...
Palette 15 is 240-255 of the palette table

So you don't need to use extended palettes (they are good for using multiple 256 colour palettes mind you) or even set them up in the BG control registers. Which also free's up those VRAM banks for other uses...

#171495 - sverx - Mon Nov 23, 2009 4:00 pm

SteveH wrote:
So you don't need to use extended palettes (they are good for using multiple 256 colour palettes mind you) or even set them up in the BG control registers. Which also free's up those VRAM banks for other uses...


Oh, I see now... I thought I had to set the corresponding bits anyway, just like when using 256 color palettes, even if I knew I wouldn't have to allocate any VRAM bank for 16 bits extended palettes.

Thanks for that clarification :)

[it'll be using those very same 16 palettes for all the 4 BGs, right?]

#171504 - SteveH - Mon Nov 23, 2009 8:45 pm

sverx wrote:
[it'll be using those very same 16 palettes for all the 4 BGs, right?]


Yes I believe that is the case