comparison Xcode-iPhoneOS/Demos/src/touch.c @ 3277:20326ba2bda2

This name inconsistency has been bugging me for a while...
author Sam Lantinga <slouken@libsdl.org>
date Sat, 19 Sep 2009 07:32:36 +0000
parents
children 64ce267332c6
comparison
equal deleted inserted replaced
3276:720d176be107 3277:20326ba2bda2
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
21 drawLine(float startx, float starty, float dx, float dy)
22 {
23
24 float distance = sqrt(dx * dx + dy * dy); /* length of line segment (pythagoras) */
25 int iterations = distance / PIXELS_PER_ITERATION + 1; /* number of brush sprites to draw for the line */
26 float dx_prime = dx / iterations; /* x-shift per iteration */
27 float dy_prime = dy / iterations; /* y-shift per iteration */
28 SDL_Rect dstRect; /* rect to draw brush sprite into */
29
30 dstRect.w = BRUSH_SIZE;
31 dstRect.h = BRUSH_SIZE;
32
33 /* setup x and y for the location of the first sprite */
34 float x = startx - BRUSH_SIZE / 2.0f;
35 float y = starty - BRUSH_SIZE / 2.0f;
36
37 int i;
38 /* draw a series of blots to form the line */
39 for (i = 0; i < iterations; i++) {
40 dstRect.x = x;
41 dstRect.y = y;
42 /* shift x and y for next sprite location */
43 x += dx_prime;
44 y += dy_prime;
45 /* draw brush blot */
46 SDL_RenderCopy(brushID, NULL, &dstRect);
47 }
48 }
49
50 /*
51 loads the brush texture
52 */
53 void
54 initializeTexture()
55 {
56 SDL_Surface *bmp_surface;
57 bmp_surface = SDL_LoadBMP("stroke.bmp");
58 if (bmp_surface == NULL) {
59 fatalError("could not load stroke.bmp");
60 }
61 brushID =
62 SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, bmp_surface);
63 SDL_FreeSurface(bmp_surface);
64 if (brushID == 0) {
65 fatalError("could not create brush texture");
66 }
67 /* additive blending -- laying strokes on top of eachother makes them brighter */
68 SDL_SetTextureBlendMode(brushID, SDL_BLENDMODE_ADD);
69 /* set brush color (red) */
70 SDL_SetTextureColorMod(brushID, 255, 100, 100);
71 }
72
73 int
74 main(int argc, char *argv[])
75 {
76
77 int x, y, dx, dy; /* mouse location */
78 Uint8 state; /* mouse (touch) state */
79 SDL_Event event;
80 SDL_WindowID windowID; /* main window */
81 int done; /* does user want to quit? */
82
83 /* initialize SDL */
84 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
85 fatalError("Could not initialize SDL");
86 }
87
88 /* create main window and renderer */
89 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
90 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
91 SDL_WINDOW_BORDERLESS);
92 SDL_CreateRenderer(windowID, 0, 0);
93
94 /*load brush texture */
95 initializeTexture();
96
97 /* fill canvass initially with all black */
98 SDL_SetRenderDrawColor(0, 0, 0, 255);
99 SDL_RenderFill(NULL);
100 SDL_RenderPresent();
101
102 done = 0;
103 while (!done && SDL_WaitEvent(&event)) {
104 switch (event.type) {
105 case SDL_QUIT:
106 done = 1;
107 break;
108 case SDL_MOUSEMOTION:
109 SDL_SelectMouse(event.motion.which); /* select 'mouse' (touch) that moved */
110 state = SDL_GetMouseState(&x, &y); /* get its location */
111 SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */
112 if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */
113 drawLine(x - dx, y - dy, dx, dy); /* draw line segment */
114 SDL_RenderPresent();
115 }
116 break;
117 }
118 }
119
120 /* cleanup */
121 SDL_DestroyTexture(brushID);
122 SDL_Quit();
123
124 return 0;
125 }