Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2019-04-28 03:25:22

asmitter
New member
Registered: 2019-04-28
Posts: 3

Tiling background glitch in BGB

Hey all,
Super new to GB development (and fairly new to C) so giving GBDK a whirl.
I'm seeing a weird behaviour when tiling backgrounds which is only apparent in BGB (or, at least, not apparent in VisualBoyAdvance). I've heard it's the best emulator so of course I'm still at the stage of "it must be me" and I don't yet understand the platform (or GBDK) quirks enough to work around it.

I tried adding delays, waiting for button presses, calling it more than once, different sprites, but always with that same blank area. And again, ONLY when running in BGB.

Here's a simple implementation:

Code:

#include <gb/gb.h>
#include "alpha.c"

unsigned char bgdiagonal[] =
{
  0x2C,0x76,0x2C,0xF7,0xDB,0xEB,0x34,0xD7,
  0xEB,0x2C,0xD7,0xDB,0x2C,0xF7,0x2C,0x76
};

void init();
void showbg();
void updateSwitches();

void main() {
    init();
    showbg();
    updateSwitches();
}

void init() {
    DISPLAY_ON;
}

void showbg() {
    set_bkg_data(0, 1, bgdiagonal);
}

void updateSwitches() {
    HIDE_WIN;
    SHOW_SPRITES;
    SHOW_BKG;    
}

CLUE: When using a larger set of sprites, that area is filled with subsequent sprites, as if it just starts loading a few extra sets of sprites?
I'm super suspicious that it seems to map to the area that the "Nintendo" boot text appears, but that might be a red herring...

Any ideas?

(Also, first post, so Hello!)

Last edited by asmitter (2019-04-28 03:26:22)

Offline

 

#2 2019-04-28 06:18:29

Robbi_Blechdose
Member
Registered: 2017-12-10
Posts: 29

Re: Tiling background glitch in BGB

You might want to try it like this (shortened example, but you get the idea):

Code:

void main()
{
    set_bkg_data(0, 1, bgdiagonal);
    SHOW_BGK;
    HIDE_WIN;
    HIDE_SPRITES;
    DISPLAY_ON;
    while(1){};
}

Basically, load stuff when the LCD is off, otherwise you get glitches. And turn the sprites off if you're not initializing them. Also, the infinite loop makes sure the CPU doesn't run random code (though you can get the CPU to "sleep" which is definetely better than this which will just burn power).

Oh, and adding a ROM or at least some screenshots might be helpful next time smile

Last edited by Robbi_Blechdose (2019-04-28 06:19:19)

Offline

 

#3 2019-04-28 07:40:27

asmitter
New member
Registered: 2019-04-28
Posts: 3

Re: Tiling background glitch in BGB

Thanks for the help!
I gave your suggestion a try, but still the same results!

Loaded in BGB:
https://gameboydevdiary.andysmith.co.uk/tmp_img/bg_missing_tiles.PNG

Loaded in VisualBoyAdvance:
https://gameboydevdiary.andysmith.co.uk/tmp_img/bg_missing_tiles_vba.PNG

Code:

Code:

#include <gb/gb.h>

unsigned char bgdiagonal[] =
{
  0x2C,0x76,0x2C,0xF7,0xDB,0xEB,0x34,0xD7,
  0xEB,0x2C,0xD7,0xDB,0x2C,0xF7,0x2C,0x76
};

void main() {
    set_bkg_data(0, 1, bgdiagonal);
    SHOW_BKG;
    HIDE_WIN;
    HIDE_SPRITES;
    DISPLAY_ON;
    while(1){};
}

and here's the resulting ROM

Offline

 

#4 2019-04-28 08:13:16

Robbi_Blechdose
Member
Registered: 2017-12-10
Posts: 29

Re: Tiling background glitch in BGB

Ah, I see the problem. If you look at the VRAM viewer in BGB, you'll see that the tile indices in those positions are different - because they had to be for the Nintendo logo.
To get a good result, you need to write a tilemap with set_bkg_tiles().
Currently, your ROM is relying on the fact that VBA (it's really inaccurate, don't use it) doesn't emulate the Nintendo logo.
Word of advice - don't rely on uninitialized memory, anywhere. It'll come back to bite you in the a** when you least expect it. :p

Offline

 

#5 2019-04-28 14:14:22

asmitter
New member
Registered: 2019-04-28
Posts: 3

Re: Tiling background glitch in BGB

This is great - thank you! I got it working.

To check my understanding (and please bear with me because I'm super new):
The background layer comprises of a map, and a set of sprites. I had overwritten the set of background sprites, but without updating the map it was still trying to lay out a "Nintendo" logo as per the power-on screen.

I'll share my working example here for the benefit of others, with comments. Unfortunately the tutorials that Google is serving up don't really introduce this so clearly, so perhaps I can help any fellow newcomers

Code:

#include <gb/gb.h>

unsigned char bgdiagonal[] =
{
  0x2C,0x76,0x2C,0xF7,0xDB,0xEB,0x34,0xD7,
  0xEB,0x2C,0xD7,0xDB,0x2C,0xF7,0x2C,0x76
}; // The 8x8 tile as exported by GBTM

#define bgdiagonalmapWidth 20    // Width of the Game Boy screen in tiles (8x8)
#define bgdiagonalmapHeight 18    // Height of the Game Boy screen in tiles (8x8)
#define bgdiagonalmapBank 0

unsigned char bgdiagonalmap[] =
{
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
}; // Creates a map where every tile in the 20x18 area is the first one in the loaded set.

void main() {
    set_bkg_data(0, 1, bgdiagonal);        // load our sprite into the background set
    set_bkg_tiles(0, 0, 18, 20, bgdiagonalmap);    // Set the background to the whole map we defined above
    SHOW_BKG;                    // Show the background
    DISPLAY_ON;                    // Turn on the display
    while(1) {                    // Run loop - have fun!
    scroll_bkg(1, -1);            // Scroll our background 1 pixel down, 1 pixel left
    wait_vbl_done();            // Good for ensuring smooth movements
    }
}

(Let me know if any of that is incorrect or misleading).

The result:
https://gameboydevdiary.andysmith.co.uk/tmp_img/fullscreen_bg_tile.PNG
(I nabbed a gif but I didn't want to upload a potential epilepsy trigger hmm )

The resulting ROM

Thanks again for the help!

Offline

 

#6 2019-04-28 15:35:26

Robbi_Blechdose
Member
Registered: 2017-12-10
Posts: 29

Re: Tiling background glitch in BGB

I suggest you check the wiki. Even though it's targeting ASM (in a way), it'll help you gain a better understanding of the hardware.
The background is made of a map, that's it. The map is a bunch of tile indices, which refer to tiles in VRAM. Sprites are overlayed on top of that.

Oh, and I'm glad I could be of some help smile

Last edited by Robbi_Blechdose (2019-04-28 15:35:49)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson