test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Fri, 30 Jul 2010 18:04:21 -0400
changeset 4816 eb433f0d2ac5
parent 4809 329708ffe2a7
child 4817 c68e7490e4cf
permissions -rw-r--r--
Added Andreas's VS2010 patch for testshape.
     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/60
    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;
    47 	Uint32 format_enum;
    48 	SDL_Window *window;
    49 	SDL_Color black = {0,0,0,0xff};
    50 	SDL_Event event;
    51 	int event_pending = 0;
    52 	int should_exit = 0;
    53 	unsigned int current_picture;
    54 	int button_down;
    55 	Uint32 pixelFormat;
    56 	int access;
    57 	SDL_Rect texture_dimensions;;
    58 
    59 	if(argc < 2) {
    60         	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    61         	exit(-1);
    62         }
    63 	
    64 	if(SDL_VideoInit(NULL,0) == -1) {
    65 		printf("Could not initialize SDL video.\n");
    66 		exit(-2);
    67 	}
    68 	
    69 	num_pictures = argc - 1;
    70 	pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
    71 	for(i=0;i<num_pictures;i++)
    72 		pictures[i].surface = NULL;
    73 	for(i=0;i<num_pictures;i++) {
    74 		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    75 		if(pictures[i].surface == NULL) {
    76 			j = 0;
    77 			for(j=0;j<num_pictures;j++)
    78 				if(pictures[j].surface != NULL)
    79 					SDL_FreeSurface(pictures[j].surface);
    80 			free(pictures);
    81 			SDL_VideoQuit();
    82 			printf("Could not load surface from named bitmap file.\n");
    83 			exit(-3);
    84 		}
    85 
    86 		format = pictures[i].surface->format;
    87 		format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
    88 		if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
    89 			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    90 			pictures[i].mode.parameters.binarizationCutoff = 1;
    91 		}
    92 		else {
    93 			pictures[i].mode.mode = ShapeModeColorKey;
    94 			pictures[i].mode.parameters.colorKey = black;
    95 		}
    96 	}
    97 	
    98 	window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
    99 	if(window == NULL) {
   100 		for(i=0;i<num_pictures;i++)
   101 			SDL_FreeSurface(pictures[i].surface);
   102 		free(pictures);
   103 		SDL_VideoQuit();
   104 		printf("Could not create shaped window for SDL_Shape.\n");
   105 		exit(-4);
   106 	}
   107 	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
   108 		SDL_DestroyWindow(window);
   109 		for(i=0;i<num_pictures;i++)
   110 			SDL_FreeSurface(pictures[i].surface);
   111 		free(pictures);
   112 		SDL_VideoQuit();
   113 		printf("Could not create rendering context for SDL_Shape window.\n");
   114 		exit(-5);
   115 	}
   116 	
   117 	for(i=0;i<num_pictures;i++)
   118 		pictures[i].texture = NULL;
   119 	for(i=0;i<num_pictures;i++) {
   120 		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
   121 		if(pictures[i].texture == NULL) {
   122 			j = 0;
   123 			for(j=0;j<num_pictures;i++)
   124 				if(pictures[i].texture != NULL)
   125 					SDL_DestroyTexture(pictures[i].texture);
   126 			for(i=0;i<num_pictures;i++)
   127 				SDL_FreeSurface(pictures[i].surface);
   128 			free(pictures);
   129 			SDL_DestroyRenderer(window);
   130 			SDL_DestroyWindow(window);
   131 			SDL_VideoQuit();
   132 			printf("Could not create texture for SDL_shape.\n");
   133 			exit(-6);
   134 		}
   135 	}
   136 	
   137 	event_pending = 0;
   138 	should_exit = 0;
   139 	event_pending = SDL_PollEvent(&event);
   140 	current_picture = 0;
   141 	button_down = 0;
   142 	format = 0,access = 0;
   143 	texture_dimensions.h = 0;
   144 	texture_dimensions.w = 0;
   145 	texture_dimensions.x = 0;
   146 	texture_dimensions.y = 0;
   147 	SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   148 	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   149 	SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   150 	next_time = SDL_GetTicks() + TICK_INTERVAL;
   151 	while(should_exit == 0) {
   152 		event_pending = SDL_PollEvent(&event);
   153 		if(event_pending == 1) {
   154 			if(event.type == SDL_KEYDOWN) {
   155 				button_down = 1;
   156 				if(event.key.keysym.sym == SDLK_ESCAPE)
   157 					should_exit = 1;
   158 			}
   159 			if(button_down && event.type == SDL_KEYUP) {
   160 				button_down = 0;
   161 				current_picture += 1;
   162 				if(current_picture >= num_pictures)
   163 					current_picture = 0;
   164 				SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   165 				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   166 				SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   167 			}
   168 			if(event.type == SDL_QUIT)
   169 				should_exit = 1;
   170 			event_pending = 0;
   171 		}
   172 		render(window,pictures[current_picture].texture,texture_dimensions);
   173 		SDL_Delay(time_left());
   174 		next_time += TICK_INTERVAL;
   175 	}
   176 	
   177 	//Free the textures.
   178 	for(i=0;i<num_pictures;i++)
   179 		SDL_DestroyTexture(pictures[i].texture);
   180 	//Destroy the window.
   181 	SDL_DestroyWindow(window);
   182 	//Free the original surfaces backing the textures.
   183 	for(i=0;i<num_pictures;i++)
   184 		SDL_FreeSurface(pictures[i].surface);
   185 	free(pictures);
   186 	//Call SDL_VideoQuit() before quitting.
   187 	SDL_VideoQuit();
   188 
   189 	return 0;
   190 }