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 > [RESOLVED] Tile Based Background Showing up Distorted

#161420 - thesean - Sun Aug 03, 2008 4:12 am

Hey everyone,

I know this is some simple newb error but I really can't figure it out, I've tried a lot of different things... Anyway cut to the chase:

I'm trying to display a regular tile based background on the screen, but it's showing up as some garbage at the top of the screen (screenshot): http://www.atlantic7.com/temp/Clipboard01.png

The image is 256x256 px wide, being converted to 8bpp using grit with rules:

#include pallete
-p
# Tile the image
-gt
# 8-bit, 256 color
-gB8
#tile in 32x32 chunks
-Mh4
-Mw4
#perform reductions - tile flipping, etc
-mRtpf
#Map layout standard format
-mLS

But I don't think that's causing the current problem, because if I change those options, I just see different garbage, but still garbage =]

Here's my code to display the BG itself, I've also got some other BG's that I've set up, and that's where I suspect the problem is coming from. My guess is that I'm DMA'ing the data to a bad location or something but I'm too inexperienced to know for sure. For the screenshot, I commented out the other DMA calls so I can view the garbage properly.

Code:

void init() {
   srand(22); //set the seed (sew the seed?)

   // enable vblank irq for swiWaitForVBlank
   irqInit();
   irqEnable(IRQ_VBLANK);

   vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000,
                     VRAM_B_MAIN_BG_0x06020000,
                     VRAM_C_SUB_BG_0x06200000,
                     VRAM_D_LCD
   );
   vramSetBankE(VRAM_E_MAIN_SPRITE);

   videoSetMode(MODE_5_2D | // Set the graphics mode to Mode 5
                 DISPLAY_BG2_ACTIVE | // Enable BG2 for display
                 DISPLAY_BG3_ACTIVE | // Enable BG3 for display
                 DISPLAY_SPR_ACTIVE | // Enable sprites for display
                 DISPLAY_SPR_1D       // Enable 1D tiled sprites
   );

   // screen for console output and bitmap
   // bg0 = console, bg3 = bitmap
   // mode 5 allows extended BGs
   videoSetModeSub( MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG3_ACTIVE);
   
   powerON(POWER_ALL_2D); //turn on 2d graphics core
   consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(2), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);
   lcdMainOnBottom();
   
   // set console font colour
   BG_PALETTE_SUB[255] = RGB15(7,15,31);
}

void setupBackgrounds() {
   //setup backgrounds:
   //priorities: 3 is low, 2 is med, 1 is upper, 0 is highest
   BG3_CR = BG_BMP16_256x256 | BG_BMP_BASE(0) | BG_PRIORITY(3) | BG_WRAP_ON;

    BG3_XDX = 1 << 8;
    BG3_XDY = 0;
    BG3_YDX = 0;
    BG3_YDY = 1 << 8;
   //background at origin
    BG3_CX = 0;
    BG3_CY = 0;

   // setup bg3 control
   // SUB_BG3_CR is the register that controls BG3's behavior on the sub-display
   // this is the top screen background
   SUB_BG3_CR = BG_BMP16_256x256 |  BG_BMP_BASE(0) | BG_PRIORITY(2) | BG_WRAP_ON;
   
   // identity matrix
   SUB_BG3_XDX = 1 << 8;
   SUB_BG3_XDY = 0;
   SUB_BG3_YDX = 0;
   SUB_BG3_YDY = 1 << 8;  //1<<8 =  256
   
   // scroll the bg past the section used by the console
   SUB_BG3_CY = 16 << 8; // CY is a fixed point number, 16*256 = 16.0 pixels

   // the character tiles will occupy map bases 0 and 1
   // setup the console bg to map base 2
   // BG0 already has higher priority than BG3, so no need to mess with that stuff.
   SUB_BG0_CR = BG_MAP_BASE( 2 );

   //first attempt at tilemap usage using bg CR 2
   //old try - //BG2_CR = BG_64x64 | BG_BMP_BASE(8) | BG_PRIORITY(2) | BG_TILE_BASE(1) | BG_COLOR_256;  // A higher priority
   BG2_CR = BG_TILE_BASE(0) | BG_MAP_BASE(8) | BG_COLOR_256 | TEXTBG_SIZE_256x256 | BG_PRIORITY(2);

    BG2_XDX = 1 << 8;
    BG2_XDY = 0;
    BG2_YDX = 0;
    BG2_YDY = 1 << 8;

   //start the regular tile background at origin
    BG2_CX = 0;
    BG2_CY = 0;

   // use dma to copy the tile, map and palette data to VRAM
   dmaCopyHalfWords(DMA_CHANNEL, testmapPal, BG_PALETTE, testmapPalLen);
   dmaCopyHalfWords(DMA_CHANNEL, testmapTiles, (uint16 *)BG_TILE_RAM(0), testmapTilesLen);
   dmaCopyHalfWords(DMA_CHANNEL, testmapMap, (uint16 *)BG_MAP_RAM(8), testmapMapLen);

   //BG_PALETTE[0] = 0xFFFF;
   //copy the backgrounds in to MM
   //dmaCopyHalfWords(DMA_CHANNEL, botbgBitmap, (uint16 *) BG_BMP_RAM(0), botbgBitmapLen); //main
   //dmaCopyHalfWords(DMA_CHANNEL, testmapTiles, (uint16 *)BG_BMP_RAM(6), testmapBitmapLen);//main
   //dmaCopyHalfWords(DMA_CHANNEL, topbgBitmap, (uint16 *)BG_BMP_RAM_SUB(0)+(4*1024), topbgBitmapLen); //sub + 4 KB from console

   //sanity checks...
   assert(BG_BMP_BASE(2) == BG_MAP_BASE(2));
   assert(BG_TILE_RAM(2) == BG_BMP_RAM(2));
   assert(BG_MAP_RAM(2) == SCREEN_BASE_BLOCK(2));
   //assert(BG_BMP_RAM(4) == BG_MAP_RAM(4)); -- fails!!
   //tile ram == char base block == bg_bmp_ram
   assert(BG_TILE_RAM(6) == CHAR_BASE_BLOCK(6));
   assert(BG_BMP_RAM(6) == CHAR_BASE_BLOCK(6));
}

init();
setupBackgrounds();


Also, if I change the main palette's 0 color to white, the screen will show white with the garbage, so I'm fairly certain I have the background initialized properly.

Thanks for any help with this!

-thesean


Last edited by thesean on Sun Aug 03, 2008 6:12 am; edited 2 times in total

#161423 - tepples - Sun Aug 03, 2008 4:28 am

The screenshot is 404 not found.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#161424 - thesean - Sun Aug 03, 2008 4:42 am

Ack! Thanks for noticing, fixed.

#161426 - thesean - Sun Aug 03, 2008 6:15 am

Figured it out with some help from stragerLN and LiraNuna (thanks a lot!!)

Quick Version:
Was using wrong graphics mode and wrong grit rules.

Details:
I was using Mode 5 on the main screen, where BG2 is an extended rot background, so I either needed to change my VideoSetMode call to a different mode, or use a text or affine background, like BG1 (text) I ended up just changing from Mode 5 to Mode 3. The other problem was my grit rule file, I shouldn't have been using -Mh4 and -Mw4. Problem solved!