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