SDL  2.0
touch.c
Go to the documentation of this file.
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
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
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 */
71  /* set brush color (red) */
72  SDL_SetTextureColorMod(brush, 255, 100, 100);
73 }
74 
75 int
76 main(int argc, char *argv[])
77 {
78 
79  int x, y, dx, dy; /* mouse location */
80  Uint8 state; /* mouse (touch) state */
82  SDL_Window *window; /* main window */
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 */
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 }
void drawLine(SDL_Renderer *renderer, float startx, float starty, float dx, float dy)
Definition: touch.c:21
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
void fatalError(const char *string)
Definition: common.c:32
GLfloat GLfloat GLfloat GLfloat h
struct xkb_state * state
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SetTextureBlendMode
#define SDL_CreateWindow
static int iterations
Definition: testsprite2.c:43
#define PIXELS_PER_ITERATION
Definition: touch.c:12
GLsizei GLsizei GLfloat distance
#define SDL_RenderCopy
#define SDL_GetWindowSize
#define BRUSH_SIZE
Definition: touch.c:11
#define SDL_CreateTextureFromSurface
int main(int argc, char *argv[])
Definition: touch.c:76
#define SDL_FreeSurface
static SDL_Renderer * renderer
#define SDL_GetRelativeMouseState
uint8_t Uint8
Definition: SDL_stdinc.h:179
struct _cl_event * event
#define SDL_RenderSetLogicalSize
#define SDL_Quit
int done
Definition: checkkeys.c:28
GLubyte GLubyte GLubyte GLubyte w
static SDL_Texture * brush
Definition: touch.c:14
#define SDL_BUTTON_LMASK
Definition: SDL_mouse.h:287
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
#define SDL_SetTextureColorMod
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
#define SDL_WaitEvent
#define SDL_RenderClear
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_DestroyTexture
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:73
void initializeTexture(SDL_Renderer *renderer)
Definition: touch.c:56
#define SDL_Init
General event structure.
Definition: SDL_events.h:557
#define SDL_SetRenderDrawColor
int y
Definition: SDL_rect.h:66
#define SDL_GetMouseState
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_CreateRenderer
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:559