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.

Graphics > Text Background Tilemaps

#21881 - faz99 - Tue Jun 08, 2004 7:28 pm

Hey,
I'm stuck on drawing onto backgrounds (Or rather the map data) in backgrounds that don't rotate.
If this has been asked before then direct me because i searched around...

I can get rotation backgrounds working fine they aren't so bad since the map is a linear array..
But according to The Pern Project:

"Unfortunately text backgrounds are not quite as simple. Size 0 (256x256) and size 2(256x512) still work as linear arrays but size 1 and 3 are broken into separate blocks."

But when i try to treat a size 0 map as a linear array on a text background, i just don't get the same outcome that i get when i do this with a rotational background.
I know it has to be something simple but i don't know what, frustrating stuff :)

Here's my code:

Code:

#include "gba.h"
#include "dispcnt.h"
#include "tilesetup.h"
#include "tiles.h"
#include "tilemap.c"
#include "keypad.h"


Bg bg2;

//wait for the screen to stop drawing
void WaitForVsync()
{
   while((volatile u16)REG_VCOUNT != 160){}
}

int main()
{
   int index = 0;  //generic loop variables
   u16 loop;
   u16* temp;      //temporary storage pointer

   //set mode 2 and enable sprites and 1d mapping
   SetMode(MODE_1);


   bg2.number = 1;            //background number 0-3
   bg2.charBaseBlock = 0;
   bg2.screenBaseBlock = 28;      //map data position on 2Kb boundary
   bg2.colorMode = BG_COLOR_256;           //256-colour background
   bg2.size = TEXTBG_SIZE_256x256;          //size of map
   bg2.mosaic = 0;                         //not enabled
   bg2.x_scroll = 0;         //scrolling variables
   bg2.y_scroll = 0;
   bg2.wraparound = WRAPAROUND;
   
   //Point to correct tile and map data, update the Background and Display registers accordingly
    EnableBackground(&bg2);



   for(loop = 0; loop < 256; loop++)
      BGPaletteMem[loop] = tilesPalette[loop];     //load the background palette into memory

   for(loop = 0; loop < tiles_WIDTH * tiles_HEIGHT /2; loop++)  //load tile image data
      bg2.tileData[loop] = tilesData[loop];

   //load the map image data
   temp = (u16*)tilemap;
   for(loop = 0; loop < 32 * 32 /2; loop++)
      bg2.mapData[loop] = temp[loop];

   //Main Game loop
   while(1)
   {
      
      WaitForVsync();               //waits for the screen to stop drawing
      UpdateBackground(&bg2); //make sure registers are updated if anything changes e.g. scrolling
      if (!(REG_P1 & KEY_UP))
            bg2.y_scroll += 1;
            
      
   }
   return(0);
}


I got most of this code from various tutorials around, so i just left all their comments in.
This is practically the same code i used for the rotational backgrounds except these changed..

bg2.number=1 => bg2.number=2
TEXTBG_SIZE... => ROTBG_SIZE...

And then i changed the key input so that is increments y_scroll instead of DX.

So.. What the hell's the problem? o_O
_________________
\(^_^)/

#21883 - Lupin - Tue Jun 08, 2004 8:03 pm

I am not sure what you are doing now, but for rotation backgrounds you use 8 bit values and for text backgrounds you have to use 16 bit values (bit 0-9 = tile ID; bit 10 = HF; bit 11 = VF; bit 12-15 = palette ID). Did you take care of the different format?
_________________
Team Pokeme
My blog and PM ASM tutorials

#21884 - faz99 - Tue Jun 08, 2004 8:13 pm

Lupin wrote:
I am not sure what you are doing now, but for rotation backgrounds you use 8 bit values and for text backgrounds you have to use 16 bit values (bit 0-9 = tile ID; bit 10 = HF; bit 11 = VF; bit 12-15 = palette ID). Did you take care of the different format?


Should this not sort it out?

Code:
temp = (u16*)tilemap;

_________________
\(^_^)/

#21890 - sajiimori - Tue Jun 08, 2004 9:05 pm

If tilemap is a u8[] where each element is a tile index, then casting it to u16* and reading an element will return a pair of tiles (which is probably not what you want). You can copy from a u8 to a u16 and the compiler will automatically pad the u8 with zeros (which is probably what you want).

Better yet, arrange the source data in text bg format and DMA it. Then you'll get the full range of 1024 tiles, instead of just 256, along with flags for flipping and such.

#21968 - ZeroX - Thu Jun 10, 2004 8:48 am

I'm thinking maybe, just maybe because you didnt enable the background in the REG_DISPCNT register. In your code you used Setmode(MODE_1) only. Try SetMode(MODE_1 | BG0_ENABLE), or whatever BG you're using. Check your dispcnt.h header file.

#21972 - faz99 - Thu Jun 10, 2004 3:35 pm

ZeroX wrote:
I'm thinking maybe, just maybe because you didnt enable the background in the REG_DISPCNT register. In your code you used Setmode(MODE_1) only. Try SetMode(MODE_1 | BG0_ENABLE), or whatever BG you're using. Check your dispcnt.h header file.


I did do that.. didn't include the function here, its done in... Updatebackground.



Quote:
If tilemap is a u8[] where each element is a tile index, then casting it to u16* and reading an element will return a pair of tiles (which is probably not what you want). You can copy from a u8 to a u16 and the compiler will automatically pad the u8 with zeros (which is probably what you want).

Interesting...
Anyway i think it has something to do with the format... it need to be a u16 but its a u8.
When i used MapEd it output it as a u16 and then it worked! \(^_^)/
But i still think its strange... Hm..
I think i have it sorted anyway im not really sure, if anything else comes uup i'll let you all know ;)
_________________
\(^_^)/

#21983 - sajiimori - Thu Jun 10, 2004 6:50 pm

What you need to understand is that these 3 arrays contain exactly the same data:
Code:

const u8 byte_array[] = { 0x0A, 0x0B, 0x0C, 0x0D };
const u16 hword_array[] = { 0x0B0A, 0x0D0C };
const u32 word_array[] = { 0x0D0C0B0A };

And that these expressions are true (assuming the arrays are aligned correctly):
Code:

((u32*) hword_array)[0] == word_array[0]
((u32*) byte_array)[0]  == word_array[0]
((u16*) word_array)[0]  == hword_array[0]
((u16*) byte_array)[0]  == hword_array[0]
((u8*) word_array)[0]   == byte_array[0]
((u8*) hword_array)[0]  == byte_array[0]

It's a matter of perspective -- an array is just a string of bits in the end.