Page 1 of 1

Load BMP Sprites

Posted: Sun May 03, 2009 9:17 pm
by ex-peluo
Hi. I'm trying to make a BMP sprite loader class. I've made a class to manage sprites and it works fine with sprites i process with grit. Now I want to load BMP (to keep im memory only data i need in each moment and no keep there all the images of a game). My class loads the bmp header, bmp info, palette and tiles well (i have to transform the pixel information in tiles). But i have some problems. First, to save palette information i use an u16 pointer:

Code: Select all

     u16 *palette;
     palette = new u16[256];
I have in my loader class a function called load_palette(u16 *ppalette). This function receives the pointer to the first element and load in the palette all the bmp palette using this:

Code: Select all

  for (i=0;i<256;i++){
    red = rgb[i].red >> 3;
    green = rgb[i].green >> 3;
    blue = rgb[i].blue >> 3;
    ppalette[i] = RGB15(red, green, blue);
  }
rgb is a array of this structure:

Code: Select all

typedef struct
{
	unsigned char blue;
	unsigned char green;
	unsigned char red;
	unsigned char reserved;
}__attribute__ ((packed)) RGB_type;
and i load in it the bmp palette. This works fine, but if i define my palette using u16 palette[256]; it doesn't work :shock: . I think it could be because memory is not aligned. But now my second problem. I decide to use an u16* and use new to reserve memory, but when i make a second u16 pointer to load another file, if I reserve its memory using new, the first palette don't take the first 8 colors. I think all this problems are caused by the memory alignment. Any solution??. Thanks

Re: Load BMP Sprites

Posted: Sun May 03, 2009 10:23 pm
by WinterMute
Convert your sprite graphics at compile time, not runtime - this is one of the things that grit does well.

Re: Load BMP Sprites

Posted: Sun May 03, 2009 10:58 pm
by ex-peluo
Yes, it does, but i'm trying to make a game in which playes can insert its owns bmp without compile anything. I'am making a lot of tests with this.

Re: Load BMP Sprites

Posted: Mon May 04, 2009 1:22 pm
by Al_Bro
When you declared the palette as u16 palette[256]; you've allocated it on the stack, when you used "new" you've allocating it on the heap. Both approaches should work - but if you're trying to load the palette into palette memory with dmaCopy then it won't work from the stack. It appears that stack allocation occurs at very high addresses which are out of the range of the DMA registers. Just use swiCopy and either will work.

As far as the colours not taking - are you sure you have set up your palette memory correctly?

Re: Load BMP Sprites

Posted: Mon May 04, 2009 4:19 pm
by ex-peluo
Al_Bro wrote:As far as the colours not taking - are you sure you have set up your palette memory correctly?
I think all is correct. If I only declare an array for the sprite and another one for pallette all works. But if i declare another array, the sprite is showed bad. Some colors disapears. And I only do this:

Code: Select all

u16 palette2;
palette2 = new u16[256];
Then, the first sprite doesn't show all colors. But more strange is that if i restart DS, sometimes it works. It happens randomly. After that i decided to save in a txt file the information of palette and tiles. To do that i have to cross the two arrays, and i discover it works well when i do that. I am desperated. For more information, I load the sprite with an u8 array and this algorithm:

Code: Select all

for (i=0;i<(image.width*image.height);i++){
   index = take_tile_position(i);
   fread(&tile[index],sizeof(u8),1, file);
}
Image is stored linearly, and tile is stored in blocks 8x8 pixels, so take_tile_position returns the index position of i in the tile. I could read 8 bytes instead 1 and increment i by 8, but it works worst. tile is an array created using operator new in other function and passed as u8*.

Re: Load BMP Sprites

Posted: Mon May 04, 2009 7:22 pm
by Al_Bro
ex-peluo wrote:I think all is correct. If I only declare an array for the sprite and another one for pallette all works. But if i declare another array, the sprite is showed bad. Some colors disapears.
Yes - but simply loading the palette information into an array isn't enough - you must be using some method to transfer the palette information from your array to palette memory.

From what you've posted I don't think your issue is wth bitmap or palette loading but more to do with VRAM and SRAM management. If you can put together a proof of concept program that doesn't work then that may be easier to comment on rather than isolated code snippets.

Re: Load BMP Sprites

Posted: Mon May 04, 2009 7:54 pm
by ex-peluo
You're right, but i have my code in class and headers and i'll have to put here all may proyect. It will be very difficult to examine it. I'm copying palette to Vram using:

Code: Select all

dmaCopy(palette, SPRITE_PALETTE_SUB, 512);
(sprite in the sub screen and 256*2bytes = 512)
I'll try to copy/paste code in a cpp file and upload here.

I think is solved. Now i'm loading a bg from a bmp file and all works well. Now before copy tiles and palette to Vram i do this

Code: Select all

DC_FlushAll();
And to copy i use:

Code: Select all

dmaCopyHalfWords(3, u8 *data, u8*dest, bytes); //<=to u8 data (tiles)
dmaCopyWords(3, u16 *data, u16*dest, bytes); //<=to u16 data (palette and map)
If I detect a problem, i'll post it here, thanks a lot