Mercurial > sdl-ios-xcode
comparison test/testshape.c @ 4808:2ae79ed78a5a
More work on color-key mode.
author | Eli Gottlieb <eligottlieb@gmail.com> |
---|---|
date | Fri, 23 Jul 2010 01:48:42 -0400 |
parents | de3b3455f8ec |
children | 329708ffe2a7 |
comparison
equal
deleted
inserted
replaced
4807:c9eb95f29770 | 4808:2ae79ed78a5a |
---|---|
11 #define SHAPED_WINDOW_X 150 | 11 #define SHAPED_WINDOW_X 150 |
12 #define SHAPED_WINDOW_Y 150 | 12 #define SHAPED_WINDOW_Y 150 |
13 #define SHAPED_WINDOW_DIMENSION 640 | 13 #define SHAPED_WINDOW_DIMENSION 640 |
14 | 14 |
15 #define TICK_INTERVAL 18 | 15 #define TICK_INTERVAL 18 |
16 | |
17 typedef struct LoadedPicture { | |
18 SDL_Surface *surface; | |
19 SDL_Texture *texture; | |
20 SDL_WindowShapeMode mode; | |
21 } LoadedPicture; | |
16 | 22 |
17 void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) { | 23 void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) { |
18 SDL_SelectRenderer(window); | 24 SDL_SelectRenderer(window); |
19 | 25 |
20 //Clear render-target to blue. | 26 //Clear render-target to blue. |
47 printf("Could not initialize SDL video.\n"); | 53 printf("Could not initialize SDL video.\n"); |
48 exit(-2); | 54 exit(-2); |
49 } | 55 } |
50 | 56 |
51 Uint8 num_pictures = argc - 1; | 57 Uint8 num_pictures = argc - 1; |
52 SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures); | 58 LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures); |
53 int i = 0; | 59 int i = 0; |
54 for(i=0;i<num_pictures;i++) | 60 for(i=0;i<num_pictures;i++) |
55 pictures[i] = NULL; | 61 pictures[i].surface = NULL; |
56 for(i=0;i<num_pictures;i++) { | 62 for(i=0;i<num_pictures;i++) { |
57 pictures[i] = SDL_LoadBMP(argv[i+1]); | 63 pictures[i].surface = SDL_LoadBMP(argv[i+1]); |
58 if(pictures[i] == NULL) { | 64 if(pictures[i].surface == NULL) { |
59 int j = 0; | 65 int j = 0; |
60 for(j=0;j<num_pictures;j++) | 66 for(j=0;j<num_pictures;j++) |
61 if(pictures[j] != NULL) | 67 if(pictures[j].surface != NULL) |
62 SDL_FreeSurface(pictures[j]); | 68 SDL_FreeSurface(pictures[j].surface); |
63 free(pictures); | 69 free(pictures); |
64 SDL_VideoQuit(); | 70 SDL_VideoQuit(); |
65 printf("Could not load surface from named bitmap file.\n"); | 71 printf("Could not load surface from named bitmap file.\n"); |
66 exit(-3); | 72 exit(-3); |
73 } | |
74 SDL_PixelFormat* format = pictures[i].surface->format; | |
75 Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask); | |
76 if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) { | |
77 pictures[i].mode.mode = ShapeModeBinarizeAlpha; | |
78 pictures[i].mode.parameters.binarizationCutoff = 1; | |
79 } | |
80 else { | |
81 pictures[i].mode.mode = ShapeModeColorKey; | |
82 SDL_Color black = {0,0,0,0xff}; | |
83 pictures[i].mode.parameters.colorKey = black; | |
67 } | 84 } |
68 } | 85 } |
69 | 86 |
70 SDL_Window *window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN); | 87 SDL_Window *window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN); |
71 if(window == NULL) { | 88 if(window == NULL) { |
72 for(i=0;i<num_pictures;i++) | 89 for(i=0;i<num_pictures;i++) |
73 SDL_FreeSurface(pictures[i]); | 90 SDL_FreeSurface(pictures[i].surface); |
74 free(pictures); | 91 free(pictures); |
75 SDL_VideoQuit(); | 92 SDL_VideoQuit(); |
76 printf("Could not create shaped window for SDL_Shape.\n"); | 93 printf("Could not create shaped window for SDL_Shape.\n"); |
77 exit(-4); | 94 exit(-4); |
78 } | 95 } |
79 if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) { | 96 if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) { |
80 SDL_DestroyWindow(window); | 97 SDL_DestroyWindow(window); |
81 for(i=0;i<num_pictures;i++) | 98 for(i=0;i<num_pictures;i++) |
82 SDL_FreeSurface(pictures[i]); | 99 SDL_FreeSurface(pictures[i].surface); |
83 free(pictures); | 100 free(pictures); |
84 SDL_VideoQuit(); | 101 SDL_VideoQuit(); |
85 printf("Could not create rendering context for SDL_Shape window.\n"); | 102 printf("Could not create rendering context for SDL_Shape window.\n"); |
86 exit(-5); | 103 exit(-5); |
87 } | 104 } |
88 | 105 |
89 SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures); | |
90 for(i=0;i<num_pictures;i++) | 106 for(i=0;i<num_pictures;i++) |
91 textures[i] = NULL; | 107 pictures[i].texture = NULL; |
92 for(i=0;i<num_pictures;i++) { | 108 for(i=0;i<num_pictures;i++) { |
93 textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]); | 109 pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface); |
94 if(textures[i] == NULL) { | 110 if(pictures[i].texture == NULL) { |
95 int j = 0; | 111 int j = 0; |
96 for(j=0;j<num_pictures;i++) | 112 for(j=0;j<num_pictures;i++) |
97 if(textures[i] != NULL) | 113 if(pictures[i].texture != NULL) |
98 SDL_DestroyTexture(textures[i]); | 114 SDL_DestroyTexture(pictures[i].texture); |
99 free(textures); | |
100 for(i=0;i<num_pictures;i++) | 115 for(i=0;i<num_pictures;i++) |
101 SDL_FreeSurface(pictures[i]); | 116 SDL_FreeSurface(pictures[i].surface); |
102 free(pictures); | 117 free(pictures); |
103 SDL_DestroyRenderer(window); | 118 SDL_DestroyRenderer(window); |
104 SDL_DestroyWindow(window); | 119 SDL_DestroyWindow(window); |
105 SDL_VideoQuit(); | 120 SDL_VideoQuit(); |
106 printf("Could not create texture for SDL_shape.\n"); | 121 printf("Could not create texture for SDL_shape.\n"); |
114 unsigned int current_picture = 0; | 129 unsigned int current_picture = 0; |
115 SDL_WindowShapeMode mode = {ShapeModeDefault,1}; | 130 SDL_WindowShapeMode mode = {ShapeModeDefault,1}; |
116 int button_down = 0; | 131 int button_down = 0; |
117 Uint32 format = 0,access = 0; | 132 Uint32 format = 0,access = 0; |
118 SDL_Rect texture_dimensions = {0,0,0,0}; | 133 SDL_Rect texture_dimensions = {0,0,0,0}; |
119 SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h); | 134 SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h); |
120 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); | 135 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); |
121 SDL_SetWindowShape(window,pictures[current_picture],&mode); | 136 SDL_SetWindowShape(window,pictures[current_picture].surface,&mode); |
122 next_time = SDL_GetTicks() + TICK_INTERVAL; | 137 next_time = SDL_GetTicks() + TICK_INTERVAL; |
123 while(should_exit == 0) { | 138 while(should_exit == 0) { |
124 event_pending = SDL_PollEvent(&event); | 139 event_pending = SDL_PollEvent(&event); |
125 if(event_pending == 1) { | 140 if(event_pending == 1) { |
126 if(event.type == SDL_KEYDOWN) { | 141 if(event.type == SDL_KEYDOWN) { |
131 if(button_down && event.type == SDL_KEYUP) { | 146 if(button_down && event.type == SDL_KEYUP) { |
132 button_down = 0; | 147 button_down = 0; |
133 current_picture += 1; | 148 current_picture += 1; |
134 if(current_picture >= num_pictures) | 149 if(current_picture >= num_pictures) |
135 current_picture = 0; | 150 current_picture = 0; |
136 SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h); | 151 SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h); |
137 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); | 152 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); |
138 SDL_SetWindowShape(window,pictures[current_picture],&mode); | 153 SDL_SetWindowShape(window,pictures[current_picture].surface,&mode); |
139 } | 154 } |
140 if(event.type == SDL_QUIT) | 155 if(event.type == SDL_QUIT) |
141 should_exit = 1; | 156 should_exit = 1; |
142 event_pending = 0; | 157 event_pending = 0; |
143 } | 158 } |
144 render(window,textures[current_picture],texture_dimensions); | 159 render(window,pictures[current_picture].texture,texture_dimensions); |
145 SDL_Delay(time_left()); | 160 SDL_Delay(time_left()); |
146 next_time += TICK_INTERVAL; | 161 next_time += TICK_INTERVAL; |
147 } | 162 } |
148 | 163 |
149 //Free the textures. | 164 //Free the textures. |
150 for(i=0;i<num_pictures;i++) | 165 for(i=0;i<num_pictures;i++) |
151 SDL_DestroyTexture(textures[i]); | 166 SDL_DestroyTexture(pictures[i].texture); |
152 free(textures); | |
153 //Destroy the window. | 167 //Destroy the window. |
154 SDL_DestroyWindow(window); | 168 SDL_DestroyWindow(window); |
155 //Free the original surfaces backing the textures. | 169 //Free the original surfaces backing the textures. |
156 for(i=0;i<num_pictures;i++) | 170 for(i=0;i<num_pictures;i++) |
157 SDL_FreeSurface(pictures[i]); | 171 SDL_FreeSurface(pictures[i].surface); |
158 free(pictures); | 172 free(pictures); |
159 //Call SDL_VideoQuit() before quitting. | 173 //Call SDL_VideoQuit() before quitting. |
160 SDL_VideoQuit(); | 174 SDL_VideoQuit(); |
161 } | 175 } |