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.

Beginners > Double Buffer

#21056 - bogston - Sat May 22, 2004 3:16 pm

I'm learning GBA programming. This is a modification of a double buffer example. I don't believe it's works correctly. Also, I don't believe the two original examples worked correctly either. The problem is that I see the screen being painted. The examples are from gbajunkie and simpdemo. Using VisualBoyAdvance, will I still see the screen being painted using double buffer?

#include <gba.h>

u16 *bg_palette_mem = BG_PALETTE_MEM;
u16 *video_buffer = BACK_BUFFER;

void swap_buffer(void)
{
VSYNC();

if(REG_DISPCNT & DOUBLE_BUFFER)
{
REG_DISPCNT &= ~DOUBLE_BUFFER;
video_buffer = (u16 *) BACK_BUFFER;
}
else
{
REG_DISPCNT |= DOUBLE_BUFFER;
video_buffer = (u16 *) FRONT_BUFFER;
}
}

int main()
{
int i;

SET_MODE(MODE_4 | BG2_ENABLE);

*bg_palette_mem++ = RGB(31, 31, 31); // white
*bg_palette_mem++ = RGB(0, 31, 0); // green
*bg_palette_mem++ = RGB(31, 0, 0); // red
*bg_palette_mem++ = RGB(0, 31, 31); // cyan

// back buffer
i = 38400;
while(i--)
*video_buffer++ = 0x0303;
swap_buffer();

// front buffer
i = 38400;
while(i--)
*video_buffer++ = 0x0202;
swap_buffer();
}

#21093 - Lupin - Sun May 23, 2004 8:33 pm

did you try to call swap_buffer before drawing anything? What does VSYNC look like?
_________________
Team Pokeme
My blog and PM ASM tutorials

#21176 - Cearn - Tue May 25, 2004 12:53 pm

Like Lupin said, what does VSYNC look like? It should be something like
Code:

   while(REG_VCOUNT >= 160);
   while(REG_VCOUNT < 160);

not just a wait until REG_VCOUNT >= 160. Since swap_buffer() is small, you'll still be inside the VDraw period when the second vsync is called, while will basically be ignored then. There's a recent thread on this subject. (And, plz, no comments on VBlankIntrWait being the Right Thing to do)

Also, there are number of more sinister problems with the code. First, there's no main loop. GBA programs should never end. Ever. Even when you don't have code for a main loop, still use something like
Code:

while(1);

at the bottom of main.

Secondly, on the use of *var++. As much as I can understand the use, it's really really ill adviced to do it on global variables. It's not a big deal with short short code, but if you have a bigger program, you'll going to wonder why the hell bg_palette_mem[0] won't set the first background palette entry anymore. (answer, because after *bg_palette_mem++,
bg_palette_mem points to the second entry)

On a cosmetic note, the use of code tags is adviced when listing code, especially since it allows indenting.

#21218 - bogston - Wed May 26, 2004 2:02 am

Here's VSYNC():

#define VSYNC() while(REG_VCOUNT < 160);

#21219 - bogston - Wed May 26, 2004 2:09 am

And ...

Don't worry about the bad coding practices. This code is for me to learn.

#21221 - sajiimori - Wed May 26, 2004 2:25 am

Quote:

This code is for me to learn.

And you're posting it for us to read, so have some consideration.

Fix the problems that have already been mentioned, then post the new version (in code tags) if you still have trouble.

#21251 - Lupin - Wed May 26, 2004 1:54 pm

Quote:
Don't worry about the bad coding practices.


Bad coding practices might be the reason for your problem :)

Now you got quoted twice, doesn't that make you worry? :P
_________________
Team Pokeme
My blog and PM ASM tutorials