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.

C/C++ > Sprite troubles

#51254 - triton - Wed Aug 17, 2005 5:45 pm

Been trying to display a sprite. Here's my gba.h code.

Code:

....
typedef struct
{
    u16 att0, att1, att2, att3;
}OAM;
OAM oamc[128];
typedef struct
{
    int x, y;
    OAM* oam;
}SPRITE;

void initspr()
{
    int i;
    for (i=0; i<128; i++)
    {
        oamc[i].att0=160;
        oamc[i].att1=240;
    }
}
void oamup()
{
    int i;
    for(i=0; i<128*sizeof(OAM)/4; i++)
    ((u32*)charmem)[i]=((u32*)oamc)[i];
}
void DMA(int dma, u32* source, u32* dest,unsigned int count)
{//      0,1,2,3                          # words/halfwords      2, 4
{
 (*(unsigned int*)(0x40000b0 + dma * 12))= (unsigned int)source;
 (*(unsigned int*)(0x40000b4 + dma * 12)) = (unsigned int)dest;
 (*(unsigned int*)(0x40000b8 + dma * 12)) = 0x84000000| count;
}
return;
}
void keys(SPRITE* ex)
{
   if(!(KEY & 0x0010))//right
        {
            ex->x += 1;
        }
        if(!(KEY & 0x0020))
        {
            ex->x -= 1;
        }
}
....


And my main file:
Code:

int main()
{
    DISP_CNT=0x1000;
    initspr();
    DMA(3, (u32*)spr_palette, (u32*)0x5000200, 128);
    DMA(3, (u32*)spr, (u32*)0x6010000, 1024);
    SPRITE sprite;
    sprite.x = 120;
    sprite.y = 80;
    sprite.oam=&oamc[0];
    sprite.oam->att0=sprite.y|COLOR_256;
    sprite.oam->att1=sprite.x|SIZE_64;
    while(1)
    {
   keys(&sprite);
        vblank();
   oamup();
    }
}


But the sprite doesn't move can someone help?

#51258 - rodolforg - Wed Aug 17, 2005 6:51 pm

You don't change OAM data, only SPRITE fields (x,y).
You must copy these values to oamc[0] first, and only after update the real oam.

#51291 - triton - Thu Aug 18, 2005 12:33 am

I kinda get what you're saying. Could you give me some code?

#51295 - sajiimori - Thu Aug 18, 2005 12:50 am

There are lots of tutorials with code. Try copying one and making sure it works, then modifying it to do what you want.

Also, don't put functions definitions in header files (unless they're declared inline, but that's a more advanced topic).

#51310 - rodolforg - Thu Aug 18, 2005 5:04 am

You update OAM copying oamc vector by using oamup(), right? But you didn't change oamc data: you don't assign the new y value to oamc[0].att0 neither x value to oamc[0].att1... You must change these data before call the function oamup(), just how you do here:
Code:
sprite.oam->att0=sprite.y|COLOR_256;
sprite.oam->att1=sprite.x|SIZE_64;

Check you're just changing the x value bits, though. You can do this through and and or operations.
Code:
sprite.oam->att0 &= 0x00FF;
sprite.oam->att0 |= sprite.y;