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_Texture *brush = 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(SDL_Renderer * renderer,float startx,float starty,float dx,float dy)21 drawLine(SDL_Renderer *renderer, 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 float x;
30 float y;
31 int i;
32
33 dstRect.w = BRUSH_SIZE;
34 dstRect.h = BRUSH_SIZE;
35
36 /* setup x and y for the location of the first sprite */
37 x = startx - BRUSH_SIZE / 2.0f;
38 y = starty - BRUSH_SIZE / 2.0f;
39
40 /* draw a series of blots to form the line */
41 for (i = 0; i < iterations; i++) {
42 dstRect.x = x;
43 dstRect.y = y;
44 /* shift x and y for next sprite location */
45 x += dx_prime;
46 y += dy_prime;
47 /* draw brush blot */
48 SDL_RenderCopy(renderer, brush, NULL, &dstRect);
49 }
50 }
51
52 /*
53 loads the brush texture
54 */
55 void
initializeTexture(SDL_Renderer * renderer)56 initializeTexture(SDL_Renderer *renderer)
57 {
58 SDL_Surface *bmp_surface;
59 bmp_surface = SDL_LoadBMP("stroke.bmp");
60 if (bmp_surface == NULL) {
61 fatalError("could not load stroke.bmp");
62 }
63 brush =
64 SDL_CreateTextureFromSurface(renderer, bmp_surface);
65 SDL_FreeSurface(bmp_surface);
66 if (brush == 0) {
67 fatalError("could not create brush texture");
68 }
69 /* additive blending -- laying strokes on top of eachother makes them brighter */
70 SDL_SetTextureBlendMode(brush, SDL_BLENDMODE_ADD);
71 /* set brush color (red) */
72 SDL_SetTextureColorMod(brush, 255, 100, 100);
73 }
74
75 int
main(int argc,char * argv[])76 main(int argc, char *argv[])
77 {
78
79 int x, y, dx, dy; /* mouse location */
80 Uint8 state; /* mouse (touch) state */
81 SDL_Event event;
82 SDL_Window *window; /* main window */
83 SDL_Renderer *renderer;
84 int done; /* does user want to quit? */
85 int w, h;
86
87 /* initialize SDL */
88 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
89 fatalError("Could not initialize SDL");
90 }
91
92 /* create main window and renderer */
93 window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI);
94 renderer = SDL_CreateRenderer(window, 0, 0);
95
96 SDL_GetWindowSize(window, &w, &h);
97 SDL_RenderSetLogicalSize(renderer, w, h);
98
99 /* load brush texture */
100 initializeTexture(renderer);
101
102 /* fill canvass initially with all black */
103 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
104 SDL_RenderClear(renderer);
105 SDL_RenderPresent(renderer);
106
107 done = 0;
108 while (!done && SDL_WaitEvent(&event)) {
109 switch (event.type) {
110 case SDL_QUIT:
111 done = 1;
112 break;
113 case SDL_MOUSEMOTION:
114 state = SDL_GetMouseState(&x, &y); /* get its location */
115 SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */
116 if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */
117 drawLine(renderer, x - dx, y - dy, dx, dy); /* draw line segment */
118 SDL_RenderPresent(renderer);
119 }
120 break;
121 }
122 }
123
124 /* cleanup */
125 SDL_DestroyTexture(brush);
126 SDL_Quit();
127
128 return 0;
129 }
130