touch.c 3.8 KB

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