DoubleBuffer.pp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. program DoubleBuffer;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9;
  5. var
  6. backBuffer: pcuint16;
  7. colorMask: cuint16;
  8. iy, ix: integer;
  9. bg: cuint16;
  10. begin
  11. randomize;
  12. //set the mode for 2 text layers and two extended background layers
  13. videoSetMode(MODE_5_2D);
  14. //set the first two banks as background memory and the third as sub background memory
  15. //D is not used..if you need a bigger background then you will need to map
  16. //more vram banks consecutivly (VRAM A-D are all 0x20000 bytes in size)
  17. vramSetPrimaryBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_MAIN_BG_0x06020000,
  18. VRAM_C_SUB_BG, VRAM_D_LCD);
  19. consoleDemoInit();
  20. iprintf(#10#10#9 + 'Hello DS devers' + #10);
  21. iprintf(#9 + 'www.drunkencoders.com' + #10);
  22. iprintf(#9 + 'double buffer demo');
  23. bg := bgInit(3, BgType_Bmp16, BgSize_B16_256x256, 0,0);
  24. colorMask := $1F;
  25. backBuffer := pcuint16(bgGetGfxPtr(bg)) + 256*256;
  26. while true do
  27. begin
  28. //draw a box (60,60,196,136)
  29. for iy := 60 to 196 - 60 - 1 do
  30. for ix := 60 to 256 - 60 - 1 do
  31. backBuffer[iy * 256 + ix] := random(colorMask) or BIT(15);
  32. swiWaitForVBlank();
  33. scanKeys();
  34. if (keysDown() and KEY_START) <> 0 then
  35. exit;
  36. //swap the back buffer to the current buffer
  37. backBuffer := pcuint16(bgGetGfxPtr(bg));
  38. //swap the current buffer by changing the base. Each base
  39. //represents 16KB of offset and each screen is 256x256x2 (128KB)
  40. //this requires a map base seperation of 8 (could get away with smaller
  41. //as the screen is really only showing 256x192 (96KB or map base 6)
  42. if (bgGetMapBase(bg) = 8) then
  43. bgSetMapBase(bg, 0)
  44. else
  45. bgSetMapBase(bg, 8);
  46. colorMask := colorMask xor $3FF;
  47. end;
  48. end.