touchTest.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. program touchTest;
  2. {$L build/ballpalette.bin.o}
  3. {$L build/balldata.bin.o}
  4. uses
  5. nds9, ctypes;
  6. var
  7. OAMCopy: array [0..127] of SpriteEntry;
  8. {$include inc/ballpalette.bin.inc}
  9. {$include inc/balldata.bin.inc}
  10. procedure initOAM();
  11. var
  12. i: integer;
  13. begin
  14. for i := 0 to 127 do
  15. OAMCopy[i].attribute[0] := ATTR0_DISABLED;
  16. end;
  17. procedure updateOAM();
  18. begin
  19. Move(OAMCopy, OAM^, 128 * sizeof(SpriteEntry));
  20. end;
  21. type
  22. TTouchType = (ttContinuous, ttSingle);
  23. var
  24. frame: integer;
  25. TouchType: TTouchType = ttContinuous;
  26. procedure Vblank();
  27. begin
  28. inc(frame);
  29. end;
  30. var
  31. min_x, min_y, max_x, max_y: integer;
  32. min_px, min_py, max_px, max_py: integer;
  33. touch: touchPosition;
  34. i: integer;
  35. pressed, held: integer;
  36. begin
  37. min_x := 4096;
  38. min_y := 4096;
  39. max_x := 0;
  40. max_y := 0;
  41. min_px := 4096;
  42. min_py := 4096;
  43. max_px := 0;
  44. max_py := 0;
  45. // put the main screen on the bottom lcd
  46. lcdMainOnBottom();
  47. initOAM();
  48. //set the video mode
  49. videoSetMode( MODE_0_2D or
  50. DISPLAY_SPR_ACTIVE or //turn on sprites
  51. DISPLAY_BG0_ACTIVE or //turn on background 0
  52. DISPLAY_SPR_1D //this is used when in tile mode
  53. );
  54. // Sprite initialisation
  55. Move(ballpalette_bin, SPRITE_PALETTE^, ballpalette_bin_size);
  56. Move(balldata_bin, SPRITE_GFX^, balldata_bin_size);
  57. consoleInit(nil, 0, BgType_Text4bpp, BgSize_T_256x256, 31, 0, true, true);
  58. iprintf(#27'[4;8H' + 'Touch Screen Test');
  59. iprintf(#27'[15;4H' + 'Right Shoulder toggles');
  60. while true do
  61. begin
  62. swiWaitForVBlank();
  63. updateOAM();
  64. // read the button states
  65. scanKeys();
  66. // read the touchscreen coordinates
  67. touchRead(touch);
  68. pressed := keysDown(); // buttons pressed this loop
  69. held := keysHeld(); // buttons currently held
  70. // Right Shoulder button toggles the mode
  71. if ( pressed and KEY_R) <> 0 then Inc(TouchType);
  72. if TouchType = ttContinuous then
  73. printf(#27 + '[14;4H' + 'Touch mode: CONTINUOUS ')
  74. else
  75. printf(#27 + '[14;4H' + 'Touch mode: SINGLE SHOT');
  76. iprintf(#27'[6;5H' + 'Touch x = %04X, %04X'#10, touch.rawx, touch.px);
  77. iprintf(#27'[7;5H' + 'Touch y = %04X, %04X'#10, touch.rawy, touch.py);
  78. iprintf(#27'[0;18H' + 'keys: %08X'#10, keysHeld());
  79. iprintf(#27'[9;10H' + 'Frame %d'#10, frame);
  80. if (TouchType = ttSingle) and not ( (pressed and KEY_TOUCH) <> 0) then continue;
  81. if ((held and KEY_TOUCH) = 0) or (touch.rawx = 0) or (touch.rawy = 0) then continue;
  82. iprintf(#27'[12;12H' + '(%d,%d) ', touch.px, touch.py);
  83. if ( touch.rawx > max_x) then max_x := touch.rawx;
  84. if ( touch.rawy > max_y) then max_y := touch.rawy;
  85. if ( touch.px > max_px) then max_px := touch.px;
  86. if ( touch.py > max_py) then max_py := touch.py;
  87. if ( touch.rawx < min_x) then min_x := touch.rawx;
  88. if ( touch.rawy < min_y) then min_y := touch.rawy;
  89. if ( touch.px < min_px) then min_px := touch.px;
  90. if ( touch.py < min_py) then min_py := touch.py;
  91. iprintf(#27'[0;0H' + '(%d,%d) ', min_px, min_py);
  92. iprintf(#27'[1;0H' + '(%d,%d) ', min_x, min_y);
  93. iprintf(#27'[22;21H' + '(%d,%d)', max_x, max_y);
  94. iprintf(#27'[23;23H' + '(%d,%d)', max_px, max_py);
  95. OAMCopy[0].attribute[2] := 0;
  96. OAMCopy[0].attribute[1] := ATTR1_SIZE_32 or ((touch.px - 16) and $01FF);
  97. OAMCopy[0].attribute[0] := ATTR0_COLOR_256 or ATTR0_SQUARE or ((touch.py - 16) and $00FF);
  98. end;
  99. end.