test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 22:17:52 -0400
changeset 4800 6d4be626225f
parent 4799 a0e096916474
child 4802 f14a8c05f5bb
permissions -rw-r--r--
Same place as before, but optimizing a bit to try to isolate the spot in the program that locks things up.
     1 #include <stdlib.h>
     2 #include <math.h>
     3 #include <SDL_events.h>
     4 #include <SDL_rect.h>
     5 #include <SDL_pixels.h>
     6 #include <SDL_video.h>
     7 #include <SDL_shape.h>
     8 
     9 #define SHAPED_WINDOW_X 150
    10 #define SHAPED_WINDOW_Y 150
    11 #define SHAPED_WINDOW_DIMENSION 640
    12 
    13 int main(int argc,char** argv) {
    14         if(argc < 2) {
    15         	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    16         	exit(-1);
    17         }
    18 	
    19 	if(SDL_VideoInit(NULL,0) == -1) {
    20 		printf("Could not initialize SDL video.\n");
    21 		exit(-2);
    22 	}
    23 	
    24 	Uint8 num_pictures = argc - 1;
    25 	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
    26 	int i = 0;
    27 	for(i=0;i<num_pictures;i++)
    28 		pictures[i] = NULL;
    29 	for(i=0;i<num_pictures;i++) {
    30 		SDL_Surface *original = SDL_LoadBMP(argv[i+1]);
    31 		if(original == NULL) {
    32 			int j = 0;
    33 			for(j=0;j<num_pictures;j++)
    34 				if(pictures[j] != NULL)
    35 					SDL_FreeSurface(pictures[j]);
    36 			free(pictures);
    37 			SDL_VideoQuit();
    38 			printf("Could not load surface from named bitmap file.\n");
    39 			exit(-3);
    40 		}
    41 		//THIS CONVERSION ROUTINE IS FRAGILE!  It relies in the fact that only certain portions of the format structure must be filled in to use it.
    42 		SDL_PixelFormat format = {NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    43 		int bpp = 0;
    44 		SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_RGBA8888,&bpp,&format.Rmask,&format.Gmask,&format.Bmask,&format.Amask);
    45 		format.BitsPerPixel = bpp;
    46 		format.BytesPerPixel = format.BitsPerPixel / 8 + (format.BitsPerPixel % 8 > 0 ? 1 : 0);
    47 		pictures[i] = SDL_ConvertSurface(original,&format,0);
    48 		//We have no more need of the original now that we have our desired format.
    49 		SDL_FreeSurface(original);
    50 		if(pictures[i] == NULL) {
    51 			int j = 0;
    52 			for(j=0;j<num_pictures;j++)
    53 				if(pictures[j] != NULL)
    54 					SDL_FreeSurface(pictures[j]);
    55 			free(pictures);
    56 			SDL_VideoQuit();
    57 			printf("Could not convert bitmap surface to desired format.\n");
    58 			exit(-3);
    59 		}
    60 		
    61 		if(SDL_MUSTLOCK(pictures[i]))
    62 			SDL_LockSurface(pictures[i]);
    63 			
    64 		void* pixels = pictures[i]->pixels;
    65 		unsigned int pitch = pictures[i]->pitch;
    66 		int y =0,x = 0;
    67 		for(y=0;y<pictures[i]->h;y++)
    68 			for(x=0;x<pictures[i]->w;x++) {
    69 				Uint32* pixel = pixels + y * pitch + x * pictures[i]->format->BytesPerPixel;
    70 				Uint8 r = 0,g = 0,b = 0;
    71 				SDL_GetRGB(*pixel,pictures[i]->format,&r,&g,&b);
    72 				//if(r == g == b == 0xff)
    73 				//	*pixel = SDL_MapRGBA(pictures[i]->format,r,g,b,0);
    74 			}
    75 			
    76 		if(SDL_MUSTLOCK(pictures[i]))
    77 			SDL_UnlockSurface(pictures[i]);
    78 	}
    79 	
    80 	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);
    81 	if(window == NULL) {
    82 		for(i=0;i<num_pictures;i++)
    83 			SDL_FreeSurface(pictures[i]);
    84 		free(pictures);
    85 		SDL_VideoQuit();
    86 		printf("Could not create shaped window for SDL_Shape.\n");
    87 		exit(-4);
    88 	}
    89 	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    90 		SDL_DestroyWindow(window);
    91 		for(i=0;i<num_pictures;i++)
    92 			SDL_FreeSurface(pictures[i]);
    93 		free(pictures);
    94 		SDL_VideoQuit();
    95 		printf("Could not create rendering context for SDL_Shape window.\n");
    96 		exit(-5);
    97 	}
    98 	
    99 	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
   100 	for(i=0;i<num_pictures;i++)
   101 		textures[i] = NULL;
   102 	for(i=0;i<num_pictures;i++) {
   103 		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
   104 		if(textures[i] == NULL) {
   105 			int j = 0;
   106 			for(j=0;j<num_pictures;i++)
   107 				if(textures[i] != NULL)
   108 					SDL_DestroyTexture(textures[i]);
   109 			free(textures);
   110 			for(i=0;i<num_pictures;i++)
   111 				SDL_FreeSurface(pictures[i]);
   112 			free(pictures);
   113 			SDL_DestroyRenderer(window);
   114 			SDL_DestroyWindow(window);
   115 			SDL_VideoQuit();
   116 			printf("Could not create texture for SDL_shape.\n");
   117 			exit(-6);
   118 		}
   119 	}
   120 	
   121 	SDL_Event event;
   122 	int event_pending = 0,should_exit = 0;
   123 	event_pending = SDL_PollEvent(&event);
   124 	unsigned int current_picture = 0;
   125 	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
   126 	SDL_SetWindowShape(window,pictures[current_picture],&mode);
   127 	int mouse_down = 0;
   128 	Uint32 format,access;
   129 	SDL_Rect texture_dimensions = {0,0,0,0};
   130 	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   131 	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   132 	while(should_exit == 0) {
   133 		event_pending = SDL_PollEvent(&event);
   134 		if(event_pending == 1) {
   135 			if(event.type == SDL_MOUSEBUTTONDOWN)
   136 				mouse_down = 1;
   137 			if(mouse_down && event.type == SDL_MOUSEBUTTONUP) {
   138 				mouse_down = 0;
   139 				current_picture += 1;
   140 				if(current_picture >= num_pictures)
   141 					current_picture = 0;
   142 				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   143 				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   144 				SDL_SetWindowShape(window,pictures[current_picture],&mode);
   145 			}
   146 			if(event.type == SDL_QUIT)
   147 				should_exit = 1;
   148 			event_pending = 0;
   149 		}
   150 		
   151 		SDL_SelectRenderer(window);
   152 	
   153 		//Clear render-target to blue.
   154 		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
   155 		SDL_RenderClear();
   156 		
   157 		//Render the texture.
   158 		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
   159 		
   160 		SDL_RenderPresent();
   161 	}
   162 	
   163 	//Free the textures.
   164 	for(i=0;i<num_pictures;i++)
   165 		SDL_DestroyTexture(textures[i]);
   166 	free(textures);
   167 	//Destroy the window.
   168 	SDL_DestroyWindow(window);
   169 	//Free the original surfaces backing the textures.
   170 	for(i=0;i<num_pictures;i++)
   171 		SDL_FreeSurface(pictures[i]);
   172 	free(pictures);
   173 	//Call SDL_VideoQuit() before quitting.
   174 	SDL_VideoQuit();
   175 }