Mercurial > sdl-ios-xcode
comparison touchTest/touchPong.c~ @ 4639:f5cd4b6231ba
Added Test Directory. Touch input works in touchSimp for wacom bamboo fun on Ubuntu linux. Not yet integrated into library. Should support other touch devices and other linux distros, but not tested on anything else.
author | Jim Grandpre <jim.tla@gmail.com> |
---|---|
date | Mon, 24 May 2010 23:44:24 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4464:fa77a6429698 | 4639:f5cd4b6231ba |
---|---|
1 #include <stdio.h> | |
2 #include <SDL.h> | |
3 #include <math.h> | |
4 #include <linux/input.h> | |
5 #include <fcntl.h> | |
6 | |
7 | |
8 #define PI 3.1415926535897 | |
9 #define WIDTH 640 | |
10 #define HEIGHT 480 | |
11 #define BPP 4 | |
12 #define DEPTH 32 | |
13 | |
14 | |
15 #define MAX_FINGERS 2 | |
16 | |
17 int mousx,mousy; | |
18 int keystat[512]; | |
19 int bstatus; | |
20 | |
21 | |
22 //Pong Code | |
23 Point ball,ballv; | |
24 | |
25 typedef struct { | |
26 int x,y; | |
27 } Point; | |
28 | |
29 Point finger[MAX_FINGERS]; | |
30 | |
31 void handler (int sig) | |
32 { | |
33 printf ("\nexiting...(%d)\n", sig); | |
34 exit (0); | |
35 } | |
36 | |
37 void perror_exit (char *error) | |
38 { | |
39 perror (error); | |
40 handler (9); | |
41 } | |
42 | |
43 | |
44 void setpix(SDL_Surface *screen, int x, int y, int col) | |
45 { | |
46 Uint32 *pixmem32; | |
47 Uint32 colour; | |
48 | |
49 if((unsigned)x > screen->w) return; | |
50 if((unsigned)y > screen->h) return; | |
51 | |
52 colour = SDL_MapRGB( screen->format, (col>>16)&0xFF, (col>>8)&0xFF, col&0xFF); | |
53 | |
54 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x; | |
55 *pixmem32 = colour; | |
56 } | |
57 | |
58 void drawCircle(SDL_Surface* screen,int x,int y,int r,int c) | |
59 { | |
60 | |
61 float a; | |
62 for(a=0;a<2*PI;a+=1.f/(float)r) | |
63 { | |
64 setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c); | |
65 } | |
66 } | |
67 | |
68 void DrawScreen(SDL_Surface* screen, int h) | |
69 { | |
70 int x, y, xm,ym,c; | |
71 if(SDL_MUSTLOCK(screen)) | |
72 { | |
73 if(SDL_LockSurface(screen) < 0) return; | |
74 } | |
75 for(y = 0; y < screen->h; y++ ) | |
76 { | |
77 for( x = 0; x < screen->w; x++ ) | |
78 { | |
79 //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h); | |
80 //xm = (x+h)%screen->w; | |
81 //ym = (y+h)%screen->w; | |
82 //c = sin(h/256*2*PI)*x*y/screen->w/screen->h; | |
83 //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c); | |
84 setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255); | |
85 } | |
86 } | |
87 drawCircle(screen,mousx,mousy,30,0xFFFFFF); | |
88 int i; | |
89 for(i=0;i<MAX_FINGERS;i++) | |
90 drawCircle(screen,finger[i].x,finger[i].y,30,0xFFFFFF); | |
91 | |
92 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); | |
93 | |
94 SDL_Flip(screen); | |
95 } | |
96 | |
97 SDL_Surface* initScreen(int width,int height) | |
98 { | |
99 return SDL_SetVideoMode(width, height, DEPTH, | |
100 SDL_HWSURFACE | SDL_RESIZABLE); | |
101 } | |
102 | |
103 int main(int argc, char* argv[]) | |
104 { | |
105 struct input_event ev[64]; | |
106 int fd, rd, value, size = sizeof (struct input_event); | |
107 char name[256] = "Unknown"; | |
108 char *device = NULL; | |
109 | |
110 | |
111 | |
112 //Setup check | |
113 if (argv[1] == NULL){ | |
114 printf("Please specify (on the command line) the path to the dev event interface device\n"); | |
115 exit (0); | |
116 } | |
117 | |
118 if ((getuid ()) != 0) | |
119 printf ("You are not root! This may not work...\n"); | |
120 | |
121 if (argc > 1) | |
122 device = argv[1]; | |
123 | |
124 //Open Device | |
125 if ((fd = open (device, O_RDONLY)) == -1) | |
126 printf ("%s is not a vaild device.\n", device); | |
127 | |
128 //Print Device Name | |
129 ioctl (fd, EVIOCGNAME (sizeof (name)), name); | |
130 printf ("Reading From : %s (%s)\n", device, name); | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 SDL_Surface *screen; | |
137 SDL_Event event; | |
138 | |
139 int keypress = 0; | |
140 int h=0,s=1,i,j; | |
141 int tx,ty,curf=0; | |
142 | |
143 | |
144 | |
145 memset(keystat,0,512*sizeof(keystat[0])); | |
146 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; | |
147 | |
148 if (!(screen = initScreen(WIDTH,HEIGHT))) | |
149 { | |
150 SDL_Quit(); | |
151 return 1; | |
152 } | |
153 ball.x = screen->width()/2; | |
154 ball.y = screen->height()/2; | |
155 while(!keystat[27]) | |
156 { | |
157 //Poll SDL | |
158 while(SDL_PollEvent(&event)) | |
159 { | |
160 switch (event.type) | |
161 { | |
162 case SDL_QUIT: | |
163 keystat[27] = 1; | |
164 break; | |
165 case SDL_KEYDOWN: | |
166 //printf("%i\n",event.key.keysym.sym); | |
167 keystat[event.key.keysym.sym] = 1; | |
168 //keypress = 1; | |
169 break; | |
170 case SDL_KEYUP: | |
171 //printf("%i\n",event.key.keysym.sym); | |
172 keystat[event.key.keysym.sym] = 0; | |
173 //keypress = 1; | |
174 break; | |
175 case SDL_VIDEORESIZE: | |
176 if (!(screen = initScreen(event.resize.w, | |
177 event.resize.h))) | |
178 { | |
179 SDL_Quit(); | |
180 return 1; | |
181 } | |
182 break; | |
183 case SDL_MOUSEMOTION: | |
184 mousx = event.motion.x; | |
185 mousy = event.motion.y; | |
186 break; | |
187 case SDL_MOUSEBUTTONDOWN: | |
188 bstatus |= (1<<(event.button.button-1)); | |
189 break; | |
190 case SDL_MOUSEBUTTONUP: | |
191 bstatus &= ~(1<<(event.button.button-1)); | |
192 break; | |
193 } | |
194 } | |
195 | |
196 //poll for Touch <- Goal: make this a case: | |
197 if ((rd = read (fd, ev, size * 64)) < size) | |
198 perror_exit ("read()"); | |
199 //printf("time: %i\n type: %X\n code: %X\n value: %i\n ", | |
200 // ev->time,ev->type,ev->code,ev->value); | |
201 for (i = 0; i < rd / sizeof(struct input_event); i++) | |
202 switch (ev[i].type) | |
203 { | |
204 case EV_ABS: | |
205 if(ev[i].code == ABS_X) | |
206 tx = ev[i].value; | |
207 else if (ev[i].code == ABS_Y) | |
208 ty = ev[i].value; | |
209 else if (ev[i].code == ABS_MISC) | |
210 { | |
211 //printf("Misc:type: %X\n code: %X\n value: %i\n ", | |
212 // ev[i].type,ev[i].code,ev[i].value); | |
213 } | |
214 break; | |
215 case EV_MSC: | |
216 if(ev[i].code == MSC_SERIAL) | |
217 curf = ev[i].value; | |
218 break; | |
219 case EV_SYN: | |
220 curf -= 1; | |
221 if(tx >= 0) | |
222 finger[curf].x = tx; | |
223 if(ty >= 0) | |
224 finger[curf].y = ty; | |
225 | |
226 //printf("Synched: %i tx: %i, ty: %i\n",curf,finger[curf].x,finger[curf].y); | |
227 tx = -1; | |
228 ty = -1; | |
229 break; | |
230 | |
231 } | |
232 //And draw | |
233 DrawScreen(screen,h); | |
234 /* | |
235 for(i=0;i<512;i++) | |
236 if(keystat[i]) printf("%i\n",i); | |
237 printf("Buttons:%i\n",bstatus); | |
238 */ | |
239 } | |
240 SDL_Quit(); | |
241 | |
242 return 0; | |
243 } |