Making the API simpler, the renderer present semantics are always having a backbuffer and then discarding it. This is best for hardware accelerated rendering.
7 #define SHAPED_WINDOW_X 150
8 #define SHAPED_WINDOW_Y 150
9 #define SHAPED_WINDOW_DIMENSION 640
11 #define TICK_INTERVAL 1000/10
13 typedef struct LoadedPicture {
16 SDL_WindowShapeMode mode;
19 void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
20 SDL_SelectRenderer(window);
22 //Clear render-target to blue.
23 SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
27 SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions);
32 static Uint32 next_time;
35 Uint32 now = SDL_GetTicks();
39 return next_time - now;
42 int main(int argc,char** argv) {
44 LoadedPicture* pictures;
46 SDL_PixelFormat* format = NULL;
48 SDL_Color black = {0,0,0,0xff};
50 int event_pending = 0;
52 unsigned int current_picture;
54 Uint32 pixelFormat = 0;
56 SDL_Rect texture_dimensions;;
59 printf("SDL_Shape requires at least one bitmap file as argument.\n");
63 if(SDL_VideoInit(NULL) == -1) {
64 printf("Could not initialize SDL video.\n");
68 num_pictures = argc - 1;
69 pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
70 for(i=0;i<num_pictures;i++)
71 pictures[i].surface = NULL;
72 for(i=0;i<num_pictures;i++) {
73 pictures[i].surface = SDL_LoadBMP(argv[i+1]);
74 if(pictures[i].surface == NULL) {
76 for(j=0;j<num_pictures;j++)
77 if(pictures[j].surface != NULL)
78 SDL_FreeSurface(pictures[j].surface);
81 printf("Could not load surface from named bitmap file.\n");
85 format = pictures[i].surface->format;
86 if(format->Amask != 0) {
87 pictures[i].mode.mode = ShapeModeBinarizeAlpha;
88 pictures[i].mode.parameters.binarizationCutoff = 255;
91 pictures[i].mode.mode = ShapeModeColorKey;
92 pictures[i].mode.parameters.colorKey = black;
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 for(i=0;i<num_pictures;i++)
99 SDL_FreeSurface(pictures[i].surface);
102 printf("Could not create shaped window for SDL_Shape.\n");
105 if(SDL_CreateRenderer(window,-1,0) == -1) {
106 SDL_DestroyWindow(window);
107 for(i=0;i<num_pictures;i++)
108 SDL_FreeSurface(pictures[i].surface);
111 printf("Could not create rendering context for SDL_Shape window.\n");
115 for(i=0;i<num_pictures;i++)
116 pictures[i].texture = NULL;
117 for(i=0;i<num_pictures;i++) {
118 pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
119 if(pictures[i].texture == NULL) {
121 for(j=0;j<num_pictures;i++)
122 if(pictures[i].texture != NULL)
123 SDL_DestroyTexture(pictures[i].texture);
124 for(i=0;i<num_pictures;i++)
125 SDL_FreeSurface(pictures[i].surface);
127 SDL_DestroyRenderer(window);
128 SDL_DestroyWindow(window);
130 printf("Could not create texture for SDL_shape.\n");
137 event_pending = SDL_PollEvent(&event);
140 texture_dimensions.h = 0;
141 texture_dimensions.w = 0;
142 texture_dimensions.x = 0;
143 texture_dimensions.y = 0;
144 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
145 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
146 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
147 next_time = SDL_GetTicks() + TICK_INTERVAL;
148 while(should_exit == 0) {
149 event_pending = SDL_PollEvent(&event);
150 if(event_pending == 1) {
151 if(event.type == SDL_KEYDOWN) {
153 if(event.key.keysym.sym == SDLK_ESCAPE)
156 if(button_down && event.type == SDL_KEYUP) {
158 current_picture += 1;
159 if(current_picture >= num_pictures)
161 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
162 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
163 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
165 if(event.type == SDL_QUIT)
169 render(window,pictures[current_picture].texture,texture_dimensions);
170 SDL_Delay(time_left());
171 next_time += TICK_INTERVAL;
175 for(i=0;i<num_pictures;i++)
176 SDL_DestroyTexture(pictures[i].texture);
177 //Destroy the window.
178 SDL_DestroyWindow(window);
179 //Free the original surfaces backing the textures.
180 for(i=0;i<num_pictures;i++)
181 SDL_FreeSurface(pictures[i].surface);
183 //Call SDL_VideoQuit() before quitting.