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 display problem (solved)

#172704 - zelbo - Thu Feb 25, 2010 11:47 pm

Up til now, i've been using one function per background, but i'm trying to organize my code a bit better, since at some point i will be needing stuff like this to be more reusable. All of my sample maps are currently predefined constants, eventually they're going to be built on the fly.

the old way, that works:
in display.cpp:
Code:
int drawTerrain()
{
   int bgMap = bgInit(TERRAIN_LAYER, BgType_Text4bpp, BgSize_T_512x512, BG_TERRAIN_MB, BG_MAIN_TB);
   dmaCopy(testmap3, bgGetMapPtr(bgMap), sizeof(testmap3));
   return bgMap;
}

called from game.cpp with:
Code:
bgTerrain = drawTerrain();

I've got a copy of the whole mess for each background layer (terrain, items, fog of war)

now i'm trying to do something like this:
display.cpp:
Code:
int setBackground(int layer, const unsigned short* map, int mapbase, int tilebase)
{
   int background = bgInit(layer, BgType_Text4bpp, BgSize_T_512x512, mapbase, tilebase);
   dmaCopy(map, bgGetMapPtr(background), sizeof(map));
   return background;
}


game.cpp
Code:
bgTerrain = setBackground(TERRAIN_LAYER, testmap3, BG_TERRAIN_MB, BG_MAIN_TB);

also tried:
Code:
int setBackground(int layer, const unsigned short map[4096], int mapbase, int tilebase)

both of my attemps at changing it compile without error, but when i run it, i just get a purple background (my clear color), except for two tiles in the upper left corner of the screen (???). I imagine this is why i need to start using containers for my arrays.
Oh, and i initialize my backgrounds with this:
Code:
void initBackground()
{
   int bg = bgInit(TERRAIN_LAYER, BgType_Text4bpp, BgSize_T_512x512, BG_TERRAIN_MB, BG_MAIN_TB);
   dmaCopy(bgTiles, bgGetGfxPtr(bg), sizeof(bgTiles));
   dmaCopy(bgPal, BG_PALETTE, sizeof(bgPal));
}

which could probably be done differently. haven't messed with bgSetTileBase much yet.

Any ideas?


Last edited by zelbo on Fri Feb 26, 2010 5:22 am; edited 1 time in total

#172706 - Azenris - Fri Feb 26, 2010 12:29 am

sizeof(map) would give you the size of the pointer. which isn't what you want. I think.
_________________
My Homebrew Games

#172707 - zelbo - Fri Feb 26, 2010 12:34 am

ok, so you think it would work if i just changed the sizeof(map) to whatever the size actually is? I don't currently plan on changing the size of my maps, i'm pretty happy with 64x64 tiles. or maybe i need to pass that in to start with...

#172710 - sajiimori - Fri Feb 26, 2010 2:24 am

Try this:
Code:
struct Map
{
  unsigned short array[4096];  // Maybe use a 2D array instead?
};

int setBackground(int layer, const Map& map, int mapbase, int tilebase);

Then sizeof(map) will give the correct result.

In plain C, pass a const Map* instead, and use sizeof(*map).

#172712 - zelbo - Fri Feb 26, 2010 4:36 am

Interesting. I've fixed it for now by using a constant that represents the size of a map, since they're all the same, and i don't plan on changing them anytime soon. But i might try your method, sajiimori. I need to get in the habit of wrapping things. Haven't quite focused on that yet, but i do plan on using more containers for things like arrays.
As for making it 2D, i'm still undecided about that. My first sample maps were copies of dovoto's smileyface map from the all in one example that i made some changes to, so i've just been working with 1D arrays and doing some math when needed. I guess the math would be a bit of a slow-down, but thats only during level loads, so i'm not sure how much of an impact that has on everything. Do you think 2D arrays would be better?

#172725 - sajiimori - Fri Feb 26, 2010 8:03 pm

2D arrays aren't magic -- memory is 1D no matter what you do. Transforming between 2D and 1D indices requires the same math either way, so use whichever you prefer. I just like using 2D arrays when my data is conceptually 2D.

(But come to think of it, GBA background data is sometimes kinda 4D, when it's a 2D array of 2D screen blocks, rather than being linear.)