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