2
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
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.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
13
#include <SDL3/SDL_main.h>
18
static SDL_HitTestResult SDLCALL ShapeHitTest(SDL_Window *window, const SDL_Point *area, void *userdata)
20
SDL_Surface *shape = (SDL_Surface *)userdata;
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;
29
return SDL_HITTEST_NORMAL;
32
int main(int argc, char *argv[])
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;
45
for (i = 1; i < argc; ++i) {
46
if (SDL_strcmp(argv[i], "--resizable") == 0) {
48
} else if (!image_file) {
51
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s [--resizable] [shape.bmp]\n", argv[0]);
57
shape = SDL_LoadBMP(image_file);
59
SDL_Log("Couldn't load %s: %s\n", image_file, SDL_GetError());
63
shape = SDL_LoadBMP_IO(SDL_IOFromConstMem(glass_bmp, sizeof(glass_bmp)), SDL_TRUE);
65
SDL_Log("Couldn't load glass.bmp: %s\n", SDL_GetError());
70
/* Create the window hidden so we can set the shape before it's visible */
71
flags = (SDL_WINDOW_HIDDEN | SDL_WINDOW_TRANSPARENT);
73
flags |= SDL_WINDOW_RESIZABLE;
75
flags |= SDL_WINDOW_BORDERLESS;
77
window = SDL_CreateWindow("SDL Shape Test", shape->w, shape->h, flags);
79
SDL_Log("Couldn't create transparent window: %s\n", SDL_GetError());
83
renderer = SDL_CreateRenderer(window, NULL);
85
SDL_Log("Couldn't create renderer: %s\n", SDL_GetError());
89
if (!SDL_ISPIXELFORMAT_ALPHA(shape->format)) {
90
/* Set the colorkey to the top-left pixel */
93
SDL_ReadSurfacePixel(shape, 0, 0, &r, &g, &b, &a);
94
SDL_SetSurfaceColorKey(shape, 1, SDL_MapSurfaceRGBA(shape, r, g, b, a));
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());
105
/* Set the window size to the size of our shape and show it */
106
SDL_SetWindowShape(window, shape);
107
SDL_ShowWindow(window);
109
/* We're ready to go! */
111
while (SDL_PollEvent(&event)) {
112
switch (event.type) {
113
case SDL_EVENT_KEY_DOWN:
114
if (event.key.key == SDLK_ESCAPE) {
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);
130
/* Show everything on the screen and wait a bit */
131
SDL_RenderPresent(renderer);
139
SDL_DestroySurface(shape);