Page 1 of 1
Background layering (or: how to get backgrounds to overlap)
Posted: Tue Apr 05, 2011 1:51 am
by thebobwell
Hello, I'm new here and to DS programming in general, but I'm curious about the possibility of having it so the top screen will display two backgrounds at once, with one atop the other. I know it's possible because I've seen example code on the matter, but whenever I made an attempt at this I seemed to fail. The closest I got was getting it so the top layer (which had transparency written into GRIT) would blend with the first color it saw from the bottom layer (which was a gradient) and I figure that's a start but I'm pretty positive I'm missing something very important here. Here's what I wrote in terms of the two layers:
Code: Select all
videoSetMode(MODE_5_2D);
videoSetModeSub(MODE_5_2D);
vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
vramSetBankC(VRAM_C_SUB_BG_0x06200000);
int bg3 = bgInit(3,BgType_Bmp8,BgSize_B8_256x256,2,0); //background background
bgSetPriority(bg3,3);
bgUpdate();
dmaCopy(topwallBitmap,bgGetGfxPtr(bg3),topwallBitmapLen);
dmaCopy(topwallPal,BG_PALETTE,topwallPalLen);
int bg2 = bgInit(3,BgType_Bmp16,BgSize_B16_256x256,2,0); //foreground background
dmaCopy(goldBitmap,bgGetGfxPtr(bg2),goldBitmapLen);
Excuse me if my code seems very amateur, in my defense I am one. XD
Thank you for your help!
Re: Background layering (or: how to get backgrounds to overl
Posted: Tue Apr 05, 2011 4:20 am
by elhobbs
you need to assign more vram to the main screen - you only have VRAM_A mapped so far for MAIN_BG. that is not enough for two backgrounds. you are also initialing backgrounds 2 and 3 with the same offset: 2; you will need to change one of the bgInit calls.
Re: Background layering (or: how to get backgrounds to overl
Posted: Tue Apr 05, 2011 6:20 pm
by thebobwell
Ah, I got it working now. I'm going to try experimenting with bgInit now to learn more on it, but that definitely helped get me in the right direction. Thank you very much!
Re: Background layering (or: how to get backgrounds to overl
Posted: Tue Apr 05, 2011 8:20 pm
by mtheall
There are several things wrong with your setup.
1.) You do not have enough VRAM allocated (like elhobbs said). Your first background is 256x256 8bpp bitmap; this takes 64KB of memory. Your second background is 256x256 16bpp bitmap; this takes 128KB of memory. This totals to 192KB of memory. VRAM Banks A-D are each 128KB. You should allocate Bank A as VRAM_A_MAIN_BG and Bank B as VRAM_B_MAIN_BG. This will give you two contiguous blocks of 128KB.
2.) bg2 and bg3 are both inited as Background Layer 3; they need to point to separate layers. Effectively you have only set up one background. Since they are both bitmaps, they have to be Layers 2 and 3. Mode 5 is the correct mode for using two bitmaps.
3.) bg2 and bg3 are both inited into Map Base 2. This means that they point to the same location in memory. When you copy bg2 into VRAM, it actually overwrites the data for bg3. If you only fixed problems 1 and 2, your VRAM would be mapped as follows:
http://theall.dyn-o-saur.com/vram.php?M ... x256&MB3=2
Please use the worksheet to allocate your VRAM. It will help you understand how the parameters in bgInit map your VRAM.
Re: Background layering (or: how to get backgrounds to overl
Posted: Tue Apr 05, 2011 9:46 pm
by elhobbs
that worksheet is very nice - I just looked at it quickly. I can never remember which is 2k, 16k, 64k... looks very helpful.
your link was bad though - should be 2 and 6 for the map offsets - not 2 and 2.
Re: Background layering (or: how to get backgrounds to overl
Posted: Tue Apr 05, 2011 10:51 pm
by mtheall
elhobbs: I used 2 and 2 to exemplify what his code was doing, hence the "If you only fixed problems 1 and 2." I left it as an exercise for thebobwell to figure out how to fix by experimenting with different settings on the worksheet.
Re: Background layering (or: how to get backgrounds to overl
Posted: Tue Apr 05, 2011 11:42 pm
by elhobbs
Yep, should have read it again instead of assuming you made a mistake. Sorry about that.
Re: Background layering (or: how to get backgrounds to overl
Posted: Wed Apr 06, 2011 7:43 am
by thebobwell
Wow, that is an amazingly helpful link you sent, though I saw the folly in my code beforehand thanks to the help of elhobbs. I will still keep that link since it certainly has incredible uses. Thanks very much all!
Re: Background layering (or: how to get backgrounds to overl
Posted: Fri Apr 15, 2011 2:49 pm
by sverx
interesting indeed, but I think it isn't yet supporting 512x512 16bpp modes (they need 512KB) and it's showing only 256KB for 1024x512 and 512x1024 8 bpp modes (they again need 512KB)