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 > Mode 0 tilemap problems

#6497 - OrangyTang - Tue May 27, 2003 9:46 pm

If I use TEXTBG_SIZE_256x256 for a mode 0 background, is this 256x256 pixels, or 256x256 tiles?

Assuming that its pixels, and so i'm using 8x8 tiles, whats wrong with this code snippit?:
Code:
   // Load level tile map (256x256 tile, 2 bytes at a time)
   count = 0;
   for(u16 loop=0; loop < 32*32/2; loop++)
   {
      u16 mapTile = (count << 8) + (count+1);
      layer0.mapData[loop] = mapTile;// index into 0-255 tile images.x2

      count += 2;
      if (count >= 255)
         count = 0;
   }


Where the Bg object is borrowed from GBA Junkies tutorials:
Code:
#ifndef BG_H
#define BG_H


///BGCNT defines ///
#define BG_MOSAIC_ENABLE      0x40
#define BG_COLOR_256         0x80
#define BG_COLOR_16         0x0

#define CharBaseBlock(n)      (((n)*0x4000)+0x6000000)
#define ScreenBaseBlock(n)      (((n)*0x800)+0x6000000)

#define CHAR_SHIFT         2
#define SCREEN_SHIFT         8
#define TEXTBG_SIZE_256x256      0x0
#define TEXTBG_SIZE_256x512      0x8000
#define TEXTBG_SIZE_512x256      0x4000
#define TEXTBG_SIZE_512x512      0xC000

#define ROTBG_SIZE_128x128      0x0
#define ROTBG_SIZE_256x256      0x4000
#define ROTBG_SIZE_512x512      0x8000
#define ROTBG_SIZE_1024x1024      0xC000

#define WRAPAROUND                 0x1

typedef struct Bg
{
   u16* tileData;
   u16* mapData;
   u8 mosaic;
   u8 colorMode;
   u8 number;
   u16 size;
   u8 charBaseBlock;
   u8 screenBaseBlock;
   u8 wraparound;
   s16 x_scroll,y_scroll;
   s32 DX,DY;
   s16 PA,PB,PC,PD;
}Bg;

#ifndef BG_C
extern void EnableBackground(Bg* bg);
extern void RotateBackground(Bg* bg, int angle, int center_x, int center_y, FIXED zoom);
extern void UpdateBackground(Bg* bg);
#endif

#endif// BG_H defined


The result i'm aiming for is to get all the tiles loaded displayed, so just running though the tile indices and resetting on 255. The actual result is black lines every other collumn (mistake with writing two at the same time?) and the bottom quater of the screen is still showing the 0 tile, as if my loop isn't long enough.

Any pointers? Thanks.

#6499 - niltsair - Tue May 27, 2003 10:01 pm

Each tile's entry in the map, takes 16bits.

Bit 0-9 : Tile number
Bit 10 : Horizaontal Flip
Bit 11: Vertical Flip
Bit 12-15 : Palette used (in 16colors mode only)

thus your loop shoulds look more like this :
Code:

// Load level tile map (32x32 tiles for a total of 256x256pixels Map)
   count = 0;
   for(u16 loop=0; loop < 32*32; loop++)
   {
        layer0.mapData[loop] = loop % 255;// index into 0-255 tile images.x2
   }

I simplified your loop. The % makes sure that your number is always running between 0 and 255.

Because 10bits are used for the tile's number, you actually have access to 0-1024 tiles, this mean you could replace the %255 by %1024.

Of course, in order to see something, you would had to have loaded the palette's entries and the Tile's entries in video memory, before trying to display a map.

#6500 - tepples - Tue May 27, 2003 10:13 pm

niltsair wrote:
Code:
layer0.mapData[loop] = loop % 255;// index into 0-255 tile images.x2

[...]
I simplified your loop. The % makes sure that your number is always running between 0 and 255.

You probably caught this right after you clicked submit, but it's % 256, or & 255.

Explanation: Taking n % d is a remainder after division. Where d is a power of 2, n % d = n & (d - 1) provided n and d are positive. But where d is not a power of 2, that's slow.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#6502 - niltsair - Tue May 27, 2003 10:30 pm

Oups :-) Yeah, i knew it should have been 0x100 (256).

What i never realized though, is that & 0xFF was faster, i shall use it from now on. Is it still faster than % on bases 2?

#6521 - OrangyTang - Wed May 28, 2003 9:20 am

Thanks, I was following the GbaJunkie tutorials, but while it notes the differences for warping a rotation background, it never mentions that text backgrounds are stored differently in the tile map :( I should have my palette and tile image data loaded correctly, so i'll give this a whirl when i get a chance.

Anyone know any good mode 0, 16 colour tile tutorials/info? Everything i've come across assumes you'll be using 256 always, or only rotation backgrounds.

#6531 - niltsair - Wed May 28, 2003 2:39 pm

Mmm, no, but there is small difference from a 256colors background.

Each pixels of Tile's Data now only takes 4bits. This mean that when you write to VideoMemory for it, you need to write 4 pixels at a time.

In your Map, for each Tile's Number Entry, you also need to specify which palette to use( between 0-15 ) a palette is simply a sub-range of 16 colors inside your 256 colors palette. So, Pal 0 = Color 0-15, Pal 1 = Color 16-31, Pal 6 = 96 - 111... The palette is specified in bits 12-15, so in the preceding example it would looks like
Code:

// Load level tile map (32x32 tiles for a total of 256x256pixels Map, 16colors Tiles)
    u8 iPal = 5;
   for(u16 loop=0; loop < 32*32; loop++)
   {
        layer0.mapData[loop] = (iPal<<12) | (loop % 256);// index into 0-255 tile images.x2
   }


(iPal<<12) : Specify which palette to use for this position in the Map.
(loop % 256) : Specify which tile to use for this position in the Map.
((loop % 16)<<12) | (loop % 256) : Assign both Palette and Tile number value to this position in the Map.

Take not that you can now use 2x Tiles because they each takes 1/2 spaces of 256colors.