More work on color-key mode.
authorEli Gottlieb <eligottlieb@gmail.com>
Fri, 23 Jul 2010 01:48:42 -0400
changeset 48082ae79ed78a5a
parent 4807 c9eb95f29770
child 4809 329708ffe2a7
More work on color-key mode.
src/video/SDL_shape.c
test/testshape.c
     1.1 --- a/src/video/SDL_shape.c	Thu Jul 22 23:11:01 2010 -0400
     1.2 +++ b/src/video/SDL_shape.c	Fri Jul 23 01:48:42 2010 -0400
     1.3 @@ -96,7 +96,7 @@
     1.4  					break;
     1.5  				case(ShapeModeColorKey):
     1.6  					key = mode.parameters.colorKey;
     1.7 -					bitmap[bitmap_pixel / ppb] |= (key.r == r && key.g == g && key.b == b ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
     1.8 +					bitmap[bitmap_pixel / ppb] |= ((key.r == r && key.g == g && key.b == b) ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
     1.9  					break;
    1.10  			}
    1.11  		}
     2.1 --- a/test/testshape.c	Thu Jul 22 23:11:01 2010 -0400
     2.2 +++ b/test/testshape.c	Fri Jul 23 01:48:42 2010 -0400
     2.3 @@ -14,6 +14,12 @@
     2.4  
     2.5  #define TICK_INTERVAL 18
     2.6  
     2.7 +typedef struct LoadedPicture {
     2.8 +	SDL_Surface *surface;
     2.9 +	SDL_Texture *texture;
    2.10 +	SDL_WindowShapeMode mode;
    2.11 +} LoadedPicture;
    2.12 +
    2.13  void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
    2.14  	SDL_SelectRenderer(window);
    2.15  	
    2.16 @@ -49,28 +55,39 @@
    2.17  	}
    2.18  	
    2.19  	Uint8 num_pictures = argc - 1;
    2.20 -	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
    2.21 +	LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures);
    2.22  	int i = 0;
    2.23  	for(i=0;i<num_pictures;i++)
    2.24 -		pictures[i] = NULL;
    2.25 +		pictures[i].surface = NULL;
    2.26  	for(i=0;i<num_pictures;i++) {
    2.27 -		pictures[i] = SDL_LoadBMP(argv[i+1]);
    2.28 -		if(pictures[i] == NULL) {
    2.29 +		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    2.30 +		if(pictures[i].surface == NULL) {
    2.31  			int j = 0;
    2.32  			for(j=0;j<num_pictures;j++)
    2.33 -				if(pictures[j] != NULL)
    2.34 -					SDL_FreeSurface(pictures[j]);
    2.35 +				if(pictures[j].surface != NULL)
    2.36 +					SDL_FreeSurface(pictures[j].surface);
    2.37  			free(pictures);
    2.38  			SDL_VideoQuit();
    2.39  			printf("Could not load surface from named bitmap file.\n");
    2.40  			exit(-3);
    2.41  		}
    2.42 +		SDL_PixelFormat* format = pictures[i].surface->format;
    2.43 +		Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
    2.44 +		if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
    2.45 +			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    2.46 +			pictures[i].mode.parameters.binarizationCutoff = 1;
    2.47 +		}
    2.48 +		else {
    2.49 +			pictures[i].mode.mode = ShapeModeColorKey;
    2.50 +			SDL_Color black = {0,0,0,0xff};
    2.51 +			pictures[i].mode.parameters.colorKey = black;
    2.52 +		}
    2.53  	}
    2.54  	
    2.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);
    2.56  	if(window == NULL) {
    2.57  		for(i=0;i<num_pictures;i++)
    2.58 -			SDL_FreeSurface(pictures[i]);
    2.59 +			SDL_FreeSurface(pictures[i].surface);
    2.60  		free(pictures);
    2.61  		SDL_VideoQuit();
    2.62  		printf("Could not create shaped window for SDL_Shape.\n");
    2.63 @@ -79,26 +96,24 @@
    2.64  	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    2.65  		SDL_DestroyWindow(window);
    2.66  		for(i=0;i<num_pictures;i++)
    2.67 -			SDL_FreeSurface(pictures[i]);
    2.68 +			SDL_FreeSurface(pictures[i].surface);
    2.69  		free(pictures);
    2.70  		SDL_VideoQuit();
    2.71  		printf("Could not create rendering context for SDL_Shape window.\n");
    2.72  		exit(-5);
    2.73  	}
    2.74  	
    2.75 -	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
    2.76  	for(i=0;i<num_pictures;i++)
    2.77 -		textures[i] = NULL;
    2.78 +		pictures[i].texture = NULL;
    2.79  	for(i=0;i<num_pictures;i++) {
    2.80 -		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
    2.81 -		if(textures[i] == NULL) {
    2.82 +		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
    2.83 +		if(pictures[i].texture == NULL) {
    2.84  			int j = 0;
    2.85  			for(j=0;j<num_pictures;i++)
    2.86 -				if(textures[i] != NULL)
    2.87 -					SDL_DestroyTexture(textures[i]);
    2.88 -			free(textures);
    2.89 +				if(pictures[i].texture != NULL)
    2.90 +					SDL_DestroyTexture(pictures[i].texture);
    2.91  			for(i=0;i<num_pictures;i++)
    2.92 -				SDL_FreeSurface(pictures[i]);
    2.93 +				SDL_FreeSurface(pictures[i].surface);
    2.94  			free(pictures);
    2.95  			SDL_DestroyRenderer(window);
    2.96  			SDL_DestroyWindow(window);
    2.97 @@ -116,9 +131,9 @@
    2.98  	int button_down = 0;
    2.99  	Uint32 format = 0,access = 0;
   2.100  	SDL_Rect texture_dimensions = {0,0,0,0};
   2.101 -	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   2.102 +	SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   2.103  	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   2.104 -	SDL_SetWindowShape(window,pictures[current_picture],&mode);
   2.105 +	SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   2.106  	next_time = SDL_GetTicks() + TICK_INTERVAL;
   2.107  	while(should_exit == 0) {
   2.108  		event_pending = SDL_PollEvent(&event);
   2.109 @@ -133,28 +148,27 @@
   2.110  				current_picture += 1;
   2.111  				if(current_picture >= num_pictures)
   2.112  					current_picture = 0;
   2.113 -				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   2.114 +				SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   2.115  				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   2.116 -				SDL_SetWindowShape(window,pictures[current_picture],&mode);
   2.117 +				SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   2.118  			}
   2.119  			if(event.type == SDL_QUIT)
   2.120  				should_exit = 1;
   2.121  			event_pending = 0;
   2.122  		}
   2.123 -		render(window,textures[current_picture],texture_dimensions);
   2.124 +		render(window,pictures[current_picture].texture,texture_dimensions);
   2.125  		SDL_Delay(time_left());
   2.126  		next_time += TICK_INTERVAL;
   2.127  	}
   2.128  	
   2.129  	//Free the textures.
   2.130  	for(i=0;i<num_pictures;i++)
   2.131 -		SDL_DestroyTexture(textures[i]);
   2.132 -	free(textures);
   2.133 +		SDL_DestroyTexture(pictures[i].texture);
   2.134  	//Destroy the window.
   2.135  	SDL_DestroyWindow(window);
   2.136  	//Free the original surfaces backing the textures.
   2.137  	for(i=0;i<num_pictures;i++)
   2.138 -		SDL_FreeSurface(pictures[i]);
   2.139 +		SDL_FreeSurface(pictures[i].surface);
   2.140  	free(pictures);
   2.141  	//Call SDL_VideoQuit() before quitting.
   2.142  	SDL_VideoQuit();