#149770 - yaazz - Fri Jan 25, 2008 3:33 am
Hey everyone I am back again. I am trying to get a picture (All the picture is is 4 rectangles of different colors that should fill the screen) to scroll around on the GBA screen.
I am again using the book "Programming the Nintendo Game Boy Advance"
The rectangle scrolls just fine, but is being sheared to the right for some reason. It also looks completely wrong vertically, with all green for a few pixels......
hopefully someone can help me out...
The code is pretty much copy and pasted from the pdf..
Here it is
And the picture files
colors.raw.c
colorz.map.c
colorz.pal.c
I am again using the book "Programming the Nintendo Game Boy Advance"
The rectangle scrolls just fine, but is being sheared to the right for some reason. It also looks completely wrong vertically, with all green for a few pixels......
hopefully someone can help me out...
The code is pretty much copy and pasted from the pdf..
Here it is
Code: |
//////////////////////////////////////////////////////////// // Programming The Game Boy Advance // Chapter 6: Tile-Based Video Modes // TileMode0 Project // main.c source code file //////////////////////////////////////////////////////////// //include the sample tileset/map #include "colorz.pal.c" #include "colorz.map.c" #include "colorz.raw.c" #define MULTIBOOT int __gba_multiboot; MULTIBOOT //function prototype void DMAFastCopy(void*, void*, unsigned int, unsigned int); //defines needed by DMAFastCopy #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 background 0 #define REG_BG0HOFS *(volatile unsigned short*)0x4000010 #define REG_BG0VOFS *(volatile unsigned short*)0x4000012 //background setup registers and data #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 //background tile bitmap sizes #define TEXTBG_SIZE_256x256 0x0 #define TEXTBG_SIZE_256x512 0x8000 #define TEXTBG_SIZE_512x256 0x4000 #define TEXTBG_SIZE_512x512 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_RIGHT 16 #define BUTTON_LEFT 32 #define BUTTON_UP 64 #define BUTTON_DOWN 128 #define BUTTONS (*(volatile unsigned int*)0x04000130) //wait for vertical refresh void WaitVBlank(void) { while((REG_DISPSTAT & 1)); } //////////////////////////////////////////////////////////// // Function: main() // Entry point for the program //////////////////////////////////////////////////////////// int main(void) { int x = 0, y = 0; int n; //create a pointer to background 0 tilemap buffer unsigned short* bg0map =(unsigned short*)ScreenBaseBlock(31); //set up background 0 REG_BG0CNT = BG_COLOR256 | TEXTBG_SIZE_256x256 |(31 << SCREEN_SHIFT) | WRAPAROUND; //set video mode 0 with background 0 SetMode(0 | BG0_ENABLE); //copy the palette into the background palette memory DMAFastCopy((void*)colorz_Palette, (void*)BGPaletteMem,256, DMA_16NOW); //copy the tile images into the tile memory DMAFastCopy((void*)colorz_Tiles, (void*)CharBaseBlock(0),640, DMA_32NOW); //copy the tile map into background 0 DMAFastCopy((void*)colorz_Map, (void*)bg0map, 256, DMA_32NOW); //main game loop while(1) { //wait for vertical refresh WaitVBlank(); //D-pad moves background if(!(BUTTONS & BUTTON_LEFT)) x--; if(!(BUTTONS & BUTTON_RIGHT)) x++; if(!(BUTTONS & BUTTON_UP)) y--; if(!(BUTTONS & BUTTON_DOWN)) y++; //use hardware background scrolling REG_BG0VOFS = y ; REG_BG0HOFS = x ; //wait for vertical refresh WaitVBlank(); for(n = 0; n < 4000; n++); } return 0; } //////////////////////////////////////////////////////////// // Function: DMAFastCopy // Fast memory copy function built into hardware //////////////////////////////////////////////////////////// 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; } } |
And the picture files
colors.raw.c
Code: |
// // colorz (640 uncompressed bytes) // const unsigned char colorz_Tiles[640] = { 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x01 }; |
colorz.map.c
Code: |
/* * 8x32 map data */ const unsigned short colorz_Map[256] = { 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, 0x0000, 0x0001, 0x0002, 0x0003, 0x0004,0x0000, 0x0001, 0x0002, }; |
colorz.pal.c
Code: |
const unsigned short colorz_Palette[256] = { 0x0000, 0x0010, 0x001f, 0x03e0, 0x03ff, 0x7c00, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; |