#152898 - ben0bi - Sat Mar 22, 2008 11:14 am
i copy/pasted some tutorial code and created a simple sprite engine for me.
now the problem is, rotating the first sprite affects ALL sprites while rotating the "second" sprite does not work.
i do not know what memory magic i (again) have to use, to make this work.
short: i can only rotate the first sprite, all other sprites are rotated in the same direction. it changes ALL sprites, not only the ones with the same picture....
here is my code, it is much again, but i really dont know where to search for this rotating stuff:
now the problem is, rotating the first sprite affects ALL sprites while rotating the "second" sprite does not work.
i do not know what memory magic i (again) have to use, to make this work.
short: i can only rotate the first sprite, all other sprites are rotated in the same direction. it changes ALL sprites, not only the ones with the same picture....
here is my code, it is much again, but i really dont know where to search for this rotating stuff:
Code: |
a=new CSpriteImageInfo; a->Initialize(orangeShuttleTilesLen,orangeShuttleTiles,orangeShuttlePalLen,orangeShuttlePal,OBJSIZE_64); b=new CSpriteImageInfo; b->Initialize(moonTilesLen,moonTiles,moonPalLen,moonPal,OBJSIZE_32); int shuttleId=m_Sprites.CreateSprite(a,10,10,300); int shuttleId2=m_Sprites.CreateSprite(a,100,10,100); int moonId=m_Sprites.CreateSprite(b,50,10,100); ------------------------------------------------------------------ typedef struct{ bool used; int oamId; int width; int height; int angle; SpriteEntry * entry; } SpriteInfo; class CSpriteImageInfo { protected: static int m_iNEXTTILEMEMADRESS; int m_iTilesLen; int m_iPalLen; int m_iTileMemAdress; const unsigned int *m_pTiles; const short unsigned int *m_pPalette; tObjSize m_objectSize; public: void CreateMemAdress() { m_iTileMemAdress=m_iNEXTTILEMEMADRESS; m_iNEXTTILEMEMADRESS+=m_iTilesLen / OAM_BYTES_PER_16_COLOR_TILE; } static int GetNextAvailableMemAdress(void) {return m_iNEXTTILEMEMADRESS;}; int GetMemAdress(void) {return m_iTileMemAdress;}; int GetTilesLen(void) {return m_iTilesLen;}; int GetPalLen(void) {return m_iPalLen;}; tObjSize GetObjSize(void) {return m_objectSize;}; const unsigned int *GetTiles(void) {return m_pTiles;}; const short unsigned int *GetPalette(void) {return m_pPalette;}; CSpriteImageInfo() { Initialize(0,NULL,0,NULL,OBJSIZE_8); m_iTileMemAdress=-1; } void Initialize(int imageLen,const unsigned int *imagePtr, int imagePalLen,const short unsigned int *imagePalette,tObjSize objSize) { m_iTilesLen=imageLen; m_pTiles=imagePtr; m_iPalLen=imagePalLen; m_pPalette=imagePalette; m_objectSize=objSize; } }; int CreateSprite(CSpriteImageInfo *Image, int posX, int posY, int angle) { // eventually load the image/s to OAM if this has not happened before. int iImageMemAdress=imageLoadToOAM(Image); ////// first search an entry to use. int emptyOAMID = -1; for(int i=0;i<SPRITE_COUNT;i++) { if(!m_SpriteInfo[i].used) { emptyOAMID=i; break; } } //// found an entry. if(emptyOAMID!=-1) { printf("C\n"); int width=0; int height=0; switch(Image->GetObjSize()) { case OBJSIZE_8: width=height=8; break; case OBJSIZE_16: width=height=16; break; case OBJSIZE_32: width=height=32; break; case OBJSIZE_64: width=height=64; break; } SpriteInfo * sprInfo = &m_SpriteInfo[emptyOAMID]; SpriteEntry * OAMEntry = &oam->spriteBuffer[emptyOAMID]; /* Initialize sprite info struct */ sprInfo->used=true; sprInfo->oamId = emptyOAMID; sprInfo->width = width; sprInfo->height = height; sprInfo->angle = angle; sprInfo->entry = OAMEntry; /* * 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. */ OAMEntry->posY = posY; OAMEntry->posX = posX; OAMEntry->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(!OAMEntry->isRotoscale || (sprInfo->oamId < MATRIX_COUNT)); OAMEntry->rsDouble = false; OAMEntry->objMode = OBJMODE_NORMAL; OAMEntry->isMosaic = false; OAMEntry->colMode = OBJCOLOR_16; OAMEntry->objShape = OBJSHAPE_SQUARE; /* * Configure attribute 1. * * rsMatrixId refers to the location 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. */ OAMEntry->rsMatrixIdx = ATTR1_ROTDATA(sprInfo->oamId); OAMEntry->objSize = Image->GetObjSize();//OBJSIZE_32;//OBJSIZE_64; /* * 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. */ OAMEntry->tileIdx = iImageMemAdress; OAMEntry->objPriority = OBJPRIORITY_0; OAMEntry->objPal = sprInfo->oamId; /* Rotate the sprite */ spriteRotate(&oam->matrixBuffer[sprInfo->oamId], sprInfo->angle); /*************************************************************************/ /* Copy over the sprite palette */ dmaCopyHalfWords(OAM_SPRITE_DMA_CHANNEL, Image->GetPalette(), &SPRITE_PALETTE[sprInfo->oamId * OAM_COLORS_PER_PALETTE], Image->GetPalLen()); printf("-------------------------------\n"); printf("OAM ID: %i/%i (real)\n", emptyOAMID,sprInfo->oamId); printf("TileMem : %08x\nNextTileMem: %08x\n",Image->GetMemAdress(),Image->GetNextAvailableMemAdress()); return emptyOAMID; } return -1; }; // returns the mem adress of the picture and sets it in the given struct. // returns the same adress as bevore if it was loaded before. int imageLoadToOAM(CSpriteImageInfo *Image) { if(Image->GetMemAdress()==-1) { printf("Not Existing\n"); // load the image to OAM. /* Copy the sprite graphics to sprite graphics memory */ Image->CreateMemAdress(); dmaCopyHalfWords(OAM_SPRITE_DMA_CHANNEL, Image->GetTiles(), &SPRITE_GFX[Image->GetMemAdress() * OAM_MAINSPR_OFFSET_MULTIPLIER], Image->GetTilesLen()); } return Image->GetMemAdress(); } void CSpriteEngine::spriteRotate(SpriteRotation *spriteRotation, u16 angle) { s16 s = SIN[angle & OAM_SPRITE_ANGLE_MASK]>>4; s16 c = COS[angle & OAM_SPRITE_ANGLE_MASK]>>4; spriteRotation->hdx=c; spriteRotation->hdy=s; spriteRotation->vdx=-s; spriteRotation->vdy=c; } |