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.

#1 2021-05-29 07:59:35

pajcek
New member
Registered: 2021-05-25
Posts: 4

How to manage memory when switching or preparing backgrounds/sprites?

Hello!

I am a bit confused about how memory is handled in GBDK2020.
My understanding is that if I touch nothing, the game will use 1 bank of 16kb, and that's the amount of text that my .c file may have. That's the ROM.
But what about RAM? I remember reading you can only have 40 sprites, and only 10 overlapping in a horizontal line or something.

Can you point me to some explanation or give me some overview of how RAM works? One would have to destroy sprites eventually probably, so if you just set_sprite_data(0, ... to something else, would the old tile in spritedata 0 be overwritten and memory freed? That would be the best if it was the case.

What if there is now a hole in memory because the new data is smaller than the old one? How does the game not waste memory if it has holes?

I asked about backgrounds because if you wanted to do classic zelda, you would reload backgrounds every time you passed to the next section of a tiled map.
Perhaps you would want to preload the maps around if loading was an issue? Escpecially in a scrolling mario map. You would probably have sections where two would always be active and then you would dispose of the one off-screen to the left, to which you can't return.

Wasn't pokemon kind of a giant map? Or maybe it just felt like that. I wonder what kind of background loading system they used.


Either way, please give me all the resources, knowledge and data because I need to know this stuff.
Thenk you very much!

Offline

 

#2 2021-05-30 03:41:16

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: How to manage memory when switching or preparing backgrounds/sprites?

Probably you should run zelda in bgb or any other emulator with nice VRAM viewer and you will see how game manipulates objects and data in VRAM. Also i suggest to look into gbdk-2020 examples which are located in gbdk-2020/examples/gb/

Last edited by toxa (2021-05-30 03:42:17)

Offline

 

#3 2021-05-30 10:01:56

pajcek
New member
Registered: 2021-05-25
Posts: 4

Re: How to manage memory when switching or preparing backgrounds/sprites?

Ok, I made this:

#include <gb/gb.h>

const unsigned char Tiles1[] = {  0xFF,0xFF,0x81,0x81,0x9D,0x9D,0x95,0x95,  0x95,0x95,0x9D,0x9D,0x81,0x81,0xFF,0xFF,  0xFF,0xFF,0xFF,0x81,0xFF,0x99,0xFF,0x89,  0xFF,0x89,0xFF,0x89,0xFF,0x81,0xFF,0xFF,  0xFF,0xFF,0x81,0xFF,0x81,0xE3,0x81,0xFB,  0x81,0xE3,0x81,0xEF,0x81,0xE3,0xFF,0xFF};

const unsigned char Tiles2[] = {  0xFF,0xFF,0x81,0x81,0x9D,0x9D,0x95,0x95,  0x95,0x95,0x9D,0x9D,0x81,0x81,0xFF,0xFF,  0xFF,0xFF,0xFF,0x81,0xFF,0x99,0xFF,0x89,  0xFF,0x89,0xFF,0x89,0xFF,0x81,0xFF,0xFF,  0xFF,0xFF,0x81,0xFF,0x81,0xE3,0x81,0xFB,  0x81,0xE3,0x81,0xEF,0x81,0xE3,0xFF,0xFF,  0xFF,0xFF,0xFF,0xFF,0xE3,0xE3,0xFB,0xFB,  0xE3,0xE3,0xFB,0xFB,0xE3,0xE3,0xFF,0xFF,  0x00,0xFF,0x00,0xFF,0x00,0xEF,0x00,0xEB,  0x00,0xE3,0x00,0xFB,0x00,0xFB,0x00,0xFF};

const unsigned char Tiles3[] = {  0xFF,0xFF,0x81,0x81,0x9D,0x9D,0x95,0x95,  0x95,0x95,0x9D,0x9D,0x81,0x81,0xFF,0xFF,  0xFF,0xFF,0xFF,0x81,0xFF,0x99,0xFF,0x89,  0xFF,0x89,0xFF,0x89,0xFF,0x81,0xFF,0xFF,  0xFF,0xFF,0x81,0xFF,0x81,0xE3,0x81,0xFB,  0x81,0xE3,0x81,0xEF,0x81,0xE3,0xFF,0xFF,  0xFF,0xFF,0xFF,0xFF,0xE3,0xE3,0xFB,0xFB,  0xE3,0xE3,0xFB,0xFB,0xE3,0xE3,0xFF,0xFF,  0x00,0xFF,0x00,0xFF,0x00,0xEF,0x00,0xEB,  0x00,0xE3,0x00,0xFB,0x00,0xFB,0x00,0xFF,  0xFF,0x00,0xFF,0x00,0xFF,0x1C,0xFF,0x10,  0xFF,0x1C,0xFF,0x04,0xFF,0x1C,0xFF,0x00,  0x00,0x00,0x00,0x00,0x1C,0x1C,0x10,0x10,  0x1C,0x1C,0x14,0x14,0x1C,0x1C,0x00,0x00
};

#define Tilemap1Width 10
#define Tilemap1Height 10
#define Tilemap1Bank 0

const unsigned char Tilemap1[] = {  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,  0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,  0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,  0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

#define Tilemap2Width 10
#define Tilemap2Height 10
#define Tilemap2Bank 0

const unsigned char Tilemap2[] = {  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,  0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,  0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,  0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,  0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04};

#define Tilemap3Width 10
#define Tilemap3Height 10
#define Tilemap3Bank 0

const unsigned char Tilemap3[] = {  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,  0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,  0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,  0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,  0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,0x05,  0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,  0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02};

void main()
{
    set_bkg_data(0, 2, Tiles3);
    set_bkg_tiles(2, 2, 10, 10, Tilemap3);

    SHOW_BKG;
    DISPLAY_ON;
    delay(2000);

    while(1) {
        set_bkg_data(0, 4, Tiles1);
        set_bkg_tiles(2, 2, 10, 10, Tilemap1);
        delay(2000);

        set_bkg_data(0, 6, Tiles3);
        set_bkg_tiles(2, 2, 10, 10, Tilemap2);
        delay(2000);

        set_bkg_data(0, 2, Tiles1);
        set_bkg_tiles(2, 2, 10, 10, Tilemap3);
        delay(2000);
    }
}

________________________________________
And when I view the VRAM in BGB, then the memory fills up 3 tiles, 5 tiles, and then 7, and then when it goes back to 3, then the tiles on index 3-6 become grey. I assume that means they are ready for overwriting if needed. Or does that mean that they are only not being actively used? Hmmm.

So is this how you do it? Just change the tilemap, and the old one gets garbage collected if the new one has less tiles?

Offline

 

#4 2021-05-31 08:24:55

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: How to manage memory when switching or preparing backgrounds/sprites?

1. put  fill_bkg_rect(0,0, 20, 18, 0); into the first line of your main() function.

2. you load 2 tiles, then 4, then 6 then 2, always starting from tile 0. i don't know why you are talking about 3, 5 and 7.

3. some of your tiles becoming gray because you use tilemap, that uses tile number 6, but you load only 6 tiles into vram, those are 0..5.

probably you need to read some docs about hardware: https://gbdev.io/pandocs/ and also gbdk-2020 function reference: https://gbdk-2020.github.io/gbdk-2020/docs/api/

Offline

 

#5 2021-05-31 08:45:21

radiosity
New member
Registered: 2021-02-01
Posts: 5

Re: How to manage memory when switching or preparing backgrounds/sprites?

As Toxa says, you are loading each tile table ALWAYS from position 0 of the VRAM. If the first tile table occupies 2 tiles, the next ones should be loaded in VRAM position 2. That is, add the length of previously tiles to the VRAM offset.

Offline

 

#6 2021-06-30 08:05:54

BlakeBernadette
New member
Registered: 2021-06-29
Posts: 1

Re: How to manage memory when switching or preparing backgrounds/sprites?

toxa wrote:

1. put  fill_bkg_rect(0,0, 20, 18, 0); into the first line of your main() function.

2. you load 2 tiles, then 4, then 6 then 2, always starting from tile 0. i don't know why you are talking about 3, 5 and 7.

3. some of your tiles becoming gray because you use tilemap, that uses tile number 6, but you load only 6 tiles into vram, those are 0..5.

probably you need to read some docs about hardware: https://gbdev.io/pandocs/ and also gbdk-2020 function reference: https://gbdk-2020.github.io/gbdk-2020/docs/api/

Thx for information! Helpful!!

Last edited by BlakeBernadette (2021-07-01 12:57:25)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson