#84122 - Jwraith - Sat May 20, 2006 5:13 pm
Okay, so I've been working through the "Programming the Gameboy Advance" book upto chapter 6, Dealing with Tiled Backgrounds.
Specifically, rotation backgrounds. Now, my tile data/map/palette are all loaded into memory and can be drawn without transformations (yay).
However, when I try and use the rotate method (as provided in the book). It doesn't work.
I've searched just about ever tutorial and forum on GBA dev and cannot find anywhere that explains what I need to do to rotate the image.
Code provided :
Please help me! Otherwise I think I'm just going to have to quit because I am too stupid to understand.
_________________
"All Your Megadrive Are Belong to Us"
Specifically, rotation backgrounds. Now, my tile data/map/palette are all loaded into memory and can be drawn without transformations (yay).
However, when I try and use the rotate method (as provided in the book). It doesn't work.
I've searched just about ever tutorial and forum on GBA dev and cannot find anywhere that explains what I need to do to rotate the image.
Code provided :
Code: |
/********************************************************************************* * Includes ********************************************************************************/ #include <math.h> #include "tile.raw.c" #include "tiles.pal.c" #include "tilemap.h" //prototype functions void DMAFastCopy(void*,void*,unsigned int,unsigned int); void WaitVBlank(void); void RotateBackground(int,int,int,int); //DMA defines #define REG_DMA3SAD *(volatile unsigned int*)0x40000D4 #define REG_DMA3DAD *(volatile unsigned int*)0x40000D8 #define REG_DMA3CNT *(volatile unsigned int*)0x40000DC #define DMA_ENABLE 0x80000000 #define DMA_TIMING_IMMEDIATE 0x00000000 #define DMA_16 0x00000000 #define DMA_32 0x04000000 #define DMA_32NOW (DMA_ENABLE | DMA_TIMING_IMMEDIATE | DMA_32) #define DMA_16NOW (DMA_ENABLE | DMA_TIMING_IMMEDIATE | DMA_16) //scrolling registers for bg_0 #define REG_BG0HOFS *(volatile unsigned short*)0x4000010 #define REG_BG0VOFS *(volatile unsigned short*)0x4000012 //bg2 registers #define REG_BG2X *(volatile unsigned int*)0x4000028 #define REG_BG2Y *(volatile unsigned int*)0x400002C #define REG_BG2PA *(volatile unsigned int*)0x4000020 #define REG_BG2PB *(volatile unsigned int*)0x4000022 #define REG_BG2PC *(volatile unsigned int*)0x4000024 #define REG_BG2PD *(volatile unsigned int*)0x4000026 //background setup registers #define REG_BG0CNT *(volatile unsigned short*)0x4000008 #define REG_BG1CNT *(volatile unsigned short*)0x400000A #define REG_BG2CNT *(volatile unsigned short*)0x400000C #define REG_BG3CNT *(volatile unsigned short*)0x400000E #define BG_COLOR256 0x80 #define CHAR_SHIFT 2 #define SCREEN_SHIFT 8 #define WRAPAROUND 0x1 #define BG_MOSAIC_ENABLE 0x40 //background tile bitmap sizes #define TEXTBG_SIZE_256x256 0x0 #define TEXTBG_SIZE_256x512 0x8000 #define TEXTBG_SIZE_512x256 0x4000 #define TEXTBG_SIZE_512x512 0xC000 #define ROTBG_SIZE_128x128 0x0 #define ROTBG_SIZE_256x256 0x4000 #define ROTBG_SIZE_512x512 0x8000 #define ROTBG_SIZE_1024x1024 0xC000 //background memory offset macros #define CharBaseBlock(n) (((n)*0x4000)+0x6000000) #define ScreenBaseBlock(n) (((n)*0x800)+0x6000000) //background mode identifiers #define BG0_ENABLE 0x100 #define BG1_ENABLE 0x200 #define BG2_ENABLE 0x400 #define BG3_ENABLE 0x800 //video identifiers #define REG_DISPCNT *(unsigned int*)0x4000000 #define BGPaletteMem ((unsigned short*)0x5000000) #define SetMode(mode) REG_DISPCNT = (mode) //vertical refresh register #define REG_DISPSTAT *(volatile unsigned short*)0x4000004 //button identifiers #define BUTTON_A 1 #define BUTTON_B 2 #define BUTTON_RIGHT 16 #define BUTTON_LEFT 32 #define BUTTON_UP 64 #define BUTTON_DOWN 128 #define BUTTON_R 256 #define BUTTON_L 512 #define BUTTONS (*(volatile unsigned int*)0x04000130) //wait for v blank void WaitVBlank(void) { while((REG_DISPSTAT & 1)); } //math values needed for rotation #define PI 3.14159265 #define RADIAN(n) (((float)n) / (float)180 * PI) //precomputed sine and cosine arrays signed int SIN[360]; signed int COS[360]; //rotation variables int x_scroll=0,y_scroll=0; int DX=0,DY=0; int PA,PB,PC,PD; int zoom = 2; int angle = 0; int center_y,center_x; int main(void) { int n; int charbase = 0; int screenbase = 31; for(n = 0; n < 360; n++) { SIN[n] = (signed int)(sin(RADIAN(n)) * 256); COS[n] = (signed int)(cos(RADIAN(n)) * 256); } unsigned short * bg2map = (unsigned short *)ScreenBaseBlock(screenbase); //set up bg0 REG_BG2CNT = BG_COLOR256 | ROTBG_SIZE_128x128 | (charbase << CHAR_SHIFT) | (screenbase << SCREEN_SHIFT); //set up display mode SetMode(2 | BG2_ENABLE); //set the palette DMAFastCopy((void*)tiles_Palette, (void*)BGPaletteMem, 256, DMA_16NOW); //set the tile images DMAFastCopy((void*)tile_Tiles, (void*)CharBaseBlock(0), 64, DMA_32NOW); //copy the tile map to bg0 DMAFastCopy((void*)tiles_Map, (void*)bg2map, 64, DMA_32NOW); while(1) { WaitVBlank(); //use the hardware to scroll around some if(!(BUTTONS & BUTTON_LEFT)) x_scroll--; if(!(BUTTONS & BUTTON_RIGHT)) x_scroll++; if(!(BUTTONS & BUTTON_UP)) y_scroll--; if(!(BUTTONS & BUTTON_DOWN)) y_scroll++; if(!(BUTTONS & BUTTON_A)) zoom--; if(!(BUTTONS & BUTTON_B)) zoom++; if(!(BUTTONS & BUTTON_L)) angle--; if(!(BUTTONS & BUTTON_R)) angle++; if(angle > 359) angle = 0; if(angle < 0) angle = 359; //rotate the background RotateBackground(angle,64,64,zoom); WaitVBlank(); //update the background REG_BG2X = DX; REG_BG2Y = DY; REG_BG2PA = PA; REG_BG2PB = PB; REG_BG2PC = PC; REG_BG2PD = PD; WaitVBlank(); for(n = 0; n < 100000; n++); } return 0; } //other methods void RotateBackground(int ang, int cx, int cy, int zoom) { DX= cx - ( (PA*cx + PC*cy)>>8 ); DY= cy - ( (PC*cx + PD*cy)>>8 ); PA = (COS[ang] * zoom) >> 8; PB = (SIN[ang] * zoom) >> 8; PC = -PB; PD = PA; } //other functions void DMAFastCopy(void* source,void* dest,unsigned int count, unsigned int mode) { if(mode == DMA_16NOW || mode == DMA_32NOW) { REG_DMA3SAD = (unsigned int)source; REG_DMA3DAD = (unsigned int)dest; REG_DMA3CNT = count | mode; } } /* END OF FILE */ |
Please help me! Otherwise I think I'm just going to have to quit because I am too stupid to understand.
_________________
"All Your Megadrive Are Belong to Us"