drawing-lines.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * This example code reads pen/stylus input and draws lines. Darker lines
  3. * for harder pressure.
  4. *
  5. * SDL can track multiple pens, but for simplicity here, this assumes any
  6. * pen input we see was from one device.
  7. *
  8. * This code is public domain. Feel free to use it for any purpose!
  9. */
  10. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  11. #include <SDL3/SDL.h>
  12. #include <SDL3/SDL_main.h>
  13. /* We will use this renderer to draw into this window every frame. */
  14. static SDL_Window *window = NULL;
  15. static SDL_Renderer *renderer = NULL;
  16. static SDL_Texture *render_target = NULL;
  17. static float pressure = 0.0f;
  18. static float previous_touch_x = -1.0f;
  19. static float previous_touch_y = -1.0f;
  20. /* This function runs once at startup. */
  21. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  22. {
  23. if (SDL_Init(SDL_INIT_VIDEO) == -1) {
  24. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
  25. return SDL_APP_FAILURE;
  26. }
  27. if (SDL_CreateWindowAndRenderer("examples/pen/drawing-lines", 640, 480, 0, &window, &renderer) == -1) {
  28. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  29. return SDL_APP_FAILURE;
  30. }
  31. /* we make a render target so we can draw lines to it and not have to record and redraw every pen stroke each frame.
  32. Instead rendering a frame for us is a single texture draw. */
  33. render_target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480);
  34. if (!render_target) {
  35. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create render target!", SDL_GetError(), NULL);
  36. return SDL_APP_FAILURE;
  37. }
  38. /* just blank the render target to gray to start. */
  39. SDL_SetRenderTarget(renderer, render_target);
  40. SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
  41. SDL_RenderClear(renderer);
  42. SDL_SetRenderTarget(renderer, NULL);
  43. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  44. return SDL_APP_CONTINUE; /* carry on with the program! */
  45. }
  46. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  47. SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event)
  48. {
  49. if (event->type == SDL_EVENT_QUIT) {
  50. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  51. }
  52. /* There are several events that track the specific stages of pen activity,
  53. but we're only going to look for motion and pressure, for simplicity. */
  54. if (event->type == SDL_EVENT_PEN_MOTION) {
  55. /* you can check for when the pen is touching, but if pressure > 0.0f, it's definitely touching! */
  56. if (pressure > 0.0f) {
  57. if (previous_touch_x >= 0.0f) { /* only draw if we're moving while touching */
  58. /* draw with the alpha set to the pressure, so you effectively get a fainter line for lighter presses. */
  59. SDL_SetRenderTarget(renderer, render_target);
  60. SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, pressure);
  61. SDL_RenderLine(renderer, previous_touch_x, previous_touch_y, event->pmotion.x, event->pmotion.y);
  62. }
  63. previous_touch_x = event->pmotion.x;
  64. previous_touch_y = event->pmotion.y;
  65. } else {
  66. previous_touch_x = previous_touch_y = -1.0f;
  67. }
  68. } else if (event->type == SDL_EVENT_PEN_AXIS) {
  69. if (event->paxis.axis == SDL_PEN_AXIS_PRESSURE) {
  70. pressure = event->paxis.value; /* remember new pressure for later draws. */
  71. }
  72. }
  73. return SDL_APP_CONTINUE; /* carry on with the program! */
  74. }
  75. /* This function runs once per frame, and is the heart of the program. */
  76. SDL_AppResult SDL_AppIterate(void *appstate)
  77. {
  78. /* make sure we're drawing to the window and not the render target */
  79. SDL_SetRenderTarget(renderer, NULL);
  80. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  81. SDL_RenderClear(renderer); /* just in case. */
  82. SDL_RenderTexture(renderer, render_target, NULL, NULL);
  83. SDL_RenderPresent(renderer);
  84. return SDL_APP_CONTINUE; /* carry on with the program! */
  85. }
  86. /* This function runs once at shutdown. */
  87. void SDL_AppQuit(void *appstate)
  88. {
  89. SDL_DestroyTexture(render_target);
  90. /* SDL will clean up the window/renderer for us. */
  91. }