oamAllocateGfx() and mixing bmp and tiled sprites

Post Reply
Sylus101
Posts: 179
Joined: Wed Dec 24, 2008 5:08 am

oamAllocateGfx() and mixing bmp and tiled sprites

Post by Sylus101 » Thu Jul 09, 2009 10:12 pm

I'm having a bit of an issue with this and I appear to have duplicated the problem by making some minor changes to the bmp sprite example. (Please move to bug reports if seen fit).

The example isn't changed much except for some size differences and removal of the bmp sprite rotating. I could not duplicate the issue leaving them all 32 x 32, but changing the sizes gave me the same issue as I had in my game code.

I'm using 1.3.6 libnds. Basically, if the bmp sprite in the code below is allocated gfx memory first, then it appears as it should, but if it's allocated gfx after the other 2, then the sprite is not seen. I'm assuming there's an issue with what oamAlllocateGfx() returned.

Code: Select all

#include <nds.h>

#include <stdio.h>

//a simple sprite structure 
//it is generally preferred to separate your game object
//from OAM
typedef struct  
{
   u16* gfx;
   SpriteSize size;
   SpriteColorFormat format;
   int rotationIndex;
   int paletteAlpha;
   int x;
   int y;
}MySprite;

int main(int argc, char** argv) {
	
   //three sprites of differing color format
   MySprite sprites[] = {
      {0, SpriteSize_16x16, SpriteColorFormat_Bmp, -1, 15, 20, 15},
      {0, SpriteSize_32x32, SpriteColorFormat_256Color, -1, 0, 20, 80},
      {0, SpriteSize_64x64, SpriteColorFormat_16Color, -1, 1, 20, 136}
   };

   videoSetModeSub(MODE_0_2D);

   consoleDemoInit();
   
   //initialize the sub sprite engine with 1D mapping 128 byte boundary
   //and no external palette support
   oamInit(&oamSub, SpriteMapping_Bmp_1D_128, false);

  
   vramSetBankD(VRAM_D_SUB_SPRITE);
   
	//Swapping which for loop is used here changes the result. Initializing the bmp sprite first
	//works fine, but if last the sprite gfx are blank on screen.
	
   //allocate some space for the sprite graphics
	//for(int i = 0; i < 3; i++)
   for(int i = 2; i >= 0; i--)
		sprites[i].gfx = oamAllocateGfx(&oamSub, sprites[i].size, sprites[i].format);
   
   //ugly positional printf
   iprintf("\x1b[1;1HDirect Bitmap:");
   iprintf("\x1b[9;1H256 color:");
   iprintf("\x1b[16;1H16 color:");
   
   //fill bmp sprite with the color red
   dmaFillHalfWords(ARGB16(1,31,0,0), sprites[0].gfx, 256*2);
   //fill the 256 color sprite with index 1 (2 pixels at a time)
   dmaFillHalfWords((1<<8)|1, sprites[1].gfx, 32*32);
   //fill the 16 color sprite with index 1 (4 pixels at a time)
   dmaFillHalfWords((1<<12)|(1<<8)|(1<<4)|1, sprites[2].gfx, 64*64 / 2);

   //set index 1 to blue...this will be the 256 color sprite
   SPRITE_PALETTE_SUB[1] = RGB15(0,31,0);
   //set index 17 to green...this will be the 16 color sprite
   SPRITE_PALETTE_SUB[16 + 1] = RGB15(0,0,31);

   int angle = 0;

   while(1) {
      for(int i = 0; i < 3; i++) {
         oamSet(
         &oamSub, //sub display 
         i,       //oam entry to set
         sprites[i].x, sprites[i].y, //position 
         0, //priority
		 sprites[i].paletteAlpha, //palette for 16 color sprite or alpha for bmp sprite
         sprites[i].size, 
		 sprites[i].format, 
		 sprites[i].gfx, 
		 sprites[i].rotationIndex, 
         true, //double the size of rotated sprites
         false, //don't hide the sprite
		 false, false, //vflip, hflip
		 false //apply mosaic
         );
      }

      //oamRotateScale(&oamSub, 0, angle, (1 << 8), (1<<8));

	  //angle += 64;

      swiWaitForVBlank();

      //send the updates to the hardware
      oamUpdate(&oamSub);
   }
   return 0;
}
-Sylus "Not Stylus..." McFrederickson

Come visit my web site.

User avatar
vuurrobin
Posts: 219
Joined: Fri Jul 11, 2008 8:49 pm
Location: The Netherlands
Contact:

Re: oamAllocateGfx() and mixing bmp and tiled sprites

Post by vuurrobin » Fri Jul 10, 2009 12:42 am

hmm...

it happens here on no$gba to. I also changed the code a bit, and the returned adresses doesn't look weard to me (no null at least).
I also changed the order of the array, and then the 256color sprite disappeared instead of the bmp sprite, and the bottom half of the 16color sprite disappeared. and when I used the other for loop, the bmp sprite disappeared, the bottom half of the 16color disappeared, the upper half of the 16color sprite has vertical black lines trough it, and the 256color sprite was blue instead of green.

here is my version of the code:

Code: Select all

#include <nds.h>

#include <stdio.h>
#include <iostream>

using namespace std;


const char* getSpriteColorFormatAsName(SpriteColorFormat format)
{
    switch(format)
    {
        case SpriteColorFormat_256Color:
            return "256Color: ";
        case SpriteColorFormat_16Color:
            return "16Color : ";
        case SpriteColorFormat_Bmp:
            return "bmp     : ";//with some padding space
        default:
            return "no idea";
    }
}


//a simple sprite structure
//it is generally preferred to separate your game object
//from OAM
typedef struct
{
   u16* gfx;
   SpriteSize size;
   SpriteColorFormat format;
   int rotationIndex;
   int paletteAlpha;
   int x;
   int y;
}MySprite;

int main()
{
    videoSetModeSub(MODE_0_2D);
    //initialize the sub sprite engine with 1D mapping 128 byte boundary
    //and no external palette support

    consoleDemoInit();
    //iprintf("test");

    oamInit(&oamSub, SpriteMapping_Bmp_1D_128, false);
    vramSetBankD(VRAM_D_SUB_SPRITE);


    const int MAX_SPRITES = 3;

    //three sprites of differing color format
    MySprite sprites[MAX_SPRITES] =
    {
        {0, SpriteSize_32x32, SpriteColorFormat_256Color, -1, 0, 200, 80},
        {0, SpriteSize_64x64, SpriteColorFormat_16Color,  -1, 1, 200, 136},
        {0, SpriteSize_16x16, SpriteColorFormat_Bmp,      -1, 0, 200, 15}
    };



	//Swapping which for loop is used here changes the result. Initializing the bmp sprite first
	//works fine, but if last the sprite gfx are blank on screen.

    //allocate some space for the sprite graphics
	for(int i = 0; i < MAX_SPRITES; i++){
    //for(int i = MAX_SPRITES-1; i >= 0; i--){
		sprites[i].gfx = oamAllocateGfx(&oamSub, sprites[i].size, sprites[i].format);
        cout << getSpriteColorFormatAsName(sprites[i].format) << sprites[i].gfx << endl;
    }

   //ugly positional printf
   //iprintf("\x1b[1;1HDirect Bitmap:");
   //iprintf("\x1b[9;1H256 color:");
   //iprintf("\x1b[16;1H16 color:");

   //fill bmp sprite with the color red
   dmaFillHalfWords(ARGB16(1,31,0,0), sprites[0].gfx, 16*16 *2);
   //fill the 256 color sprite with index 1 (2 pixels at a time)
   dmaFillHalfWords((1<<8)|1, sprites[1].gfx, 32*32);
   //fill the 16 color sprite with index 1 (4 pixels at a time)
   dmaFillHalfWords((1<<12)|(1<<8)|(1<<4)|1, sprites[2].gfx, 64*64 / 2);

   //set index 1 to blue...this will be the 256 color sprite
   SPRITE_PALETTE_SUB[1] = RGB15(0,31,0);
   //set index 17 to green...this will be the 16 color sprite
   SPRITE_PALETTE_SUB[16 + 1] = RGB15(0,0,31);




      for(int i = 0; i < 3; i++) {
         oamSet(
         &oamSub, //sub display
         i,       //oam entry to set
         sprites[i].x, sprites[i].y, //position
         0, //priority
		 sprites[i].paletteAlpha, //palette for 16 color sprite or alpha for bmp sprite
         sprites[i].size,
		 sprites[i].format,
		 sprites[i].gfx,
		 sprites[i].rotationIndex,
         true, //double the size of rotated sprites
         false, //don't hide the sprite
		 false, false, //vflip, hflip
		 false //apply mosaic
         );
      }

      swiWaitForVBlank();

      //send the updates to the hardware
      oamUpdate(&oamSub);

    while(true)
    {
        swiWaitForVBlank();
    }

    return 0;
}

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest