Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
124 lines (106 loc) · 3.64 KB

File metadata and controls

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