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