diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testshape.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testshape.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testshape.c b/contrib/SDL-3.2.8/test/testshape.c new file mode 100644 index 0000000..9a884e6 --- /dev/null +++ b/contrib/SDL-3.2.8/test/testshape.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | /* | ||
| 2 | Copyright (C) 1997-2025 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)) { | ||
| 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 | bool resizable = false; | ||
| 39 | SDL_WindowFlags flags; | ||
| 40 | bool done = 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 = 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]", 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", image_file, SDL_GetError()); | ||
| 60 | goto quit; | ||
| 61 | } | ||
| 62 | } else { | ||
| 63 | SDL_IOStream *stream = SDL_IOFromConstMem(glass_bmp, sizeof(glass_bmp)); | ||
| 64 | if (!stream) { | ||
| 65 | SDL_Log("Couldn't create iostream for glass.bmp: %s", SDL_GetError()); | ||
| 66 | goto quit; | ||
| 67 | } | ||
| 68 | shape = SDL_LoadBMP_IO(stream, true); | ||
| 69 | if (!shape) { | ||
| 70 | SDL_Log("Couldn't load glass.bmp: %s", SDL_GetError()); | ||
| 71 | goto quit; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | /* Create the window hidden so we can set the shape before it's visible */ | ||
| 76 | flags = (SDL_WINDOW_HIDDEN | SDL_WINDOW_TRANSPARENT); | ||
| 77 | if (resizable) { | ||
| 78 | flags |= SDL_WINDOW_RESIZABLE; | ||
| 79 | } else { | ||
| 80 | flags |= SDL_WINDOW_BORDERLESS; | ||
| 81 | } | ||
| 82 | window = SDL_CreateWindow("SDL Shape Test", shape->w, shape->h, flags); | ||
| 83 | if (!window) { | ||
| 84 | SDL_Log("Couldn't create transparent window: %s", SDL_GetError()); | ||
| 85 | goto quit; | ||
| 86 | } | ||
| 87 | |||
| 88 | renderer = SDL_CreateRenderer(window, NULL); | ||
| 89 | if (!renderer) { | ||
| 90 | SDL_Log("Couldn't create renderer: %s", SDL_GetError()); | ||
| 91 | goto quit; | ||
| 92 | } | ||
| 93 | |||
| 94 | if (!SDL_ISPIXELFORMAT_ALPHA(shape->format)) { | ||
| 95 | /* Set the colorkey to the top-left pixel */ | ||
| 96 | Uint8 r, g, b, a; | ||
| 97 | |||
| 98 | SDL_ReadSurfacePixel(shape, 0, 0, &r, &g, &b, &a); | ||
| 99 | SDL_SetSurfaceColorKey(shape, 1, SDL_MapSurfaceRGBA(shape, r, g, b, a)); | ||
| 100 | } | ||
| 101 | |||
| 102 | if (!resizable) { | ||
| 103 | /* Set the hit test callback so we can drag the window */ | ||
| 104 | if (!SDL_SetWindowHitTest(window, ShapeHitTest, shape)) { | ||
| 105 | SDL_Log("Couldn't set hit test callback: %s", SDL_GetError()); | ||
| 106 | goto quit; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | /* Set the window size to the size of our shape and show it */ | ||
| 111 | SDL_SetWindowShape(window, shape); | ||
| 112 | SDL_ShowWindow(window); | ||
| 113 | |||
| 114 | /* We're ready to go! */ | ||
| 115 | while (!done) { | ||
| 116 | while (SDL_PollEvent(&event)) { | ||
| 117 | switch (event.type) { | ||
| 118 | case SDL_EVENT_KEY_DOWN: | ||
| 119 | if (event.key.key == SDLK_ESCAPE) { | ||
| 120 | done = true; | ||
| 121 | } | ||
| 122 | break; | ||
| 123 | case SDL_EVENT_QUIT: | ||
| 124 | done = true; | ||
| 125 | break; | ||
| 126 | default: | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | /* We'll clear to white, but you could do other drawing here */ | ||
| 132 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); | ||
| 133 | SDL_RenderClear(renderer); | ||
| 134 | |||
| 135 | /* Show everything on the screen and wait a bit */ | ||
| 136 | SDL_RenderPresent(renderer); | ||
| 137 | SDL_Delay(100); | ||
| 138 | } | ||
| 139 | |||
| 140 | /* Success! */ | ||
| 141 | return_code = 0; | ||
| 142 | |||
| 143 | quit: | ||
| 144 | SDL_DestroySurface(shape); | ||
| 145 | SDL_Quit(); | ||
| 146 | return return_code; | ||
| 147 | } | ||
