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