SDL

Форк
0
/
testshape.c 
142 строки · 4.1 Кб
1
/*
2
  Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
3

4
  This software is provided 'as-is', without any express or implied
5
  warranty.  In no event will the authors be held liable for any damages
6
  arising from the use of this software.
7

8
  Permission is granted to anyone to use this software for any purpose,
9
  including commercial applications, and to alter it and redistribute it
10
  freely.
11
*/
12
#include <SDL3/SDL.h>
13
#include <SDL3/SDL_main.h>
14

15
#include "glass.h"
16

17

18
static SDL_HitTestResult SDLCALL ShapeHitTest(SDL_Window *window, const SDL_Point *area, void *userdata)
19
{
20
    SDL_Surface *shape = (SDL_Surface *)userdata;
21
    Uint8 r, g, b, a;
22

23
    if (SDL_ReadSurfacePixel(shape, area->x, area->y, &r, &g, &b, &a) == 0) {
24
        if (a != SDL_ALPHA_TRANSPARENT) {
25
            /* We'll just make everything draggable */
26
            return SDL_HITTEST_DRAGGABLE;
27
        }
28
    }
29
    return SDL_HITTEST_NORMAL;
30
}
31

32
int main(int argc, char *argv[])
33
{
34
    const char *image_file = NULL;
35
    SDL_Window *window = NULL;
36
    SDL_Renderer *renderer = NULL;
37
    SDL_Surface *shape = NULL;
38
    SDL_bool resizable = SDL_FALSE;
39
    SDL_WindowFlags flags;
40
    SDL_bool done = SDL_FALSE;
41
    SDL_Event event;
42
    int i;
43
    int return_code = 1;
44

45
    for (i = 1; i < argc; ++i) {
46
        if (SDL_strcmp(argv[i], "--resizable") == 0) {
47
            resizable = SDL_TRUE;
48
        } else if (!image_file) {
49
            image_file = argv[i];
50
        } else {
51
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s [--resizable] [shape.bmp]\n", argv[0]);
52
            goto quit;
53
        }
54
    }
55

56
    if (image_file) {
57
        shape = SDL_LoadBMP(image_file);
58
        if (!shape) {
59
            SDL_Log("Couldn't load %s: %s\n", image_file, SDL_GetError());
60
            goto quit;
61
        }
62
    } else {
63
        shape = SDL_LoadBMP_IO(SDL_IOFromConstMem(glass_bmp, sizeof(glass_bmp)), SDL_TRUE);
64
        if (!shape) {
65
            SDL_Log("Couldn't load glass.bmp: %s\n", SDL_GetError());
66
            goto quit;
67
        }
68
    }
69

70
    /* Create the window hidden so we can set the shape before it's visible */
71
    flags = (SDL_WINDOW_HIDDEN | SDL_WINDOW_TRANSPARENT);
72
    if (resizable) {
73
        flags |= SDL_WINDOW_RESIZABLE;
74
    } else {
75
        flags |= SDL_WINDOW_BORDERLESS;
76
    }
77
    window = SDL_CreateWindow("SDL Shape Test", shape->w, shape->h, flags);
78
    if (!window) {
79
        SDL_Log("Couldn't create transparent window: %s\n", SDL_GetError());
80
        goto quit;
81
    }
82

83
    renderer = SDL_CreateRenderer(window, NULL);
84
    if (!renderer) {
85
        SDL_Log("Couldn't create renderer: %s\n", SDL_GetError());
86
        goto quit;
87
    }
88

89
    if (!SDL_ISPIXELFORMAT_ALPHA(shape->format)) {
90
        /* Set the colorkey to the top-left pixel */
91
        Uint8 r, g, b, a;
92

93
        SDL_ReadSurfacePixel(shape, 0, 0, &r, &g, &b, &a);
94
        SDL_SetSurfaceColorKey(shape, 1, SDL_MapSurfaceRGBA(shape, r, g, b, a));
95
    }
96

97
    if (!resizable) {
98
        /* Set the hit test callback so we can drag the window */
99
        if (SDL_SetWindowHitTest(window, ShapeHitTest, shape) < 0) {
100
            SDL_Log("Couldn't set hit test callback: %s\n", SDL_GetError());
101
            goto quit;
102
        }
103
    }
104

105
    /* Set the window size to the size of our shape and show it */
106
    SDL_SetWindowShape(window, shape);
107
    SDL_ShowWindow(window);
108

109
    /* We're ready to go! */
110
    while (!done) {
111
        while (SDL_PollEvent(&event)) {
112
            switch (event.type) {
113
            case SDL_EVENT_KEY_DOWN:
114
                if (event.key.key == SDLK_ESCAPE) {
115
                    done = SDL_TRUE;
116
                }
117
                break;
118
            case SDL_EVENT_QUIT:
119
                done = SDL_TRUE;
120
                break;
121
            default:
122
                break;
123
            }
124
        }
125

126
        /* We'll clear to white, but you could do other drawing here */
127
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
128
        SDL_RenderClear(renderer);
129

130
        /* Show everything on the screen and wait a bit */
131
        SDL_RenderPresent(renderer);
132
        SDL_Delay(100);
133
    }
134

135
    /* Success! */
136
    return_code = 0;
137

138
quit:
139
    SDL_DestroySurface(shape);
140
    SDL_Quit();
141
    return return_code;
142
}
143

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

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

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

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