comparison XCodeiPhoneOS/Demos/src/touch.c @ 2386:c5cbbba41c65 gsoc2008_iphone

Touch input demo (multitouch)
author Holmes Futrell <hfutrell@umail.ucsb.edu>
date Fri, 18 Jul 2008 20:52:32 +0000
parents
children
comparison
equal deleted inserted replaced
2385:0705e8ebe951 2386:c5cbbba41c65
1 /*
2 * touch.c
3 * written by Holmes Futrell
4 * use however you want
5 */
6
7 #include "SDL.h"
8 #include "math.h"
9 #include "common.h"
10
11 #define BRUSH_SIZE 32 /* width and height of the brush */
12 #define PIXELS_PER_ITERATION 5 /* number of pixels between brush blots when forming a line */
13
14 static SDL_TextureID brushID=0; /* texture for the brush */
15
16 /*
17 draws a line from (startx, starty) to (startx + dx, starty + dy)
18 this is accomplished by drawing several blots spaced PIXELS_PER_ITERATION apart
19 */
20 void drawLine(float startx, float starty, float dx, float dy) {
21
22 float distance = sqrt(dx*dx+dy*dy); /* length of line segment (pythagoras) */
23 int iterations = distance / PIXELS_PER_ITERATION + 1; /* number of brush sprites to draw for the line */
24 float dx_prime = dx / iterations; /* x-shift per iteration */
25 float dy_prime = dy / iterations; /* y-shift per iteration */
26 SDL_Rect dstRect; /* rect to draw brush sprite into */
27
28 dstRect.w = BRUSH_SIZE;
29 dstRect.h = BRUSH_SIZE;
30
31 /* setup x and y for the location of the first sprite */
32 float x = startx - BRUSH_SIZE / 2.0f;
33 float y = starty - BRUSH_SIZE / 2.0f;
34
35 int i;
36 /* draw a series of blots to form the line */
37 for (i=0; i<iterations; i++) {
38 dstRect.x = x;
39 dstRect.y = y;
40 /* shift x and y for next sprite location */
41 x += dx_prime;
42 y += dy_prime;
43 /* draw brush blot */
44 SDL_RenderCopy(brushID, NULL, &dstRect);
45 }
46 }
47
48 /*
49 loads the brush texture
50 */
51 void initializeTexture() {
52 SDL_Surface *bmp_surface;
53 bmp_surface = SDL_LoadBMP("stroke.bmp");
54 if (bmp_surface == NULL) {
55 fatalError("could not load stroke.bmp");
56 }
57 brushID = SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, bmp_surface);
58 SDL_FreeSurface(bmp_surface);
59 if (brushID == 0) {
60 fatalError("could not create brush texture");
61 }
62 /* additive blending -- laying strokes on top of eachother makes them brighter */
63 SDL_SetTextureBlendMode(brushID, SDL_TEXTUREBLENDMODE_ADD);
64 /* set brush color (red) */
65 SDL_SetTextureColorMod(brushID, 255, 100, 100);
66 }
67
68 int main(int argc, char *argv[]) {
69
70 int x, y, dx, dy; /* mouse location */
71 Uint8 state; /* mouse (touch) state */
72 SDL_Event event;
73 SDL_WindowID windowID; /* main window */
74 int done; /* does user want to quit? */
75
76 /* initialize SDL */
77 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
78 fatalError("Could not initialize SDL");
79 }
80
81 /* create main window and renderer */
82 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,\
83 SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS);
84 SDL_CreateRenderer(windowID, 0, 0);
85
86 /*load brush texture */
87 initializeTexture();
88
89 /* fill canvass initially with all black */
90 SDL_RenderFill(0, 0, 0, 255, NULL);
91 SDL_RenderPresent();
92
93 done = 0;
94 while (!done && SDL_WaitEvent(&event)) {
95 switch (event.type) {
96 case SDL_QUIT:
97 done = 1;
98 break;
99 case SDL_MOUSEMOTION:
100 SDL_SelectMouse(event.motion.which); /* select 'mouse' (touch) that moved */
101 state = SDL_GetMouseState(&x, &y); /* get its location */
102 SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */
103 if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */
104 drawLine(x - dx, y - dy, dx, dy); /* draw line segment */
105 SDL_RenderPresent();
106 }
107 break;
108 }
109 }
110
111 /* cleanup */
112 SDL_DestroyTexture(brushID);
113 SDL_Quit();
114
115 return 0;
116 }