Atlas - drawing-lines.c
Home / ext / SDL / examples / pen / 01-drawing-lines Lines: 1 | Size: 4988 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]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 11#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 12#include <SDL3/SDL.h> 13#include <SDL3/SDL_main.h> 14 15/* We will use this renderer to draw into this window every frame. */ 16static SDL_Window *window = NULL; 17static SDL_Renderer *renderer = NULL; 18static SDL_Texture *render_target = NULL; 19static float pressure = 0.0f; 20static float previous_touch_x = -1.0f; 21static float previous_touch_y = -1.0f; 22static float tilt_x = 0.0f; 23static float tilt_y = 0.0f; 24 25/* This function runs once at startup. */ 26SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 27{ 28 int w, h; 29 30 SDL_SetAppMetadata("Example Pen Drawing Lines", "1.0", "com.example.pen-drawing-lines"); 31 32 if (!SDL_Init(SDL_INIT_VIDEO)) { 33 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 34 return SDL_APP_FAILURE; 35 } 36 37 if (!SDL_CreateWindowAndRenderer("examples/pen/drawing-lines", 640, 480, 0, &window, &renderer)) { 38 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 39 return SDL_APP_FAILURE; 40 } 41 42 /* we make a render target so we can draw lines to it and not have to record and redraw every pen stroke each frame. 43 Instead rendering a frame for us is a single texture draw. */ 44 45 /* make sure the render target matches output size (for hidpi displays, etc) so drawing matches the pen's position on a tablet display. */ 46 SDL_GetRenderOutputSize(renderer, &w, &h); 47 render_target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h); 48 if (!render_target) { 49 SDL_Log("Couldn't create render target: %s", SDL_GetError()); 50 return SDL_APP_FAILURE; 51 } 52 53 /* just blank the render target to gray to start. */ 54 SDL_SetRenderTarget(renderer, render_target); 55 SDL_SetRenderDrawColor(renderer, 100, 100, 100, SDL_ALPHA_OPAQUE); 56 SDL_RenderClear(renderer); 57 SDL_SetRenderTarget(renderer, NULL); 58 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); 59 60 return SDL_APP_CONTINUE; /* carry on with the program! */ 61} 62 63/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 64SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 65{ 66 if (event->type == SDL_EVENT_QUIT) { 67 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 68 } 69 70 /* There are several events that track the specific stages of pen activity, 71 but we're only going to look for motion and pressure, for simplicity. */ 72 if (event->type == SDL_EVENT_PEN_MOTION) { 73 /* you can check for when the pen is touching, but if pressure > 0.0f, it's definitely touching! */ 74 if (pressure > 0.0f) { 75 if (previous_touch_x >= 0.0f) { /* only draw if we're moving while touching */ 76 /* draw with the alpha set to the pressure, so you effectively get a fainter line for lighter presses. */ 77 SDL_SetRenderTarget(renderer, render_target); 78 SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, pressure); 79 SDL_RenderLine(renderer, previous_touch_x, previous_touch_y, event->pmotion.x, event->pmotion.y); 80 } 81 previous_touch_x = event->pmotion.x; 82 previous_touch_y = event->pmotion.y; 83 } else { 84 previous_touch_x = previous_touch_y = -1.0f; 85 } 86 } else if (event->type == SDL_EVENT_PEN_AXIS) { 87 if (event->paxis.axis == SDL_PEN_AXIS_PRESSURE) { 88 pressure = event->paxis.value; /* remember new pressure for later draws. */ 89 } else if(event->paxis.axis == SDL_PEN_AXIS_XTILT) { 90 tilt_x = event->paxis.value; 91 } else if(event->paxis.axis == SDL_PEN_AXIS_YTILT) { 92 tilt_y = event->paxis.value; 93 } 94 } 95 96 return SDL_APP_CONTINUE; /* carry on with the program! */ 97} 98 99/* This function runs once per frame, and is the heart of the program. */ 100SDL_AppResult SDL_AppIterate(void *appstate) 101{ 102 char debug_text[1024]; 103 104 /* make sure we're drawing to the window and not the render target */ 105 SDL_SetRenderTarget(renderer, NULL); 106 SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); 107 SDL_RenderClear(renderer); /* just in case. */ 108 SDL_RenderTexture(renderer, render_target, NULL, NULL); 109 SDL_snprintf(debug_text, sizeof(debug_text), "Tilt: %f %f", tilt_x, tilt_y); 110 SDL_RenderDebugText(renderer, 0, 8, debug_text); 111 SDL_RenderPresent(renderer); 112 return SDL_APP_CONTINUE; /* carry on with the program! */ 113} 114 115/* This function runs once at shutdown. */ 116void SDL_AppQuit(void *appstate, SDL_AppResult result) 117{ 118 SDL_DestroyTexture(render_target); 119 /* SDL will clean up the window/renderer for us. */ 120} 121 122[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.