test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 23:05:40 -0400
changeset 4802 f14a8c05f5bb
parent 4800 6d4be626225f
child 4803 a4c1df880690
permissions -rw-r--r--
Minor bugfixes. testshape now draws a shaped window with bizarre, pixellated gashes of transparency across it, and in doing so seems to hog a system resource and slow the rest of the video system down.
     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 		pictures[i] = SDL_LoadBMP(argv[i+1]);
    31 		if(pictures[i] == 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 	}
    42 	
    43 	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);
    44 	if(window == NULL) {
    45 		for(i=0;i<num_pictures;i++)
    46 			SDL_FreeSurface(pictures[i]);
    47 		free(pictures);
    48 		SDL_VideoQuit();
    49 		printf("Could not create shaped window for SDL_Shape.\n");
    50 		exit(-4);
    51 	}
    52 	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    53 		SDL_DestroyWindow(window);
    54 		for(i=0;i<num_pictures;i++)
    55 			SDL_FreeSurface(pictures[i]);
    56 		free(pictures);
    57 		SDL_VideoQuit();
    58 		printf("Could not create rendering context for SDL_Shape window.\n");
    59 		exit(-5);
    60 	}
    61 	
    62 	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
    63 	for(i=0;i<num_pictures;i++)
    64 		textures[i] = NULL;
    65 	for(i=0;i<num_pictures;i++) {
    66 		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
    67 		if(textures[i] == NULL) {
    68 			int j = 0;
    69 			for(j=0;j<num_pictures;i++)
    70 				if(textures[i] != NULL)
    71 					SDL_DestroyTexture(textures[i]);
    72 			free(textures);
    73 			for(i=0;i<num_pictures;i++)
    74 				SDL_FreeSurface(pictures[i]);
    75 			free(pictures);
    76 			SDL_DestroyRenderer(window);
    77 			SDL_DestroyWindow(window);
    78 			SDL_VideoQuit();
    79 			printf("Could not create texture for SDL_shape.\n");
    80 			exit(-6);
    81 		}
    82 	}
    83 	
    84 	SDL_Event event;
    85 	int event_pending = 0,should_exit = 0;
    86 	event_pending = SDL_PollEvent(&event);
    87 	unsigned int current_picture = 0;
    88 	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
    89 	int mouse_down = 0;
    90 	Uint32 format = 0,access = 0;
    91 	SDL_Rect texture_dimensions = {0,0,0,0};
    92 	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
    93 	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
    94 	SDL_SetWindowShape(window,pictures[current_picture],&mode);
    95 	while(should_exit == 0) {
    96 		event_pending = SDL_PollEvent(&event);
    97 		if(event_pending == 1) {
    98 			if(event.type == SDL_MOUSEBUTTONDOWN)
    99 				mouse_down = 1;
   100 			if(mouse_down && event.type == SDL_MOUSEBUTTONUP) {
   101 				mouse_down = 0;
   102 				current_picture += 1;
   103 				if(current_picture >= num_pictures)
   104 					current_picture = 0;
   105 				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   106 				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   107 				SDL_SetWindowShape(window,pictures[current_picture],&mode);
   108 			}
   109 			if(event.type == SDL_QUIT)
   110 				should_exit = 1;
   111 			event_pending = 0;
   112 		}
   113 		
   114 		SDL_SelectRenderer(window);
   115 	
   116 		//Clear render-target to blue.
   117 		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
   118 		SDL_RenderClear();
   119 		
   120 		//Render the texture.
   121 		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
   122 		
   123 		SDL_RenderPresent();
   124 	}
   125 	
   126 	//Free the textures.
   127 	for(i=0;i<num_pictures;i++)
   128 		SDL_DestroyTexture(textures[i]);
   129 	free(textures);
   130 	//Destroy the window.
   131 	SDL_DestroyWindow(window);
   132 	//Free the original surfaces backing the textures.
   133 	for(i=0;i<num_pictures;i++)
   134 		SDL_FreeSurface(pictures[i]);
   135 	free(pictures);
   136 	//Call SDL_VideoQuit() before quitting.
   137 	SDL_VideoQuit();
   138 }