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.

C/C++ > Double Buffering error

#140242 - ShadowX - Fri Sep 14, 2007 7:39 pm

I tried to display a picture in mode 4.
I first tried to display it without Dougle Buffering. OK.
I tried to display it using Double Buffering. Error.

Result of Double Buffering in GBA:
http://img74.imageshack.us/my.php?image=errogbayu2.png

The code:
Code:
#include <gba.h>
#include "m374lx.c"

int main()
{
    SetMode(MODE_4 | BG2_ENABLE);
    int i;
    for (i=0;i<256;i++)
    {
        BGPaletteMem[i]=m374lx_pal[i];
    }
    int x;
    for (x=0; x<(240*160);x++)
    {
        BackBuffer[x]=m374lx_data[x];
    }
    REG_DISPCNT ^= BACKBUFFER;
    while (1) {}
    return 0;
}


I used the headers from "the Pern Project".

#140288 - Cearn - Sat Sep 15, 2007 1:08 am

You're copying too much data.

While mode 4 may have 240x160 pixels/buffer, these are 8bit pixels, not 16bit. Backbuffer is an u16-pointer, so copying 240x160 items here means 240x160x2 bytes while you should only copy 240x160 bytes. Don't ignore the datatypes of the source and destination when copying: they will decide how and how much data is actually copied.

That funky stuff is probably because you're reading parts over the edge of ROM (the binary itself is probably smaller than 76.8kb) and writing over the edge of VRAM (0600:A000 + 240*160*2 = 0601:CC00, while VRAM ends at 0601:8000). What happens then is anyone's guess. Apparently, VBA responds by reading in incrementing values after ROM-end, and wrapping back to 0601:0000 after 0601:8000. Interestingly enough, my test image here should up alright in hardware >_>

#140337 - ShadowX - Sat Sep 15, 2007 1:29 pm

If I change the line
Code:
for (x=0; x<(240*160);x++)

to
Code:
for (x=0; x<((240*160)/2);x++)

will it work fine?

#140343 - Cearn - Sat Sep 15, 2007 2:07 pm

Yes. You don't need all those parentheses though. You could also use a dedicated copier like memcpy(), where problems due to datatypes aren't a problem (usually, anyways). They also have the benefit of being much faster.

#140451 - ShadowX - Sun Sep 16, 2007 3:43 pm

I tried now to wait a button hit and draw other picture.
It draws quickly and erases the screen.