2015-06-21 18:33:46 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 07:58:20 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-11-22 22:46:36 +03:00
|
|
|
|
2024-02-09 18:09:59 +03:00
|
|
|
#include "glass.h"
|
|
|
|
|
|
|
|
|
|
|
|
static SDL_HitTestResult SDLCALL ShapeHitTest(SDL_Window *window, const SDL_Point *area, void *userdata)
|
|
|
|
{
|
|
|
|
SDL_Surface *shape = (SDL_Surface *)userdata;
|
|
|
|
Uint8 r, g, b, a;
|
|
|
|
|
2024-08-23 03:33:49 +03:00
|
|
|
if (SDL_ReadSurfacePixel(shape, area->x, area->y, &r, &g, &b, &a)) {
|
2024-02-09 18:09:59 +03:00
|
|
|
if (a != SDL_ALPHA_TRANSPARENT) {
|
|
|
|
/* We'll just make everything draggable */
|
|
|
|
return SDL_HITTEST_DRAGGABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SDL_HITTEST_NORMAL;
|
|
|
|
}
|
2023-03-17 02:25:39 +03:00
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
int main(int argc, char *argv[])
|
2023-03-11 15:53:54 +03:00
|
|
|
{
|
2024-02-09 18:09:59 +03:00
|
|
|
const char *image_file = NULL;
|
2024-02-09 12:46:09 +03:00
|
|
|
SDL_Window *window = NULL;
|
|
|
|
SDL_Renderer *renderer = NULL;
|
|
|
|
SDL_Surface *shape = NULL;
|
2024-09-18 17:52:28 +03:00
|
|
|
bool resizable = false;
|
2024-03-03 22:30:57 +03:00
|
|
|
SDL_WindowFlags flags;
|
2024-09-18 17:52:28 +03:00
|
|
|
bool done = false;
|
2024-02-09 18:09:59 +03:00
|
|
|
SDL_Event event;
|
|
|
|
int i;
|
2024-02-09 12:46:09 +03:00
|
|
|
int return_code = 1;
|
2023-03-11 15:53:54 +03:00
|
|
|
|
2024-02-09 18:09:59 +03:00
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
if (SDL_strcmp(argv[i], "--resizable") == 0) {
|
2024-09-18 17:52:28 +03:00
|
|
|
resizable = true;
|
2024-02-09 18:09:59 +03:00
|
|
|
} else if (!image_file) {
|
|
|
|
image_file = argv[i];
|
|
|
|
} else {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s [--resizable] [shape.bmp]\n", argv[0]);
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image_file) {
|
|
|
|
shape = SDL_LoadBMP(image_file);
|
|
|
|
if (!shape) {
|
|
|
|
SDL_Log("Couldn't load %s: %s\n", image_file, SDL_GetError());
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
} else {
|
2024-10-15 04:54:17 +03:00
|
|
|
SDL_IOStream *stream = SDL_IOFromConstMem(glass_bmp, sizeof(glass_bmp));
|
|
|
|
if (!stream) {
|
|
|
|
SDL_Log("Couldn't create iostream for glass.bmp: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
shape = SDL_LoadBMP_IO(stream, true);
|
2024-02-09 18:09:59 +03:00
|
|
|
if (!shape) {
|
|
|
|
SDL_Log("Couldn't load glass.bmp: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
|
|
|
}
|
2023-03-11 15:53:54 +03:00
|
|
|
}
|
|
|
|
|
2024-02-09 18:09:59 +03:00
|
|
|
/* Create the window hidden so we can set the shape before it's visible */
|
|
|
|
flags = (SDL_WINDOW_HIDDEN | SDL_WINDOW_TRANSPARENT);
|
|
|
|
if (resizable) {
|
|
|
|
flags |= SDL_WINDOW_RESIZABLE;
|
|
|
|
} else {
|
|
|
|
flags |= SDL_WINDOW_BORDERLESS;
|
|
|
|
}
|
|
|
|
window = SDL_CreateWindow("SDL Shape Test", shape->w, shape->h, flags);
|
2024-02-09 12:46:09 +03:00
|
|
|
if (!window) {
|
|
|
|
SDL_Log("Couldn't create transparent window: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
2023-03-11 15:53:54 +03:00
|
|
|
}
|
|
|
|
|
2024-05-13 20:31:55 +03:00
|
|
|
renderer = SDL_CreateRenderer(window, NULL);
|
2024-02-09 12:46:09 +03:00
|
|
|
if (!renderer) {
|
|
|
|
SDL_Log("Couldn't create renderer: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
2023-11-27 23:20:01 +03:00
|
|
|
}
|
|
|
|
|
2024-07-09 00:59:18 +03:00
|
|
|
if (!SDL_ISPIXELFORMAT_ALPHA(shape->format)) {
|
2024-02-09 12:46:09 +03:00
|
|
|
/* Set the colorkey to the top-left pixel */
|
|
|
|
Uint8 r, g, b, a;
|
2023-03-11 15:53:54 +03:00
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
SDL_ReadSurfacePixel(shape, 0, 0, &r, &g, &b, &a);
|
2024-07-09 00:59:18 +03:00
|
|
|
SDL_SetSurfaceColorKey(shape, 1, SDL_MapSurfaceRGBA(shape, r, g, b, a));
|
2023-03-11 15:53:54 +03:00
|
|
|
}
|
|
|
|
|
2024-02-09 18:09:59 +03:00
|
|
|
if (!resizable) {
|
|
|
|
/* Set the hit test callback so we can drag the window */
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!SDL_SetWindowHitTest(window, ShapeHitTest, shape)) {
|
2024-02-09 18:09:59 +03:00
|
|
|
SDL_Log("Couldn't set hit test callback: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
|
|
|
}
|
2023-03-11 15:53:54 +03:00
|
|
|
}
|
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
/* Set the window size to the size of our shape and show it */
|
2024-02-09 18:09:59 +03:00
|
|
|
SDL_SetWindowShape(window, shape);
|
2024-02-09 12:46:09 +03:00
|
|
|
SDL_ShowWindow(window);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
/* We're ready to go! */
|
2023-11-27 23:20:01 +03:00
|
|
|
while (!done) {
|
2017-06-25 00:45:19 +03:00
|
|
|
while (SDL_PollEvent(&event)) {
|
2024-02-09 12:46:09 +03:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_EVENT_KEY_DOWN:
|
2024-06-22 05:50:10 +03:00
|
|
|
if (event.key.key == SDLK_ESCAPE) {
|
2024-09-18 17:52:28 +03:00
|
|
|
done = true;
|
2024-02-09 18:09:59 +03:00
|
|
|
}
|
|
|
|
break;
|
2024-02-09 12:46:09 +03:00
|
|
|
case SDL_EVENT_QUIT:
|
2024-09-18 17:52:28 +03:00
|
|
|
done = true;
|
2024-02-09 12:46:09 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2017-06-25 00:45:19 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
/* We'll clear to white, but you could do other drawing here */
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
|
|
|
|
SDL_RenderClear(renderer);
|
2023-03-17 02:25:39 +03:00
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
/* Show everything on the screen and wait a bit */
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
SDL_Delay(100);
|
2023-08-22 19:44:28 +03:00
|
|
|
}
|
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
/* Success! */
|
|
|
|
return_code = 0;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-02-09 12:46:09 +03:00
|
|
|
quit:
|
|
|
|
SDL_DestroySurface(shape);
|
|
|
|
SDL_Quit();
|
|
|
|
return return_code;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|