Doubling my buffer, quadruples my anguish. (SOLVED)

Post Reply
sand7000
Posts: 7
Joined: Fri Oct 02, 2009 9:52 pm

Doubling my buffer, quadruples my anguish. (SOLVED)

Post by sand7000 » 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();
  }
}
Last edited by sand7000 on Sat Oct 03, 2009 2:19 pm, edited 2 times in total.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Doubling my buffer, quadruples my anguish.

Post by elhobbs » Sat Oct 03, 2009 2:59 am

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.

sand7000
Posts: 7
Joined: Fri Oct 02, 2009 9:52 pm

Re: Doubling my buffer, quadruples my anguish.

Post by sand7000 » Sat Oct 03, 2009 3:35 am

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.
"For a variety of reasons, Fortran is the best and easiest programming language." ~Sooves (a physics professor who had never tried any programming language other than Fortran)

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Doubling my buffer, quadruples my anguish.

Post by elhobbs » Sat Oct 03, 2009 8:20 am

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.

sand7000
Posts: 7
Joined: Fri Oct 02, 2009 9:52 pm

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

Post by sand7000 » Sat Oct 03, 2009 2:21 pm

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)
"For a variety of reasons, Fortran is the best and easiest programming language." ~Sooves (a physics professor who had never tried any programming language other than Fortran)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 15 guests