SDL

Форк
0
/
drawing-lines.c 
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. */
16
static SDL_Window *window = NULL;
17
static SDL_Renderer *renderer = NULL;
18
static SDL_Texture *render_target = NULL;
19
static float pressure = 0.0f;
20
static float previous_touch_x = -1.0f;
21
static float previous_touch_y = -1.0f;
22

23
/* This function runs once at startup. */
24
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
25
{
26
    if (SDL_Init(SDL_INIT_VIDEO) == -1) {
27
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
28
        return SDL_APP_FAILURE;
29
    }
30

31
    if (SDL_CreateWindowAndRenderer("examples/pen/drawing-lines", 640, 480, 0, &window, &renderer) == -1) {
32
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
33
        return 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.
37
       Instead rendering a frame for us is a single texture draw. */
38
    render_target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480);
39
    if (!render_target) {
40
        SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create render target!", SDL_GetError(), NULL);
41
        return SDL_APP_FAILURE;
42
    }
43

44
    /* just blank the render target to gray to start. */
45
    SDL_SetRenderTarget(renderer, render_target);
46
    SDL_SetRenderDrawColor(renderer, 100, 100, 100, 255);
47
    SDL_RenderClear(renderer);
48
    SDL_SetRenderTarget(renderer, NULL);
49
    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
50

51
    return SDL_APP_CONTINUE;  /* carry on with the program! */
52
}
53

54
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
55
SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event)
56
{
57
    if (event->type == SDL_EVENT_QUIT) {
58
        return 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,
62
       but we're only going to look for motion and pressure, for simplicity. */
63
    if (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! */
65
        if (pressure > 0.0f) {
66
            if (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. */
68
                SDL_SetRenderTarget(renderer, render_target);
69
                SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, pressure);
70
                SDL_RenderLine(renderer, previous_touch_x, previous_touch_y, event->pmotion.x, event->pmotion.y);
71
            }
72
            previous_touch_x = event->pmotion.x;
73
            previous_touch_y = event->pmotion.y;
74
        } else {
75
            previous_touch_x = previous_touch_y = -1.0f;
76
        }
77
    } else if (event->type == SDL_EVENT_PEN_AXIS) {
78
        if (event->paxis.axis == SDL_PEN_AXIS_PRESSURE) {
79
            pressure = event->paxis.value;  /* remember new pressure for later draws. */
80
        }
81
    }
82

83
    return SDL_APP_CONTINUE;  /* carry on with the program! */
84
}
85

86
/* This function runs once per frame, and is the heart of the program. */
87
SDL_AppResult SDL_AppIterate(void *appstate)
88
{
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, 255);
92
    SDL_RenderClear(renderer);  /* just in case. */
93
    SDL_RenderTexture(renderer, render_target, NULL, NULL);
94
    SDL_RenderPresent(renderer);
95
    return SDL_APP_CONTINUE;  /* carry on with the program! */
96
}
97

98
/* This function runs once at shutdown. */
99
void SDL_AppQuit(void *appstate)
100
{
101
    SDL_DestroyTexture(render_target);
102
    /* SDL will clean up the window/renderer for us. */
103
}
104

105

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.