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-04 18:09:48

New member
Registered: 2021-05-04
Posts: 2

GBDK - how to swap the background during vblank ?

Hello, i am new to Gameboy development and one of my first projects with GBDK is to play a small animation (1 second, 30 frames) at full resolution (160x144 pixels).

I have already converted the base video into 30 files, each with its tile and map data.

I am starting with only 2 frames of animation.

Displaying the first frame is always ok, my issue is whenever I want to update the screen with the second frame. The new tiles are loaded in VRAM, then used with the old map data, and on the next refresh only the new map data is used. So for a full image swap, i have 1 frame of garbage data on screen.

Here is my code:


void vblCallback(){
    set_bkg_data(0, frame2_tile_count, frame2_tile_data);
    set_bkg_tiles(0, 0, frame2_tile_map_width, frame2_tile_map_height, frame2_map_data);

void main() {

    set_bkg_data(0, frame1_tile_count, frame1_tile_data);
    set_bkg_tiles(0, 0, frame1_tile_map_width, frame1_tile_map_height, frame1_map_data);


How would you do a full background swap in only one screen refresh ? Is it even possible in GBDK or should I use Assembly ?

Thanks in advance.

Last edited by Pouaite (2021-05-05 04:36:14)



#2 2021-05-05 04:52:22

Registered: 2020-02-13
Posts: 307

Re: GBDK - how to swap the background during vblank ?

it is not clear how much tiles you are going to load inside a VBlank period, but, in general, it is impossible to load all 256 tiles within a VBlank period on the DMG, neither with GBDK nor assembly simply because it is 4096 bytes and VBlank period is only 1140 cycles and copying of one byte consists of the read and write and there will be something like 4.5 .. 5.5 cycles per byte.

but you can use some tricks. the most simple one is to use sprite tiles for odd frames and bkg tiles for even frames (LCDC bit 4). tiles 128..255 will be shared between frames, but 0..127 will be unique. to avoid flickering you can use two tilemaps: from address 9800 for the odd frames and from 9C00 for even frames (LCDC bit 3). you load the first frame, activate it within VBlank by setting LCDC bits. then load another frame into the map and tiles that are not visible at the moment, that might take the entire frame, and then you switch it at the next VBlank.

you can do that easily with GBDK or assembler, if you wish. in both cases without optimization of the image framerate will be poor.



#3 2021-05-05 05:54:04

New member
Registered: 2021-05-04
Posts: 2

Re: GBDK - how to swap the background during vblank ?

Thank you for your answer, i did not realise that i could work with two tilemaps.

My animation does not always have the same amount of tiles, but i would say the average is ~140 unique tiles to load in vram per frame. So if i load one full frame in memory from 8000 and the other from 9000, the second one will override the tiles in 128...~140.

I will try to load the tiles for the even frames in 8000 to 8BFF, use them for the tilemap at 9800, and for the odd frames use the tiles from 8C00 to 97FF for the tilemap at 9C00. Does is sound doable or stupid ?



Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson