drawing-lines.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. int w, h;
  24. SDL_SetAppMetadata("Example Pen Drawing Lines", "1.0", "com.example.pen-drawing-lines");
  25. if (!SDL_Init(SDL_INIT_VIDEO)) {
  26. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  27. return SDL_APP_FAILURE;
  28. }
  29. if (!SDL_CreateWindowAndRenderer("examples/pen/drawing-lines", 640, 480, 0, &window, &renderer)) {
  30. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  31. return SDL_APP_FAILURE;
  32. }
  33. /* we make a render target so we can draw lines to it and not have to record and redraw every pen stroke each frame.
  34. Instead rendering a frame for us is a single texture draw. */
  35. /* make sure the render target matches output size (for hidpi displays, etc) so drawing matches the pen's position on a tablet display. */
  36. SDL_GetRenderOutputSize(renderer, &w, &h);
  37. render_target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
  38. if (!render_target) {
  39. SDL_Log("Couldn't create render target: %s", SDL_GetError());
  40. return SDL_APP_FAILURE;
  41. }
  42. /* just blank the render target to gray to start. */
  43. SDL_SetRenderTarget(renderer, render_target);
  44. SDL_SetRenderDrawColor(renderer, 100, 100, 100, SDL_ALPHA_OPAQUE);
  45. SDL_RenderClear(renderer);
  46. SDL_SetRenderTarget(renderer, NULL);
  47. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  48. return SDL_APP_CONTINUE; /* carry on with the program! */
  49. }
  50. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  51. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  52. {
  53. if (event->type == SDL_EVENT_QUIT) {
  54. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  55. }
  56. /* There are several events that track the specific stages of pen activity,
  57. but we're only going to look for motion and pressure, for simplicity. */
  58. if (event->type == SDL_EVENT_PEN_MOTION) {
  59. /* you can check for when the pen is touching, but if pressure > 0.0f, it's definitely touching! */
  60. if (pressure > 0.0f) {
  61. if (previous_touch_x >= 0.0f) { /* only draw if we're moving while touching */
  62. /* draw with the alpha set to the pressure, so you effectively get a fainter line for lighter presses. */
  63. SDL_SetRenderTarget(renderer, render_target);
  64. SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, pressure);
  65. SDL_RenderLine(renderer, previous_touch_x, previous_touch_y, event->pmotion.x, event->pmotion.y);
  66. }
  67. previous_touch_x = event->pmotion.x;
  68. previous_touch_y = event->pmotion.y;
  69. } else {
  70. previous_touch_x = previous_touch_y = -1.0f;
  71. }
  72. } else if (event->type == SDL_EVENT_PEN_AXIS) {
  73. if (event->paxis.axis == SDL_PEN_AXIS_PRESSURE) {
  74. pressure = event->paxis.value; /* remember new pressure for later draws. */
  75. }
  76. }
  77. return SDL_APP_CONTINUE; /* carry on with the program! */
  78. }
  79. /* This function runs once per frame, and is the heart of the program. */
  80. SDL_AppResult SDL_AppIterate(void *appstate)
  81. {
  82. /* make sure we're drawing to the window and not the render target */
  83. SDL_SetRenderTarget(renderer, NULL);
  84. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  85. SDL_RenderClear(renderer); /* just in case. */
  86. SDL_RenderTexture(renderer, render_target, NULL, NULL);
  87. SDL_RenderPresent(renderer);
  88. return SDL_APP_CONTINUE; /* carry on with the program! */
  89. }
  90. /* This function runs once at shutdown. */
  91. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  92. {
  93. SDL_DestroyTexture(render_target);
  94. /* SDL will clean up the window/renderer for us. */
  95. }