test/testshape.c
author egottlieb
Sat, 14 Aug 2010 16:14:36 -0400
changeset 4849 0b918c186938
parent 4837 2e446923c9fb
child 4857 579dabb141ea
permissions -rw-r--r--
Finally got the Win32 API code for shaping to work! Just need to fix SDL_CalculateShapeTree() now!
     1 #include <stdlib.h>
     2 #include <math.h>
     3 #include <stdio.h>
     4 #include "SDL.h"
     5 #include "SDL_shape.h"
     6 
     7 #define SHAPED_WINDOW_X 150
     8 #define SHAPED_WINDOW_Y 150
     9 #define SHAPED_WINDOW_DIMENSION 640
    10 
    11 #define TICK_INTERVAL 1000/10
    12 
    13 typedef struct LoadedPicture {
    14 	SDL_Surface *surface;
    15 	SDL_Texture *texture;
    16 	SDL_WindowShapeMode mode;
    17 } LoadedPicture;
    18 
    19 void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
    20 	SDL_SelectRenderer(window);
    21 	
    22 	//Clear render-target to blue.
    23 	SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
    24 	SDL_RenderClear();
    25 	
    26 	//Render the texture.
    27 	SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions);
    28 	
    29 	SDL_RenderPresent();
    30 }
    31 
    32 static Uint32 next_time;
    33 
    34 Uint32 time_left() {
    35     Uint32 now = SDL_GetTicks();
    36     if(next_time <= now)
    37         return 0;
    38 	else
    39         return next_time - now;
    40 }
    41 
    42 int main(int argc,char** argv) {
    43 	Uint8 num_pictures;
    44 	LoadedPicture* pictures;
    45 	int i, j;
    46 	SDL_PixelFormat* format = NULL;
    47 	SDL_Window *window;
    48 	SDL_Color black = {0,0,0,0xff};
    49 	SDL_Event event;
    50 	int event_pending = 0;
    51 	int should_exit = 0;
    52 	unsigned int current_picture;
    53 	int button_down;
    54 	Uint32 pixelFormat = 0;
    55 	int access = 0;
    56 	SDL_Rect texture_dimensions;;
    57 
    58 	if(argc < 2) {
    59         	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    60         	exit(-1);
    61         }
    62 	
    63 	if(SDL_VideoInit(NULL,0) == -1) {
    64 		printf("Could not initialize SDL video.\n");
    65 		exit(-2);
    66 	}
    67 	
    68 	num_pictures = argc - 1;
    69 	pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
    70 	for(i=0;i<num_pictures;i++)
    71 		pictures[i].surface = NULL;
    72 	for(i=0;i<num_pictures;i++) {
    73 		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    74 		if(pictures[i].surface == NULL) {
    75 			j = 0;
    76 			for(j=0;j<num_pictures;j++)
    77 				if(pictures[j].surface != NULL)
    78 					SDL_FreeSurface(pictures[j].surface);
    79 			free(pictures);
    80 			SDL_VideoQuit();
    81 			printf("Could not load surface from named bitmap file.\n");
    82 			exit(-3);
    83 		}
    84 
    85 		format = pictures[i].surface->format;
    86 		if(format->Amask != 0) {
    87 			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    88 			pictures[i].mode.parameters.binarizationCutoff = 1;
    89 		}
    90 		else {
    91 			pictures[i].mode.mode = ShapeModeColorKey;
    92 			pictures[i].mode.parameters.colorKey = black;
    93 		}
    94 	}
    95 	
    96 	window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
    97 	if(window == NULL) {
    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 shaped window for SDL_Shape.\n");
   103 		exit(-4);
   104 	}
   105 	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
   106 		SDL_DestroyWindow(window);
   107 		for(i=0;i<num_pictures;i++)
   108 			SDL_FreeSurface(pictures[i].surface);
   109 		free(pictures);
   110 		SDL_VideoQuit();
   111 		printf("Could not create rendering context for SDL_Shape window.\n");
   112 		exit(-5);
   113 	}
   114 	
   115 	for(i=0;i<num_pictures;i++)
   116 		pictures[i].texture = NULL;
   117 	for(i=0;i<num_pictures;i++) {
   118 		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
   119 		if(pictures[i].texture == NULL) {
   120 			j = 0;
   121 			for(j=0;j<num_pictures;i++)
   122 				if(pictures[i].texture != NULL)
   123 					SDL_DestroyTexture(pictures[i].texture);
   124 			for(i=0;i<num_pictures;i++)
   125 				SDL_FreeSurface(pictures[i].surface);
   126 			free(pictures);
   127 			SDL_DestroyRenderer(window);
   128 			SDL_DestroyWindow(window);
   129 			SDL_VideoQuit();
   130 			printf("Could not create texture for SDL_shape.\n");
   131 			exit(-6);
   132 		}
   133 	}
   134 	
   135 	event_pending = 0;
   136 	should_exit = 0;
   137 	event_pending = SDL_PollEvent(&event);
   138 	current_picture = 0;
   139 	button_down = 0;
   140 	texture_dimensions.h = 0;
   141 	texture_dimensions.w = 0;
   142 	texture_dimensions.x = 0;
   143 	texture_dimensions.y = 0;
   144 	SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   145 	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   146 	SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   147 	next_time = SDL_GetTicks() + TICK_INTERVAL;
   148 	while(should_exit == 0) {
   149 		event_pending = SDL_PollEvent(&event);
   150 		if(event_pending == 1) {
   151 			if(event.type == SDL_KEYDOWN) {
   152 				button_down = 1;
   153 				if(event.key.keysym.sym == SDLK_ESCAPE)
   154 					should_exit = 1;
   155 			}
   156 			if(button_down && event.type == SDL_KEYUP) {
   157 				button_down = 0;
   158 				current_picture += 1;
   159 				if(current_picture >= num_pictures)
   160 					current_picture = 0;
   161 				SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   162 				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   163 				SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   164 			}
   165 			if(event.type == SDL_QUIT)
   166 				should_exit = 1;
   167 			event_pending = 0;
   168 		}
   169 		render(window,pictures[current_picture].texture,texture_dimensions);
   170 		SDL_Delay(time_left());
   171 		next_time += TICK_INTERVAL;
   172 	}
   173 	
   174 	//Free the textures.
   175 	for(i=0;i<num_pictures;i++)
   176 		SDL_DestroyTexture(pictures[i].texture);
   177 	//Destroy the window.
   178 	SDL_DestroyWindow(window);
   179 	//Free the original surfaces backing the textures.
   180 	for(i=0;i<num_pictures;i++)
   181 		SDL_FreeSurface(pictures[i].surface);
   182 	free(pictures);
   183 	//Call SDL_VideoQuit() before quitting.
   184 	SDL_VideoQuit();
   185 
   186 	return 0;
   187 }