main.pp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. program main;
  2. {$apptype arm9}
  3. {$define ARM9}
  4. {$mode objfpc}
  5. uses
  6. ctypes, nds9;
  7. var
  8. frontBuffer: pcuint16;
  9. backBuffer: pcuint16;
  10. colorMask: cuint16;
  11. iy, ix: integer;
  12. temp: pcuint16;
  13. begin
  14. irqInit();
  15. // a vblank interrupt is needed to use swiWaitForVBlank()
  16. // since the dispatcher handles the flags no handler is required
  17. irqEnable(IRQ_VBLANK);
  18. //set the mode for 2 text layers and two extended background layers
  19. videoSetMode(MODE_5_2D or DISPLAY_BG3_ACTIVE);
  20. //set the sub background up for text display (we could just print to one
  21. //of the main display text backgrounds just as easily
  22. videoSetModeSub(MODE_0_2D or DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text
  23. //set the first two banks as background memory and the third as sub background memory
  24. //D is not used..if you need a bigger background then you will need to map
  25. //more vram banks consecutivly (VRAM A-D are all 0x20000 bytes in size)
  26. vramSetMainBanks( VRAM_A_MAIN_BG_0x06000000, VRAM_B_MAIN_BG_0x06020000,
  27. VRAM_C_SUB_BG , VRAM_D_LCD);
  28. // set up text background for text
  29. SUB_BG0_CR^ := BG_MAP_BASE(31);
  30. BG_PALETTE_SUB[255] := u32(RGB15(31,31,31));//by default font will be rendered with color 255
  31. //consoleInit() is a lot more flexible but this gets you up and running quick
  32. consoleInitDefault(pu16(SCREEN_BASE_BLOCK_SUB(31)), pu16(CHAR_BASE_BLOCK_SUB(0)), 16);
  33. printf(#10#10#9 + 'Hello DS devers' + #10);
  34. printf(#9 + 'www.drunkencoders.com' + #10);
  35. printf(#9 + 'double buffer demo');
  36. // set up our bitmap background
  37. BG3_CR^ := BG_BMP16_256x256;
  38. //these are rotation backgrounds so you must set the rotation attributes:
  39. //these are fixed point numbers with the low 8 bits the fractional part
  40. //this basicaly gives it a 1:1 translation in x and y so you get a nice flat bitmap
  41. BG3_XDX^ := 1 shl 8;
  42. BG3_XDY^ := 0;
  43. BG3_YDX^ := 0;
  44. BG3_YDY^ := 1 shl 8;
  45. //our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2
  46. BG3_CX^ := 0;
  47. BG3_CY^ := 0;
  48. frontBuffer := pcuint16($06000000);
  49. backBuffer := pcuint16($06000000 + 256 * 256 * 2);
  50. //this is just used so we can write red color bits to one frame and green to the
  51. //other
  52. colorMask := $1F;
  53. while true do
  54. begin
  55. //draw a box
  56. for iy := 60 to 196 - 60 - 1 do
  57. for ix := 60 to 256 - 60 - 1 do
  58. backBuffer[iy * 256 + ix] := (rand() and colorMask) or BIT(15);
  59. swiWaitForVBlank();
  60. //swap
  61. temp := frontBuffer;
  62. frontBuffer := backBuffer;
  63. backBuffer := temp;
  64. //flip
  65. //base is 16KB and screen size is 256x256x2 (128KB)
  66. BG3_CR^ := BG3_CR^ xor BG_BMP_BASE( 128 div 16 );
  67. //this will cause red or green bits only to be set and swap each
  68. //frame
  69. colorMask := colorMask xor $3FF;
  70. end;
  71. end.