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.

DS development > learning how to use sprites, please help [SOLVED]

#163045 - anomalous_underdog - Sat Sep 20, 2008 11:45 am

I'm making scrollbar GUI's and I decided to use HW sprites so they can be moved around easily.

This is how the sprite should look like:
[Images not permitted - Click here to view it]

This is how it looks like right now:
[Images not permitted - Click here to view it]

Its the one on the bottom screen, the one floating on the white area. (That's only one sprite) It certainly shouldn't look like that. Any help is appreciated.


I based my code off from the patatersoft manual:

Code:


typedef struct {
   int oamId;
   int width;
   int height;
   int angle;
   SpriteEntry * entry;
} SpriteInfo;


void
initSprites(tOAM * oam, SpriteInfo *spriteInfo)
{
   /*  Define some sprite configuration specific constants.
   *
   *  We will use these to compute the proper index into memory for certain
   *  tiles or palettes.
   *
   *  OFFSET_MULTIPLIER is calculated based on the following formula from
   *  GBATEK (http://nocash.emubase.de/gbatek.htm#dsvideoobjs):
   *      TileVramAddress = TileNumber * BoundaryValue
   *  Since SPRITE_GFX is a uint16*, the compiler will increment the address
   *  it points to by 2 for each change in 1 of the array index into
   *  SPRITE_GFX. (The compiler does pointer arithmetic.)
   */
   static const int BYTES_PER_16_COLOR_TILE = 32;
   static const int COLORS_PER_PALETTE = 16;

   // This is the default boundary value
   // (can be set in REG_DISPCNT)
   static const int BOUNDARY_VALUE = 32;

   static const int OFFSET_MULTIPLIER =
      BOUNDARY_VALUE / sizeof(SPRITE_GFX[0]);

   // Keep track of the available tiles
   int nextAvailableTileIdx = 0;

   /* Create the ship sprite. */
   static const int SHUTTLE_OAM_ID = 0;
   assert(SHUTTLE_OAM_ID < SPRITE_COUNT);
   SpriteInfo * scrollbarInfo = &spriteInfo[SHUTTLE_OAM_ID];
   SpriteEntry * scrollbarSprite = &oam->spriteBuffer[SHUTTLE_OAM_ID];

   /* Initialize scrollbarInfo */
   scrollbarInfo->oamId = SHUTTLE_OAM_ID;
   scrollbarInfo->entry = scrollbarSprite;
   scrollbarInfo->width = 7;
   scrollbarInfo->height = 23;
   scrollbarInfo->angle = 0;

   scrollbarSprite->posX = 70;
   scrollbarSprite->posY = 50;

   /*
   *  Configure attribute 0.
   *
   *  OBJCOLOR_16 will make a 16-color sprite. We specify that we want an
   *  affine sprite (via isRotoscale) here because we would like to rotate
   *  the ship.
   */
   //scrollbarSprite->isRotoscale = true;

   /* This assert is a check to see a matrix is available to store the affine
   * transformation matrix for this sprite. Of course, you don't have to have
   * the matrix id match the affine id, but if you do make them match, this
   * assert can be helpful. */
   assert(!scrollbarSprite->isRotoscale || (scrollbarInfo->oamId < MATRIX_COUNT));

   scrollbarSprite->rsDouble = false;
   scrollbarSprite->isMosaic = false;
   scrollbarSprite->objMode = OBJMODE_NORMAL;
   scrollbarSprite->colMode = OBJCOLOR_16;
   scrollbarSprite->objShape = OBJSHAPE_TALL;

   /*
   *  Configure attribute 1.
   *
   *  rsMatrixId refers to the loation of affine transformation matrix. We
   *  set it to a location computed with a macro. OBJSIZE_64, in our case
   *  since we are making a square sprite, creates a 64x64 sprite.
   */
   scrollbarSprite->rsMatrixIdx = ATTR1_ROTDATA(scrollbarInfo->oamId);
   scrollbarSprite->objSize = OBJSIZE_32;

   /*
   *  Configure attribute 2.
   *
   *  Configure which tiles the sprite will use, which priority layer it will
   *  be placed onto, which palette the sprite should use, and whether or not
   *  to show the sprite.
   */
   scrollbarSprite->tileIdx = nextAvailableTileIdx;
   nextAvailableTileIdx += scrollbarHandle_selectedTilesLen / BYTES_PER_16_COLOR_TILE;
   scrollbarSprite->objPriority = OBJPRIORITY_2;
   scrollbarSprite->objPal = scrollbarInfo->oamId;

   /* Rotate the sprite */
   //rotateSprite(&oam->matrixBuffer[scrollbarInfo->oamId],
   //            scrollbarInfo->angle);

   /*************************************************************************/

   /* Copy over the sprite palettes */
   dmaCopyHalfWords(SPRITE_DMA_CHANNEL,
      scrollbarHandle_selectedPal,
      &SPRITE_PALETTE[scrollbarInfo->oamId * COLORS_PER_PALETTE],
      scrollbarHandle_selectedPalLen);

   /* Copy the sprite graphics to sprite graphics memory */
   dmaCopyHalfWords(SPRITE_DMA_CHANNEL,
      scrollbarHandle_selectedTiles,
      &SPRITE_GFX[scrollbarSprite->tileIdx * OFFSET_MULTIPLIER],
      scrollbarHandle_selectedTilesLen);
}



Last edited by anomalous_underdog on Mon Sep 22, 2008 2:35 pm; edited 1 time in total

#163046 - silent_code - Sat Sep 20, 2008 12:22 pm

Read Tonc's sprite mapping section. :^)
Also, you want "tall" sprites, so check that, too.
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#163047 - anomalous_underdog - Sat Sep 20, 2008 12:57 pm

it already is set to OBJSHAPE_TALL, in case you haven't seen. it still looks like that

is this the URL? http://www.coranac.com/tonc/text/regobj.htm

#163050 - anomalous_underdog - Sat Sep 20, 2008 1:49 pm

by trial and error, I found that this made it work:

Code:

   scrollbarSprite->objSize = OBJSIZE_16;


the image I use is 7x23 pixels, so naturally I'd choose OBJSIZE_32, but for some reason I need to set it to OBJSIZE_16 so it would show up properly (I'm assuming this would lead to graphics data corruptions sooner or later?). if anyone can explain this to me I'd appreciate it.

#163058 - silent_code - Sat Sep 20, 2008 9:52 pm

Does it work on hardware?
I remeber the sizes being mode dependent, but it's been a while.
Basically, they would work different with wide or tall sprites. (See "Table 8.4: GBA sprite sizes")

And, yes, it's that URL.

PS: Nice looking application. :^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#163068 - tepples - Sun Sep 21, 2008 12:13 pm

The sizes are numbered 8, 16, 32, and 64. They don't have much to do with any actual dimension other than they happen to be increasing. In square shape, size 16 happens to give you 16x16; in tall shape, it gives you 8x32.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#163072 - anomalous_underdog - Sun Sep 21, 2008 5:16 pm

thanks, guys, still got some other bugs, but I really appreciate the help.

I had the up and down buttons, now it looks like this:
[Images not permitted - Click here to view it]

somehow, the scrollbar position indicator (or whatever its called) also renders the up button along with it. the down button hasn't been flipped yet in that screenshot, and the light green color was intentional. its really that up button (the one in black and orange) that shouldn't be there.

#163076 - tepples - Sun Sep 21, 2008 8:55 pm

Are you making sure to leave an empty cell below the position indicator? There is no 8x24 size, only an 8x32 size with the bottom rows of pixels drawn as transparent.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#163079 - silent_code - Sun Sep 21, 2008 10:34 pm

And once you're there, you've got to remeber, (remeber, the 5th of November) that there's a sprite object cycle limit for each scanline, which means, you only can have so much sprite pixels drawn each scanline (again, depending on modes [e.g. affine]) before sprites start to disappear. ;^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#163095 - anomalous_underdog - Mon Sep 22, 2008 2:29 pm

So it means I'm really forced to use 8x32 pixels even if I'm using only a 7x23 image for it. I had hoped I could somehow conserve the memory used, but never mind, at least its working. Thanks for the advice again guys

I've made a test .nds file for it, you guys can play around with it if you want. Its got two test scrollbars on the screen, they really don't do anything yet.

BLARGHTextEditor.zip 186 KB


Quote:
And once you're there, you've got to remeber, (remeber, the 5th of November) that there's a sprite object cycle limit for each scanline, which means, you only can have so much sprite pixels drawn each scanline (again, depending on modes [e.g. affine]) before sprites start to disappear. ;^)


I'll try to keep the sprites' height and width as small as possible then.

#163096 - Dwedit - Mon Sep 22, 2008 2:37 pm

The sprite limit is very large, around 4 screen widths worth of sprites.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."