SDL  2.0
touch.c File Reference
#include "SDL.h"
#include <math.h>
#include "common.h"
+ Include dependency graph for touch.c:

Go to the source code of this file.

Macros

#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 */
 

Functions

void drawLine (SDL_Renderer *renderer, float startx, float starty, float dx, float dy)
 
void initializeTexture (SDL_Renderer *renderer)
 
int main (int argc, char *argv[])
 

Variables

static SDL_Texturebrush = 0
 

Macro Definition Documentation

◆ BRUSH_SIZE

#define BRUSH_SIZE   32 /* width and height of the brush */

Definition at line 11 of file touch.c.

Referenced by drawLine().

◆ PIXELS_PER_ITERATION

#define PIXELS_PER_ITERATION   5 /* number of pixels between brush blots when forming a line */

Definition at line 12 of file touch.c.

Referenced by drawLine().

Function Documentation

◆ drawLine()

void drawLine ( SDL_Renderer renderer,
float  startx,
float  starty,
float  dx,
float  dy 
)

Definition at line 21 of file touch.c.

References BRUSH_SIZE, SDL_Rect::h, i, iterations, NULL, PIXELS_PER_ITERATION, SDL_RenderCopy, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

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 }
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
static int iterations
Definition: testsprite2.c:43
#define PIXELS_PER_ITERATION
Definition: touch.c:12
GLsizei GLsizei GLfloat distance
#define SDL_RenderCopy
#define BRUSH_SIZE
Definition: touch.c:11
static SDL_Texture * brush
Definition: touch.c:14
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
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
int h
Definition: SDL_rect.h:67
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ initializeTexture()

void initializeTexture ( SDL_Renderer renderer)

Definition at line 56 of file touch.c.

References fatalError(), NULL, SDL_BLENDMODE_ADD, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_LoadBMP, SDL_SetTextureBlendMode, and SDL_SetTextureColorMod.

Referenced by main().

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 }
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
void fatalError(const char *string)
Definition: common.c:32
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SetTextureBlendMode
#define SDL_CreateTextureFromSurface
#define SDL_FreeSurface
static SDL_Texture * brush
Definition: touch.c:14
#define SDL_SetTextureColorMod
#define NULL
Definition: begin_code.h:164

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 76 of file touch.c.

References done, drawLine(), fatalError(), initializeTexture(), NULL, renderer, SDL_BUTTON_LMASK, SDL_CreateRenderer, SDL_CreateWindow, SDL_DestroyTexture, SDL_GetMouseState, SDL_GetRelativeMouseState, SDL_GetWindowSize, SDL_Init, SDL_INIT_VIDEO, SDL_MOUSEMOTION, SDL_Quit, SDL_QUIT, SDL_RenderClear, SDL_RenderPresent, SDL_RenderSetLogicalSize, SDL_SetRenderDrawColor, SDL_WaitEvent, SDL_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_BORDERLESS, state, and SDL_Event::type.

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 */
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
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
#define SDL_CreateWindow
#define SDL_GetWindowSize
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 NULL
Definition: begin_code.h:164
#define SDL_WaitEvent
#define SDL_RenderClear
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_DestroyTexture
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
#define SDL_GetMouseState
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_CreateRenderer
#define SDL_RenderPresent
Uint32 type
Definition: SDL_events.h:559

Variable Documentation

◆ brush

SDL_Texture* brush = 0
static

Definition at line 14 of file touch.c.