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 > First steps towards tiles

#19931 - CyberSlag5k - Wed Apr 28, 2004 5:04 am

Ok, I"m picking up making maps from tiles. It's going kind of slowly. Out of sprites, animation, drawing a bitmap to the screen, and the DMA, this is turning out to be the most difficult thing I've faced yet. Sound reasonable?

Anyway, I've gone through the gbajunkie and pern tutorials (they're quite similar because gbajunkie kinda modeled his off of pern) twice and am still not entirely sure what's going on, but I'm sure the more I play with it the more I'll understand (I'm a hands on learning kinda guy). So anyway, I've set up a background struct as the tutorials suggest and went through the setup procedures, but my program is acting a little funny. Rather than post a huge bit of code and ask you guys to comb through it (I hate doing that), I've decided to break it into chunks and ask specific questions.

So! Question #1: What I gather from my tutorials, loading tile and map data are a little different from each other. My loop for loading tile data looks like this:

Code:

for(int i = 0; i < 240 * 160 / 2; i++)
{
   back2.tileData[i] = bgTiles[i];
}



I would just assume that this is for each pixel on the screen (divided by 2 of course due to the double horizontal thing). Where the tutorial and I differ a little bit (and the bulk of my question lies) is right here:

Code:

for(int i = 0; i < 30 * 20 /2; i++)
{
   back2.mapData[i] = bgMap[i];
}


gbajunkie has variables (exptiles_WIDTH and exptiles_HEIGHT) in place of my 30 and 20. I'm not sure where they've come from, but I believe they are just the number of tiles across and down the map. In my case, since my map was 240 * 160 and each tile is 8 pixels, I surmised that my loop should run 30 * 20 and then divided by 2 (a quick quesiton, I'm not sure why I'm dividing by 2 here. Isn't this creating the map based on tiles?). Is this the correct way to do this? A bit of clarity, bgMap and bgTiles are both created by my map editor (Nessie's MapEd) and mapData and tileData are definied in my struct (arrays of type u16*).

Anyway, does what I have here look sort of kosher? I haven't included most of the code for as I said, I'm not terribly clear on what's going on here and would like to take it section by section to get a better feeling for it. If, however, further information is required to answer the quesitons I've asked, I would be happy to supply it.

Thanks as always!
_________________
When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...

#19933 - niltsair - Wed Apr 28, 2004 5:52 am

Starter Maps mode come in 3 main flavors:
-Text 16colors (Each Tile can use 1 of 16 16colors Palette)
-Text 256colors (Each Tile can access the main 256colors Palette)
-Rotation 256colors (Same as Text 256 + you can Scale/Rotate the Background)

Which one are you using? From your code I'll assume Text 256colors.
Code:
for(int i = 0; i < 240 * 160 / 2; i++)
{
   back2.tileData[i] = bgTiles[i];
}
You should thinking in term of a bitmap, you have to think of the screen as a big 32x32 chessboard (or bigger if you change the settings). Now, each square is a 8x8 Tile and can contain any picture you want. In one part of the memory you store how each tile shoudl look. In another part of memory, you tell which Tile to display on each checkboard square.

In this case you seem to have 600 8x8Tiles in 256colors. The '/2' is because Video Memory can only be accessed with 16 or 32Bits pointer. 1 Pixel in 256colors, takes 8Bits, thus since 'back2.tileData' and 'bgTiles' are 16bits pointer, you are transfering 2pixels at a time.
Code:
for(int i = 0; i < 30 * 20 /2; i++)
{
   back2.mapData[i] = bgMap[i];
}
The 30*20 is because the Gba can display 30Tiles Wide and 20Tiles High, the rest is offsreen untill you scroll around. I do not understand why there's a '/2' unless 'back2.mapData' and 'bgMap' are 32bits pointer. In that case, since each Map entry takes 1bits, you would be copying 2 at a time.

**EDIT** Ok, I see that you are using a rotation Background, it explain the '/2'. Rotation Bg only take 8bits for each entry(opposed to16bits for Text BG), you're then copying 2map entry per copy. This bring a problem though, 8bits mean that you can only access 256Tiles with the same BG, and you seem to be using more than that (from your Tiles Copy). You'll have to look into this (perhaps swithc to Text BG if you don't need scaling/rotation). Be aware that your Map Entries can use the same Tile as many time as you want, which mean even though it's 32x32, the number of Tiles used could be 1.


The code seems wrong though.On the Gba, maps are 32x32(or bigger if other settings), by only copying 30Map entries then immediately starting the next line, the first 2 Map entries of the second line will be added to te end of line 1(offfscreen) and so on for the others lines. Check if 'bgMap' is 32wide, else you will need ot pad with Map Entry 0 (or any other Blank Tile you are using).
_________________
-Inside every large program is a small program struggling to get out. (Hoare's Law of Large Programs)
-The man who can smile when things go wrong has thought of someone he can blame it on. (Nixon's Theorem)


Last edited by niltsair on Wed Apr 28, 2004 6:18 am; edited 1 time in total

#19936 - CyberSlag5k - Wed Apr 28, 2004 6:03 am

Right, I should have posted my values for back2:

Code:

   back2.number = 2;
   back2.charBaseBlock = 0;
   back2.screenBaseBlock = 28;
   back2.colorMode = BG_COLOR_256;
   back2.size = ROTBG_SIZE_256x256;
   back2.mosaic = 0;
   back2.x_scroll = 120;
   back2.y_scroll = 80;

_________________
When you find yourself in the company of a halfling and an ill-tempered Dragon, remember, you do not have to outrun the Dragon...

#19937 - sajiimori - Wed Apr 28, 2004 6:04 am

I say skip the code in the tutorials, and just get the basic information -- the concepts involved (control registers, palette, map format, tile format), the locations of the registers and memory areas, and the meanings of the various bitfields.

Then write your own code from scratch, the way you want it. VBA makes it easy to take small steps at a time, so you don't have to have a whole scrolling background up and running at once. You can write a program that just sets the video mode and the BG control registers, then hangs so you can use VBA to look at the state of things and make sure it's what you expected.

You can do the same after loading tile data and map data (though at that point you should have something on screen as well).

#19954 - dagamer34 - Wed Apr 28, 2004 10:06 pm

Here's a tutorial that deals with tiles, in text mode.

http://home.no/neogeo/HOVEDSIDE_INDEX/GBA_HOVEDSIDE_INDEX_ENGELSK/index.html

A word of caution: make sure you know how your tile data is supposed to be set up. As other people have said, text bgs are 16-bit where as rot bgs are 8-bit. That screwed my up several times. And rotation backgrounds can only have upto 256 tiles (all 256-colored), and there is not horizontal or vertical flipping. VRAM space gets used up very quickly, and that's why many games use text mode.

Other than that, I think you've got it.
_________________
Little kids and Playstation 2's don't mix. :(