test/testshape.c
changeset 5147 ad50b3db78bd
parent 5142 c8e049de174c
child 5158 b3ccd1947786
     1.1 --- a/test/testshape.c	Tue Feb 01 19:15:42 2011 -0800
     1.2 +++ b/test/testshape.c	Tue Feb 01 19:19:43 2011 -0800
     1.3 @@ -11,177 +11,183 @@
     1.4  #define TICK_INTERVAL 1000/10
     1.5  
     1.6  typedef struct LoadedPicture {
     1.7 -	SDL_Surface *surface;
     1.8 -	SDL_Texture *texture;
     1.9 -	SDL_WindowShapeMode mode;
    1.10 +    SDL_Surface *surface;
    1.11 +    SDL_Texture *texture;
    1.12 +    SDL_WindowShapeMode mode;
    1.13  } LoadedPicture;
    1.14  
    1.15 -void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
    1.16 -	SDL_SelectRenderer(window);
    1.17 -	
    1.18 -	//Clear render-target to blue.
    1.19 -	SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
    1.20 -	SDL_RenderClear();
    1.21 -	
    1.22 -	//Render the texture.
    1.23 -	SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions);
    1.24 -	
    1.25 -	SDL_RenderPresent();
    1.26 +void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
    1.27 +{
    1.28 +    //Clear render-target to blue.
    1.29 +    SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
    1.30 +    SDL_RenderClear(renderer);
    1.31 +    
    1.32 +    //Render the texture.
    1.33 +    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
    1.34 +    
    1.35 +    SDL_RenderPresent(renderer);
    1.36  }
    1.37  
    1.38  static Uint32 next_time;
    1.39  
    1.40 -Uint32 time_left() {
    1.41 +Uint32 time_left()
    1.42 +{
    1.43      Uint32 now = SDL_GetTicks();
    1.44      if(next_time <= now)
    1.45          return 0;
    1.46 -	else
    1.47 +    else
    1.48          return next_time - now;
    1.49  }
    1.50  
    1.51 -int main(int argc,char** argv) {
    1.52 -	Uint8 num_pictures;
    1.53 -	LoadedPicture* pictures;
    1.54 -	int i, j;
    1.55 -	SDL_PixelFormat* format = NULL;
    1.56 -	SDL_Window *window;
    1.57 -	SDL_Color black = {0,0,0,0xff};
    1.58 -	SDL_Event event;
    1.59 -	int event_pending = 0;
    1.60 -	int should_exit = 0;
    1.61 -	unsigned int current_picture;
    1.62 -	int button_down;
    1.63 -	Uint32 pixelFormat = 0;
    1.64 -	int access = 0;
    1.65 -	SDL_Rect texture_dimensions;;
    1.66 +int main(int argc,char** argv)
    1.67 +{
    1.68 +    Uint8 num_pictures;
    1.69 +    LoadedPicture* pictures;
    1.70 +    int i, j;
    1.71 +    SDL_PixelFormat* format = NULL;
    1.72 +    SDL_Window *window;
    1.73 +    SDL_Renderer *renderer;
    1.74 +    SDL_Color black = {0,0,0,0xff};
    1.75 +    SDL_Event event;
    1.76 +    int event_pending = 0;
    1.77 +    int should_exit = 0;
    1.78 +    unsigned int current_picture;
    1.79 +    int button_down;
    1.80 +    Uint32 pixelFormat = 0;
    1.81 +    int access = 0;
    1.82 +    SDL_Rect texture_dimensions;;
    1.83  
    1.84 -	if(argc < 2) {
    1.85 -    	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    1.86 -    	exit(-1);
    1.87 +    if(argc < 2) {
    1.88 +        printf("SDL_Shape requires at least one bitmap file as argument.\n");
    1.89 +        exit(-1);
    1.90      }
    1.91 -	
    1.92 -	if(SDL_VideoInit(NULL) == -1) {
    1.93 -		printf("Could not initialize SDL video.\n");
    1.94 -		exit(-2);
    1.95 -	}
    1.96 -	
    1.97 -	num_pictures = argc - 1;
    1.98 -	pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
    1.99 -	for(i=0;i<num_pictures;i++)
   1.100 -		pictures[i].surface = NULL;
   1.101 -	for(i=0;i<num_pictures;i++) {
   1.102 -		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
   1.103 -		if(pictures[i].surface == NULL) {
   1.104 -			j = 0;
   1.105 -			for(j=0;j<num_pictures;j++)
   1.106 -				if(pictures[j].surface != NULL)
   1.107 -					SDL_FreeSurface(pictures[j].surface);
   1.108 -			free(pictures);
   1.109 -			SDL_VideoQuit();
   1.110 -			printf("Could not load surface from named bitmap file.\n");
   1.111 -			exit(-3);
   1.112 -		}
   1.113 +    
   1.114 +    if(SDL_VideoInit(NULL) == -1) {
   1.115 +        printf("Could not initialize SDL video.\n");
   1.116 +        exit(-2);
   1.117 +    }
   1.118 +    
   1.119 +    num_pictures = argc - 1;
   1.120 +    pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
   1.121 +    for(i=0;i<num_pictures;i++)
   1.122 +        pictures[i].surface = NULL;
   1.123 +    for(i=0;i<num_pictures;i++) {
   1.124 +        pictures[i].surface = SDL_LoadBMP(argv[i+1]);
   1.125 +        if(pictures[i].surface == NULL) {
   1.126 +            j = 0;
   1.127 +            for(j=0;j<num_pictures;j++)
   1.128 +                if(pictures[j].surface != NULL)
   1.129 +                    SDL_FreeSurface(pictures[j].surface);
   1.130 +            free(pictures);
   1.131 +            SDL_VideoQuit();
   1.132 +            printf("Could not load surface from named bitmap file.\n");
   1.133 +            exit(-3);
   1.134 +        }
   1.135  
   1.136 -		format = pictures[i].surface->format;
   1.137 -		if(format->Amask != 0) {
   1.138 -			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
   1.139 -			pictures[i].mode.parameters.binarizationCutoff = 255;
   1.140 -		}
   1.141 -		else {
   1.142 -			pictures[i].mode.mode = ShapeModeColorKey;
   1.143 -			pictures[i].mode.parameters.colorKey = black;
   1.144 -		}
   1.145 -	}
   1.146 -	
   1.147 -	window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
   1.148 -	if(window == NULL) {
   1.149 -		for(i=0;i<num_pictures;i++)
   1.150 -			SDL_FreeSurface(pictures[i].surface);
   1.151 -		free(pictures);
   1.152 -		SDL_VideoQuit();
   1.153 -		printf("Could not create shaped window for SDL_Shape.\n");
   1.154 -		exit(-4);
   1.155 -	}
   1.156 -	if(SDL_CreateRenderer(window,-1,0) == -1) {
   1.157 -		SDL_DestroyWindow(window);
   1.158 -		for(i=0;i<num_pictures;i++)
   1.159 -			SDL_FreeSurface(pictures[i].surface);
   1.160 -		free(pictures);
   1.161 -		SDL_VideoQuit();
   1.162 -		printf("Could not create rendering context for SDL_Shape window.\n");
   1.163 -		exit(-5);
   1.164 -	}
   1.165 -	
   1.166 -	for(i=0;i<num_pictures;i++)
   1.167 -		pictures[i].texture = NULL;
   1.168 -	for(i=0;i<num_pictures;i++) {
   1.169 -		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
   1.170 -		if(pictures[i].texture == NULL) {
   1.171 -			j = 0;
   1.172 -			for(j=0;j<num_pictures;i++)
   1.173 -				if(pictures[i].texture != NULL)
   1.174 -					SDL_DestroyTexture(pictures[i].texture);
   1.175 -			for(i=0;i<num_pictures;i++)
   1.176 -				SDL_FreeSurface(pictures[i].surface);
   1.177 -			free(pictures);
   1.178 -			SDL_DestroyRenderer(window);
   1.179 -			SDL_DestroyWindow(window);
   1.180 -			SDL_VideoQuit();
   1.181 -			printf("Could not create texture for SDL_shape.\n");
   1.182 -			exit(-6);
   1.183 -		}
   1.184 -	}
   1.185 -	
   1.186 -	event_pending = 0;
   1.187 -	should_exit = 0;
   1.188 -	event_pending = SDL_PollEvent(&event);
   1.189 -	current_picture = 0;
   1.190 -	button_down = 0;
   1.191 -	texture_dimensions.h = 0;
   1.192 -	texture_dimensions.w = 0;
   1.193 -	texture_dimensions.x = 0;
   1.194 -	texture_dimensions.y = 0;
   1.195 -	SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   1.196 -	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.197 -	SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   1.198 -	next_time = SDL_GetTicks() + TICK_INTERVAL;
   1.199 -	while(should_exit == 0) {
   1.200 -		event_pending = SDL_PollEvent(&event);
   1.201 -		if(event_pending == 1) {
   1.202 -			if(event.type == SDL_KEYDOWN) {
   1.203 -				button_down = 1;
   1.204 -				if(event.key.keysym.sym == SDLK_ESCAPE)
   1.205 -					should_exit = 1;
   1.206 -			}
   1.207 -			if(button_down && event.type == SDL_KEYUP) {
   1.208 -				button_down = 0;
   1.209 -				current_picture += 1;
   1.210 -				if(current_picture >= num_pictures)
   1.211 -					current_picture = 0;
   1.212 -				SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   1.213 -				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.214 -				SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   1.215 -			}
   1.216 -			if(event.type == SDL_QUIT)
   1.217 -				should_exit = 1;
   1.218 -			event_pending = 0;
   1.219 -		}
   1.220 -		render(window,pictures[current_picture].texture,texture_dimensions);
   1.221 -		SDL_Delay(time_left());
   1.222 -		next_time += TICK_INTERVAL;
   1.223 -	}
   1.224 -	
   1.225 -	//Free the textures.
   1.226 -	for(i=0;i<num_pictures;i++)
   1.227 -		SDL_DestroyTexture(pictures[i].texture);
   1.228 -	//Destroy the window.
   1.229 -	SDL_DestroyWindow(window);
   1.230 -	//Free the original surfaces backing the textures.
   1.231 -	for(i=0;i<num_pictures;i++)
   1.232 -		SDL_FreeSurface(pictures[i].surface);
   1.233 -	free(pictures);
   1.234 -	//Call SDL_VideoQuit() before quitting.
   1.235 -	SDL_VideoQuit();
   1.236 +        format = pictures[i].surface->format;
   1.237 +        if(format->Amask != 0) {
   1.238 +            pictures[i].mode.mode = ShapeModeBinarizeAlpha;
   1.239 +            pictures[i].mode.parameters.binarizationCutoff = 255;
   1.240 +        }
   1.241 +        else {
   1.242 +            pictures[i].mode.mode = ShapeModeColorKey;
   1.243 +            pictures[i].mode.parameters.colorKey = black;
   1.244 +        }
   1.245 +    }
   1.246 +    
   1.247 +    window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
   1.248 +    if(window == NULL) {
   1.249 +        for(i=0;i<num_pictures;i++)
   1.250 +            SDL_FreeSurface(pictures[i].surface);
   1.251 +        free(pictures);
   1.252 +        SDL_VideoQuit();
   1.253 +        printf("Could not create shaped window for SDL_Shape.\n");
   1.254 +        exit(-4);
   1.255 +    }
   1.256 +    renderer = SDL_CreateRenderer(window,-1,0);
   1.257 +    if (!renderer) {
   1.258 +        SDL_DestroyWindow(window);
   1.259 +        for(i=0;i<num_pictures;i++)
   1.260 +            SDL_FreeSurface(pictures[i].surface);
   1.261 +        free(pictures);
   1.262 +        SDL_VideoQuit();
   1.263 +        printf("Could not create rendering context for SDL_Shape window.\n");
   1.264 +        exit(-5);
   1.265 +    }
   1.266 +    
   1.267 +    for(i=0;i<num_pictures;i++)
   1.268 +        pictures[i].texture = NULL;
   1.269 +    for(i=0;i<num_pictures;i++) {
   1.270 +        pictures[i].texture = SDL_CreateTextureFromSurface(renderer,0,pictures[i].surface);
   1.271 +        if(pictures[i].texture == NULL) {
   1.272 +            j = 0;
   1.273 +            for(j=0;j<num_pictures;i++)
   1.274 +                if(pictures[i].texture != NULL)
   1.275 +                    SDL_DestroyTexture(pictures[i].texture);
   1.276 +            for(i=0;i<num_pictures;i++)
   1.277 +                SDL_FreeSurface(pictures[i].surface);
   1.278 +            free(pictures);
   1.279 +            SDL_DestroyRenderer(renderer);
   1.280 +            SDL_DestroyWindow(window);
   1.281 +            SDL_VideoQuit();
   1.282 +            printf("Could not create texture for SDL_shape.\n");
   1.283 +            exit(-6);
   1.284 +        }
   1.285 +    }
   1.286 +    
   1.287 +    event_pending = 0;
   1.288 +    should_exit = 0;
   1.289 +    event_pending = SDL_PollEvent(&event);
   1.290 +    current_picture = 0;
   1.291 +    button_down = 0;
   1.292 +    texture_dimensions.h = 0;
   1.293 +    texture_dimensions.w = 0;
   1.294 +    texture_dimensions.x = 0;
   1.295 +    texture_dimensions.y = 0;
   1.296 +    SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   1.297 +    SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.298 +    SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   1.299 +    next_time = SDL_GetTicks() + TICK_INTERVAL;
   1.300 +    while(should_exit == 0) {
   1.301 +        event_pending = SDL_PollEvent(&event);
   1.302 +        if(event_pending == 1) {
   1.303 +            if(event.type == SDL_KEYDOWN) {
   1.304 +                button_down = 1;
   1.305 +                if(event.key.keysym.sym == SDLK_ESCAPE)
   1.306 +                    should_exit = 1;
   1.307 +            }
   1.308 +            if(button_down && event.type == SDL_KEYUP) {
   1.309 +                button_down = 0;
   1.310 +                current_picture += 1;
   1.311 +                if(current_picture >= num_pictures)
   1.312 +                    current_picture = 0;
   1.313 +                SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   1.314 +                SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.315 +                SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   1.316 +            }
   1.317 +            if(event.type == SDL_QUIT)
   1.318 +                should_exit = 1;
   1.319 +            event_pending = 0;
   1.320 +        }
   1.321 +        render(renderer,pictures[current_picture].texture,texture_dimensions);
   1.322 +        SDL_Delay(time_left());
   1.323 +        next_time += TICK_INTERVAL;
   1.324 +    }
   1.325 +    
   1.326 +    //Free the textures.
   1.327 +    for(i=0;i<num_pictures;i++)
   1.328 +        SDL_DestroyTexture(pictures[i].texture);
   1.329 +    SDL_DestroyRenderer(renderer);
   1.330 +    //Destroy the window.
   1.331 +    SDL_DestroyWindow(window);
   1.332 +    //Free the original surfaces backing the textures.
   1.333 +    for(i=0;i<num_pictures;i++)
   1.334 +        SDL_FreeSurface(pictures[i].surface);
   1.335 +    free(pictures);
   1.336 +    //Call SDL_VideoQuit() before quitting.
   1.337 +    SDL_VideoQuit();
   1.338  
   1.339 -	return 0;
   1.340 +    return 0;
   1.341  }
   1.342 +
   1.343 +/* vi: set ts=4 sw=4 expandtab: */