test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Fri, 23 Jul 2010 01:48:42 -0400
changeset 4808 2ae79ed78a5a
parent 4805 de3b3455f8ec
child 4809 329708ffe2a7
permissions -rw-r--r--
More work on color-key mode.
     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 typedef struct LoadedPicture {
    18 	SDL_Surface *surface;
    19 	SDL_Texture *texture;
    20 	SDL_WindowShapeMode mode;
    21 } LoadedPicture;
    22 
    23 void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
    24 	SDL_SelectRenderer(window);
    25 	
    26 	//Clear render-target to blue.
    27 	SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
    28 	SDL_RenderClear();
    29 	
    30 	//Render the texture.
    31 	SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions);
    32 	
    33 	SDL_RenderPresent();
    34 }
    35 
    36 static Uint32 next_time;
    37 
    38 Uint32 time_left() {
    39 	Uint32 now = SDL_GetTicks();
    40 	if(next_time <= now)
    41 		return 0;
    42 	else
    43 		return next_time - now;
    44 }
    45 
    46 int main(int argc,char** argv) {
    47         if(argc < 2) {
    48         	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    49         	exit(-1);
    50         }
    51 	
    52 	if(SDL_VideoInit(NULL,0) == -1) {
    53 		printf("Could not initialize SDL video.\n");
    54 		exit(-2);
    55 	}
    56 	
    57 	Uint8 num_pictures = argc - 1;
    58 	LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures);
    59 	int i = 0;
    60 	for(i=0;i<num_pictures;i++)
    61 		pictures[i].surface = NULL;
    62 	for(i=0;i<num_pictures;i++) {
    63 		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    64 		if(pictures[i].surface == NULL) {
    65 			int j = 0;
    66 			for(j=0;j<num_pictures;j++)
    67 				if(pictures[j].surface != NULL)
    68 					SDL_FreeSurface(pictures[j].surface);
    69 			free(pictures);
    70 			SDL_VideoQuit();
    71 			printf("Could not load surface from named bitmap file.\n");
    72 			exit(-3);
    73 		}
    74 		SDL_PixelFormat* format = pictures[i].surface->format;
    75 		Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
    76 		if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
    77 			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    78 			pictures[i].mode.parameters.binarizationCutoff = 1;
    79 		}
    80 		else {
    81 			pictures[i].mode.mode = ShapeModeColorKey;
    82 			SDL_Color black = {0,0,0,0xff};
    83 			pictures[i].mode.parameters.colorKey = black;
    84 		}
    85 	}
    86 	
    87 	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);
    88 	if(window == NULL) {
    89 		for(i=0;i<num_pictures;i++)
    90 			SDL_FreeSurface(pictures[i].surface);
    91 		free(pictures);
    92 		SDL_VideoQuit();
    93 		printf("Could not create shaped window for SDL_Shape.\n");
    94 		exit(-4);
    95 	}
    96 	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    97 		SDL_DestroyWindow(window);
    98 		for(i=0;i<num_pictures;i++)
    99 			SDL_FreeSurface(pictures[i].surface);
   100 		free(pictures);
   101 		SDL_VideoQuit();
   102 		printf("Could not create rendering context for SDL_Shape window.\n");
   103 		exit(-5);
   104 	}
   105 	
   106 	for(i=0;i<num_pictures;i++)
   107 		pictures[i].texture = NULL;
   108 	for(i=0;i<num_pictures;i++) {
   109 		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
   110 		if(pictures[i].texture == NULL) {
   111 			int j = 0;
   112 			for(j=0;j<num_pictures;i++)
   113 				if(pictures[i].texture != NULL)
   114 					SDL_DestroyTexture(pictures[i].texture);
   115 			for(i=0;i<num_pictures;i++)
   116 				SDL_FreeSurface(pictures[i].surface);
   117 			free(pictures);
   118 			SDL_DestroyRenderer(window);
   119 			SDL_DestroyWindow(window);
   120 			SDL_VideoQuit();
   121 			printf("Could not create texture for SDL_shape.\n");
   122 			exit(-6);
   123 		}
   124 	}
   125 	
   126 	SDL_Event event;
   127 	int event_pending = 0,should_exit = 0;
   128 	event_pending = SDL_PollEvent(&event);
   129 	unsigned int current_picture = 0;
   130 	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
   131 	int button_down = 0;
   132 	Uint32 format = 0,access = 0;
   133 	SDL_Rect texture_dimensions = {0,0,0,0};
   134 	SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   135 	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   136 	SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   137 	next_time = SDL_GetTicks() + TICK_INTERVAL;
   138 	while(should_exit == 0) {
   139 		event_pending = SDL_PollEvent(&event);
   140 		if(event_pending == 1) {
   141 			if(event.type == SDL_KEYDOWN) {
   142 				button_down = 1;
   143 				if(event.key.keysym.sym == SDLK_ESCAPE)
   144 					should_exit = 1;
   145 			}
   146 			if(button_down && event.type == SDL_KEYUP) {
   147 				button_down = 0;
   148 				current_picture += 1;
   149 				if(current_picture >= num_pictures)
   150 					current_picture = 0;
   151 				SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   152 				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   153 				SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   154 			}
   155 			if(event.type == SDL_QUIT)
   156 				should_exit = 1;
   157 			event_pending = 0;
   158 		}
   159 		render(window,pictures[current_picture].texture,texture_dimensions);
   160 		SDL_Delay(time_left());
   161 		next_time += TICK_INTERVAL;
   162 	}
   163 	
   164 	//Free the textures.
   165 	for(i=0;i<num_pictures;i++)
   166 		SDL_DestroyTexture(pictures[i].texture);
   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].surface);
   172 	free(pictures);
   173 	//Call SDL_VideoQuit() before quitting.
   174 	SDL_VideoQuit();
   175 }