Mercurial > sdl-ios-xcode
comparison test/testgesture.c @ 4918:f5f70fed2c4c
Added a Visual Studio project for testgesture
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 30 Nov 2010 12:38:46 -0800 |
parents | f9ab8df6d45a |
children | 40a43c6d9220 |
comparison
equal
deleted
inserted
replaced
4917:7dafe21ca5d4 | 4918:f5f70fed2c4c |
---|---|
60 | 60 |
61 Knob knob; | 61 Knob knob; |
62 | 62 |
63 void handler (int sig) | 63 void handler (int sig) |
64 { | 64 { |
65 printf ("\exiting...(%d)\n", sig); | 65 printf ("exiting...(%d)\n", sig); |
66 exit (0); | 66 exit (0); |
67 } | 67 } |
68 | 68 |
69 void perror_exit (char *error) | 69 void perror_exit (char *error) |
70 { | 70 { |
71 perror (error); | 71 perror (error); |
72 handler (9); | 72 handler (9); |
73 } | 73 } |
74 | 74 |
75 void setpix(SDL_Surface *screen, int x, int y, unsigned int col) | 75 void setpix(SDL_Surface *screen, float _x, float _y, unsigned int col) |
76 { | 76 { |
77 Uint32 *pixmem32; | 77 Uint32 *pixmem32; |
78 Uint32 colour; | 78 Uint32 colour; |
79 | 79 Uint8 r,g,b; |
80 if((unsigned)x > screen->w) return; | 80 int x = (int)_x; |
81 if((unsigned)y > screen->h) return; | 81 int y = (int)_y; |
82 float a; | |
83 | |
84 if(x > screen->w) return; | |
85 if(y > screen->h) return; | |
82 | 86 |
83 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x; | 87 pixmem32 = (Uint32*) screen->pixels + y*screen->pitch/BPP + x; |
84 | 88 |
85 Uint8 r,g,b; | 89 SDL_memcpy(&colour,pixmem32,screen->format->BytesPerPixel); |
86 float a; | |
87 | |
88 memcpy(&colour,pixmem32,screen->format->BytesPerPixel); | |
89 | 90 |
90 SDL_GetRGB(colour,screen->format,&r,&g,&b); | 91 SDL_GetRGB(colour,screen->format,&r,&g,&b); |
91 //r = 0;g = 0; b = 0; | 92 //r = 0;g = 0; b = 0; |
92 a = (col>>24)&0xFF; | 93 a = (float)((col>>24)&0xFF); |
93 if(a == 0) a = 0xFF; //Hack, to make things easier. | 94 if(a == 0) a = 0xFF; //Hack, to make things easier. |
94 a /= 0xFF; | 95 a /= 0xFF; |
95 r = r*(1-a) + ((col>>16)&0xFF)*(a); | 96 r = (Uint8)(r*(1-a) + ((col>>16)&0xFF)*(a)); |
96 g = g*(1-a) + ((col>> 8)&0xFF)*(a); | 97 g = (Uint8)(g*(1-a) + ((col>> 8)&0xFF)*(a)); |
97 b = b*(1-a) + ((col>> 0)&0xFF)*(a); | 98 b = (Uint8)(b*(1-a) + ((col>> 0)&0xFF)*(a)); |
98 colour = SDL_MapRGB( screen->format,r, g, b); | 99 colour = SDL_MapRGB( screen->format,r, g, b); |
99 | 100 |
100 | 101 |
101 *pixmem32 = colour; | 102 *pixmem32 = colour; |
102 } | 103 } |
103 | 104 |
104 void drawLine(SDL_Surface *screen,int x0,int y0,int x1,int y1,unsigned int col) { | 105 void drawLine(SDL_Surface *screen,float x0,float y0,float x1,float y1,unsigned int col) { |
105 float t; | 106 float t; |
106 for(t=0;t<1;t+=1.f/SDL_max(abs(x0-x1),abs(y0-y1))) | 107 for(t=0;t<1;t+=(float)(1.f/SDL_max(SDL_fabs(x0-x1),SDL_fabs(y0-y1)))) |
107 setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col); | 108 setpix(screen,x1+t*(x0-x1),y1+t*(y0-y1),col); |
108 } | 109 } |
109 | 110 |
110 void drawCircle(SDL_Surface* screen,int x,int y,int r,unsigned int c) | 111 void drawCircle(SDL_Surface* screen,float x,float y,float r,unsigned int c) |
111 { | 112 { |
112 int tx,ty; | 113 float tx,ty; |
113 float xr; | 114 float xr; |
114 for(ty = -abs(r);ty <= abs(r);ty++) { | 115 for(ty = (float)-SDL_fabs(r);ty <= (float)SDL_fabs((int)r);ty++) { |
115 xr = sqrt(r*r - ty*ty); | 116 xr = (float)sqrt(r*r - ty*ty); |
116 if(r > 0) { //r > 0 ==> filled circle | 117 if(r > 0) { //r > 0 ==> filled circle |
117 for(tx=-xr+.5;tx<=xr-.5;tx++) { | 118 for(tx=-xr+.5f;tx<=xr-.5;tx++) { |
118 setpix(screen,x+tx,y+ty,c); | 119 setpix(screen,x+tx,y+ty,c); |
119 } | 120 } |
120 } | 121 } |
121 else { | 122 else { |
122 setpix(screen,x-xr+.5,y+ty,c); | 123 setpix(screen,x-xr+.5f,y+ty,c); |
123 setpix(screen,x+xr-.5,y+ty,c); | 124 setpix(screen,x+xr-.5f,y+ty,c); |
124 } | 125 } |
125 } | 126 } |
126 } | 127 } |
127 | 128 |
128 void drawKnob(SDL_Surface* screen,Knob k) { | 129 void drawKnob(SDL_Surface* screen,Knob k) { |
129 drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF); | 130 drawCircle(screen,k.p.x*screen->w,k.p.y*screen->h,k.r*screen->w,0xFFFFFF); |
130 drawCircle(screen,(k.p.x+k.r/2*cos(k.ang))*screen->w, | 131 drawCircle(screen,(k.p.x+k.r/2*cosf(k.ang))*screen->w, |
131 (k.p.y+k.r/2*sin(k.ang))*screen->h,k.r/4*screen->w,0); | 132 (k.p.y+k.r/2*sinf(k.ang))*screen->h,k.r/4*screen->w,0); |
132 } | 133 } |
133 | 134 |
134 void DrawScreen(SDL_Surface* screen) | 135 void DrawScreen(SDL_Surface* screen) |
135 { | 136 { |
136 int x, y; | 137 int x, y, i; |
137 if(SDL_MUSTLOCK(screen)) | 138 if(SDL_MUSTLOCK(screen)) |
138 { | 139 { |
139 if(SDL_LockSurface(screen) < 0) return; | 140 if(SDL_LockSurface(screen) < 0) return; |
140 } | 141 } |
141 for(y = 0;y < screen->h;y++) | 142 for(y = 0;y < screen->h;y++) |
142 for(x = 0;x < screen->w;x++) | 143 for(x = 0;x < screen->w;x++) |
143 setpix(screen,x,y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255); | 144 setpix(screen,(float)x,(float)y,((x%255)<<16) + ((y%255)<<8) + (x+y)%255); |
144 | 145 |
145 int i; | |
146 //draw Touch History | 146 //draw Touch History |
147 for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) { | 147 for(i = SDL_max(0,eventWrite - EVENT_BUF_SIZE);i < eventWrite;i++) { |
148 SDL_Event event = events[i&(EVENT_BUF_SIZE-1)]; | 148 SDL_Event event = events[i&(EVENT_BUF_SIZE-1)]; |
149 int age = eventWrite - i - 1; | 149 int age = eventWrite - i - 1; |
150 float x, y; | |
151 unsigned int c, col; | |
152 | |
150 if(event.type == SDL_FINGERMOTION || | 153 if(event.type == SDL_FINGERMOTION || |
151 event.type == SDL_FINGERDOWN || | 154 event.type == SDL_FINGERDOWN || |
152 event.type == SDL_FINGERUP) { | 155 event.type == SDL_FINGERUP) { |
153 SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId); | 156 SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId); |
154 if(inTouch == NULL) continue; | 157 if(inTouch == NULL) continue; |
155 | 158 |
156 float x = ((float)event.tfinger.x)/inTouch->xres; | 159 x = ((float)event.tfinger.x)/inTouch->xres; |
157 float y = ((float)event.tfinger.y)/inTouch->yres; | 160 y = ((float)event.tfinger.y)/inTouch->yres; |
158 | 161 |
159 //draw the touch: | 162 //draw the touch: |
160 unsigned int c = colors[event.tfinger.touchId%7]; | 163 c = colors[event.tfinger.touchId%7]; |
161 unsigned int col = | 164 col = |
162 ((unsigned int)(c*(.1+.85))) | | 165 ((unsigned int)(c*(.1+.85))) | |
163 ((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24; | 166 ((unsigned int)((0xFF*(1-((float)age)/EVENT_BUF_SIZE))) & 0xFF)<<24; |
164 | 167 |
165 if(event.type == SDL_FINGERMOTION) | 168 if(event.type == SDL_FINGERMOTION) |
166 drawCircle(screen,x*screen->w,y*screen->h,5,col); | 169 drawCircle(screen,x*screen->w,y*screen->h,5,col); |
184 | 187 |
185 int main(int argc, char* argv[]) | 188 int main(int argc, char* argv[]) |
186 { | 189 { |
187 SDL_Surface *screen; | 190 SDL_Surface *screen; |
188 SDL_Event event; | 191 SDL_Event event; |
189 | |
190 //gesture variables | |
191 knob.r = .1; | |
192 knob.ang = 0; | |
193 | |
194 | |
195 SDL_bool quitting = SDL_FALSE; | 192 SDL_bool quitting = SDL_FALSE; |
196 SDL_RWops *src; | 193 SDL_RWops *src; |
194 | |
195 //gesture variables | |
196 knob.r = .1f; | |
197 knob.ang = 0; | |
197 | 198 |
198 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; | 199 if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1; |
199 | 200 |
200 if (!(screen = initScreen(WIDTH,HEIGHT))) | 201 if (!(screen = initScreen(WIDTH,HEIGHT))) |
201 { | 202 { |
242 { | 243 { |
243 SDL_Quit(); | 244 SDL_Quit(); |
244 return 1; | 245 return 1; |
245 } | 246 } |
246 break; | 247 break; |
247 case SDL_FINGERMOTION: | 248 case SDL_FINGERMOTION: |
248 ; | |
249 #if VERBOSE | 249 #if VERBOSE |
250 printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId, | 250 printf("Finger: %i,x: %i, y: %i\n",event.tfinger.fingerId, |
251 event.tfinger.x,event.tfinger.y); | 251 event.tfinger.x,event.tfinger.y); |
252 #endif | 252 #endif |
253 SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId); | 253 { |
254 SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId); | 254 SDL_Touch* inTouch = SDL_GetTouch(event.tfinger.touchId); |
255 SDL_Finger* inFinger = SDL_GetFinger(inTouch,event.tfinger.fingerId); | |
256 } | |
255 break; | 257 break; |
256 case SDL_FINGERDOWN: | 258 case SDL_FINGERDOWN: |
257 #if VERBOSE | 259 #if VERBOSE |
258 printf("Finger: %"PRIs64" down - x: %i, y: %i\n", | 260 printf("Finger: %"PRIs64" down - x: %i, y: %i\n", |
259 event.tfinger.fingerId,event.tfinger.x,event.tfinger.y); | 261 event.tfinger.fingerId,event.tfinger.x,event.tfinger.y); |