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 > [solved] Sprites in mode 4, why won't they show up?

#22660 - Wriggler - Sat Jun 26, 2004 3:20 pm

Hi guys,

I'm trying to combine a couple of tutorials really. The first showed me how sprites worked, but it used Mode 1. As I haven't really figured out the tile modes yet, I thought it would be cool to use this new fangled sprite stuff in Mode 4. So I grabbed my previous code from the Mode 4 tutorial, and tried combining the two.

I've gotten as far as the background being shown, but the sprites do not appear. Looking through the debug options in the emulator, the sprites have loaded correctly they are just not actually appearing. I suspect this is a problem with the drawing priority of the background and the sprites.

Of course, here is the code. I've only included the main() function, but if anything else is needed then please let me know!

Code:

//Entry point
int main() {

   //Variable declarations
   u16 loop;
   u16 x = 50;
   u16 y = 50;
   u16 x2 = 150;
   u16 y2 = 50;

   //set mode 1 and enable sprites and 1d mapping
    SetMode(MODE_4 | BG2_ENABLE | OBJ_ENABLE | OBJ_MAP_1D);

   //Load screen palette and background data
   for (loop = 0; loop < 256; loop++) {
      BGPaletteMem[loop]=bgPalette[loop];
   }

   for (loop = 0; loop < (120*160); loop++) {
      VideoBuffer[loop] = bgData[loop] ;
   }

   //load the sprite palette into memory
   for(loop = 0; loop < 256; loop++) {
      OBJPaletteMem[loop] = ballPalette[loop];
   }

   InitializeSprites();

      sprites[0].attribute0 = COLOR_256 | SQUARE | y;   //setup sprite info, 256 colour, shape and y-coord
    sprites[0].attribute1 = SIZE_16 | x;            //size 16x16 and x-coord
    sprites[0].attribute2 = 0;                      //pointer to tile where sprite starts   
    for(loop = 0; loop < 128; loop++) {            //load sprite image data
      OAMData[loop] = ballData[loop];
   }

   //2nd sprite
   sprites[1].attribute0 = COLOR_256 | SQUARE | y2;
    sprites[1].attribute1 = SIZE_16 | x2;
    sprites[1].attribute2 = 8;
   for(loop=128; loop < 256; loop++) {
      OAMData[loop] = ballData[loop-128];
   }


   //Game loop
   while(1) {
      WaitForVsync();
      CopyOAM();
   }

   return 0;

}


Can anyone help me out with this? Thanks in advance, I'm sure it's a simple problem but I'm not sure how to go about fixing it.

Cheers,

Ben


Last edited by Wriggler on Sat Jun 26, 2004 4:17 pm; edited 1 time in total

#22662 - johnny_north - Sat Jun 26, 2004 3:41 pm

Ben,

The following is a section from Korth's gbatek document pertainent to both of your questions:

Code:
OBJ Tile Number
There are two situations which may divide the amount of available tiles by two (by four if both situations apply):

1. When using the 256 Colors/1 Palette mode, only each second tile may be used, the lower bit of the tile number should be zero (in 2-dimensional mapping mode, the bit is completely ignored).

2. When using BG Mode 3-5 (Bitmap Modes), only tile numbers 512-1023 may be used. That is because lower 16K of OBJ memory are used for BG. Attempts to use tiles 0-511 are ignored (not displayed).

Priority
In case that the 'Priority relative to BG' is the same than the priority of one of the background layers, then the OBJ becomes higher priority and is displayed on top of that BG layer.
Caution: Take care not to mess up BG Priority and OBJ priority. For example, the following would cause garbage to be displayed:
  OBJ No. 0 with Priority relative to BG=1   ;hi OBJ prio, lo BG prio
  OBJ No. 1 with Priority relative to BG=0   ;lo OBJ prio, hi BG prio

 
That is, OBJ0 is always having priority above OBJ1-127, so assigning a lower BG Priority to OBJ0 than for OBJ1-127 would be a bad idea.


This wonderful ref can be found at http://www.work.de/nocash/gbatek.htm

#22664 - Wriggler - Sat Jun 26, 2004 4:16 pm

Awesome, thanks johnny_north. After a couple of minutes of fiddling I've got sprites working in bitmap mode! :)

It was the 2nd point that had me stumped, although I should have really assumed that bitmap modes would have taken up more memory than tile mode. I had the first point nailed though, as the 256 colour sprite frame takes up 16bits and each character tile is only 8bits (therefore taking up 2 char tiles)...

Simply starting the OAM memory location at 512*16 bits down the line (and updating the sprite attributes) did the trick. This is all starting to make sense in my head, thanks a lot for your help!

(I'll be sure to check out this reference in the future, it's a good read. Cheers).

Ben

#22668 - poslundc - Sat Jun 26, 2004 4:51 pm

You should also bookmark the Cowbite Virtual Hardware Spec. It is not quite "the bible" as much as the gbatek document is, but it generally reads a lot easier and is an invaluable tool.

Dan.