test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 23:36:39 -0400
changeset 4803 a4c1df880690
parent 4802 f14a8c05f5bb
child 4804 b33752266d8f
permissions -rw-r--r--
Added code to make testshape switch shapes on keystrokes and exit on an ESC keystroke.
     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 #include <SDL_keysym.h>
     9 
    10 #define SHAPED_WINDOW_X 150
    11 #define SHAPED_WINDOW_Y 150
    12 #define SHAPED_WINDOW_DIMENSION 640
    13 
    14 int main(int argc,char** argv) {
    15         if(argc < 2) {
    16         	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    17         	exit(-1);
    18         }
    19 	
    20 	if(SDL_VideoInit(NULL,0) == -1) {
    21 		printf("Could not initialize SDL video.\n");
    22 		exit(-2);
    23 	}
    24 	
    25 	Uint8 num_pictures = argc - 1;
    26 	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
    27 	int i = 0;
    28 	for(i=0;i<num_pictures;i++)
    29 		pictures[i] = NULL;
    30 	for(i=0;i<num_pictures;i++) {
    31 		pictures[i] = SDL_LoadBMP(argv[i+1]);
    32 		if(pictures[i] == NULL) {
    33 			int j = 0;
    34 			for(j=0;j<num_pictures;j++)
    35 				if(pictures[j] != NULL)
    36 					SDL_FreeSurface(pictures[j]);
    37 			free(pictures);
    38 			SDL_VideoQuit();
    39 			printf("Could not load surface from named bitmap file.\n");
    40 			exit(-3);
    41 		}
    42 	}
    43 	
    44 	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);
    45 	if(window == NULL) {
    46 		for(i=0;i<num_pictures;i++)
    47 			SDL_FreeSurface(pictures[i]);
    48 		free(pictures);
    49 		SDL_VideoQuit();
    50 		printf("Could not create shaped window for SDL_Shape.\n");
    51 		exit(-4);
    52 	}
    53 	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    54 		SDL_DestroyWindow(window);
    55 		for(i=0;i<num_pictures;i++)
    56 			SDL_FreeSurface(pictures[i]);
    57 		free(pictures);
    58 		SDL_VideoQuit();
    59 		printf("Could not create rendering context for SDL_Shape window.\n");
    60 		exit(-5);
    61 	}
    62 	
    63 	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
    64 	for(i=0;i<num_pictures;i++)
    65 		textures[i] = NULL;
    66 	for(i=0;i<num_pictures;i++) {
    67 		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
    68 		if(textures[i] == NULL) {
    69 			int j = 0;
    70 			for(j=0;j<num_pictures;i++)
    71 				if(textures[i] != NULL)
    72 					SDL_DestroyTexture(textures[i]);
    73 			free(textures);
    74 			for(i=0;i<num_pictures;i++)
    75 				SDL_FreeSurface(pictures[i]);
    76 			free(pictures);
    77 			SDL_DestroyRenderer(window);
    78 			SDL_DestroyWindow(window);
    79 			SDL_VideoQuit();
    80 			printf("Could not create texture for SDL_shape.\n");
    81 			exit(-6);
    82 		}
    83 	}
    84 	
    85 	SDL_Event event;
    86 	int event_pending = 0,should_exit = 0;
    87 	event_pending = SDL_PollEvent(&event);
    88 	unsigned int current_picture = 0;
    89 	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
    90 	int button_down = 0;
    91 	Uint32 format = 0,access = 0;
    92 	SDL_Rect texture_dimensions = {0,0,0,0};
    93 	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
    94 	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
    95 	SDL_SetWindowShape(window,pictures[current_picture],&mode);
    96 	while(should_exit == 0) {
    97 		event_pending = SDL_PollEvent(&event);
    98 		if(event_pending == 1) {
    99 			if(event.type == SDL_KEYDOWN) {
   100 				button_down = 1;
   101 				if(event.key.keysym.sym == SDLK_ESCAPE)
   102 					should_exit = 1;
   103 			}
   104 			if(button_down && event.type == SDL_KEYUP) {
   105 				button_down = 0;
   106 				current_picture += 1;
   107 				if(current_picture >= num_pictures)
   108 					current_picture = 0;
   109 				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   110 				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   111 				SDL_SetWindowShape(window,pictures[current_picture],&mode);
   112 			}
   113 			if(event.type == SDL_QUIT)
   114 				should_exit = 1;
   115 			event_pending = 0;
   116 		}
   117 		
   118 		SDL_SelectRenderer(window);
   119 	
   120 		//Clear render-target to blue.
   121 		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
   122 		SDL_RenderClear();
   123 		
   124 		//Render the texture.
   125 		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
   126 		
   127 		SDL_RenderPresent();
   128 	}
   129 	
   130 	//Free the textures.
   131 	for(i=0;i<num_pictures;i++)
   132 		SDL_DestroyTexture(textures[i]);
   133 	free(textures);
   134 	//Destroy the window.
   135 	SDL_DestroyWindow(window);
   136 	//Free the original surfaces backing the textures.
   137 	for(i=0;i<num_pictures;i++)
   138 		SDL_FreeSurface(pictures[i]);
   139 	free(pictures);
   140 	//Call SDL_VideoQuit() before quitting.
   141 	SDL_VideoQuit();
   142 }