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 > Background and Sprite Loading

#99486 - dannyboy - Tue Aug 22, 2006 1:06 am

Hi all, I started messing around with 2D stuff on the DS and a couple of issues have come up.

1) I had some code to load a 2D background working nicely as so:

Code:
for(int i = 0; i < 256*192; i++) {
        BG_GFX_SUB[i] = ((u16*)ColourField_bin)[i] | BIT(15);
}


However I decided to start using a filesystem, namely gbfs. I changed the code to the following:

Code:
for(int i = 0; i < 256*192; i++) {
        BG_GFX_SUB[i] = ((u16*)gbfs_get_obj(&data_gbfs, "ColourField.bin", NULL)) [i] | BIT(15);
}


The problem is, it works, but the new code is a lot slower. Where the first code seemed instantaneous the new code takes a short while, even on hardware. Is there a way to speed it up? Oh, and what about 8 bit backgrounds. What's the best way to load those?

2) I am having problems with the colours for my sprites. In this example, I load 3 sprites. I use the palette of the second sprite which shows perfectly. However the other 2 show with distorted colouring. Is there a way to synchronise the palettes?

Code:
sImage Image;

void CSprite::LoadPalette()
{
    for(int i = 0; i < 256; i++) { SPRITE_PALETTE_SUB[i] = Image.palette[i]; }
}

void CSprite::LoadImage(unsigned char* pcxFile, int i) // Load simage into the given memory location
{
    int offset = i*32*16;           // offset in memory for multiple sprites

    loadPCX(pcxFile, &Image);           // load our pcx file into an image
    imageTileData(&Image);              // tile it so it is useful as sprite data
    for(int i = 0; i < 32*16; i++) { SPRITE_GFX_SUB[offset + i] = Image.data16[i]; }
}

    LoadImage((u8*)gbfs_get_obj(&data_gbfs, "one.pcx", NULL), 1);
    LoadImage((u8*)gbfs_get_obj(&data_gbfs, "two.pcx", NULL), 2);
    for(int i = 0; i < 256; i++) { SPRITE_PALETTE_SUB[i] = Image.palette[i];
    LoadImage((u8*)gbfs_get_obj(&data_gbfs, "three.pcx", NULL), 3);
}

#99494 - tepples - Tue Aug 22, 2006 1:50 am

dannyboy wrote:
However I decided to start using a filesystem, namely gbfs. I changed the code to the following:

Code:
for(int i = 0; i < 256*192; i++) {
        BG_GFX_SUB[i] = ((u16*)gbfs_get_obj(&data_gbfs, "ColourField.bin", NULL)) [i] | BIT(15);
}

Oh my gosh. You're doing a file system lookup for every single pixel! Try not calling gbfs_get_obj() in an inner loop. Rather, call it once before the loop and put the result in a variable:
Code:
u16 *bg = (u16*)gbfs_get_obj(&data_gbfs, "ColourField.bin", NULL);
for(int i = 0; i < 256*192; i++) {
  BG_GFX_SUB[i] = bg[i] | BIT(15);
}


Quote:
Oh, and what about 8 bit backgrounds. What's the best way to load those?

Same way, except you're just copying 2 pixels at a time instead of 4.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#99570 - silent_code - Tue Aug 22, 2006 1:50 pm

draw the sprites using the same palette... if you're using 4-bit sprites, you also need to assign one sub palette out of the 16 available to each sprite.

that's about everything i can think of.