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 > Background Tiles

#170165 - Azenris - Sat Sep 05, 2009 1:00 am

Hello I have the following code

Code:

REG_BG0CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(0) | BG_TILE_BASE(1) | BG_PRIORITY(2);
REG_BG1CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(1) | BG_TILE_BASE(3) | BG_PRIORITY(2);
REG_BG2CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(2) | BG_TILE_BASE(5) | BG_PRIORITY(2);


Have I done this correctly, I was wanting 3 layers, to one screen (using MODE_0_2D) and a tile set
of (128 - 16*16 size tiles (512 meta tiles)).

Later I have the following
Code:

m_pMain_Map      = BG_MAP_RAM(0);
m_pMain_Tileset      = BG_TILE_RAM(1);


Looking ok so far? :p

I been looking through some of the libnds files and is has

Code:

#define BG_TILE_BASE(base)       ((base) << TILE_BASE_SHIFT)
#define BG_MAP_BASE(base)        ((base) << MAP_BASE_SHIFT)

#define BG_MAP_RAM(base)      ((u16*)(((base)*0x800) + 0x06000000))
#define BG_TILE_RAM(base)      ((u16*)(((base)*0x4000) + 0x06000000))

#define CHAR_BASE_BLOCK(base)      (((base)*0x4000)+ 0x06000000)
#define SCREEN_BASE_BLOCK(base)      (((base)*0x800) + 0x06000000)


So i use BG_TILE_BASE and BG_MAP_BASE macros when setting up the BG control and BG_MAP_RAM and BG_TILE_RAM
when I want to read and write to those locations?

Basically just wanted to know if I atleast got this bit right and then I can assume the problem
lies within another section of my code. TY


EDIT:
sorry to ask another question, hope you don't mind. I have

Code:

void CScreenManager::LoadTileIntoSet(u8 layer, u8 tileIndex, u16 tile)
{
   // add the tileID into the loaded tiles
   m_loadedTiles[layer][tileIndex] = tile;

   // locate data on this tile
   const tileData *pTile = g_pGame->GetTileData(tile);

   // locate memory location to write to
   u16 *pMemoryLoc = NULL;
   if (layer == 0)
      pMemoryLoc = m_pMain_Tileset0 + (tileIndex * tile_graphic_size);
   else
      pMemoryLoc = m_pMain_Tileset1 + (tileIndex * tile_graphic_size);

   // write it into memory
   dmaCopy(pTile->pGraphics, pMemoryLoc, tile_graphic_size);
}


It basically loads a tile into the tileset. I made some text output to the lower screen on some variables. It showed:
Code:

tileIndex: 0, tile: 159, mem: 6004000
tileIndex: 1, tile: 207, mem: 6004200
tileIndex: 2, tile: 343, mem: 6004400
tileIndex: 3, tile: 337, mem: 6004600
tileIndex: 4, tile: 341, mem: 6004800
tileIndex: 5, tile: 339, mem: 6004a00
tileIndex: 6, tile: 166, mem: 6004c00


tile_graphic_size is defined as 256, but the tiles are jumping by 512 bytes.

Code:

pMemoryLoc = m_pMain_Tileset1 + (tileIndex * tile_graphic_size);


This decided the writing location, as the index goes up shouldnt it add an additional 256 bytes?


Sorry I posted so many questions lately :/ I am trying on my own too! TY if any helps

EDIT:
its almost 4am, maybe I should sleep im probably doing it all wrong or just not thinking straight!

EDIT Next day:
nevermind I figured it out, i was using u16* instead of u8*. I got my entire screen loading now.
_________________
My Homebrew Games

#170176 - Azenris - Sat Sep 05, 2009 6:15 pm

Your gonna hate me but I need help, I thought i finally understood it
but now im not sure, noone replied before whether I had set it up
correct and im unsure where my problem lies.

I have the following:
Code:

mem m1: 0x6000000 (layer 0 map)
mem m2: 0x6000800 (layer 1 map)

mem t1: 0x6004000 (layer 0 tiles)
mem t2: 0x600c000 (layer 1 tiles)

My layer 0 map and tiles all seem to work ok, I can get my BG showing, but
the layer 1 stuff isnt working (shows rubbish). I thought it might have been an overlap or
some misunderstanding on my part of the whole map and tiles stuff.

Current I have it setup like
Code:

REG_BG0CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(0) | BG_TILE_BASE(1) | BG_PRIORITY(2);
REG_BG1CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(1) | BG_TILE_BASE(3) | BG_PRIORITY(1);
REG_BG2CNT = BG_32x32 | BG_COLOR_256 | BG_MAP_BASE(2) | BG_TILE_BASE(5) | BG_PRIORITY(0);

m_pMain_Tileset0   = (u8*)BG_TILE_RAM(1);
m_pMain_Map0      = BG_MAP_RAM(0);
m_pMain_Tileset1   = (u8*)BG_TILE_RAM(3);
m_pMain_Map1      = BG_MAP_RAM(1);


I load a tile into the tile set with
Code:

void CScreenManager::LoadTileIntoSet(u8 layer, u8 tileIndex, u16 tile)
{
   // add the tileID into the loaded tiles
   m_loadedTiles[layer][tileIndex] = tile;

   // locate data on this tile
   const tileData *pTile = g_pGame->GetTileData(tile);

   // locate memory location to write to
   u8 *pMemoryLoc = NULL;
   if (layer == 0)
      pMemoryLoc = (u8*)m_pMain_Tileset0 + (tileIndex * tile_graphic_size);
   else if (layer == 1)
      pMemoryLoc = (u8*)m_pMain_Tileset1 + (tileIndex * tile_graphic_size);

   // write it into memory
   dmaCopy(pTile->pGraphics, pMemoryLoc, tile_graphic_size);
}


and for the map I do
Code:

void CScreenManager::LoadTile(u8 layer, u8 location, u16 tile)
{
   // add the tile into the data array
   m_data[location + (layer * SCREEN_SIZE)] = tile;

   // check the tile isnt blank
   if (layer == 0 && tile == 0)
      return;

   u8 tileIndex = 0;

   // check if this tile has been loaded into the tile set
   // if so the index is retrieved, if not a new index is
   // needed to place the new tile
   if (TileInSet(layer, tile))
   {
      tileIndex = GetTileIndex(layer, tile);
   }
   else
   {
      tileIndex = GetFreeTileIndex(layer);
      LoadTileIntoSet(layer, tileIndex, tile);
   }

   // now the tile can be pointed to by the mapping array
   u16 mapPosition = (location * 2) + ((location / SCREEN_X) * SCREEN_TILE_X);
   u16 tilePosition = tileIndex * 4;

   if (layer == 0)
   {
      m_pMain_Map0[mapPosition] = tilePosition;
      m_pMain_Map0[mapPosition+1] = tilePosition + 1;
      m_pMain_Map0[mapPosition+SCREEN_TILE_X] = tilePosition + 2;
      m_pMain_Map0[mapPosition+SCREEN_TILE_X+1] = tilePosition + 3;
   }
   else if (layer == 1)
   {
      m_pMain_Map1[mapPosition] = tilePosition;
      m_pMain_Map1[mapPosition+1] = tilePosition + 1;
      m_pMain_Map1[mapPosition+SCREEN_TILE_X] = tilePosition + 2;
      m_pMain_Map1[mapPosition+SCREEN_TILE_X+1] = tilePosition + 3;
   }
}


basically everything for layer 0 is working and the screen is showing
fine, my problem comes when I add my layer 1 stuff. Is there a memory overlap or
something im doing wrong with the second layer ? :/

sorry for another question, I really just wanna figure this tiling out!

//////////////// EDIT:
Never mind, I figured it out, I think I might have to pretend on posting a question that way ill figure it out soon after anyway! Basically when doing the layer 1 I forgot to have transparency to let blank areas show the flooring.

anyway sorry for spamming. bye!
_________________
My Homebrew Games