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.

Beginners > Sprites getting massacred :-/

#46649 - pr0vidence - Tue Jun 28, 2005 5:03 pm

Hey guys, me again.

Trying to move along in the process of learning, getting stuck again.

I have a program that SHOULD put a sprite on screen, very simple. again I am following, mostly, the code given on one of the various tutorials out there. As far as I can tell this should work. Somehow, my sprite, which is just an 8x8 pixel smiley face, gets mangled in the copying. I get an 8x8 pixel thing on screen, but it's a mess.

I am trying to get through it myself and not have you guys hold my hand every step of the way, but it doesn't appear to be going that smoothly :-/ Sorry guys.

here is what I have:

Code:
#include "gba.h"
#include "sprite.h"

OAMEntry sprites[128];

void InitializeSprites()
{
   u16 loop;
   for(loop=0;loop<128;loop++)
   {
      sprites[loop].attribute[0]=160;
      sprites[loop].attribute[1]=240;
   }
}

void WaitForVsync()
{
   while((volatile u16)REG_VCOUNT != 160){}
}

void CopyOAM()
{
   u16 loop;
   u16* temp;
   temp = (u16*)sprites;
   for(loop = 0; loop < 128*4; loop++)
   {
      OAMMem[loop] = temp[loop];
   }
}

int main()
{
   u16 loop;
   s16 x=100;
   s16 y=60;
   
   SetMode(MODE_1|OBJ_ENABLE|OBJ_MAP_1D);
   
   for(loop=0;loop<256;loop++)
   {
      OBJPaletteMem[loop] = spritePalette[loop];
   }
   
   InitializeSprites();

   sprites[0].attribute[0]= (COLOR_16|SQUARE|y);
   sprites[0].attribute[1]= (SIZE_8|x);
   sprites[0].attribute[2]= (0);
   
   for(loop=0;loop<128;loop++)
   {
      OBJ_GFXMem[loop] = spriteData[loop];
   }
   while(1)
   {
      WaitForVsync();
      CopyOAM();
   }
}


Here is a compiled binary so you can see whats happening:

www.boomspeed.com/pr0vidence/gba/sprite.gba

and the sprite header file created by pcx2gba

www.boomspeed.com/pr0vidence/gba/sprite.h

And finally my little PCX sprite smiley

www.boomspeed.com/pr0vidence/gba/sprite.pcx

thanks for helping me out.

-pr0v

#46651 - poslundc - Tue Jun 28, 2005 5:29 pm

In your OAM, you are specifying your sprite as being 16 colours, but the sprite data you've generated from your tool is 256-colour.

Dan.

#46653 - pr0vidence - Tue Jun 28, 2005 5:38 pm

Thanks for replying Dan.

I thought that might have been it, but when I change:

sprites[0].attribute[0]= (COLOR_16|SQUARE|y);

to

sprites[0].attribute[0]= (COLOR_256|SQUARE|y);

I get the same result, or is there something else that needs to be changed as well?

-pr0v

#46660 - strager - Tue Jun 28, 2005 8:33 pm

What is the struct OAMEntry defined as? That may be part of the problem.

And it seems that the ROM takes a while to show anything. This may be my computer, or maybe a defective copy of the ROM, but maybe you should check it out.
Edit: I found out that your program is going through a long loop. Two of them, to be in fact. Once appear to be in the crt0 script, and the other after your video initialization. Try downloading the latest version of DevKitARM (r13) and see if that fixes it.

And your CopyOAM function is well un-optimized. Here's a faster version:
Code:
void CopyOAM(void)
{
    int cur_oam;
    OAMEntry *src = sprites;
    OAMEntry *dst = OAMMem;

    for(cur_oam = 0; cur_oam < 128; cur_oam++)
    {
        *dst = *src;
        src++; dst++;
    };
};

That should work, though I'm not entirely sure.
Edit: You could also do something similar to this for InitializeSprites to speed things up (see above edit).

#46664 - headspin - Tue Jun 28, 2005 10:13 pm

Try using gifs2sprites from http://www.gbadev.org/tools.php?section=gfx

gifs2sprites 16 sprite.h sprite.gif

For 16 colors...
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#46723 - pr0vidence - Wed Jun 29, 2005 4:27 pm

strager wrote:
What is the struct OAMEntry defined as? That may be part of the problem.


hi strager.

I am using dovoto's header files, so OAMEntry looks like this:

Code:
typedef struct tagOAMEntry
{
   u16 attribute[3];
   u16 filler;
}OAMEntry,*pOAMEntry;

typedef struct tagRotData
{
      
   u16 filler1[3];
   u16 pa;

   u16 filler2[3];
   u16 pb;   
      
   u16 filler3[3];
   u16 pc;   

   u16 filler4[3];
   u16 pd;
}RotData,*pRotData;


I realise that it takes a while for anything to happen when the ROM loads. I figured that was just part of it. I mean, the things I am asking the GBA to do are fairly simple, even horribly unoptimised it should be pretty quick. I believe I already do have the latest version of devkit, though I can re-download it.

(after swapping your CopyOAM code with what I had)

...whoa....it works. Why does this work and not the other? It appears to do much the same thing. though I am obviously mistaken.

Thanks for your help everyone. Now it's on to basic input. I'm gonna make that sprite move about.