| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 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 <stdlib.h> |
| 13 |
#include <math.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include "SDL.h" |
| 16 |
#include "SDL_shape.h" |
| 17 |
|
| 18 |
#define SHAPED_WINDOW_X 150 |
| 19 |
#define SHAPED_WINDOW_Y 150 |
| 20 |
#define SHAPED_WINDOW_DIMENSION 640 |
| 21 |
|
| 22 |
typedef struct LoadedPicture |
| 23 |
{ |
| 24 |
SDL_Surface *surface; |
| 25 |
SDL_Texture *texture; |
| 26 |
SDL_WindowShapeMode mode; |
| 27 |
const char *name; |
| 28 |
} LoadedPicture; |
| 29 |
|
| 30 |
void render(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect texture_dimensions) |
| 31 |
{ |
| 32 |
/* Clear render-target to blue. */ |
| 33 |
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xff, 0xff); |
| 34 |
SDL_RenderClear(renderer); |
| 35 |
|
| 36 |
/* Render the texture. */ |
| 37 |
SDL_RenderCopy(renderer, texture, &texture_dimensions, &texture_dimensions); |
| 38 |
|
| 39 |
SDL_RenderPresent(renderer); |
| 40 |
} |
| 41 |
|
| 42 |
int main(int argc, char **argv) |
| 43 |
{ |
| 44 |
Uint8 num_pictures; |
| 45 |
LoadedPicture *pictures; |
| 46 |
int i, j; |
| 47 |
SDL_PixelFormat *format = NULL; |
| 48 |
SDL_Window *window; |
| 49 |
SDL_Renderer *renderer; |
| 50 |
SDL_Color black = { 0, 0, 0, 0xff }; |
| 51 |
SDL_Event event; |
| 52 |
int should_exit = 0; |
| 53 |
unsigned int current_picture; |
| 54 |
int button_down; |
| 55 |
Uint32 pixelFormat = 0; |
| 56 |
int access = 0; |
| 57 |
SDL_Rect texture_dimensions; |
| 58 |
|
| 59 |
/* Enable standard application logging */ |
| 60 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 61 |
|
| 62 |
if (argc < 2) { |
| 63 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument."); |
| 64 |
exit(-1); |
| 65 |
} |
| 66 |
|
| 67 |
if (SDL_VideoInit(NULL) == -1) { |
| 68 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video."); |
| 69 |
exit(-2); |
| 70 |
} |
| 71 |
|
| 72 |
num_pictures = argc - 1; |
| 73 |
pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture) * num_pictures); |
| 74 |
if (!pictures) { |
| 75 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not allocate memory."); |
| 76 |
exit(1); |
| 77 |
} |
| 78 |
for (i = 0; i < num_pictures; i++) { |
| 79 |
pictures[i].surface = NULL; |
| 80 |
} |
| 81 |
for (i = 0; i < num_pictures; i++) { |
| 82 |
pictures[i].surface = SDL_LoadBMP(argv[i + 1]); |
| 83 |
pictures[i].name = argv[i + 1]; |
| 84 |
if (pictures[i].surface == NULL) { |
| 85 |
for (j = 0; j < num_pictures; j++) { |
| 86 |
SDL_FreeSurface(pictures[j].surface); |
| 87 |
} |
| 88 |
SDL_free(pictures); |
| 89 |
SDL_VideoQuit(); |
| 90 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i + 1]); |
| 91 |
exit(-3); |
| 92 |
} |
| 93 |
|
| 94 |
format = pictures[i].surface->format; |
| 95 |
if (SDL_ISPIXELFORMAT_ALPHA(format->format)) { |
| 96 |
pictures[i].mode.mode = ShapeModeBinarizeAlpha; |
| 97 |
pictures[i].mode.parameters.binarizationCutoff = 255; |
| 98 |
} else { |
| 99 |
pictures[i].mode.mode = ShapeModeColorKey; |
| 100 |
pictures[i].mode.parameters.colorKey = black; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
window = SDL_CreateShapedWindow("SDL_Shape test", |
| 105 |
SHAPED_WINDOW_X, SHAPED_WINDOW_Y, |
| 106 |
SHAPED_WINDOW_DIMENSION, SHAPED_WINDOW_DIMENSION, |
| 107 |
0); |
| 108 |
SDL_SetWindowPosition(window, SHAPED_WINDOW_X, SHAPED_WINDOW_Y); |
| 109 |
if (!window) { |
| 110 |
for (i = 0; i < num_pictures; i++) { |
| 111 |
SDL_FreeSurface(pictures[i].surface); |
| 112 |
} |
| 113 |
SDL_free(pictures); |
| 114 |
SDL_VideoQuit(); |
| 115 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape."); |
| 116 |
exit(-4); |
| 117 |
} |
| 118 |
renderer = SDL_CreateRenderer(window, -1, 0); |
| 119 |
if (!renderer) { |
| 120 |
SDL_DestroyWindow(window); |
| 121 |
for (i = 0; i < num_pictures; i++) { |
| 122 |
SDL_FreeSurface(pictures[i].surface); |
| 123 |
} |
| 124 |
SDL_free(pictures); |
| 125 |
SDL_VideoQuit(); |
| 126 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window."); |
| 127 |
exit(-5); |
| 128 |
} |
| 129 |
|
| 130 |
for (i = 0; i < num_pictures; i++) { |
| 131 |
pictures[i].texture = NULL; |
| 132 |
} |
| 133 |
for (i = 0; i < num_pictures; i++) { |
| 134 |
pictures[i].texture = SDL_CreateTextureFromSurface(renderer, pictures[i].surface); |
| 135 |
if (pictures[i].texture == NULL) { |
| 136 |
for (i = 0; i < num_pictures; i++) { |
| 137 |
if (pictures[i].texture != NULL) { |
| 138 |
SDL_DestroyTexture(pictures[i].texture); |
| 139 |
} |
| 140 |
} |
| 141 |
for (i = 0; i < num_pictures; i++) { |
| 142 |
SDL_FreeSurface(pictures[i].surface); |
| 143 |
} |
| 144 |
SDL_free(pictures); |
| 145 |
SDL_DestroyRenderer(renderer); |
| 146 |
SDL_DestroyWindow(window); |
| 147 |
SDL_VideoQuit(); |
| 148 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape."); |
| 149 |
exit(-6); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
should_exit = 0; |
| 154 |
current_picture = 0; |
| 155 |
button_down = 0; |
| 156 |
texture_dimensions.h = 0; |
| 157 |
texture_dimensions.w = 0; |
| 158 |
texture_dimensions.x = 0; |
| 159 |
texture_dimensions.y = 0; |
| 160 |
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name); |
| 161 |
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &texture_dimensions.w, &texture_dimensions.h); |
| 162 |
SDL_SetWindowSize(window, texture_dimensions.w, texture_dimensions.h); |
| 163 |
SDL_SetWindowShape(window, pictures[current_picture].surface, &pictures[current_picture].mode); |
| 164 |
while (should_exit == 0) { |
| 165 |
while (SDL_PollEvent(&event)) { |
| 166 |
if (event.type == SDL_KEYDOWN) { |
| 167 |
button_down = 1; |
| 168 |
if (event.key.keysym.sym == SDLK_ESCAPE) { |
| 169 |
should_exit = 1; |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
if (button_down && event.type == SDL_KEYUP) { |
| 174 |
button_down = 0; |
| 175 |
current_picture += 1; |
| 176 |
if (current_picture >= num_pictures) { |
| 177 |
current_picture = 0; |
| 178 |
} |
| 179 |
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name); |
| 180 |
SDL_QueryTexture(pictures[current_picture].texture, &pixelFormat, &access, &texture_dimensions.w, &texture_dimensions.h); |
| 181 |
SDL_SetWindowSize(window, texture_dimensions.w, texture_dimensions.h); |
| 182 |
SDL_SetWindowShape(window, pictures[current_picture].surface, &pictures[current_picture].mode); |
| 183 |
} |
| 184 |
if (event.type == SDL_QUIT) { |
| 185 |
should_exit = 1; |
| 186 |
break; |
| 187 |
} |
| 188 |
} |
| 189 |
render(renderer, pictures[current_picture].texture, texture_dimensions); |
| 190 |
SDL_Delay(10); |
| 191 |
} |
| 192 |
|
| 193 |
/* Free the textures. */ |
| 194 |
for (i = 0; i < num_pictures; i++) { |
| 195 |
SDL_DestroyTexture(pictures[i].texture); |
| 196 |
} |
| 197 |
SDL_DestroyRenderer(renderer); |
| 198 |
/* Destroy the window. */ |
| 199 |
SDL_DestroyWindow(window); |
| 200 |
/* Free the original surfaces backing the textures. */ |
| 201 |
for (i = 0; i < num_pictures; i++) { |
| 202 |
SDL_FreeSurface(pictures[i].surface); |
| 203 |
} |
| 204 |
SDL_free(pictures); |
| 205 |
/* Call SDL_VideoQuit() before quitting. */ |
| 206 |
SDL_VideoQuit(); |
| 207 |
|
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
/* vi: set ts=4 sw=4 expandtab: */ |