comparison test/testshape.c @ 5150:ad50b3db78bd

The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 01 Feb 2011 19:19:43 -0800
parents c8e049de174c
children b3ccd1947786
comparison
equal deleted inserted replaced
5149:3052772b59db 5150:ad50b3db78bd
9 #define SHAPED_WINDOW_DIMENSION 640 9 #define SHAPED_WINDOW_DIMENSION 640
10 10
11 #define TICK_INTERVAL 1000/10 11 #define TICK_INTERVAL 1000/10
12 12
13 typedef struct LoadedPicture { 13 typedef struct LoadedPicture {
14 SDL_Surface *surface; 14 SDL_Surface *surface;
15 SDL_Texture *texture; 15 SDL_Texture *texture;
16 SDL_WindowShapeMode mode; 16 SDL_WindowShapeMode mode;
17 } LoadedPicture; 17 } LoadedPicture;
18 18
19 void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) { 19 void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
20 SDL_SelectRenderer(window); 20 {
21 21 //Clear render-target to blue.
22 //Clear render-target to blue. 22 SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
23 SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff); 23 SDL_RenderClear(renderer);
24 SDL_RenderClear(); 24
25 25 //Render the texture.
26 //Render the texture. 26 SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
27 SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions); 27
28 28 SDL_RenderPresent(renderer);
29 SDL_RenderPresent();
30 } 29 }
31 30
32 static Uint32 next_time; 31 static Uint32 next_time;
33 32
34 Uint32 time_left() { 33 Uint32 time_left()
34 {
35 Uint32 now = SDL_GetTicks(); 35 Uint32 now = SDL_GetTicks();
36 if(next_time <= now) 36 if(next_time <= now)
37 return 0; 37 return 0;
38 else 38 else
39 return next_time - now; 39 return next_time - now;
40 } 40 }
41 41
42 int main(int argc,char** argv) { 42 int main(int argc,char** argv)
43 Uint8 num_pictures; 43 {
44 LoadedPicture* pictures; 44 Uint8 num_pictures;
45 int i, j; 45 LoadedPicture* pictures;
46 SDL_PixelFormat* format = NULL; 46 int i, j;
47 SDL_Window *window; 47 SDL_PixelFormat* format = NULL;
48 SDL_Color black = {0,0,0,0xff}; 48 SDL_Window *window;
49 SDL_Event event; 49 SDL_Renderer *renderer;
50 int event_pending = 0; 50 SDL_Color black = {0,0,0,0xff};
51 int should_exit = 0; 51 SDL_Event event;
52 unsigned int current_picture; 52 int event_pending = 0;
53 int button_down; 53 int should_exit = 0;
54 Uint32 pixelFormat = 0; 54 unsigned int current_picture;
55 int access = 0; 55 int button_down;
56 SDL_Rect texture_dimensions;; 56 Uint32 pixelFormat = 0;
57 int access = 0;
58 SDL_Rect texture_dimensions;;
57 59
58 if(argc < 2) { 60 if(argc < 2) {
59 printf("SDL_Shape requires at least one bitmap file as argument.\n"); 61 printf("SDL_Shape requires at least one bitmap file as argument.\n");
60 exit(-1); 62 exit(-1);
61 } 63 }
62 64
63 if(SDL_VideoInit(NULL) == -1) { 65 if(SDL_VideoInit(NULL) == -1) {
64 printf("Could not initialize SDL video.\n"); 66 printf("Could not initialize SDL video.\n");
65 exit(-2); 67 exit(-2);
66 } 68 }
67 69
68 num_pictures = argc - 1; 70 num_pictures = argc - 1;
69 pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures); 71 pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
70 for(i=0;i<num_pictures;i++) 72 for(i=0;i<num_pictures;i++)
71 pictures[i].surface = NULL; 73 pictures[i].surface = NULL;
72 for(i=0;i<num_pictures;i++) { 74 for(i=0;i<num_pictures;i++) {
73 pictures[i].surface = SDL_LoadBMP(argv[i+1]); 75 pictures[i].surface = SDL_LoadBMP(argv[i+1]);
74 if(pictures[i].surface == NULL) { 76 if(pictures[i].surface == NULL) {
75 j = 0; 77 j = 0;
76 for(j=0;j<num_pictures;j++) 78 for(j=0;j<num_pictures;j++)
77 if(pictures[j].surface != NULL) 79 if(pictures[j].surface != NULL)
78 SDL_FreeSurface(pictures[j].surface); 80 SDL_FreeSurface(pictures[j].surface);
79 free(pictures); 81 free(pictures);
80 SDL_VideoQuit(); 82 SDL_VideoQuit();
81 printf("Could not load surface from named bitmap file.\n"); 83 printf("Could not load surface from named bitmap file.\n");
82 exit(-3); 84 exit(-3);
83 } 85 }
84 86
85 format = pictures[i].surface->format; 87 format = pictures[i].surface->format;
86 if(format->Amask != 0) { 88 if(format->Amask != 0) {
87 pictures[i].mode.mode = ShapeModeBinarizeAlpha; 89 pictures[i].mode.mode = ShapeModeBinarizeAlpha;
88 pictures[i].mode.parameters.binarizationCutoff = 255; 90 pictures[i].mode.parameters.binarizationCutoff = 255;
89 } 91 }
90 else { 92 else {
91 pictures[i].mode.mode = ShapeModeColorKey; 93 pictures[i].mode.mode = ShapeModeColorKey;
92 pictures[i].mode.parameters.colorKey = black; 94 pictures[i].mode.parameters.colorKey = black;
93 } 95 }
94 } 96 }
95 97
96 window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN); 98 window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
97 if(window == NULL) { 99 if(window == NULL) {
98 for(i=0;i<num_pictures;i++) 100 for(i=0;i<num_pictures;i++)
99 SDL_FreeSurface(pictures[i].surface); 101 SDL_FreeSurface(pictures[i].surface);
100 free(pictures); 102 free(pictures);
101 SDL_VideoQuit(); 103 SDL_VideoQuit();
102 printf("Could not create shaped window for SDL_Shape.\n"); 104 printf("Could not create shaped window for SDL_Shape.\n");
103 exit(-4); 105 exit(-4);
104 } 106 }
105 if(SDL_CreateRenderer(window,-1,0) == -1) { 107 renderer = SDL_CreateRenderer(window,-1,0);
106 SDL_DestroyWindow(window); 108 if (!renderer) {
107 for(i=0;i<num_pictures;i++) 109 SDL_DestroyWindow(window);
108 SDL_FreeSurface(pictures[i].surface); 110 for(i=0;i<num_pictures;i++)
109 free(pictures); 111 SDL_FreeSurface(pictures[i].surface);
110 SDL_VideoQuit(); 112 free(pictures);
111 printf("Could not create rendering context for SDL_Shape window.\n"); 113 SDL_VideoQuit();
112 exit(-5); 114 printf("Could not create rendering context for SDL_Shape window.\n");
113 } 115 exit(-5);
114 116 }
115 for(i=0;i<num_pictures;i++) 117
116 pictures[i].texture = NULL; 118 for(i=0;i<num_pictures;i++)
117 for(i=0;i<num_pictures;i++) { 119 pictures[i].texture = NULL;
118 pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface); 120 for(i=0;i<num_pictures;i++) {
119 if(pictures[i].texture == NULL) { 121 pictures[i].texture = SDL_CreateTextureFromSurface(renderer,0,pictures[i].surface);
120 j = 0; 122 if(pictures[i].texture == NULL) {
121 for(j=0;j<num_pictures;i++) 123 j = 0;
122 if(pictures[i].texture != NULL) 124 for(j=0;j<num_pictures;i++)
123 SDL_DestroyTexture(pictures[i].texture); 125 if(pictures[i].texture != NULL)
124 for(i=0;i<num_pictures;i++) 126 SDL_DestroyTexture(pictures[i].texture);
125 SDL_FreeSurface(pictures[i].surface); 127 for(i=0;i<num_pictures;i++)
126 free(pictures); 128 SDL_FreeSurface(pictures[i].surface);
127 SDL_DestroyRenderer(window); 129 free(pictures);
128 SDL_DestroyWindow(window); 130 SDL_DestroyRenderer(renderer);
129 SDL_VideoQuit(); 131 SDL_DestroyWindow(window);
130 printf("Could not create texture for SDL_shape.\n"); 132 SDL_VideoQuit();
131 exit(-6); 133 printf("Could not create texture for SDL_shape.\n");
132 } 134 exit(-6);
133 } 135 }
134 136 }
135 event_pending = 0; 137
136 should_exit = 0; 138 event_pending = 0;
137 event_pending = SDL_PollEvent(&event); 139 should_exit = 0;
138 current_picture = 0; 140 event_pending = SDL_PollEvent(&event);
139 button_down = 0; 141 current_picture = 0;
140 texture_dimensions.h = 0; 142 button_down = 0;
141 texture_dimensions.w = 0; 143 texture_dimensions.h = 0;
142 texture_dimensions.x = 0; 144 texture_dimensions.w = 0;
143 texture_dimensions.y = 0; 145 texture_dimensions.x = 0;
144 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h); 146 texture_dimensions.y = 0;
145 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); 147 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
146 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); 148 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
147 next_time = SDL_GetTicks() + TICK_INTERVAL; 149 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
148 while(should_exit == 0) { 150 next_time = SDL_GetTicks() + TICK_INTERVAL;
149 event_pending = SDL_PollEvent(&event); 151 while(should_exit == 0) {
150 if(event_pending == 1) { 152 event_pending = SDL_PollEvent(&event);
151 if(event.type == SDL_KEYDOWN) { 153 if(event_pending == 1) {
152 button_down = 1; 154 if(event.type == SDL_KEYDOWN) {
153 if(event.key.keysym.sym == SDLK_ESCAPE) 155 button_down = 1;
154 should_exit = 1; 156 if(event.key.keysym.sym == SDLK_ESCAPE)
155 } 157 should_exit = 1;
156 if(button_down && event.type == SDL_KEYUP) { 158 }
157 button_down = 0; 159 if(button_down && event.type == SDL_KEYUP) {
158 current_picture += 1; 160 button_down = 0;
159 if(current_picture >= num_pictures) 161 current_picture += 1;
160 current_picture = 0; 162 if(current_picture >= num_pictures)
161 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h); 163 current_picture = 0;
162 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); 164 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
163 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode); 165 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
164 } 166 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
165 if(event.type == SDL_QUIT) 167 }
166 should_exit = 1; 168 if(event.type == SDL_QUIT)
167 event_pending = 0; 169 should_exit = 1;
168 } 170 event_pending = 0;
169 render(window,pictures[current_picture].texture,texture_dimensions); 171 }
170 SDL_Delay(time_left()); 172 render(renderer,pictures[current_picture].texture,texture_dimensions);
171 next_time += TICK_INTERVAL; 173 SDL_Delay(time_left());
172 } 174 next_time += TICK_INTERVAL;
173 175 }
174 //Free the textures. 176
175 for(i=0;i<num_pictures;i++) 177 //Free the textures.
176 SDL_DestroyTexture(pictures[i].texture); 178 for(i=0;i<num_pictures;i++)
177 //Destroy the window. 179 SDL_DestroyTexture(pictures[i].texture);
178 SDL_DestroyWindow(window); 180 SDL_DestroyRenderer(renderer);
179 //Free the original surfaces backing the textures. 181 //Destroy the window.
180 for(i=0;i<num_pictures;i++) 182 SDL_DestroyWindow(window);
181 SDL_FreeSurface(pictures[i].surface); 183 //Free the original surfaces backing the textures.
182 free(pictures); 184 for(i=0;i<num_pictures;i++)
183 //Call SDL_VideoQuit() before quitting. 185 SDL_FreeSurface(pictures[i].surface);
184 SDL_VideoQuit(); 186 free(pictures);
187 //Call SDL_VideoQuit() before quitting.
188 SDL_VideoQuit();
185 189
186 return 0; 190 return 0;
187 } 191 }
192
193 /* vi: set ts=4 sw=4 expandtab: */