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.

DS development > 2D on top, 3D on the bottom, text somewhere in between?

#159448 - ragefury32 - Tue Jul 01, 2008 3:36 am

Okay, I am trying to modify one of the NeHe demos to draw the 3D texture mapped box with full rotation (Lesson 8) on the bottom screen, and I am trying to draw a 256x256 logo on the background on the top screen, while displaying the xrot, yrot and zrot parameters using the top screen above. So here's what I got so far:

// Turn on everything
powerON(POWER_ALL);
// Setup the Main screen for 3D
videoSetMode(MODE_0_3D);
videoSetModeSub(MODE_5_2D | DISPLAY_BG3_ACTIVE);
lcdMainOnBottom();
vramSetMainBanks(VRAM_A_TEXTURE, VRAM_B_LCD, VRAM_C_SUB_BG, VRAM_D_LCD);
vramSetBankA(VRAM_A_TEXTURE);
vramSetBankC(VRAM_C_SUB_BG);
BG3_CR = BG_BMP8_256x256;
dmaCopy(drunkenlogo_dta, BG_GFX, 256*256);
dmaCopy(palette_dta, BG_PALETTE, 256*2);
irqInit();
irqSet(IRQ_VBLANK, 0);
consoleDemoInit();
glInit();
glEnable(GL_OUTLINE | GL_TEXTURE_2D);
glSetOutlineColor(0,RGB15(31,31,31));
...and etc, etc, etc.

The OpenGL worked, the text console worked, the background does not seem to want to materialize. What did I do wrong here?

#159451 - silent_code - Tue Jul 01, 2008 10:40 am

Are you sure you're writing the image to the right location? Take a look at what consoleDemoInit() does, too.

Also: check out the "Volumetric Shadow Demo" (sources are free to download) on my web site and see how I did it. That might help. :^)

Good luck!
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#159452 - eKid - Tue Jul 01, 2008 1:03 pm

I believe consoleDemoInit sets up the video mode too. You'll have to set it the console using the more flexible consoleInitDefault(), or even more flexible consoleInit().

#159453 - elhobbs - Tue Jul 01, 2008 1:21 pm

BG_GFX and BG_PALETTE are for the main backgrouund. you need to use BG_GFX_SUB and BG_PALETTE_SUB. you also need to setup the background a little more. this will setup an 8 bit 256x256 background on bg3:
Code:

SUB_BG3_CR = BG_BMP8_256x256|BG_TILE_BASE(0)|BG_PRIORITY_0;
SUB_BG3_XDX = 1<<8;
SUB_BG3_YDY = 1<<8;
SUB_BG3_XDY = 0;
SUB_BG3_YDX = 0;
SUB_BG3_CX = 0;
SUB_BG3_CY = 0;

#159459 - ragefury32 - Tue Jul 01, 2008 3:46 pm

Thanks guys -

I note that I can get it to work if I do:


Code:

SUB_BG3_CR = BG_BMP8_256x256 | BG_PRIORITY(0) | BG_TILE_BASE(4);
   SUB_BG3_XDY = 0;
   SUB_BG3_XDX = 1 << 8;
   SUB_BG3_YDX = 0;
   SUB_BG3_YDY = 1 << 8;
   SUB_BG3_CX = 0;
    SUB_BG3_CY = 32 << 8;
   dmaCopy(drunkenlogo_dta, BG_GFX_SUB, 256*256);
   dmaCopy(palette_dta, BG_PALETTE_SUB, 256*2);
   irqInit();
   irqSet(IRQ_VBLANK, 0);


And that'll get me the logo on top. However, I am not sure how to do the ConsoleInit()...Does libnds have an easily accessible online reference?

#159460 - silent_code - Tue Jul 01, 2008 3:56 pm

It does have easily accessible source code. ;^)
But no, afaIk, there is no documentation, but you can build one from the source code comments yourself, e.g. with doxygen. :^)
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#159462 - ragefury32 - Tue Jul 01, 2008 4:27 pm

silent_code wrote:
It does have easily accessible source code. ;^)
But no, afaIk, there is no documentation, but you can build one from the source code comments yourself, e.g. with doxygen. :^)


Okay. So I went through your volumetric shadow demo (great work BTW) and I came across this inside initialize.cpp:

Code:


   videoSetModeSub(MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG3_ACTIVE);
   SUB_BG0_CR = BG_256_COLOR | BG_MAP_BASE(31) | BG_TILE_BASE(4);
   SUB_BG3_CR = BG_BMP8_256x256 | BG_PRIORITY(3)

(Does the usual scaling code for the the Mode 5 stuff on BG3)

consoleInit((uint16 *)pBuffer, (uint16 *)CHAR_BASE_BLOCK_SUB(4), 95, 32, (uint16 *)SCREEN_BASE_BLOCK_SUB(31), 0, 8)



Let me see if I read this correctly. BG3 is for the background pbi files, and BG0 is primarily the text layer. BG3 gets priority so it'll get drawn first, followed by BG0 on top.

So looking at the BG0 Control Register setup, the layer is 256 Color with the mapbase at 31 and tilebase is 4, so during consoleInit - pBuffer holds the the font bitmaps, 4 holds the fonts in VRAM_C, I got 95 characters starting at ASCII 32, it's going to Mapbase 31, Palette 0, 8 bit (256 color). Correct?

So tilebase holds my font, and mapbase points back to the layer that I want the fonts to display at?

I tried to hack up the DrunkCoders 2D demo so it'll do 2 screens with the text console on top. No dice, it seems.

#159473 - silent_code - Tue Jul 01, 2008 7:05 pm

If I didn't misunderstand anything: Exactly. :^)

And thanks! :^D

PS: .pbi is my own crappy hackish "get it to work" format and by no means any standard, but better than raw data, I guess. ;^)

EDIT: What did you try? Could you post the relevant parts of the source? What exactly is not working?
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#159480 - ragefury32 - Tue Jul 01, 2008 9:44 pm

I saw this from your code:

Code:

   // assigne video ram banks
   vramSetBankA(VRAM_A_TEXTURE_SLOT0);
   vramSetBankB(VRAM_B_TEXTURE_SLOT1);
   vramSetBankC(VRAM_C_SUB_BG_0x06260000);
   vramSetBankD(VRAM_D_LCD);      // The captured image will be put here, so it *must* be set to LCD mode


But I can't for the life of me figure out why VRAM_C_SUB_BG is set to 0x06260000 instead of 0x06200000.

Okay. So basically, I tried towhat I did was to hack up one of the Drunkcoders.org 2D demos. Here's what I was trying to do:

a) Top screen is standard 2D Mode 5, background loaded on BG3.
b) Bottom screen is Mode 5, background on BG3, text on BG0.

I figured that this should work if 0 gets priority over 3.

For some reason, the code didn't exactly work. You can kind of see the background image, but it seems that layer 0 seems to overwrite it with crap. Is that an issue with the default font, or did I really screw something up here?

Here's a screenshot from iDeaS:

[Images not permitted - Click here to view it]

And here is the code I cooked up -

Code:


#include <nds.h>
#include <stdio.h>

// git adds a nice header we can include to access the data
// this has the same name as the image

#include "drunkenlogo.h"

int main(void)
{
   // irqs are nice
   irqInit();
   irqSet(IRQ_VBLANK, 0);

    // set the mode for 2 text layers and two extended background layers
   videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE);

   // set the sub background up for text display (we could just print to one
   // of the main display text backgrounds just as easily
   videoSetModeSub(MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG3_ACTIVE); //sub bg 0 will be used to print text

    // set the first bank as background memory and the third as sub background memory
    // B and D are not used
    vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_LCD,
                      VRAM_C_SUB_BG_0x06200000, VRAM_D_LCD);

   
   
   BG_PALETTE_SUB[255] = RGB15(31,31,31);//by default font will be rendered with color 255

   // consoleInit() is a lot more flexible but this gets you up and running quick

   BG3_CR = BG_BMP8_256x256 | BG_PRIORITY(3);
   SUB_BG0_CR = BG_MAP_BASE(31) | BG_PRIORITY(0);
    SUB_BG3_CR = BG_BMP8_256x256;


   consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(31), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);

   iprintf("\n\n\tHello DS developers\n");
   iprintf("\twww.drunkencoders.com\n");
   iprintf("\t256 color bitmap demo");
 
   // set up our bitmap background

   dmaCopy(drunkenlogoBitmap, BG_GFX, 256*256);
   dmaCopy(drunkenlogoPal, BG_PALETTE, 256*2);

   // these are rotation backgrounds so you must set the rotation attributes:
    // these are fixed point numbers with the low 8 bits the fractional part
    // this basicaly gives it a 1:1 translation in x and y so you get a nice flat bitmap
        BG3_XDX = 1 << 8;
        BG3_XDY = 0;
        BG3_YDX = 0;
        BG3_YDY = 1 << 8;
    // our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2
        BG3_CX = 0;
        BG3_CY = 32 << 8;


   dmaCopy(drunkenlogoBitmap, BG_GFX_SUB, 256*256);
   dmaCopy(drunkenlogoPal, BG_PALETTE_SUB, 256*2);

       SUB_BG3_XDX = 1 << 8;
        SUB_BG3_XDY = 0;
        SUB_BG3_YDX = 0;
        SUB_BG3_YDY = 1 << 8;

    // our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2
        SUB_BG3_CX = 0;
        SUB_BG3_CY = 32 << 8;

    while(1)swiWaitForVBlank();

   return 0;
}


#159561 - ragefury32 - Wed Jul 02, 2008 6:07 pm

Never mind. I figured it out - I think the DMA copy demolished BG0.

I reversed the order so the background gets copied in first (this is fine as it is a static background), and then the ConsoleDefaultInit() kicks in afterwards.

I still don't know WHY silentCode's demo uses 0x0626000 as the base for BG0 though. Is it because of TileBase(4) used for the custom font load?

#159573 - silent_code - Wed Jul 02, 2008 6:54 pm

Duh, I really don't remember, it's been nearly (or even over) a year I have set that up... I'd say "Because it works"... but your guess sounds quite convincing. :^)
No really, I think it was because I was getting garbage all the time and then figured out that using an offset fixed it.
_________________
July 5th 08: "Volumetric Shadow Demo" 1.6.0 (final) source released
June 5th 08: "Zombie NDS" WIP released!
It's all on my page, just click WWW below.

#159578 - elhobbs - Wed Jul 02, 2008 7:23 pm

ragefury32 wrote:
Never mind. I figured it out - I think the DMA copy demolished BG0.

I reversed the order so the background gets copied in first (this is fine as it is a static background), and then the ConsoleDefaultInit() kicks in afterwards.

I still don't know WHY silentCode's demo uses 0x0626000 as the base for BG0 though. Is it because of TileBase(4) used for the custom font load?
consoleDemoInit which maps VRAM_C to 0x06200000 and puts the map at 31*2048 and the tiles at 0. so you are already using 6200000-620ffff for the console. you are then mapping the background to 0x6200000. so they overlap.

this is the configuration that I would recommend:
Code:

//set the tiles at 6200000
//the the map at 6207800
SUB_BG0_CR = BG_32x32 | BG_COLOR_16 | BG_MAP_BASE(15) | BG_TILE_BASE(0) | BG_PRIORITY_0;
BG_PALETTE_SUB[0]=0;
BG_PALETTE_SUB[255]=0xff00;
consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(15), (u16*CHAR_BASE_BLOCK_SUB(0), 16);

//set the sub bg3 at 6208000
SUB_BG3_CR = BG_BMP8_256x256 | BG_TILE_BASE(2) | BG_PRIORITY_1;
SUB_BG3_XDX = 1<<8;
SUB_BG3_YDY = 1<<8;
SUB_BG3_XDY = 0;
SUB_BG3_YDX = 0;
SUB_BG3_CX = 0;
SUB_BG3_CY = 0;

//move the background image into bg3
dmaCopy(drunkenlogoBitmap, BG_BMP_RAM_SUB(2), 256*256);
dmaCopy(drunkenlogoPal, BG_PALETTE, 256*2);

#159636 - ragefury32 - Thu Jul 03, 2008 11:32 am

elhobbs wrote:
ragefury32 wrote:
Never mind. I figured it out - I think the DMA copy demolished BG0.

I reversed the order so the background gets copied in first (this is fine as it is a static background), and then the ConsoleDefaultInit() kicks in afterwards.

I still don't know WHY silentCode's demo uses 0x0626000 as the base for BG0 though. Is it because of TileBase(4) used for the custom font load?
consoleDemoInit which maps VRAM_C to 0x06200000 and puts the map at 31*2048 and the tiles at 0. so you are already using 6200000-620ffff for the console. you are then mapping the background to 0x6200000. so they overlap.

this is the configuration that I would recommend:
Code:

//set the tiles at 6200000
//the the map at 6207800
SUB_BG0_CR = BG_32x32 | BG_COLOR_16 | BG_MAP_BASE(15) | BG_TILE_BASE(0) | BG_PRIORITY_0;
BG_PALETTE_SUB[0]=0;
BG_PALETTE_SUB[255]=0xff00;
consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(15), (u16*CHAR_BASE_BLOCK_SUB(0), 16);

//set the sub bg3 at 6208000
SUB_BG3_CR = BG_BMP8_256x256 | BG_TILE_BASE(2) | BG_PRIORITY_1;
SUB_BG3_XDX = 1<<8;
SUB_BG3_YDY = 1<<8;
SUB_BG3_XDY = 0;
SUB_BG3_YDX = 0;
SUB_BG3_CX = 0;
SUB_BG3_CY = 0;

//move the background image into bg3
dmaCopy(drunkenlogoBitmap, BG_BMP_RAM_SUB(2), 256*256);
dmaCopy(drunkenlogoPal, BG_PALETTE, 256*2);


Hm. I tried that...were you trying to move the DMACopy in the lower screen to BG_BMP_RAM_SUB(2) (which requires an integer pointer cast)? When I tried the code the DMACopy on the lower screen will still clobber BG0. In fact, the only thing that moving the DMACopy on the lower screen SEEMS to do is to move the bitmap up.

#159639 - elhobbs - Thu Jul 03, 2008 1:46 pm

this is what I get for not testing my code. I used the wrong macro for setting up bg3

change
Code:

SUB_BG3_CR = BG_BMP8_256x256 | BG_TILE_BASE(2) | BG_PRIORITY_1;

to
Code:

SUB_BG3_CR = BG_BMP8_256x256 | BG_BMP_BASE(2) | BG_PRIORITY_1;


sorry about that. I tested it and it really works this time. to avoid further confusion here is the entire code I used:
Code:

#include <nds.h>
#include <stdio.h>

// git adds a nice header we can include to access the data
// this has the same name as the image

#include "drunkenlogo.h"

int main(void)
{
   // irqs are nice
   irqInit();
   irqSet(IRQ_VBLANK, 0);

    // set the mode for 2 text layers and two extended background layers
   videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE);

   // set the sub background up for text display (we could just print to one
   // of the main display text backgrounds just as easily
   videoSetModeSub(MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG3_ACTIVE); //sub bg 0 will be used to print text

    // set the first bank as background memory and the third as sub background memory
    // B and D are not used
    vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_LCD,
                      VRAM_C_SUB_BG_0x06200000, VRAM_D_LCD);

   
   
//set the tiles at 6200000
//the the map at 6207800
SUB_BG0_CR = BG_32x32 | BG_COLOR_16 | BG_MAP_BASE(15) | BG_TILE_BASE(0) | BG_PRIORITY_0;
BG_PALETTE_SUB[0]=0;
BG_PALETTE_SUB[255]=0xff00;
consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(15), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);

//set the sub bg3 at 6208000
SUB_BG3_CR = BG_BMP8_256x256 | BG_BMP_BASE(2) | BG_PRIORITY_1;
SUB_BG3_XDX = 1<<8;
SUB_BG3_YDY = 1<<8;
SUB_BG3_XDY = 0;
SUB_BG3_YDX = 0;
SUB_BG3_CX = 0;
SUB_BG3_CY = 0;

//move the background image into bg3
dmaCopy(drunkenlogoBitmap,(void*) BG_BMP_RAM_SUB(2), 256*256);
dmaCopy(drunkenlogoPal, BG_PALETTE_SUB, 256*2);

iprintf("Hello World\n");

    while(1)swiWaitForVBlank();

   return 0;
}

#159734 - ragefury32 - Sat Jul 05, 2008 3:40 pm

Okay, so basically, you are pushing the starting address of SUB_BG0 from BG_MAP_BASE(31) to BG_MAP_BASE(15) (essentially pushing it up in memory) and SUB_BG3 to start from BG_MAP_BASE(0) to BG_MAP_BASE(2) so it can allocate the memory space for the text console.

So if I read this correctly, the base units are 2k each, so you are essentially moving the SUB_BG0's memory allocation up 32k, and SUB_BG3 down 4K, freeing up 36k for the console. Is that right?

#159749 - elhobbs - Sun Jul 06, 2008 4:21 am

not exactly. you are correct about the map base units, the slots are 2k for the text background slot size for the character map. the character tile base uses a 16k slot size though. the console is located in the first 32k of sub background memory. the bitmap background slot size if 16k. so slot 2 is at 32k. If you cheat a little this leaves enough room in VRAM_C for another 256 color bitmap sub background at slot 5. this would actually overlap the two 256 color 256x256 bitmaps, but if you only use 256x192 bytes for each with no scrolling then you wont have an issue.