🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.3 KiB

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