Mercurial > sdl-ios-xcode
comparison touchTest/Iphone Test/touchTestIPhone2/touchTestIPhone/main.c @ 4677:31607094315c
Added Iphone project. Iphone multi-touch is now functional.
author | jimtla |
---|---|
date | Sat, 31 Jul 2010 01:24:50 +0400 |
parents | |
children | f8431f66613d |
comparison
equal
deleted
inserted
replaced
4676:99b4560b7aa1 | 4677:31607094315c |
---|---|
1 #include <stdio.h> | |
2 #include <SDL.h> | |
3 #include <math.h> | |
4 #include <SDL_touch.h> | |
5 #include <SDL_gesture.h> | |
6 | |
7 #define PI 3.1415926535897 | |
8 #define PHI ((sqrt(5)-1)/2) | |
9 #define WIDTH 640 | |
10 #define HEIGHT 480 | |
11 #define BPP 4 | |
12 #define DEPTH 32 | |
13 | |
14 #define MAXFINGERS 5 | |
15 | |
16 #define DOLLARNPOINTS 64 | |
17 #define DOLLARSIZE 256 | |
18 | |
19 //MUST BE A POWER OF 2! | |
20 #define EVENT_BUF_SIZE 256 | |
21 | |
22 SDL_Event events[EVENT_BUF_SIZE]; | |
23 int eventWrite; | |
24 | |
25 int mousx,mousy; | |
26 int keystat[512]; | |
27 int bstatus; | |
28 | |
29 int colors[7] = {0xFF,0xFF00,0xFF0000,0xFFFF00,0x00FFFF,0xFF00FF,0xFFFFFF}; | |
30 | |
31 long index2fingerid[MAXFINGERS]; | |
32 int fingersDown; | |
33 | |
34 typedef struct { | |
35 float x,y; | |
36 } Point; | |
37 | |
38 typedef struct { | |
39 Point p; | |
40 float pressure; | |
41 long id; | |
42 } Finger; | |
43 | |
44 typedef struct { | |
45 Finger f; | |
46 Point cv; | |
47 float dtheta,dDist; | |
48 } TouchPoint; | |
49 | |
50 | |
51 typedef struct { //dt + s | |
52 Point d,s; //direction, start | |
53 int points; | |
54 } Line; | |
55 | |
56 | |
57 typedef struct { | |
58 float length; | |
59 | |
60 int numPoints; | |
61 Point p[EVENT_BUF_SIZE]; //To be safe | |
62 } DollarPath; | |
63 | |
64 typedef struct { | |
65 float ang,r; | |
66 Point p; | |
67 } Knob; | |
68 | |
69 Knob knob; | |
70 | |
71 Finger finger[MAXFINGERS]; | |
72 | |
73 | |
74 DollarPath dollarPath[MAXFINGERS]; | |
75 | |
76 #define MAXTEMPLATES 4 | |
77 | |
78 Point dollarTemplate[MAXTEMPLATES][DOLLARNPOINTS]; | |
79 int numDollarTemplates = 0; | |
80 #ifdef DRAW_VECTOR_EST | |
81 Line gestureLine[MAXFINGERS]; | |
82 #endif | |
83 | |
84 void handler (int sig) | |
85 { | |
86 printf ("\exiting...(%d)\n", sig); | |
87 exit (0); | |
88 } | |
89 | |
90 void perror_exit (char *error) | |
91 { | |
92 perror (error); | |
93 handler (9); | |
94 } | |
95 | |
96 | |
97 void setpix(SDL_Surface *screen, int x, int y, unsigned int col) | |
98 { | |
99 Uint32 *pixmem32; | |
100 Uint32 colour; | |
101 | |
102 if((unsigned)x > screen->w) return; | |
103 if((unsigned)y > screen->h) return; | |
104 | |
105 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x; | |
106 | |
107 Uint8 r,g,b; | |
108 float a; | |
109 | |
110 memcpy(&colour,pixmem32,screen->format->BytesPerPixel); | |
111 | |
112 SDL_GetRGB(colour,screen->format,&r,&g,&b); //Always returns 0xFFFFFF? | |
113 //r = 0;g = 0; b = 0; | |
114 a = (col>>24)&0xFF; | |
115 if(a == 0) a = 0xFF; //Hack, to make things easier. | |
116 a /= 0xFF; | |
117 r = r*(1-a) + ((col>>16)&0xFF)*(a); | |
118 g = g*(1-a) + ((col>> 8)&0xFF)*(a); | |
119 b = b*(1-a) + ((col>> 0)&0xFF)*(a); | |
120 colour = SDL_MapRGB( screen->format,r, g, b); | |
121 | |
122 | |
123 *pixmem32 = colour; | |
124 } | |
125 | |
126 void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) { | |
127 float t; | |
128 for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1))) | |
129 setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col); | |
130 } | |
131 void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c) | |
132 { | |
133 | |
134 float a; | |
135 int tx; | |
136 | |
137 int ty; | |
138 float xr; | |
139 for(ty = -abs(r);ty <= abs(r);ty++) { | |
140 xr = sqrt(r*r - ty*ty); | |
141 if(r > 0) { //r > 0 ==> filled circle | |
142 for(tx=-xr+.5;tx<=xr-.5;tx++) { | |
143 setpix(screen,x+tx,y+ty,c); | |
144 } | |
145 } | |
146 else { | |
147 setpix(screen,x-xr+.5,y+ty,c); | |
148 setpix(screen,x+xr-.5,y+ty,c); | |
149 } | |
150 } | |
151 } | |
152 | |
153 void drawKnob(SDL_Surface* screen,Knob k) { | |
154 //printf("Knob: x = %f, y = %f, r = %f, a = %f\n",k.p.x,k.p.y,k.r,k.ang); | |
155 | |
156 drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF); | |
157 | |
158 drawCircle(screen,(k.p.x+k.r/2*cos(k.ang))*screen->w, | |
159 (k.p.y+k.r/2*sin(k.ang))*screen->h,k.r/4*screen->w,0); | |
160 | |
161 } | |
162 | |
163 void DrawScreen(SDL_Surface* screen, int h) | |
164 { | |
165 int x, y, xm,ym,c; | |
166 if(SDL_MUSTLOCK(screen)) | |
167 { | |
168 if(SDL_LockSurface(screen) < 0) return; | |
169 } | |
170 for(y = 0; y < screen->h; y++ ) | |
171 { | |
172 for( x = 0; x < screen->w; x++ ) | |
173 { | |
174 //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h); | |
175 //xm = (x+h)%screen->w; | |
176 //ym = (y+h)%screen->w; | |
177 //c = sin(h/256*2*PI)*x*y/screen->w/screen->h; | |
178 //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c); | |
179 setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255); | |
180 //setpix(screen,x,y,0); //Inefficient, but that's okay... | |
181 } | |
182 } | |
183 drawCircle(screen,mousx,mousy,-30,0xFFFFFF); | |
184 drawLine(screen,0,0,screen->w,screen->h,0xFFFFFF); | |
185 | |
186 int i; | |
187 //draw Touch History | |
188 TouchPoint gestureLast[MAXFINGERS]; | |
189 //printf("------------------Start History------------------\n"); | |
190 for(i = 0;i < MAXFINGERS;i++) { | |
191 gestureLast[i].f.id = -1; | |
192 } | |
193 int numDownFingers = 0; | |
194 Point centroid; | |
195 float gdtheta,gdDist; | |
196 | |
197 | |
198 for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) { | |
199 SDL_Event event = events[i&(EVENT_BUF_SIZE-1)]; | |
200 int age = eventWrite - i - 1; | |
201 if(event.type == SDL_FINGERMOTION || | |
202 event.type == SDL_FINGERDOWN || | |
203 event.type == SDL_FINGERUP) { | |
204 SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId); | |
205 //SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId); | |
206 | |
207 float x = ((float)event.tfinger.x)/inTouch->xres; | |
208 float y = ((float)event.tfinger.y)/inTouch->yres; | |
209 | |
210 //draw the touch: | |
211 | |
212 unsigned int c = colors[event.tfinger.touchId%7]; | |
213 unsigned int col = | |
214 ((unsigned int)(c*(.1+.85))) | | |
215 ((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24; | |
216 | |
217 if(event.type == SDL_FINGERMOTION) | |
218 drawCircle(screen,x*screen->w,y*screen->h,5,col); | |
219 else if(event.type == SDL_FINGERDOWN) | |
220 drawCircle(screen,x*screen->w,y*screen->h,-10,col); | |
221 /* | |
222 //if there is a centroid, draw it | |
223 if(numDownFingers > 1) { | |
224 unsigned int col = | |
225 ((unsigned int)(0xFFFFFF)) | | |
226 ((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24; | |
227 drawCircle(screen,centroid.x*screen->w,centroid.y*screen->h,5,col); | |
228 } | |
229 */ | |
230 } | |
231 } | |
232 | |
233 | |
234 for(i=0;i<MAXFINGERS;i++) | |
235 if(finger[i].p.x >= 0 && finger[i].p.y >= 0) | |
236 if(finger[i].pressure > 0) | |
237 drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h | |
238 ,20,0xFF*finger[i].pressure); | |
239 else | |
240 drawCircle(screen,finger[i].p.x*screen->w,finger[i].p.y*screen->h | |
241 ,20,0xFF); | |
242 | |
243 | |
244 | |
245 keystat[32] = 0; | |
246 | |
247 if(knob.p.x > 0) | |
248 drawKnob(screen,knob); | |
249 | |
250 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); | |
251 | |
252 SDL_Flip(screen); | |
253 } | |
254 | |
255 SDL_Surface* initScreen(int width,int height) | |
256 { | |
257 return SDL_SetVideoMode(width, height, DEPTH, | |
258 SDL_HWSURFACE | SDL_RESIZABLE); | |
259 } | |
260 | |
261 int main(int argc, char* argv[]) | |
262 { | |
263 SDL_Surface *screen; | |
264 SDL_Event event; | |
265 | |
266 knob.r = .1; | |
267 knob.ang = 0; | |
268 TouchPoint gestureLast[MAXFINGERS]; | |
269 | |
270 int h,i; | |
271 | |
272 memset(keystat,0,512*sizeof(keystat[0])); | |
273 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; | |
274 | |
275 if (!(screen = initScreen(WIDTH,HEIGHT))) | |
276 { | |
277 SDL_Quit(); | |
278 return 1; | |
279 } | |
280 | |
281 while(!keystat[27]) { | |
282 //Poll SDL | |
283 | |
284 | |
285 while(SDL_PollEvent(&event)) | |
286 { | |
287 //Record _all_ events | |
288 events[eventWrite & (EVENT_BUF_SIZE-1)] = event; | |
289 eventWrite++; | |
290 | |
291 switch (event.type) | |
292 { | |
293 case SDL_QUIT: | |
294 keystat[27] = 1; | |
295 break; | |
296 case SDL_KEYDOWN: | |
297 //printf("%i\n",event.key.keysym.sym); | |
298 keystat[event.key.keysym.sym] = 1; | |
299 if(event.key.keysym.sym == 32) { | |
300 SDL_RecordGesture(-1); | |
301 } | |
302 else if(event.key.keysym.sym == 115) { | |
303 SDL_RWops *src; | |
304 //fp = fopen("gestureSave","w"); | |
305 src = SDL_RWFromFile("gestureSave","w"); | |
306 | |
307 printf("Wrote %i templates\n",SDL_SaveAllDollarTemplates(src)); | |
308 //fclose(fp); | |
309 SDL_RWclose(src); | |
310 } | |
311 else if(event.key.keysym.sym == 108) { | |
312 SDL_RWops *src; | |
313 //fp = fopen("gestureSave","r"); | |
314 src = SDL_RWFromFile("gestureSave","r"); | |
315 printf("Loaded: %i\n",SDL_LoadDollarTemplates(-1,src)); | |
316 //fclose(fp); | |
317 SDL_RWclose(src); | |
318 } | |
319 | |
320 //keypress = 1; | |
321 break; | |
322 case SDL_KEYUP: | |
323 //printf("%i\n",event.key.keysym.sym); | |
324 keystat[event.key.keysym.sym] = 0; | |
325 //keypress = 1; | |
326 break; | |
327 case SDL_VIDEORESIZE: | |
328 if (!(screen = initScreen(event.resize.w, | |
329 event.resize.h))) | |
330 { | |
331 SDL_Quit(); | |
332 return 1; | |
333 } | |
334 break; | |
335 case SDL_MOUSEMOTION: | |
336 mousx = event.motion.x; | |
337 mousy = event.motion.y; | |
338 break; | |
339 case SDL_MOUSEBUTTONDOWN: | |
340 bstatus |= (1<<(event.button.button-1)); | |
341 break; | |
342 case SDL_MOUSEBUTTONUP: | |
343 bstatus &= ~(1<<(event.button.button-1)); | |
344 break; | |
345 case SDL_FINGERMOTION: | |
346 ; | |
347 printf("Finger: %li,x: %f, y: %f\n",event.tfinger.fingerId, | |
348 event.tfinger.x,event.tfinger.y); | |
349 SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId); | |
350 //SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId); | |
351 for(i = 0;i<MAXFINGERS;i++) | |
352 if(index2fingerid[i] == event.tfinger.fingerId) | |
353 break; | |
354 if(i == MAXFINGERS) break; | |
355 if(inTouch > 0) { | |
356 finger[i].p.x = ((float)event.tfinger.x)/ | |
357 inTouch->xres; | |
358 finger[i].p.y = ((float)event.tfinger.y)/ | |
359 inTouch->yres; | |
360 | |
361 finger[i].pressure = | |
362 ((float)event.tfinger.pressure)/inTouch->pressureres; | |
363 /* | |
364 printf("Finger: %i, Pressure: %f Pressureres: %i\n", | |
365 event.tfinger.fingerId, | |
366 finger[i].pressure, | |
367 inTouch->pressureres); | |
368 */ | |
369 //printf("Finger: %i, pressure: %f\n",event.tfinger.fingerId, | |
370 // finger[event.tfinger.fingerId].pressure); | |
371 } | |
372 | |
373 break; | |
374 case SDL_FINGERDOWN: | |
375 printf("Finger: %li down - x: %f, y: %f\n",event.tfinger.fingerId, | |
376 event.tfinger.x,event.tfinger.y); | |
377 | |
378 for(i = 0;i<MAXFINGERS;i++) | |
379 if(index2fingerid[i] == -1) { | |
380 index2fingerid[i] = event.tfinger.fingerId; | |
381 break; | |
382 } | |
383 finger[i].p.x = event.tfinger.x; | |
384 finger[i].p.y = event.tfinger.y; | |
385 break; | |
386 case SDL_FINGERUP: | |
387 printf("Figner: %li up - x: %f, y: %f\n",event.tfinger.fingerId, | |
388 event.tfinger.x,event.tfinger.y); | |
389 for(i = 0;i<MAXFINGERS;i++) | |
390 if(index2fingerid[i] == event.tfinger.fingerId) { | |
391 index2fingerid[i] = -1; | |
392 break; | |
393 } | |
394 finger[i].p.x = -1; | |
395 finger[i].p.y = -1; | |
396 break; | |
397 case SDL_MULTIGESTURE: | |
398 knob.p.x = event.mgesture.x; | |
399 knob.p.y = event.mgesture.y; | |
400 knob.ang += event.mgesture.dTheta; | |
401 knob.r += event.mgesture.dDist; | |
402 break; | |
403 case SDL_DOLLARGESTURE: | |
404 printf("Gesture %lu performed, error: %f\n", | |
405 event.dgesture.gestureId, | |
406 event.dgesture.error); | |
407 break; | |
408 case SDL_DOLLARRECORD: | |
409 printf("Recorded gesture: %lu\n",event.dgesture.gestureId); | |
410 break; | |
411 } | |
412 } | |
413 DrawScreen(screen,h); | |
414 for(i = 0; i < 256; i++) | |
415 if(keystat[i]) | |
416 printf("Key %i down\n",i); | |
417 } | |
418 SDL_Quit(); | |
419 | |
420 return 0; | |
421 } | |
422 |