2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 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.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
2022-11-26 02:41:46 -07:00
|
|
|
|
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>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
#define SHAPED_WINDOW_X 150
|
|
|
|
#define SHAPED_WINDOW_Y 150
|
2015-06-21 09:33:46 -06:00
|
|
|
#define SHAPED_WINDOW_DIMENSION 640
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
typedef struct LoadedPicture
|
|
|
|
{
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Surface *surface;
|
|
|
|
SDL_Texture *texture;
|
|
|
|
SDL_WindowShapeMode mode;
|
2022-11-30 13:51:59 -07:00
|
|
|
const char *name;
|
2015-06-21 09:33:46 -06:00
|
|
|
} LoadedPicture;
|
|
|
|
|
2023-03-02 11:00:15 -07:00
|
|
|
void render(SDL_Renderer *renderer, SDL_Texture *texture)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
/* Clear render-target to blue. */
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xff, 0xff);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
|
|
|
|
/* Render the texture. */
|
2023-03-02 11:00:15 -07:00
|
|
|
SDL_RenderTexture(renderer, texture, NULL, NULL);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char **argv)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
Uint8 num_pictures;
|
2022-11-30 13:51:59 -07:00
|
|
|
LoadedPicture *pictures;
|
2015-06-21 09:33:46 -06:00
|
|
|
int i, j;
|
2023-03-02 11:15:14 -07:00
|
|
|
const SDL_DisplayMode *mode;
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_PixelFormat *format = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Color black = { 0, 0, 0, 0xff };
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Event event;
|
|
|
|
int should_exit = 0;
|
|
|
|
unsigned int current_picture;
|
|
|
|
int button_down;
|
|
|
|
Uint32 pixelFormat = 0;
|
2023-03-02 09:56:54 -07:00
|
|
|
int w, h, access = 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
2022-11-27 09:38:43 -07:00
|
|
|
if (argc < 2) {
|
2015-11-25 13:39:28 -07:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
|
2015-06-21 09:33:46 -06:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2022-12-27 06:42:48 -07:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
|
|
|
|
exit(-2);
|
|
|
|
}
|
|
|
|
|
2023-03-02 11:15:14 -07:00
|
|
|
mode = SDL_GetDesktopDisplayMode(SDL_GetPrimaryDisplay());
|
|
|
|
if (!mode) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't get desktop display mode: %s", SDL_GetError());
|
|
|
|
exit(-2);
|
|
|
|
}
|
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
num_pictures = argc - 1;
|
2022-11-30 13:51:59 -07:00
|
|
|
pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture) * num_pictures);
|
2022-11-27 09:38:43 -07:00
|
|
|
if (pictures == NULL) {
|
2016-05-10 13:13:58 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not allocate memory.");
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-11-27 09:38:43 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2015-06-21 09:33:46 -06:00
|
|
|
pictures[i].surface = NULL;
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
|
|
|
pictures[i].surface = SDL_LoadBMP(argv[i + 1]);
|
|
|
|
pictures[i].name = argv[i + 1];
|
2022-11-27 09:38:43 -07:00
|
|
|
if (pictures[i].surface == NULL) {
|
|
|
|
for (j = 0; j < num_pictures; j++) {
|
2022-12-27 07:36:39 -07:00
|
|
|
SDL_DestroySurface(pictures[j].surface);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_free(pictures);
|
2022-12-27 06:42:48 -07:00
|
|
|
SDL_Quit();
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i + 1]);
|
2015-06-21 09:33:46 -06:00
|
|
|
exit(-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
format = pictures[i].surface->format;
|
2022-11-27 09:38:43 -07:00
|
|
|
if (SDL_ISPIXELFORMAT_ALPHA(format->format)) {
|
2015-06-21 09:33:46 -06:00
|
|
|
pictures[i].mode.mode = ShapeModeBinarizeAlpha;
|
|
|
|
pictures[i].mode.parameters.binarizationCutoff = 255;
|
2022-11-27 09:38:43 -07:00
|
|
|
} else {
|
2015-06-21 09:33:46 -06:00
|
|
|
pictures[i].mode.mode = ShapeModeColorKey;
|
|
|
|
pictures[i].mode.parameters.colorKey = black;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window = SDL_CreateShapedWindow("SDL_Shape test",
|
2022-11-30 13:51:59 -07:00
|
|
|
SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
|
|
|
|
SHAPED_WINDOW_DIMENSION, SHAPED_WINDOW_DIMENSION,
|
|
|
|
0);
|
2022-11-27 09:38:43 -07:00
|
|
|
if (window == NULL) {
|
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2022-12-27 07:36:39 -07:00
|
|
|
SDL_DestroySurface(pictures[i].surface);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_free(pictures);
|
2022-12-27 06:42:48 -07:00
|
|
|
SDL_Quit();
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
|
|
|
|
exit(-4);
|
|
|
|
}
|
2022-12-13 21:26:49 -07:00
|
|
|
renderer = SDL_CreateRenderer(window, NULL, 0);
|
2022-11-27 09:38:43 -07:00
|
|
|
if (renderer == NULL) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_DestroyWindow(window);
|
2022-11-27 09:38:43 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2022-12-27 07:36:39 -07:00
|
|
|
SDL_DestroySurface(pictures[i].surface);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_free(pictures);
|
2022-12-27 06:42:48 -07:00
|
|
|
SDL_Quit();
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
|
|
|
|
exit(-5);
|
|
|
|
}
|
|
|
|
|
2022-11-27 09:38:43 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2015-06-21 09:33:46 -06:00
|
|
|
pictures[i].texture = NULL;
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
|
|
|
pictures[i].texture = SDL_CreateTextureFromSurface(renderer, pictures[i].surface);
|
2022-11-27 09:38:43 -07:00
|
|
|
if (pictures[i].texture == NULL) {
|
2022-11-30 13:51:59 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2022-11-27 09:38:43 -07:00
|
|
|
if (pictures[i].texture != NULL) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_DestroyTexture(pictures[i].texture);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2022-12-27 07:36:39 -07:00
|
|
|
SDL_DestroySurface(pictures[i].surface);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_free(pictures);
|
|
|
|
SDL_DestroyRenderer(renderer);
|
|
|
|
SDL_DestroyWindow(window);
|
2022-12-27 06:42:48 -07:00
|
|
|
SDL_Quit();
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
|
|
|
|
exit(-6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
should_exit = 0;
|
|
|
|
current_picture = 0;
|
|
|
|
button_down = 0;
|
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
|
2023-03-02 09:56:54 -07:00
|
|
|
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &w, &h);
|
2023-03-02 11:15:14 -07:00
|
|
|
/* We want to set the window size in pixels */
|
|
|
|
SDL_SetWindowSize(window, (int)SDL_ceilf(w / mode->display_scale), (int)SDL_ceilf(h / mode->display_scale));
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_SetWindowShape(window, pictures[current_picture].surface, &pictures[current_picture].mode);
|
2022-11-27 09:38:43 -07:00
|
|
|
while (should_exit == 0) {
|
2017-06-24 15:45:19 -06:00
|
|
|
while (SDL_PollEvent(&event)) {
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
2015-06-21 09:33:46 -06:00
|
|
|
button_down = 1;
|
2022-11-27 09:38:43 -07:00
|
|
|
if (event.key.keysym.sym == SDLK_ESCAPE) {
|
2015-06-21 09:33:46 -06:00
|
|
|
should_exit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 18:54:09 -07:00
|
|
|
if (button_down && event.type == SDL_EVENT_KEY_UP) {
|
2015-06-21 09:33:46 -06:00
|
|
|
button_down = 0;
|
|
|
|
current_picture += 1;
|
2022-11-27 09:38:43 -07:00
|
|
|
if (current_picture >= num_pictures) {
|
2015-06-21 09:33:46 -06:00
|
|
|
current_picture = 0;
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
|
2023-03-02 09:56:54 -07:00
|
|
|
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &w, &h);
|
2023-03-02 11:15:14 -07:00
|
|
|
SDL_SetWindowSize(window, (int)SDL_ceilf(w / mode->display_scale), (int)SDL_ceilf(h / mode->display_scale));
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_SetWindowShape(window, pictures[current_picture].surface, &pictures[current_picture].mode);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_QUIT) {
|
2015-06-21 09:33:46 -06:00
|
|
|
should_exit = 1;
|
2017-06-24 15:45:19 -06:00
|
|
|
break;
|
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2023-03-02 11:00:15 -07:00
|
|
|
render(renderer, pictures[current_picture].texture);
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Delay(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the textures. */
|
2022-11-27 09:38:43 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_DestroyTexture(pictures[i].texture);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_DestroyRenderer(renderer);
|
|
|
|
/* Destroy the window. */
|
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
/* Free the original surfaces backing the textures. */
|
2022-11-27 09:38:43 -07:00
|
|
|
for (i = 0; i < num_pictures; i++) {
|
2022-12-27 07:36:39 -07:00
|
|
|
SDL_DestroySurface(pictures[i].surface);
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_free(pictures);
|
2022-12-27 06:42:48 -07:00
|
|
|
/* Call SDL_Quit() before quitting. */
|
|
|
|
SDL_Quit();
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|