🛠️🐜 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.

200 lines
6.9 KiB

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