main.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. program main;
  2. {$L ballpalette.bin.o}
  3. {$L balldata.bin.o}
  4. {$apptype arm9}
  5. {$define ARM9}
  6. {$mode objfpc}
  7. uses
  8. ctypes, nds9;
  9. var
  10. OAMCopy: array [0..127] of SpriteEntry;
  11. ballpalette_bin_end: array [0..0] of u8; cvar; external;
  12. ballpalette_bin: array [0..0] of u16; cvar; external;
  13. ballpalette_bin_size: u32; cvar; external;
  14. balldata_bin_end: array [0..0] of u8; cvar; external;
  15. balldata_bin: array [0..0] of u16; cvar; external;
  16. balldata_bin_size: u32; cvar; external;
  17. procedure initOAM();
  18. var
  19. i: integer;
  20. begin
  21. for i := 0 to 127 do
  22. OAMCopy[i].st.attribute[0] := ATTR0_DISABLED;
  23. end;
  24. //---------------------------------------------------------------------------------
  25. procedure updateOAM();
  26. begin
  27. dmaCopy(@OAMCopy, OAM, sizeof(OAMCopy));
  28. end;
  29. type
  30. TTouchType = (ttContinuous, ttSingle);
  31. var
  32. frame: integer;
  33. TouchType: TTouchType = ttContinuous;
  34. //---------------------------------------------------------------------------------
  35. function Vblank(): pointer;
  36. begin
  37. //---------------------------------------------------------------------------------
  38. inc(frame);
  39. end;
  40. var
  41. min_x, min_y, max_x, max_y: integer;
  42. min_px, min_py, max_px, max_py: integer;
  43. touch: touchPosition;
  44. i: integer;
  45. pressed, held: integer;
  46. begin
  47. min_x := 4096;
  48. min_y := 4096;
  49. max_x := 0;
  50. max_y := 0;
  51. min_px := 4096;
  52. min_py := 4096;
  53. max_px := 0;
  54. max_py := 0;
  55. powerON(POWER_ALL_2D);
  56. // put the main screen on the bottom lcd
  57. lcdMainOnBottom();
  58. // Initialise the interrupt system
  59. irqInit();
  60. // install our simple vblank handler
  61. irqSet(IRQ_VBLANK, @Vblank);
  62. // enable the interrupt
  63. irqEnable(IRQ_VBLANK);
  64. initOAM();
  65. //enable vram and map it to the right places
  66. vramSetMainBanks( VRAM_A_MAIN_SPRITE, //A and B maped consecutivly as sprite memory
  67. VRAM_B_MAIN_SPRITE, //this gives us 256KB which is the max
  68. VRAM_C_MAIN_BG_0x06000000, //map C to background memory
  69. VRAM_D_LCD //not using D
  70. );
  71. //set the video mode
  72. videoSetMode( MODE_0_2D or
  73. DISPLAY_SPR_ACTIVE or //turn on sprites
  74. DISPLAY_BG0_ACTIVE or //turn on background 0
  75. DISPLAY_SPR_1D //this is used when in tile mode
  76. );
  77. // Sprite initialisation
  78. for i := 0 to 255 do
  79. SPRITE_PALETTE[i] := u32(ballpalette_bin[i]);
  80. for i := 0 to 32*16 - 1 do
  81. SPRITE_GFX[i] := u32(balldata_bin[i]);
  82. // black backdrop
  83. BG_PALETTE[0] := (RGB15(0,0,0));
  84. BG0_CR^ := BG_MAP_BASE(31);//use bg0 for the text
  85. BG_PALETTE[255] := (RGB15(31,31,31));//by default font rendered with color 255
  86. //consoleInit() is a lot more flexible but this gets you up and running quick
  87. consoleInitDefault(pu16(SCREEN_BASE_BLOCK(31)), pu16(CHAR_BASE_BLOCK(0)), 16);
  88. printf(#27 + '[4;8H' + 'Touch Screen Test');
  89. printf(#27 + '[15;4H' + 'Right Shoulder toggles');
  90. while true do
  91. begin
  92. swiWaitForVBlank();
  93. updateOAM();
  94. // read the button states
  95. scanKeys();
  96. // read the touchscreen coordinates
  97. touch := touchReadXY();
  98. pressed := keysDown(); // buttons pressed this loop
  99. held := keysHeld(); // buttons currently held
  100. // Right Shoulder button toggles the mode
  101. if ( pressed and KEY_R) <> 0 then Inc(TouchType);
  102. if TouchType = ttContinuous then
  103. printf(#27 + '[14;4H' + 'Touch mode: CONTINUOUS ')
  104. else
  105. printf(#27 + '[14;4H' + 'Touch mode: SINGLE SHOT');
  106. iprintf(#27 + '[6;5H' + 'Touch x = %04X, %04X' + #10, [touch.x, touch.px]);
  107. iprintf(#27 + '[7;5H' + 'Touch x = %04X, %04X' + #10, [touch.x, touch.px]);
  108. iprintf(#27 + '[0;18H' + 'keys: %08X' + #10, [keysHeld()]);
  109. iprintf(#27 + '[9;10H' + 'Frame %d' + #10, [frame]);
  110. if (TouchType = ttSingle) and not ( (pressed and KEY_TOUCH) <> 0) then continue;
  111. if ((held and KEY_TOUCH)<0) or (touch.x = 0) or (touch.y = 0) then continue;
  112. iprintf(#27 + '[12;12H' + '(%d,%d) ', [touch.px,touch.py]);
  113. if ( touch.x > max_x) then max_x := touch.x;
  114. if ( touch.y > max_y) then max_y := touch.y;
  115. if ( touch.px > max_px) then max_px := touch.px;
  116. if ( touch.py > max_py) then max_py := touch.py;
  117. if ( touch.x < min_x) then min_x := touch.x;
  118. if ( touch.y < min_y) then min_y := touch.y;
  119. if ( touch.px < min_px) then min_px := touch.px;
  120. if ( touch.py < min_py) then min_py := touch.py;
  121. iprintf(#27 + '[0;0H' + '(%d,%d) ',[min_px,min_py]);
  122. iprintf(#27 + '[1;0H' + '(%d,%d) ',[min_x,min_y]);
  123. iprintf(#27 + '[22;21H' + '(%d,%d)',[max_x,max_y]);
  124. iprintf(#27 + '[23;23H' + '(%d,%d)',[max_px,max_py]);
  125. OAMCopy[0].st.attribute[2] := 0;
  126. OAMCopy[0].st.attribute[1] := ATTR1_SIZE_32 or ((touch.px - 16) and $01FF);
  127. OAMCopy[0].st.attribute[0] := ATTR0_COLOR_256 or ATTR0_SQUARE or ((touch.py -16) and $00FF);
  128. end;
  129. end.