2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2024-01-01 14:15:26 -07:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06: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-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-11-22 12:46:36 -07:00
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
int main(int argc, char *argv[])
|
2023-03-11 05:53:54 -07:00
|
|
|
{
|
2024-02-09 02:46:09 -07:00
|
|
|
SDL_Window *window = NULL;
|
|
|
|
SDL_Renderer *renderer = NULL;
|
|
|
|
SDL_Surface *shape = NULL;
|
|
|
|
SDL_Texture *shape_texture = NULL;
|
|
|
|
SDL_Event event;
|
|
|
|
SDL_bool done = SDL_FALSE;
|
|
|
|
int return_code = 1;
|
2023-03-11 05:53:54 -07:00
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
if (argc != 2) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s shape.bmp\n", argv[0]);
|
|
|
|
goto quit;
|
2023-03-11 05:53:54 -07:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
/* Create the window hidden so we can set the window size to match our shape */
|
|
|
|
window = SDL_CreateWindow("Shape test", 1, 1, SDL_WINDOW_HIDDEN | SDL_WINDOW_TRANSPARENT);
|
|
|
|
if (!window) {
|
|
|
|
SDL_Log("Couldn't create transparent window: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
2023-03-11 05:53:54 -07:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
renderer = SDL_CreateRenderer(window, NULL, 0);
|
|
|
|
if (!renderer) {
|
|
|
|
SDL_Log("Couldn't create renderer: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
2023-11-27 13:20:01 -07:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
shape = SDL_LoadBMP(argv[1]);
|
|
|
|
if (!shape) {
|
|
|
|
SDL_Log("Couldn't load %s: %s\n", argv[1], SDL_GetError());
|
|
|
|
goto quit;
|
2023-03-11 05:53:54 -07:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
if (!SDL_ISPIXELFORMAT_ALPHA(shape->format->format)) {
|
|
|
|
/* Set the colorkey to the top-left pixel */
|
|
|
|
Uint8 r, g, b, a;
|
2023-03-11 05:53:54 -07:00
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
SDL_ReadSurfacePixel(shape, 0, 0, &r, &g, &b, &a);
|
|
|
|
SDL_SetSurfaceColorKey(shape, 1, SDL_MapRGBA(shape->format, r, g, b, a));
|
2023-03-11 05:53:54 -07:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
shape_texture = SDL_CreateTextureFromSurface(renderer, shape);
|
|
|
|
if (!shape_texture) {
|
|
|
|
SDL_Log("Couldn't create shape texture: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
2023-03-11 05:53:54 -07:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
/* Set the blend mode so the alpha channel of the shape is the window transparency */
|
|
|
|
if (SDL_SetTextureBlendMode(shape_texture,
|
|
|
|
SDL_ComposeCustomBlendMode(
|
|
|
|
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDOPERATION_ADD,
|
|
|
|
SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDOPERATION_ADD)) < 0) {
|
|
|
|
SDL_Log("Couldn't set shape blend mode: %s\n", SDL_GetError());
|
|
|
|
goto quit;
|
2023-03-11 05:53:54 -07:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
/* Set the window size to the size of our shape and show it */
|
|
|
|
SDL_SetWindowSize(window, shape->w, shape->h);
|
|
|
|
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
|
|
|
SDL_ShowWindow(window);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
/* We're ready to go! */
|
2023-11-27 13:20:01 -07:00
|
|
|
while (!done) {
|
2017-06-24 15:45:19 -06:00
|
|
|
while (SDL_PollEvent(&event)) {
|
2024-02-09 02:46:09 -07:00
|
|
|
switch (event.type) {
|
|
|
|
case SDL_EVENT_KEY_DOWN:
|
|
|
|
case SDL_EVENT_QUIT:
|
|
|
|
/* Quit on keypress and quit event */
|
|
|
|
done = SDL_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2017-06-24 15:45:19 -06:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07: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-16 17:25:39 -06:00
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
/* Apply the shape */
|
|
|
|
SDL_RenderTexture(renderer, shape_texture, NULL, NULL);
|
|
|
|
|
|
|
|
/* Show everything on the screen and wait a bit */
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
SDL_Delay(100);
|
2023-08-22 10:44:28 -06:00
|
|
|
}
|
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
/* Success! */
|
|
|
|
return_code = 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2024-02-09 02:46:09 -07:00
|
|
|
quit:
|
|
|
|
SDL_DestroySurface(shape);
|
|
|
|
SDL_Quit();
|
|
|
|
return return_code;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|