4

I'm working on a pong game for the Nintendo DS. I'm using libnds to set things up and I've come across a very strange behaviour. So far I've only tried it out in emulators, but I use three different ones and they all exhibit this behaviour so I suspect I'm doing something bad.

The actual problem is that when I use background layer 1 or above for my tiled graphics, I get weird stripes all over that layer. If I use background layer 0 the problem goes away, but since that is rendered last, I cannot actually draw over it in another layer, which I want to.

My setup code:

void pong::setup_engine()
{
    // Setup DS graphics engine.
    // -------------------------
    videoSetMode(MODE_5_2D | DISPLAY_BG0_ACTIVE | DISPLAY_BG1_ACTIVE | DISPLAY_BG2_ACTIVE);
    vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
    vramSetBankB(VRAM_B_MAIN_BG_0x06020000);

    u8 *tileMemory = reinterpret_cast<u8 *>(BG_TILE_RAM(1));
    u16 *mapMemory = reinterpret_cast<u16 *>(BG_MAP_RAM(0));

    int bg0 = bgInit(1, BgType_Text8bpp, BgSize_T_256x256, 0, 1);
    int bg1 = bgInit(2, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
    //bgScroll(bg0, 256, 256);

    u16 *ptr = bgGetGfxPtr(bg1);
    for (int y = 10; y < 128*60; y++)
    {
        ptr[y] = 0xFFFF;
    }

    BG_PALETTE[1] = grey0;
    BG_PALETTE[2] = grey1;
    BG_PALETTE[3] = grey2;
    BG_PALETTE[4] = grey3;
    BG_PALETTE[5] = grey4;
    BG_PALETTE[6] = grey5;
    BG_PALETTE[7] = grey6;

    // 32 here is not 32 bytes but 32 half-words, which is 64 bytes.
    swiCopy(CORNER_TILE, tileMemory, 32);
    swiCopy(TOP_TILE, tileMemory + 64, 32);
    swiCopy(SIDE_TILE, tileMemory + (64 * 2), 32);
    swiCopy(MAIN_TILE, tileMemory + (64 * 3), 32);

    swiCopy(MAP, mapMemory, 32*24);
}

In the above code I use layer 1 for bg0, which is my tiled graphics layer. This makes the weird stripes appear; if I were to change it to 0 like this, it would show up as expected:

int bg0 = bgInit(0, BgType_Text8bpp, BgSize_T_256x256, 0, 1);

Any ideas what causes the problem, and what the solution is?

Image of weird stripes

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Skurmedel
  • 21,515
  • 5
  • 53
  • 66
  • Can you post a screenshot of the striping? – Blank Jun 14 '09 at 22:40
  • Sure, it's from some slightly altered code, but it's more or less the same as what I got the first time round. My tiles do not contain these stripes. – Skurmedel Jun 15 '09 at 00:13

2 Answers2

1

Two things. For one, the striping is most likely a VRAM conflict, although it's been a while since I did DS dev last. Double check where you're copying your graphics and your tile data. Mode 5 uses two text backgrounds, which should be behaving exactly the same if they're initialized a certain way, so I'm not sure what's going on there.

In any case, since you have two backgrounds working, why not just set their priorities in their registers to swap their z order? Background are only drawn in a particular order by default, you can set their priority to have the system draw them in any order you like.

Blank
  • 7,088
  • 12
  • 49
  • 69
  • Oh you can set the priority manually? That is very useful to know. I'll go and check my VRAM and see if it might be the problem. – Skurmedel Jun 14 '09 at 22:40
  • I'm actually not entirely sure that's accurate, I don't remember if the DS has priority quirks with its 3D buffer, but I don't think it does. In any case, the second you turn on the 3D engine for your first pong game, you've already made a big mistake. ^_^ – Blank Jun 14 '09 at 22:46
  • No I'm actually not trying to do 3D :)... but maybe I have accidently turned it on. I'm quite confused by all these layers and their individual limitations. – Skurmedel Jun 14 '09 at 22:58
  • It seems to be a VRAM issue yeah, since changing around the tile bases used produces different results. I don't get one that looks correct though. If I disable the bitmap layer all to layer (as in don't initalize it) it looks almost correct but there are some errors on some of the pixels. – Skurmedel Jun 15 '09 at 00:15
  • I marked the other one as the answer, since he has the least points but you both helped out a ton! Thanks! – Skurmedel Jun 15 '09 at 21:47
  • I'm glad you got it figured out. The DS is a strange beast when it comes to video, it certainly has a steep learning curve. Once you learn it though (and move on to something more complex than pong) it's a very powerful setup compared to the GBA, which its based on. – Blank Jun 16 '09 at 08:34
  • Yeah it seems quite potent. Will be trying to do some scrolling stuff next, like a strategy game. – Skurmedel Jun 16 '09 at 15:43
1

a breif look at background.h makes it look like you are using BG1 and BG2. For mode 5, according to:

http://nocash.emubase.de/gbatek.htm

layer 0 and 1 are normal and 2 and 3 are extended. I dont know what extended means. If you want to just do normal tile stuff you probably want to bgInit 0 and 1 not 1 and 2.

You can change the priorities around at will, layer 0 is not necessarily on top of 1, etc. Look at the BGxCNT registers (BG0CNT, BG1CNT,...) and the priority bits. Now if the priority is a tie then yes the layer number determines who wins.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Hmmm, you have BG0, BG1, and BG2 enabled but only setup two of them. Yes? – old_timer Jun 15 '09 at 00:08
  • Yeah. The BG0_ACTIVE etc doesn't seem to actually do anything. – Skurmedel Jun 15 '09 at 00:12
  • 1
    This definitely stems from his experimentation with the backgrounds, he probably has them all enabled but not all set up... Wait a second, that might actually be his issue. Wow. – Blank Jun 15 '09 at 05:29
  • Ok, you two are so very right :) My wild BG#_ACTIVE flags were one of the reasons it freaked out. Apparently, if you set them as active, but don't init them, very strange artifacts may appear. The other reason were some overlapping use of VRAM. – Skurmedel Jun 15 '09 at 21:46