Doubling my buffer, quadruples my anguish. (SOLVED)
Posted: Fri Oct 02, 2009 11:00 pm
I seem to be unable to successfully create a double buffer. I created the following simple example to show the problem I am having. The idea of this piece of code is to display the touchscreen coordinate on the top screen, and simultaneously draw a square at the point I am touching the bottom screen. I am able to display the coordinate perfectly, but I can only draw along a diagonal line from the top left corner downwards. What does get drawn is gray rather than the white I specified. On top of this, if I touch the top left corner of the screen the screen goes completly white as well. I have been trying to fix this for hours! Any help would be much appreciated.
Code: Select all
#include <nds.h>
#include <stdio.h>
#include <string>
int main()
{
//indicator to toggle when buffers swap
int swaped = 1;
//set up a video buffer
u16* video_buffer = (u16*)BG_BMP_RAM(0);
consoleDemoInit();
//draw on the bottom screen
lcdMainOnBottom();
irqSet(IRQ_VBLANK,0);
videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE);
vramSetBankA(VRAM_A_MAIN_BG_0x06000000);
REG_BG3CNT = BG_MAP_BASE(3) | BG_BMP16_256x256;
REG_BG3PA = 1 << 8;
REG_BG3PB = 0;
REG_BG3PC = 0;
REG_BG3PD = 1 << 8;
touchPosition touch;
while(true)
{
//swap the buffers
if(swaped)
{
swaped = 0;
REG_BG3CNT = BG_MAP_BASE(0) | BG_BMP16_256x256;
video_buffer = (u16*)BG_BMP_RAM(3);
}
else
{
swaped = 1;
REG_BG3CNT = BG_MAP_BASE(3) | BG_BMP16_256x256;
video_buffer = (u16*)BG_BMP_RAM(0);
}
scanKeys();
touchRead(&touch);
//draw a square at the point the touch pad is touched.
if(keysHeld() & KEY_TOUCH)
{
//print the coordinates of the screen touch
printf("Touchpad touched\n");
printf("x = %04X, y = %04X\n", touch.px, touch.px);
//color white pixels around the touched position
for( int i = touch.px - 2 ; i < touch.px + 2 ; i++ )
{
for( int k = touch.py - 2 ; k < touch.py + 2 ; k++ )
{
video_buffer[k*256 + i] = RGB15(31,31,31) | BIT(15);
}
}
}
//wait for the screen to finish drawing
swiWaitForVBlank();
//clear the consol window
consoleClear();
}
}