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.
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_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
21 //Clear render-target to blue.
22 SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
23 SDL_RenderClear(renderer);
26 SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
28 SDL_RenderPresent(renderer);
31 static Uint32 next_time;
35 Uint32 now = SDL_GetTicks();
39 return next_time - now;
42 int main(int argc,char** argv)
45 LoadedPicture* pictures;
47 SDL_PixelFormat* format = NULL;
49 SDL_Renderer *renderer;
50 SDL_Color black = {0,0,0,0xff};
52 int event_pending = 0;
54 unsigned int current_picture;
56 Uint32 pixelFormat = 0;
58 SDL_Rect texture_dimensions;;
61 printf("SDL_Shape requires at least one bitmap file as argument.\n");
65 if(SDL_VideoInit(NULL) == -1) {
66 printf("Could not initialize SDL video.\n");
70 num_pictures = argc - 1;
71 pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
72 for(i=0;i<num_pictures;i++)
73 pictures[i].surface = NULL;
74 for(i=0;i<num_pictures;i++) {
75 pictures[i].surface = SDL_LoadBMP(argv[i+1]);
76 if(pictures[i].surface == NULL) {
78 for(j=0;j<num_pictures;j++)
79 if(pictures[j].surface != NULL)
80 SDL_FreeSurface(pictures[j].surface);
83 printf("Could not load surface from named bitmap file.\n");
87 format = pictures[i].surface->format;
88 if(format->Amask != 0) {
89 pictures[i].mode.mode = ShapeModeBinarizeAlpha;
90 pictures[i].mode.parameters.binarizationCutoff = 255;
93 pictures[i].mode.mode = ShapeModeColorKey;
94 pictures[i].mode.parameters.colorKey = black;
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);
100 for(i=0;i<num_pictures;i++)
101 SDL_FreeSurface(pictures[i].surface);
104 printf("Could not create shaped window for SDL_Shape.\n");
107 renderer = SDL_CreateRenderer(window,-1,0);
109 SDL_DestroyWindow(window);
110 for(i=0;i<num_pictures;i++)
111 SDL_FreeSurface(pictures[i].surface);
114 printf("Could not create rendering context for SDL_Shape window.\n");
118 for(i=0;i<num_pictures;i++)
119 pictures[i].texture = NULL;
120 for(i=0;i<num_pictures;i++) {
121 pictures[i].texture = SDL_CreateTextureFromSurface(renderer,0,pictures[i].surface);
122 if(pictures[i].texture == NULL) {
124 for(j=0;j<num_pictures;i++)
125 if(pictures[i].texture != NULL)
126 SDL_DestroyTexture(pictures[i].texture);
127 for(i=0;i<num_pictures;i++)
128 SDL_FreeSurface(pictures[i].surface);
130 SDL_DestroyRenderer(renderer);
131 SDL_DestroyWindow(window);
133 printf("Could not create texture for SDL_shape.\n");
140 event_pending = SDL_PollEvent(&event);
143 texture_dimensions.h = 0;
144 texture_dimensions.w = 0;
145 texture_dimensions.x = 0;
146 texture_dimensions.y = 0;
147 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
148 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
149 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
150 next_time = SDL_GetTicks() + TICK_INTERVAL;
151 while(should_exit == 0) {
152 event_pending = SDL_PollEvent(&event);
153 if(event_pending == 1) {
154 if(event.type == SDL_KEYDOWN) {
156 if(event.key.keysym.sym == SDLK_ESCAPE)
159 if(button_down && event.type == SDL_KEYUP) {
161 current_picture += 1;
162 if(current_picture >= num_pictures)
164 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
165 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
166 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
168 if(event.type == SDL_QUIT)
172 render(renderer,pictures[current_picture].texture,texture_dimensions);
173 SDL_Delay(time_left());
174 next_time += TICK_INTERVAL;
178 for(i=0;i<num_pictures;i++)
179 SDL_DestroyTexture(pictures[i].texture);
180 SDL_DestroyRenderer(renderer);
181 //Destroy the window.
182 SDL_DestroyWindow(window);
183 //Free the original surfaces backing the textures.
184 for(i=0;i<num_pictures;i++)
185 SDL_FreeSurface(pictures[i].surface);
187 //Call SDL_VideoQuit() before quitting.
193 /* vi: set ts=4 sw=4 expandtab: */