comparison test/testshape.c @ 4816:eb433f0d2ac5

Added Andreas's VS2010 patch for testshape.
author Eli Gottlieb <eligottlieb@gmail.com>
date Fri, 30 Jul 2010 18:04:21 -0400
parents 329708ffe2a7
children c68e7490e4cf
comparison
equal deleted inserted replaced
4815:93402b9dd20c 4816:eb433f0d2ac5
1 #include <stdlib.h> 1 #include <stdlib.h>
2 #include <math.h> 2 #include <math.h>
3 #include <SDL_events.h> 3 #include <stdio.h>
4 #include <SDL_rect.h> 4 #include "SDL.h"
5 #include <SDL_pixels.h> 5 #include "SDL_shape.h"
6 #include <SDL_video.h>
7 #include <SDL_shape.h>
8 #include <SDL_keysym.h>
9 #include <SDL_timer.h>
10 6
11 #define SHAPED_WINDOW_X 150 7 #define SHAPED_WINDOW_X 150
12 #define SHAPED_WINDOW_Y 150 8 #define SHAPED_WINDOW_Y 150
13 #define SHAPED_WINDOW_DIMENSION 640 9 #define SHAPED_WINDOW_DIMENSION 640
14 10
42 else 38 else
43 return next_time - now; 39 return next_time - now;
44 } 40 }
45 41
46 int main(int argc,char** argv) { 42 int main(int argc,char** argv) {
47 if(argc < 2) { 43 Uint8 num_pictures;
44 LoadedPicture* pictures;
45 int i, j;
46 SDL_PixelFormat* format;
47 Uint32 format_enum;
48 SDL_Window *window;
49 SDL_Color black = {0,0,0,0xff};
50 SDL_Event event;
51 int event_pending = 0;
52 int should_exit = 0;
53 unsigned int current_picture;
54 int button_down;
55 Uint32 pixelFormat;
56 int access;
57 SDL_Rect texture_dimensions;;
58
59 if(argc < 2) {
48 printf("SDL_Shape requires at least one bitmap file as argument.\n"); 60 printf("SDL_Shape requires at least one bitmap file as argument.\n");
49 exit(-1); 61 exit(-1);
50 } 62 }
51 63
52 if(SDL_VideoInit(NULL,0) == -1) { 64 if(SDL_VideoInit(NULL,0) == -1) {
53 printf("Could not initialize SDL video.\n"); 65 printf("Could not initialize SDL video.\n");
54 exit(-2); 66 exit(-2);
55 } 67 }
56 68
57 Uint8 num_pictures = argc - 1; 69 num_pictures = argc - 1;
58 LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures); 70 pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
59 int i = 0;
60 for(i=0;i<num_pictures;i++) 71 for(i=0;i<num_pictures;i++)
61 pictures[i].surface = NULL; 72 pictures[i].surface = NULL;
62 for(i=0;i<num_pictures;i++) { 73 for(i=0;i<num_pictures;i++) {
63 pictures[i].surface = SDL_LoadBMP(argv[i+1]); 74 pictures[i].surface = SDL_LoadBMP(argv[i+1]);
64 if(pictures[i].surface == NULL) { 75 if(pictures[i].surface == NULL) {
65 int j = 0; 76 j = 0;
66 for(j=0;j<num_pictures;j++) 77 for(j=0;j<num_pictures;j++)
67 if(pictures[j].surface != NULL) 78 if(pictures[j].surface != NULL)
68 SDL_FreeSurface(pictures[j].surface); 79 SDL_FreeSurface(pictures[j].surface);
69 free(pictures); 80 free(pictures);
70 SDL_VideoQuit(); 81 SDL_VideoQuit();
71 printf("Could not load surface from named bitmap file.\n"); 82 printf("Could not load surface from named bitmap file.\n");
72 exit(-3); 83 exit(-3);
73 } 84 }
74 SDL_PixelFormat* format = pictures[i].surface->format; 85
75 Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask); 86 format = pictures[i].surface->format;
87 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
76 if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) { 88 if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
77 pictures[i].mode.mode = ShapeModeBinarizeAlpha; 89 pictures[i].mode.mode = ShapeModeBinarizeAlpha;
78 pictures[i].mode.parameters.binarizationCutoff = 1; 90 pictures[i].mode.parameters.binarizationCutoff = 1;
79 } 91 }
80 else { 92 else {
81 pictures[i].mode.mode = ShapeModeColorKey; 93 pictures[i].mode.mode = ShapeModeColorKey;
82 SDL_Color black = {0,0,0,0xff};
83 pictures[i].mode.parameters.colorKey = black; 94 pictures[i].mode.parameters.colorKey = black;
84 } 95 }
85 } 96 }
86 97
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); 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);
88 if(window == NULL) { 99 if(window == NULL) {
89 for(i=0;i<num_pictures;i++) 100 for(i=0;i<num_pictures;i++)
90 SDL_FreeSurface(pictures[i].surface); 101 SDL_FreeSurface(pictures[i].surface);
91 free(pictures); 102 free(pictures);
92 SDL_VideoQuit(); 103 SDL_VideoQuit();
106 for(i=0;i<num_pictures;i++) 117 for(i=0;i<num_pictures;i++)
107 pictures[i].texture = NULL; 118 pictures[i].texture = NULL;
108 for(i=0;i<num_pictures;i++) { 119 for(i=0;i<num_pictures;i++) {
109 pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface); 120 pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
110 if(pictures[i].texture == NULL) { 121 if(pictures[i].texture == NULL) {
111 int j = 0; 122 j = 0;
112 for(j=0;j<num_pictures;i++) 123 for(j=0;j<num_pictures;i++)
113 if(pictures[i].texture != NULL) 124 if(pictures[i].texture != NULL)
114 SDL_DestroyTexture(pictures[i].texture); 125 SDL_DestroyTexture(pictures[i].texture);
115 for(i=0;i<num_pictures;i++) 126 for(i=0;i<num_pictures;i++)
116 SDL_FreeSurface(pictures[i].surface); 127 SDL_FreeSurface(pictures[i].surface);
121 printf("Could not create texture for SDL_shape.\n"); 132 printf("Could not create texture for SDL_shape.\n");
122 exit(-6); 133 exit(-6);
123 } 134 }
124 } 135 }
125 136
126 SDL_Event event; 137 event_pending = 0;
127 int event_pending = 0,should_exit = 0; 138 should_exit = 0;
128 event_pending = SDL_PollEvent(&event); 139 event_pending = SDL_PollEvent(&event);
129 unsigned int current_picture = 0; 140 current_picture = 0;
130 SDL_WindowShapeMode mode = {ShapeModeDefault,1}; 141 button_down = 0;
131 int button_down = 0; 142 format = 0,access = 0;
132 Uint32 format = 0,access = 0; 143 texture_dimensions.h = 0;
133 SDL_Rect texture_dimensions = {0,0,0,0}; 144 texture_dimensions.w = 0;
134 SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h); 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);
135 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); 148 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
136 SDL_SetWindowShape(window,pictures[current_picture].surface,&mode); 149 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
137 next_time = SDL_GetTicks() + TICK_INTERVAL; 150 next_time = SDL_GetTicks() + TICK_INTERVAL;
138 while(should_exit == 0) { 151 while(should_exit == 0) {
139 event_pending = SDL_PollEvent(&event); 152 event_pending = SDL_PollEvent(&event);
140 if(event_pending == 1) { 153 if(event_pending == 1) {
141 if(event.type == SDL_KEYDOWN) { 154 if(event.type == SDL_KEYDOWN) {
146 if(button_down && event.type == SDL_KEYUP) { 159 if(button_down && event.type == SDL_KEYUP) {
147 button_down = 0; 160 button_down = 0;
148 current_picture += 1; 161 current_picture += 1;
149 if(current_picture >= num_pictures) 162 if(current_picture >= num_pictures)
150 current_picture = 0; 163 current_picture = 0;
151 SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h); 164 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
152 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h); 165 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
153 SDL_SetWindowShape(window,pictures[current_picture].surface,&mode); 166 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
154 } 167 }
155 if(event.type == SDL_QUIT) 168 if(event.type == SDL_QUIT)
156 should_exit = 1; 169 should_exit = 1;
157 event_pending = 0; 170 event_pending = 0;
158 } 171 }
170 for(i=0;i<num_pictures;i++) 183 for(i=0;i<num_pictures;i++)
171 SDL_FreeSurface(pictures[i].surface); 184 SDL_FreeSurface(pictures[i].surface);
172 free(pictures); 185 free(pictures);
173 //Call SDL_VideoQuit() before quitting. 186 //Call SDL_VideoQuit() before quitting.
174 SDL_VideoQuit(); 187 SDL_VideoQuit();
188
189 return 0;
175 } 190 }