comparison touchTest/touchTest.c @ 4642:057e8762d2a1

Added reading of event* for touch events.
author Jim Grandpre <jim.tla@gmail.com>
date Fri, 28 May 2010 01:26:52 -0400
parents
children 8806b78988f7
comparison
equal deleted inserted replaced
4641:49a97daea6ec 4642:057e8762d2a1
1 #include <stdio.h>
2 #include <SDL.h>
3 #include <math.h>
4 #include "../src/events/SDL_touch_c.h" //BAD!!!
5
6 #define PI 3.1415926535897
7 #define WIDTH 640
8 #define HEIGHT 480
9 #define BPP 4
10 #define DEPTH 32
11
12 int mousx,mousy;
13 int keystat[512];
14 int bstatus;
15
16
17
18
19 typedef struct {
20 int x,y;
21 } Point;
22
23 void handler (int sig)
24 {
25 printf ("\nexiting...(%d)\n", sig);
26 exit (0);
27 }
28
29 void perror_exit (char *error)
30 {
31 perror (error);
32 handler (9);
33 }
34
35
36 void setpix(SDL_Surface *screen, int x, int y, int col)
37 {
38 Uint32 *pixmem32;
39 Uint32 colour;
40
41 if((unsigned)x > screen->w) return;
42 if((unsigned)y > screen->h) return;
43
44 colour = SDL_MapRGB( screen->format, (col>>16)&0xFF, (col>>8)&0xFF, col&0xFF);
45
46 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x;
47 *pixmem32 = colour;
48 }
49
50 void drawCircle(SDL_Surface* screen,int x,int y,int r,int c)
51 {
52
53 float a;
54 for(a=0;a<2*PI;a+=1.f/(float)r)
55 {
56 setpix(screen,(int)(x+r*cos(a)),(int)(y+r*sin(a)),c);
57 }
58 }
59
60 void DrawScreen(SDL_Surface* screen, int h)
61 {
62 int x, y, xm,ym,c;
63 if(SDL_MUSTLOCK(screen))
64 {
65 if(SDL_LockSurface(screen) < 0) return;
66 }
67 for(y = 0; y < screen->h; y++ )
68 {
69 for( x = 0; x < screen->w; x++ )
70 {
71 //setpixel(screen, x, y, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
72 //xm = (x+h)%screen->w;
73 //ym = (y+h)%screen->w;
74 //c = sin(h/256*2*PI)*x*y/screen->w/screen->h;
75 //setpix(screen,x,y,255*sin(xm/screen->w*2*PI),sin(h/255*2*PI)*255*y/screen->h,c);
76 setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255);
77 }
78 }
79 drawCircle(screen,mousx,mousy,30,0xFFFFFF);
80
81
82 if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
83
84 SDL_Flip(screen);
85 }
86
87 SDL_Surface* initScreen(int width,int height)
88 {
89 return SDL_SetVideoMode(width, height, DEPTH,
90 SDL_HWSURFACE | SDL_RESIZABLE);
91 }
92
93 int main(int argc, char* argv[])
94 {
95 SDL_Surface *screen;
96 SDL_Event event;
97
98 int keypress = 0;
99 int h=0,s=1,i,j;
100
101 memset(keystat,0,512*sizeof(keystat[0]));
102 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
103
104 if (!(screen = initScreen(WIDTH,HEIGHT)))
105 {
106 SDL_Quit();
107 return 1;
108 }
109
110 while(!keystat[27]) {
111 //Poll SDL
112 while(SDL_PollEvent(&event))
113 {
114 switch (event.type)
115 {
116 case SDL_QUIT:
117 keystat[27] = 1;
118 break;
119 case SDL_KEYDOWN:
120 //printf("%i\n",event.key.keysym.sym);
121 keystat[event.key.keysym.sym] = 1;
122 //keypress = 1;
123 break;
124 case SDL_KEYUP:
125 //printf("%i\n",event.key.keysym.sym);
126 keystat[event.key.keysym.sym] = 0;
127 //keypress = 1;
128 break;
129 case SDL_VIDEORESIZE:
130 if (!(screen = initScreen(event.resize.w,
131 event.resize.h)))
132 {
133 SDL_Quit();
134 return 1;
135 }
136 break;
137 case SDL_MOUSEMOTION:
138 mousx = event.motion.x;
139 mousy = event.motion.y;
140 break;
141 case SDL_MOUSEBUTTONDOWN:
142 bstatus |= (1<<(event.button.button-1));
143 break;
144 case SDL_MOUSEBUTTONUP:
145 bstatus &= ~(1<<(event.button.button-1));
146 break;
147 case SDL_FINGERMOTION:
148 i = 1;
149
150
151 printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId,
152 event.tfinger.x,event.tfinger.y);
153
154 break;
155 }
156 }
157 //And draw
158 DrawScreen(screen,h);
159 /*
160 for(i=0;i<512;i++)
161 if(keystat[i]) printf("%i\n",i);
162 printf("Buttons:%i\n",bstatus);
163 */
164 }
165 SDL_Quit();
166
167 return 0;
168 }