drawing-lines.c 4.9 KB

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