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 > Having problems with tile-based mode in DS

#48364 - TaiyouHKabra - Tue Jul 19, 2005 3:47 pm

Hi

I'm starting to program for Nintendo DS and I'm a bit confused with the new VRAM distribution and stuff.

I've searched around the forums but I haven't found answers for the problems I'm having now.


Code:

#include <NDS/NDS.h>


#include "cuadr.h"
#include "cuadr_map.h"
#include "cuadr_pal.h"

int main(void)
{
   videoSetMode(0); //not using the main screen
   videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE);
   vramSetBankC(VRAM_C_SUB_BG);

   SUB_BG0_CR = BG_MAP_BASE(0);
   
   dmaCopy(cuadr, BG_GFX, 512*8);
   dmaCopy(cuadr_pal, BG_PALETTE, 512*8);
   dmaCopy(cuadr_map, (uint16*)SCREEN_BASE_BLOCK_SUB(0), 512*2);
   
   while(1)
   {
   }
   return 0;
}



What I'm trying to do is to put a tile-based background on screen, but I don't know where to put the map of the tiles.

cuadr is the binary of the gfx. cuadr_pal is the palette and cuadr_map is the map for the background.

If someone can explain me how this stuff works in NDS it'd help me a lot, I'm quite stuck. Thanks.


Taiyou

#48366 - Sausage Boy - Tue Jul 19, 2005 3:55 pm

I don't have access to my sources right now, and I don't know these things by heard, but my guess is that you should copy the palette to BG_PALETTE_SUB and the gfx to (uint16*)CHAR_BASE_BLOCK_SUB(something).

You can find some good stuff about base blocks and stuff at http://www.gbajunkie.co.uk/ .
Even though it's for GBA, most of it applies to the DS as well.

Good luck!
_________________
"no offense, but this is the gayest game ever"

#48367 - TheMikaus - Tue Jul 19, 2005 4:00 pm

if you're going to use BG_MAP_BASE(0) I think it starts at BASE_BLOCK(28 or 29) not 0 (while this will work on an emulator it will not work on hardware)

Also you'll probably want to initialize the SUB_BG0_CR a little bit more than just telling it the Map base. You'll have to set the tile space and such.

I know I'm only throwin' you vague answers but I don't fully remember off the top of my head without looking at code and I don't have it on me right now. But a good thing to do is read tepples GBA Faq (since the GBA and the NDS are pretty similar). I know that someone will have a better answer. but that's the best I can do now. Sorry :(

Also another thing you can use it J. Harbour's GBA book. It's usefull. Last I checked(several months ago) it was a little outdated, but I think it's a decent start with backgrounds.

#48368 - TaiyouHKabra - Tue Jul 19, 2005 4:36 pm

Thanks for your kind answers.

I'm familiar with GBA tilebased modes, I have experience with them but I don't fully understand the new layout of the VRAM in DS...

Where should I send the gfx data, gfx palette and gfx map in this case? I don't know how to set tile space nor other parameters to BG0_CR, and I haven't found any examples which do the same I'm trying to do.

Thanks in advance for your help.


Taiyou

#48371 - Sausage Boy - Tue Jul 19, 2005 4:46 pm

It works the same, only the names are different. If you've programmed on the gba without ham or something, it should be fairly obvious.

Your stuff looks pretty good to me, except that

dmaCopy(cuadr, BG_GFX, 512*8);
dmaCopy(cuadr_pal, BG_PALETTE, 512*8);
dmaCopy(cuadr_map, (uint16*)SCREEN_BASE_BLOCK_SUB(0), 512*2);

Should probably be changed to:

dmaCopy(cuadr, (uint16*)CHAR_BASE_BLOCK_SUB(0), cuadr_size);
dmaCopy(cuadr_pal, BG_PALETTE_SUB, cuadr_pal_size);
dmaCopy(cuadr_map, (uint16*)SCREEN_BASE_BLOCK_SUB(15), cuadr_map_size);

If you have the latest devkitarm, the size stuff should work. I've been having troubles with dmaCopy, so you might want to change those to for loops.

Also, my impression is that libnds is more stable and maintained than ndslib right now (they're the same basicly, only libnds has small letters and some other tiny changes).


If you still can't get it to work I'll start up my big computer (which sounds like a freaking jet engine) and debug it for you).
_________________
"no offense, but this is the gayest game ever"

#48373 - TaiyouHKabra - Tue Jul 19, 2005 5:18 pm

Yeah, I programmed GBA making my own libraries and working directly with the registers, so working with ndslib is a little difficult for me. Also I don't understand fully the layout of the VRAM. How are the CHAR_BASE_BLOCKs and the SCREEN_BASE_BLOCKs relationed? How should I configure BG0_CR?


Now I get a bunch of messed up gfx on the screen, I don't know if it's a problem copying with DMA. I'll try to copy the data using other methods. If someone has a better idea please tell me :).

Taiyou

#48384 - Sausage Boy - Tue Jul 19, 2005 7:05 pm

Try to look at the mess. Is there vertical rows running through the whole thing? If that's the case you're probably writing 8bits at the time to vram. Are the colors messed up? Somethings probably wrong with the palette. Is the same tile repeated? Then it's probably a map problem etc etc...You get the picture.

I even found a tutorial which explains char base and screen base nicely with a picture.
http://www.thepernproject.com/tutorials/GBA/day_5.pdf
_________________
"no offense, but this is the gayest game ever"

#48410 - TaiyouHKabra - Tue Jul 19, 2005 9:49 pm

Wow, actually it's the 3 things at the time lol. I'll try to look it up now and see if I can fix it.

Thanks!


Taiyou


EDIT: In GBA, there was a register where I could put what screen block and charset use for each BG. I suppose BGx_CR is that register, how is that register configured? I don't find any information available about that.

#48415 - Sausage Boy - Tue Jul 19, 2005 10:10 pm

Try this:

[CODE]SUB_BG0_CR = BG_256_COLOR | BG_TILE_BASE(whatevercharbaseis) | BG_MAP_BASE(whatyougetthepicture);|/CODE]

BG_TILE_BASE and BG_MAP_BASE stay the same no matter which screen you're using.
If you like flexible code, BGx_CR can also be accessed as BG_CR[x], to make loops/code that don't depend on which bg it's ran on etc... Of course that becomes SUB_BG_CR[x] on the subscreen.

For more information about this, you should check out video.h in ndslib, it can be quite useful when you're looking for the sub screen version of a define and the like.
_________________
"no offense, but this is the gayest game ever"

#48421 - TaiyouHKabra - Tue Jul 19, 2005 10:37 pm

Finally I discovered what was wrong. The stupidest mistake of my life as a console programmer... I didn't make the BG the correct size (256x192 instead 256x256). Now it's working sweet. Thanks to everyone who helped me. I'll post the code in case anyone has the same problem can solve it:

Code:

#include <NDS/NDS.h>

#include "cuadr.h"
#include "cuadr_map.h"
#include "cuadr_pal.h"


int main(void)
{
   powerON(POWER_ALL);

   
   videoSetMode(0);
   videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE);
   vramSetBankC(VRAM_C_SUB_BG);

   
   dmaCopy(cuadr, (uint16*)CHAR_BASE_BLOCK_SUB(0), 512*8);
   dmaCopy(cuadr_pal, BG_PALETTE_SUB, 1536);
   dmaCopy(cuadr_map, (uint16*)SCREEN_BASE_BLOCK_SUB(4), 512*8);

   SUB_BG0_CR = (BG_256_COLOR | BG_MAP_BASE(4) | BG_TILE_BASE(0) | TEXTBG_SIZE_256x256);
   
   while(1)
   {
   // nil
   }
   return 0;
}




Taiyou