Page 1 of 1

Doubling my buffer, quadruples my anguish. (SOLVED)

Posted: Fri Oct 02, 2009 11:00 pm
by sand7000
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();
  }
}

Re: Doubling my buffer, quadruples my anguish.

Posted: Sat Oct 03, 2009 2:59 am
by elhobbs

Code: Select all

i = touch.px - 2 ; i < touch.py + 2
px and py mismatch.thesame in the inner loop.you are also going to have issues with edge cases. If px or py is 0 you could end up writing outside the frame. Certainly in the wrong place.

Re: Doubling my buffer, quadruples my anguish.

Posted: Sat Oct 03, 2009 3:35 am
by sand7000
Th real problem i am having isn't caused by the px/py mismatch or the edge case problem (these were stupid mistakes on my part due to trying to quickly make a simplest case scenario, and i fixed the mismatch above to no avail). The real problem is my method of double buffering. Please keep giving me ideas/examples of how to properly double buffer.

Re: Doubling my buffer, quadruples my anguish.

Posted: Sat Oct 03, 2009 8:20 am
by elhobbs
There is a double buffer example in the nds examples.one issue is that you do not have enough VRAM mapped. One bank is not enough for 2 16bit backgrounds.so you are alternating between a black screen and the white image , making it look gray as it alternates at 60fps.

Re: Doubling my buffer, quadruples my anguish. (SOLVED)

Posted: Sat Oct 03, 2009 2:21 pm
by sand7000
Thank you so much elhobbs!! You are right, I should have realized the memory bank was too small to do what i was asking it to do. I had realized that the 3 base offset wasn't enough, but when i swapped in 6 and still got that gray color I knew I was still doing something wrong. 8)