testshape.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright (C) 1997-2014 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 event_pending = 0;
  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. for(i=0;i<num_pictures;i++)
  63. pictures[i].surface = NULL;
  64. for(i=0;i<num_pictures;i++) {
  65. pictures[i].surface = SDL_LoadBMP(argv[i+1]);
  66. pictures[i].name = argv[i+1];
  67. if(pictures[i].surface == NULL) {
  68. j = 0;
  69. for(j=0;j<num_pictures;j++)
  70. SDL_FreeSurface(pictures[j].surface);
  71. SDL_free(pictures);
  72. SDL_VideoQuit();
  73. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
  74. exit(-3);
  75. }
  76. format = pictures[i].surface->format;
  77. if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
  78. pictures[i].mode.mode = ShapeModeBinarizeAlpha;
  79. pictures[i].mode.parameters.binarizationCutoff = 255;
  80. }
  81. else {
  82. pictures[i].mode.mode = ShapeModeColorKey;
  83. pictures[i].mode.parameters.colorKey = black;
  84. }
  85. }
  86. window = SDL_CreateShapedWindow("SDL_Shape test",
  87. SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
  88. SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,
  89. 0);
  90. SDL_SetWindowPosition(window, SHAPED_WINDOW_X, SHAPED_WINDOW_Y);
  91. if(window == NULL) {
  92. for(i=0;i<num_pictures;i++)
  93. SDL_FreeSurface(pictures[i].surface);
  94. SDL_free(pictures);
  95. SDL_VideoQuit();
  96. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
  97. exit(-4);
  98. }
  99. renderer = SDL_CreateRenderer(window,-1,0);
  100. if (!renderer) {
  101. SDL_DestroyWindow(window);
  102. for(i=0;i<num_pictures;i++)
  103. SDL_FreeSurface(pictures[i].surface);
  104. SDL_free(pictures);
  105. SDL_VideoQuit();
  106. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
  107. exit(-5);
  108. }
  109. for(i=0;i<num_pictures;i++)
  110. pictures[i].texture = NULL;
  111. for(i=0;i<num_pictures;i++) {
  112. pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
  113. if(pictures[i].texture == NULL) {
  114. j = 0;
  115. for(j=0;j<num_pictures;i++)
  116. if(pictures[i].texture != NULL)
  117. SDL_DestroyTexture(pictures[i].texture);
  118. for(i=0;i<num_pictures;i++)
  119. SDL_FreeSurface(pictures[i].surface);
  120. SDL_free(pictures);
  121. SDL_DestroyRenderer(renderer);
  122. SDL_DestroyWindow(window);
  123. SDL_VideoQuit();
  124. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
  125. exit(-6);
  126. }
  127. }
  128. event_pending = 0;
  129. should_exit = 0;
  130. event_pending = SDL_PollEvent(&event);
  131. current_picture = 0;
  132. button_down = 0;
  133. texture_dimensions.h = 0;
  134. texture_dimensions.w = 0;
  135. texture_dimensions.x = 0;
  136. texture_dimensions.y = 0;
  137. SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
  138. SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
  139. SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
  140. SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
  141. while(should_exit == 0) {
  142. event_pending = SDL_PollEvent(&event);
  143. if(event_pending == 1) {
  144. if(event.type == SDL_KEYDOWN) {
  145. button_down = 1;
  146. if(event.key.keysym.sym == SDLK_ESCAPE) {
  147. should_exit = 1;
  148. break;
  149. }
  150. }
  151. if(button_down && event.type == SDL_KEYUP) {
  152. button_down = 0;
  153. current_picture += 1;
  154. if(current_picture >= num_pictures)
  155. current_picture = 0;
  156. SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
  157. SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
  158. SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
  159. SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
  160. }
  161. if(event.type == SDL_QUIT)
  162. should_exit = 1;
  163. event_pending = 0;
  164. }
  165. render(renderer,pictures[current_picture].texture,texture_dimensions);
  166. SDL_Delay(10);
  167. }
  168. /* Free the textures. */
  169. for(i=0;i<num_pictures;i++)
  170. SDL_DestroyTexture(pictures[i].texture);
  171. SDL_DestroyRenderer(renderer);
  172. /* Destroy the window. */
  173. SDL_DestroyWindow(window);
  174. /* Free the original surfaces backing the textures. */
  175. for(i=0;i<num_pictures;i++)
  176. SDL_FreeSurface(pictures[i].surface);
  177. SDL_free(pictures);
  178. /* Call SDL_VideoQuit() before quitting. */
  179. SDL_VideoQuit();
  180. return 0;
  181. }
  182. /* vi: set ts=4 sw=4 expandtab: */