Page 1 of 1

Failed sprite allocation with oamAllocateGfx

Posted: Mon Mar 21, 2011 12:35 am
by Gmeeoorh
Hi,

I have a problem concerning allocation of gfx memory for sprites. Consider the following code:

Code: Select all

#include <nds.h>
#include <stdio.h>

int main()
{
    int i;
    u16* gfx[9];

    consoleDemoInit();
    vramSetBankA(VRAM_A_MAIN_BG);
    vramSetBankB(VRAM_B_MAIN_SPRITE);
    videoSetMode(MODE_5_2D);
    bgInit(3,BgType_Bmp8,BgSize_B8_256x256, 0,0);
    oamInit(&oamMain, SpriteMapping_1D_32, false);

    for( i=0; i < 9; i++ ) {
        gfx[i] = oamAllocateGfx(&oamMain,SpriteSize_64x64,SpriteColorFormat_256Color);
        if( gfx[i] == NULL ) {
            iprintf("CANT ALLOCATE \n");
            while(1) swiWaitForVBlank();
        }	
        iprintf("%p \n",gfx[i]);
    }
    while(1) swiWaitForVBlank();
    return 0;
}
which leads to the following output:

Code: Select all

0x6400000
0x6401000
0x6402000
0x6403000
0x6404000
0x6405000
0x6406000
0x6407000
CANT ALLOCATE
I see that VRAM bank B is 128kB in size which should be sufficient to hold 9 sprites of the given size and color format !?
Why can't I allocate more than 8 sprites? Can anyone tell me what I have overseen ?

Re: Failed sprite allocation with oamAllocateGfx

Posted: Tue Mar 22, 2011 6:47 am
by zeromus
There are only 1024 sprite addresses. by choosing SpriteMapping_1D_32 you have said that you want each sprite address to reference a 32Byte block. the highest 32Byte block that can be addressed in this way would be 32*1023 ~= 32KB. Your sprites are 64x64 = 4KB apiece and so you only have room to address 8 sprites. Select a different sprite mapping and it will work.

Re: Failed sprite allocation with oamAllocateGfx

Posted: Tue Mar 22, 2011 8:29 pm
by Gmeeoorh
I understand. Thanks alot for your help!

Forum post explaining sprite mappings

Posted: Tue Mar 22, 2011 9:20 pm
by Gmeeoorh
Just for anybody else stumbling across this, I found an older forum post explaining the sprite mappings in detail:
http://devkitpro.org/viewtopic.php?f=6&t=230