test/testshape.c
changeset 4808 2ae79ed78a5a
parent 4805 de3b3455f8ec
child 4809 329708ffe2a7
     1.1 --- a/test/testshape.c	Thu Jul 22 23:11:01 2010 -0400
     1.2 +++ b/test/testshape.c	Fri Jul 23 01:48:42 2010 -0400
     1.3 @@ -14,6 +14,12 @@
     1.4  
     1.5  #define TICK_INTERVAL 18
     1.6  
     1.7 +typedef struct LoadedPicture {
     1.8 +	SDL_Surface *surface;
     1.9 +	SDL_Texture *texture;
    1.10 +	SDL_WindowShapeMode mode;
    1.11 +} LoadedPicture;
    1.12 +
    1.13  void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
    1.14  	SDL_SelectRenderer(window);
    1.15  	
    1.16 @@ -49,28 +55,39 @@
    1.17  	}
    1.18  	
    1.19  	Uint8 num_pictures = argc - 1;
    1.20 -	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
    1.21 +	LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures);
    1.22  	int i = 0;
    1.23  	for(i=0;i<num_pictures;i++)
    1.24 -		pictures[i] = NULL;
    1.25 +		pictures[i].surface = NULL;
    1.26  	for(i=0;i<num_pictures;i++) {
    1.27 -		pictures[i] = SDL_LoadBMP(argv[i+1]);
    1.28 -		if(pictures[i] == NULL) {
    1.29 +		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    1.30 +		if(pictures[i].surface == NULL) {
    1.31  			int j = 0;
    1.32  			for(j=0;j<num_pictures;j++)
    1.33 -				if(pictures[j] != NULL)
    1.34 -					SDL_FreeSurface(pictures[j]);
    1.35 +				if(pictures[j].surface != NULL)
    1.36 +					SDL_FreeSurface(pictures[j].surface);
    1.37  			free(pictures);
    1.38  			SDL_VideoQuit();
    1.39  			printf("Could not load surface from named bitmap file.\n");
    1.40  			exit(-3);
    1.41  		}
    1.42 +		SDL_PixelFormat* format = pictures[i].surface->format;
    1.43 +		Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
    1.44 +		if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
    1.45 +			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    1.46 +			pictures[i].mode.parameters.binarizationCutoff = 1;
    1.47 +		}
    1.48 +		else {
    1.49 +			pictures[i].mode.mode = ShapeModeColorKey;
    1.50 +			SDL_Color black = {0,0,0,0xff};
    1.51 +			pictures[i].mode.parameters.colorKey = black;
    1.52 +		}
    1.53  	}
    1.54  	
    1.55  	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);
    1.56  	if(window == NULL) {
    1.57  		for(i=0;i<num_pictures;i++)
    1.58 -			SDL_FreeSurface(pictures[i]);
    1.59 +			SDL_FreeSurface(pictures[i].surface);
    1.60  		free(pictures);
    1.61  		SDL_VideoQuit();
    1.62  		printf("Could not create shaped window for SDL_Shape.\n");
    1.63 @@ -79,26 +96,24 @@
    1.64  	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    1.65  		SDL_DestroyWindow(window);
    1.66  		for(i=0;i<num_pictures;i++)
    1.67 -			SDL_FreeSurface(pictures[i]);
    1.68 +			SDL_FreeSurface(pictures[i].surface);
    1.69  		free(pictures);
    1.70  		SDL_VideoQuit();
    1.71  		printf("Could not create rendering context for SDL_Shape window.\n");
    1.72  		exit(-5);
    1.73  	}
    1.74  	
    1.75 -	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
    1.76  	for(i=0;i<num_pictures;i++)
    1.77 -		textures[i] = NULL;
    1.78 +		pictures[i].texture = NULL;
    1.79  	for(i=0;i<num_pictures;i++) {
    1.80 -		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
    1.81 -		if(textures[i] == NULL) {
    1.82 +		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
    1.83 +		if(pictures[i].texture == NULL) {
    1.84  			int j = 0;
    1.85  			for(j=0;j<num_pictures;i++)
    1.86 -				if(textures[i] != NULL)
    1.87 -					SDL_DestroyTexture(textures[i]);
    1.88 -			free(textures);
    1.89 +				if(pictures[i].texture != NULL)
    1.90 +					SDL_DestroyTexture(pictures[i].texture);
    1.91  			for(i=0;i<num_pictures;i++)
    1.92 -				SDL_FreeSurface(pictures[i]);
    1.93 +				SDL_FreeSurface(pictures[i].surface);
    1.94  			free(pictures);
    1.95  			SDL_DestroyRenderer(window);
    1.96  			SDL_DestroyWindow(window);
    1.97 @@ -116,9 +131,9 @@
    1.98  	int button_down = 0;
    1.99  	Uint32 format = 0,access = 0;
   1.100  	SDL_Rect texture_dimensions = {0,0,0,0};
   1.101 -	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.102 +	SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.103  	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.104 -	SDL_SetWindowShape(window,pictures[current_picture],&mode);
   1.105 +	SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   1.106  	next_time = SDL_GetTicks() + TICK_INTERVAL;
   1.107  	while(should_exit == 0) {
   1.108  		event_pending = SDL_PollEvent(&event);
   1.109 @@ -133,28 +148,27 @@
   1.110  				current_picture += 1;
   1.111  				if(current_picture >= num_pictures)
   1.112  					current_picture = 0;
   1.113 -				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.114 +				SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.115  				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.116 -				SDL_SetWindowShape(window,pictures[current_picture],&mode);
   1.117 +				SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   1.118  			}
   1.119  			if(event.type == SDL_QUIT)
   1.120  				should_exit = 1;
   1.121  			event_pending = 0;
   1.122  		}
   1.123 -		render(window,textures[current_picture],texture_dimensions);
   1.124 +		render(window,pictures[current_picture].texture,texture_dimensions);
   1.125  		SDL_Delay(time_left());
   1.126  		next_time += TICK_INTERVAL;
   1.127  	}
   1.128  	
   1.129  	//Free the textures.
   1.130  	for(i=0;i<num_pictures;i++)
   1.131 -		SDL_DestroyTexture(textures[i]);
   1.132 -	free(textures);
   1.133 +		SDL_DestroyTexture(pictures[i].texture);
   1.134  	//Destroy the window.
   1.135  	SDL_DestroyWindow(window);
   1.136  	//Free the original surfaces backing the textures.
   1.137  	for(i=0;i<num_pictures;i++)
   1.138 -		SDL_FreeSurface(pictures[i]);
   1.139 +		SDL_FreeSurface(pictures[i].surface);
   1.140  	free(pictures);
   1.141  	//Call SDL_VideoQuit() before quitting.
   1.142  	SDL_VideoQuit();