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.

Coding > tile modes

#3865 - Ninj4D4n - Tue Mar 11, 2003 3:38 am

Hi all,
I'm trying to work with multiple backgrounds and i'm running into some trouble. To start i only wanted to load one map....Sometimes i can see the map and sometimes i get garbage, only changing the background used for the and display mode here's what i get

Mode0
bg0: garbage
bg1: garbage
bg2: garbage
bg3: garbage

Mode1
bg0: garbage
bg1: garbage
bg2: OK

Mode2
bg2: OK
bg3: OK

Does anybody know what's going on here? I think it has something to do with text vs rot backgrounds.

I want to be able to use mode0 because i would like to eventually have 4 layers with transparency working.

--Ninj4D4n

#3866 - jammin.won - Tue Mar 11, 2003 3:52 am

it looks like all the working bg is with r/s ....so i guess if u've set the r/s bit in BG*CNT ...

and all r/s bg is with 256 colors only ... so dont forget this point...

btw i'm a newbie still dunno if the suggestion will work ... -______-
_________________
[Images not permitted - Click here to view it]

#3867 - tepples - Tue Mar 11, 2003 6:17 am

Ninj4D4n wrote:
Sometimes i can see the map and sometimes i get garbage, only changing the background used for the and display mode here's what i get

(non-rot/scale bgs: garbage)
(rot/scale bgs: ok)

Are you making sure to turn on 256 color mode in the BG?CNT registers for the non-rot/scale backgrounds?

Are you making sure to write one tile per 16 bits (not per 8 bits) to the map, and to write only 32x32 tiles at a time? (Larger maps are created by replacing the map entries at the seam of the scroll.)
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#3877 - Ninj4D4n - Tue Mar 11, 2003 2:29 pm

I think i've got all of that stuff set up right... here's some of my code

main file
Code:

#include "gba.h"      //gba regisers
#include "keypad.h"      //input register
#include "screenmode.h"   //screen mode register
#include "sprite.h"      //sprite definitions
#include "bg.h"
#include "trig.h"      //sin and cos tables
#include "timers.h"


//#include "intro2.h"
#include "gfxh/tiles.h"
#include "gfxh/tilespalette.h"
#include "gfxh/Flash.c"

void GetInput(void);
Bg mask;

int main()
{
   //setup for random number generator
   REG_TM3CNT = TIME_FREQUENCY_1024 | TIME_ENABLE;
   REG_TM3D = 0;

   SetMode(MODE_1 | OBJ_ENABLE | OBJ_MAP_1D); //set mode 1 and enable sprites and 1d mapping
   InitializeSprites();



   mask.number = 2;
   mask.charBaseBlock = 0;     //tiles in 3
   mask.screenBaseBlock = 28;   //mask in 0 (above)
   mask.colorMode = BG_COLOR_256;
   mask.size = ROTBG_SIZE_256x256;
   mask.mosaic = 0;
   mask.x_scroll = 0;
   mask.y_scroll = 0;

   EnableBackground(&mask);

   u16 loop;
   //load palette
   for(loop = 0; loop < 256; loop++)
      BGPalette[loop] = tilesPalette[loop];

   //load tile set; charbase block 0
   for(loop = 0; loop < tiles_WIDTH * tiles_HEIGHT /2; loop++)
      mask.tileData[loop] = tilesData[loop];

   //load maps
   //mask in screen base block 28
   u16* tempData = (u16*)Flash;  //16bit transfer
   for(loop = 0; loop < 32*32/2; loop++)
      mask.mapData[loop] = tempData[loop];
   ////////////////////////////////////////
   //start the game loop
   while(1)
   {

   }
   return 0;
}


bg.h
Code:

#include "screenmode.h"
#include "gba.h"
#include "trig.h"

#ifndef BG_H
#define BG_H


///BGCNT defines ///
#define BG_MOSAIC_ENABLE      0x40
#define BG_COLOR_256         0x80
#define BG_COLOR_16            0x0
//macros to point to mememory
#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

u16* BGPalette     =(u16*)0x5000000;

//structure for a background
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;

//turns on a background
void EnableBackground(Bg* bg)
{
   u16 temp;

   bg->tileData = (u16*)CharBaseBlock(bg->charBaseBlock);
   bg->mapData = (u16*)ScreenBaseBlock(bg->screenBaseBlock);
   temp = bg->size | (bg->charBaseBlock<<CHAR_SHIFT) | (bg->screenBaseBlock<<SCREEN_SHIFT)
      | bg->colorMode | bg->mosaic | bg->wraparound;

   switch(bg->number)
   {
   case 0:
      {
         REG_BG0CNT = temp;
         REG_DISPCNT |= BG0_ENABLE;
      }break;
   case 1:
      {
         REG_BG1CNT = temp;
         REG_DISPCNT |= BG1_ENABLE;
      }break;
   case 2:
      {
         REG_BG2CNT = temp;
         REG_DISPCNT |= BG2_ENABLE;
      }break;
   case 3:
      {
         REG_BG3CNT = temp;
         REG_DISPCNT |= BG3_ENABLE;
      }break;

   default:break;

   }
}

//clears the background registers
void ClearBackgrounds(void)
{
   REG_BG0CNT = 0x0000;
   REG_BG1CNT = 0x0000;
   REG_BG2CNT = 0x0000;
   REG_BG3CNT = 0x0000;
   REG_DISPCNT = 0x0000;
}

//update the background
void UpdateBackground(Bg* bg)
{
   switch(bg->number)
   {
   case 0:
      REG_BG0HOFS = bg->x_scroll;
      REG_BG0VOFS = bg->y_scroll;
      break;
   case 1:
      REG_BG1HOFS = bg->x_scroll;
      REG_BG1VOFS = bg->y_scroll;
      break;
   case 2:
      if(!(REG_DISPCNT & MODE_0))//it is a rot background
      {
         REG_BG2X = bg->DX;
         REG_BG2Y = bg->DY;

         REG_BG2PA = bg->PA;
         REG_BG2PB = bg->PB;
         REG_BG2PC = bg->PC;
         REG_BG2PD = bg->PD;
      }
      else  //it is a text background
      {
         REG_BG2HOFS = bg->x_scroll;
         REG_BG2VOFS = bg->y_scroll;
      }
      break;
   case 3:
      if(!(REG_DISPCNT & MODE_0))//it is a rot background
      {
         REG_BG3X = bg->DX;
         REG_BG3Y = bg->DY;

         REG_BG3PA = bg->PA;
         REG_BG3PB = bg->PB;
         REG_BG3PC = bg->PC;
         REG_BG3PD = bg->PD;
      }
      else //it is a text background
      {
         REG_BG3HOFS = bg->x_scroll;
         REG_BG3VOFS = bg->y_scroll;
      }
      break;
   default: break;
   }
}

//rotate background
void RotateBackground(Bg* bg, int angle,int center_x, int center_y, FIXED zoom)
{

   center_y = (center_y * zoom)>>8;
   center_x = (center_x * zoom)>>8;

   bg->DX = ((bg->x_scroll<<8)-center_y*SIN[angle]-center_x*COS[angle]);
   bg->DY = ((bg->y_scroll<<8)-center_y*COS[angle]+center_x*SIN[angle]);

   bg->PA = (COS[angle]*zoom)>>8;  //cos&sin are LUTs that are .8 fixed numbers
   bg->PB = (SIN[angle]*zoom)>>8;  //zoom is also fixed
   bg->PC = (-SIN[angle]*zoom)>>8;
   bg->PD = (COS[angle]*zoom)>>8;
}

#endif



i don't know if that will help anyone tell me what's wrong

--Ninj4D4n

#3890 - tepples - Tue Mar 11, 2003 9:46 pm

"Jumbled" and "scrambled" don't help much. A screenshot would help more.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#3911 - Ninj4D4n - Wed Mar 12, 2003 7:00 pm

By "garabge", i mean that the image does use tiles from my tileset, but that the tiles aren't the right image, and they're not in anysort of pattern related to the original image.

Now i know this has to do with the differences between text and rotational backgrounds. So i guess my question is, how does the format of each differ? and how does the loading/setup of each differ?

--Ninj4D4n

#3916 - pollier - Wed Mar 12, 2003 8:02 pm

Quote:
Now i know this has to do with the differences between text and rotational backgrounds. So i guess my question is, how does the format of each differ?


Rotation maps use 8-bit data (8 bits for tile index), while text maps use 16-bit (9 or 10 bits for tile index, plus flags like vertical and horizontal flipping) Trying to load rotation map data into the text maps will defintely produce garbage; either reconvert your map data to 16-bit, or maybe changing u16* tempData to u8* tempData might cut it.
_________________
(Works for me!)

#3919 - jenswa - Wed Mar 12, 2003 8:39 pm

maybe if you changed ROTBG_SIZE_ into TEXTBG

you can only use 2 rot backgrounds as max and 4 text backgrounds.

in mode 0 there are no rot backgrounds, so that's
probably why you get garbage,
in mode 1 there is one rot background, bg2, that's
why you can see bg2 and the others are garbage,
in mode 2 there are two rot backgrounds bg2 and bg3
and that's the reason why you see both of the backgrounds right,
you're using rotational maps instead of textmaps.

so simply change it into text background or else i would
say start over in keep in mind that you should use text backgrounds.
but simply changing rot into text might work.

hope this helps a bit

JJ
_________________
It seems this wasn't lost after all.

#3922 - Ninj4D4n - Wed Mar 12, 2003 10:02 pm

and we have a winner....
Thanks polier.. that was the problem. Thanks everyone for the help.

--Ninj4D4n