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] Unexpected Sprite Rotation

#171827 - Drovor - Sun Dec 27, 2009 7:41 pm

I'm trying to use sprites, but they seem to be getting flipped. I have no idea why, since I'm creating them all in exactly the same way except for a random x/y location.

It looks like this:
http://www.flickr.com/photos/39069226@N03/4218913811/

The code I use to set the OAM attributes looks like this (spriteEntry is a reference to oam->oamBuffer[index]):
Code:

void game_state::initializeSprites()
{             
  int i;
  // Sprite initialisation
  for(i = 0; i < 256; i++)
    SPRITE_PALETTE[i] = ball.palette[i];
 
  for(i = 0; i< 32*16; i++)
    SPRITE_GFX[i] = ball.image.data16[i];
 
  for(i = 0; i < SPRITE_COUNT; i++) {
    /* Info */
    spriteInfo[i].oamId = i;
    spriteInfo[i].width = 8;
    spriteInfo[i].height = 8;
    spriteInfo[i].angle = 0;
    spriteInfo[i].entry = spriteEntry[i];
 
    /* attribute[0] */
    spriteEntry[i]->y = rand() % 192;
    spriteEntry[i]->isRotateScale = false;
    spriteEntry[i]->isSizeDouble = false;
    spriteEntry[i]->blendMode = OBJMODE_NORMAL;
    spriteEntry[i]->isMosaic = false;
    spriteEntry[i]->colorMode = OBJCOLOR_256;
    spriteEntry[i]->shape = OBJSHAPE_SQUARE;

    /* attribute[1] */
    spriteEntry[i]->x = rand() % 256;
    spriteEntry[i]->rotationIndex = spriteInfo[i].oamId;
    spriteEntry[i]->size = OBJSIZE_8;

    /* attribute[2] */
    spriteEntry[i]->gfxIndex = 0; // All use the same one.
    spriteEntry[i]->priority = OBJPRIORITY_0;
    // spriteEntry[i]->palette = N/A, only one 256 palette
  }
}


Afterwards the OAM copy is copied in:
Code:

    DC_FlushAll();
    dmaCopyHalfWords(SPRITE_DMA_CHANNEL,
                     oam->oamBuffer,
                     OAM,
                     SPRITE_COUNT * sizeof(SpriteEntry));


Any help would be greatly appreciated, I'm tempted to "fix" this by flipping my "D" and "S" images horizontally and making a new ball sprite (currently borrowed from the examples) which has horizontal and vertical symmetry!


Last edited by Drovor on Sun Dec 27, 2009 9:18 pm; edited 1 time in total

#171828 - DiscoStew - Sun Dec 27, 2009 8:49 pm

http://nocash.emubase.de/gbatek.htm#lcdobjoverview

Code:
OBJ Attribute 1 (R/W)

  Bit   Expl.
  0-8   X-Coordinate           (0-511)
  When Rotation/Scaling used (Attribute 0, bit 8 set):
    9-13  Rotation/Scaling Parameter Selection (0-31)
          (Selects one of the 32 Rotation/Scaling Parameters that
          can be defined in OAM, for details read next chapter.)
  When Rotation/Scaling not used (Attribute 0, bit 8 cleared):
    9-11  Not used
    12    Horizontal Flip      (0=Normal, 1=Mirrored)
    13    Vertical Flip        (0=Normal, 1=Mirrored)
  14-15 OBJ Size               (0..3, depends on OBJ Shape, see Attr 0)
          Size  Square   Horizontal  Vertical
          0     8x8      16x8        8x16
          1     16x16    32x8        8x32
          2     32x32    32x16       16x32
          3     64x64    64x32       32x64


When you set rotationIndex to a value when isRotateScale is set to false, you have the chance to flip your sprite horizontally and/or vertically, because both rotationIndex and vFlip/hFlip partially share the same area in memory, so unless you plan to use flipping, just set rotationIndex to 0.
_________________
DS - It's all about DiscoStew

#171829 - Drovor - Sun Dec 27, 2009 9:18 pm

Thanks, that did it.