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 palettes for sprites

#167535 - SturmxHawke - Tue Mar 17, 2009 5:30 pm

Hello, I've been looking around the interwebs for a while, have found some tutorials, but I'm not sure I've found something that works yet. I'm trying to use extended palettes on sprites, but so far I've only seen it used on BackGrounds, is it even possible to use on sprites and if it is, then can someone either point me in the right direction or explain to me how it's done?

So far I have this:

Code:
void X_copyExtPal(const unsigned short * pal, int palnr, int size, int screen)
{
   if (screen == 1)
   {
      vramSetBankG(VRAM_G_LCD);
      memcpy(VRAM_G, pal, size);
      vramSetBankG(VRAM_G_SPRITE_EXT_PALETTE);
   }

   if(screen == 0)
   {
      vramSetBankI(VRAM_I_LCD);
      memcpy(VRAM_I, pal, size);
      vramSetBankI(VRAM_I_SUB_SPRITE_EXT_PALETTE);
   }
}

_________________
Who's that?

It's superdude!

#167537 - Miked0801 - Tue Mar 17, 2009 5:41 pm

Yes it's possible as we do it all the time. I don't have any source to give you, but can tell you the process is nearly identical to that of BG land and will let someone else give you an example.

#167540 - Tommmie - Tue Mar 17, 2009 7:19 pm

i will send it to you(yes i've been a long time off line)

#167550 - Emmanuel - Wed Mar 18, 2009 3:00 am

Code:
void loadSpritePal(bool main, const char* path, u8 palette, SpriteColorFormat color){
        FILE* gfx;
   int i, size;
   u8* buffer;
   gfx = fopen(path, "rb");
   if(gfx != NULL) {
      fseek(gfx, 0, SEEK_END);
      size = ftell(gfx);
      fseek(gfx, 0, SEEK_SET);
      
      buffer = new u8[size];
      
      i = 0;
      while(i < size) {
         fread(&buffer[i], 1, 1, gfx);
         i++;
      }
      if(main){
         if(color != SpriteColorFormat_256Color){
            memcpy(SPRITE_PALETTE+palette*16, /* screen palette*/
                     buffer, size);
         }else{
            vramSetBankF(VRAM_F_LCD);
            memcpy(VRAM_F_EXT_PALETTE[palette], /* screen palette*/
                     buffer, size);
            vramSetBankF(VRAM_F_SPRITE_EXT_PALETTE);
         }
      }else{
         if(color != SpriteColorFormat_256Color){
            memcpy(SPRITE_PALETTE_SUB+palette*16, /* screen palette*/
                        buffer, size);
         }else{
            vramSetBankI(VRAM_I_LCD);
            memcpy(VRAM_I+palette*16, /* screen palette*/
                     buffer, size);
            vramSetBankI(VRAM_I_SUB_SPRITE_EXT_PALETTE);
         }
      }
      delete[] buffer;
      buffer = NULL;
      fclose(gfx);
   }else{
      iprintf("File NULL");
   }
}


That's my code for loading the palette from FAT... however, the I bank is not working... even though DSemuMe shows the palette loads correctly, the sprites are not able to make use of it...

Also:
Code:
//Ext palette sprite
   vramSetBankF(VRAM_F_SPRITE_EXT_PALETTE);
   vramSetBankI(VRAM_I_SUB_SPRITE_EXT_PALETTE);

This one maps the vrams accordingly
And
Code:
REG_DISPCNT |= DISPLAY_SPR_EXT_PALETTE; /* Tell the main screen to use
                     extended palettes for sprites*/
   REG_DISPCNT_SUB |= DISPLAY_SPR_EXT_PALETTE; /* Tell the sub screen to use
                     extended palettes for sprites*/
   oamInit(&oamMain, SpriteMapping_1D_256, true);
   oamInit(&oamSub, SpriteMapping_1D_256, true);

and the oamInit's true is for enabling extended pallete...

NOTE: Change the news and deletes with mallocs and frees accordingly if you use plain C
BTW, I'm thinking on releasing a small lib (bad news C++ only)... everyone always ask about extended palettes *and I still have my sub ext palette not working for sprites*
It would combine Bgs, Sprites and Collision Maps WITH FAT/EFS loading WITH Extended Palette

It would at least serve as a nice example... even though I think libnds is good enough... but, I remember when I started *which, wasn't too long ago* that was the stuff I took longer to get used to...

The code works... for me at least... I might have skipped some part... it uses the latest libnds...