drawing-lines.c 4.3 KB

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