More work on color-key mode.
authorEli Gottlieb <eligottlieb@gmail.com>
Fri, 23 Jul 2010 01:48:42 -0400
changeset 4808 2ae79ed78a5a
parent 4807 c9eb95f29770
child 4809 329708ffe2a7
More work on color-key mode.
src/video/SDL_shape.c
test/testshape.c
--- a/src/video/SDL_shape.c	Thu Jul 22 23:11:01 2010 -0400
+++ b/src/video/SDL_shape.c	Fri Jul 23 01:48:42 2010 -0400
@@ -96,7 +96,7 @@
 					break;
 				case(ShapeModeColorKey):
 					key = mode.parameters.colorKey;
-					bitmap[bitmap_pixel / ppb] |= (key.r == r && key.g == g && key.b == b ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
+					bitmap[bitmap_pixel / ppb] |= ((key.r == r && key.g == g && key.b == b) ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
 					break;
 			}
 		}
--- a/test/testshape.c	Thu Jul 22 23:11:01 2010 -0400
+++ b/test/testshape.c	Fri Jul 23 01:48:42 2010 -0400
@@ -14,6 +14,12 @@
 
 #define TICK_INTERVAL 18
 
+typedef struct LoadedPicture {
+	SDL_Surface *surface;
+	SDL_Texture *texture;
+	SDL_WindowShapeMode mode;
+} LoadedPicture;
+
 void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
 	SDL_SelectRenderer(window);
 	
@@ -49,28 +55,39 @@
 	}
 	
 	Uint8 num_pictures = argc - 1;
-	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
+	LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures);
 	int i = 0;
 	for(i=0;i<num_pictures;i++)
-		pictures[i] = NULL;
+		pictures[i].surface = NULL;
 	for(i=0;i<num_pictures;i++) {
-		pictures[i] = SDL_LoadBMP(argv[i+1]);
-		if(pictures[i] == NULL) {
+		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
+		if(pictures[i].surface == NULL) {
 			int j = 0;
 			for(j=0;j<num_pictures;j++)
-				if(pictures[j] != NULL)
-					SDL_FreeSurface(pictures[j]);
+				if(pictures[j].surface != NULL)
+					SDL_FreeSurface(pictures[j].surface);
 			free(pictures);
 			SDL_VideoQuit();
 			printf("Could not load surface from named bitmap file.\n");
 			exit(-3);
 		}
+		SDL_PixelFormat* format = pictures[i].surface->format;
+		Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
+		if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
+			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
+			pictures[i].mode.parameters.binarizationCutoff = 1;
+		}
+		else {
+			pictures[i].mode.mode = ShapeModeColorKey;
+			SDL_Color black = {0,0,0,0xff};
+			pictures[i].mode.parameters.colorKey = black;
+		}
 	}
 	
 	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);
 	if(window == NULL) {
 		for(i=0;i<num_pictures;i++)
-			SDL_FreeSurface(pictures[i]);
+			SDL_FreeSurface(pictures[i].surface);
 		free(pictures);
 		SDL_VideoQuit();
 		printf("Could not create shaped window for SDL_Shape.\n");
@@ -79,26 +96,24 @@
 	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
 		SDL_DestroyWindow(window);
 		for(i=0;i<num_pictures;i++)
-			SDL_FreeSurface(pictures[i]);
+			SDL_FreeSurface(pictures[i].surface);
 		free(pictures);
 		SDL_VideoQuit();
 		printf("Could not create rendering context for SDL_Shape window.\n");
 		exit(-5);
 	}
 	
-	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
 	for(i=0;i<num_pictures;i++)
-		textures[i] = NULL;
+		pictures[i].texture = NULL;
 	for(i=0;i<num_pictures;i++) {
-		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
-		if(textures[i] == NULL) {
+		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
+		if(pictures[i].texture == NULL) {
 			int j = 0;
 			for(j=0;j<num_pictures;i++)
-				if(textures[i] != NULL)
-					SDL_DestroyTexture(textures[i]);
-			free(textures);
+				if(pictures[i].texture != NULL)
+					SDL_DestroyTexture(pictures[i].texture);
 			for(i=0;i<num_pictures;i++)
-				SDL_FreeSurface(pictures[i]);
+				SDL_FreeSurface(pictures[i].surface);
 			free(pictures);
 			SDL_DestroyRenderer(window);
 			SDL_DestroyWindow(window);
@@ -116,9 +131,9 @@
 	int button_down = 0;
 	Uint32 format = 0,access = 0;
 	SDL_Rect texture_dimensions = {0,0,0,0};
-	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
+	SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
 	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
-	SDL_SetWindowShape(window,pictures[current_picture],&mode);
+	SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
 	next_time = SDL_GetTicks() + TICK_INTERVAL;
 	while(should_exit == 0) {
 		event_pending = SDL_PollEvent(&event);
@@ -133,28 +148,27 @@
 				current_picture += 1;
 				if(current_picture >= num_pictures)
 					current_picture = 0;
-				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
+				SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
 				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
-				SDL_SetWindowShape(window,pictures[current_picture],&mode);
+				SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
 			}
 			if(event.type == SDL_QUIT)
 				should_exit = 1;
 			event_pending = 0;
 		}
-		render(window,textures[current_picture],texture_dimensions);
+		render(window,pictures[current_picture].texture,texture_dimensions);
 		SDL_Delay(time_left());
 		next_time += TICK_INTERVAL;
 	}
 	
 	//Free the textures.
 	for(i=0;i<num_pictures;i++)
-		SDL_DestroyTexture(textures[i]);
-	free(textures);
+		SDL_DestroyTexture(pictures[i].texture);
 	//Destroy the window.
 	SDL_DestroyWindow(window);
 	//Free the original surfaces backing the textures.
 	for(i=0;i<num_pictures;i++)
-		SDL_FreeSurface(pictures[i]);
+		SDL_FreeSurface(pictures[i].surface);
 	free(pictures);
 	//Call SDL_VideoQuit() before quitting.
 	SDL_VideoQuit();