annotate XCodeiPhoneOS/Demos/src/touch.c @ 2461:f8cd2eecde64 gsoc2008_iphone

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