test/testshape.c
changeset 4816 eb433f0d2ac5
parent 4809 329708ffe2a7
child 4817 c68e7490e4cf
     1.1 --- a/test/testshape.c	Thu Jul 29 22:57:39 2010 -0400
     1.2 +++ b/test/testshape.c	Fri Jul 30 18:04:21 2010 -0400
     1.3 @@ -1,12 +1,8 @@
     1.4  #include <stdlib.h>
     1.5  #include <math.h>
     1.6 -#include <SDL_events.h>
     1.7 -#include <SDL_rect.h>
     1.8 -#include <SDL_pixels.h>
     1.9 -#include <SDL_video.h>
    1.10 -#include <SDL_shape.h>
    1.11 -#include <SDL_keysym.h>
    1.12 -#include <SDL_timer.h>
    1.13 +#include <stdio.h>
    1.14 +#include "SDL.h"
    1.15 +#include "SDL_shape.h"
    1.16  
    1.17  #define SHAPED_WINDOW_X 150
    1.18  #define SHAPED_WINDOW_Y 150
    1.19 @@ -44,7 +40,23 @@
    1.20  }
    1.21  
    1.22  int main(int argc,char** argv) {
    1.23 -        if(argc < 2) {
    1.24 +	Uint8 num_pictures;
    1.25 +	LoadedPicture* pictures;
    1.26 +	int i, j;
    1.27 +	SDL_PixelFormat* format;
    1.28 +	Uint32 format_enum;
    1.29 +	SDL_Window *window;
    1.30 +	SDL_Color black = {0,0,0,0xff};
    1.31 +	SDL_Event event;
    1.32 +	int event_pending = 0;
    1.33 +	int should_exit = 0;
    1.34 +	unsigned int current_picture;
    1.35 +	int button_down;
    1.36 +	Uint32 pixelFormat;
    1.37 +	int access;
    1.38 +	SDL_Rect texture_dimensions;;
    1.39 +
    1.40 +	if(argc < 2) {
    1.41          	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    1.42          	exit(-1);
    1.43          }
    1.44 @@ -54,15 +66,14 @@
    1.45  		exit(-2);
    1.46  	}
    1.47  	
    1.48 -	Uint8 num_pictures = argc - 1;
    1.49 -	LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures);
    1.50 -	int i = 0;
    1.51 +	num_pictures = argc - 1;
    1.52 +	pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
    1.53  	for(i=0;i<num_pictures;i++)
    1.54  		pictures[i].surface = NULL;
    1.55  	for(i=0;i<num_pictures;i++) {
    1.56  		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    1.57  		if(pictures[i].surface == NULL) {
    1.58 -			int j = 0;
    1.59 +			j = 0;
    1.60  			for(j=0;j<num_pictures;j++)
    1.61  				if(pictures[j].surface != NULL)
    1.62  					SDL_FreeSurface(pictures[j].surface);
    1.63 @@ -71,20 +82,20 @@
    1.64  			printf("Could not load surface from named bitmap file.\n");
    1.65  			exit(-3);
    1.66  		}
    1.67 -		SDL_PixelFormat* format = pictures[i].surface->format;
    1.68 -		Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
    1.69 +
    1.70 +		format = pictures[i].surface->format;
    1.71 +		format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
    1.72  		if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
    1.73  			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    1.74  			pictures[i].mode.parameters.binarizationCutoff = 1;
    1.75  		}
    1.76  		else {
    1.77  			pictures[i].mode.mode = ShapeModeColorKey;
    1.78 -			SDL_Color black = {0,0,0,0xff};
    1.79  			pictures[i].mode.parameters.colorKey = black;
    1.80  		}
    1.81  	}
    1.82  	
    1.83 -	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);
    1.84 +	window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
    1.85  	if(window == NULL) {
    1.86  		for(i=0;i<num_pictures;i++)
    1.87  			SDL_FreeSurface(pictures[i].surface);
    1.88 @@ -108,7 +119,7 @@
    1.89  	for(i=0;i<num_pictures;i++) {
    1.90  		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
    1.91  		if(pictures[i].texture == NULL) {
    1.92 -			int j = 0;
    1.93 +			j = 0;
    1.94  			for(j=0;j<num_pictures;i++)
    1.95  				if(pictures[i].texture != NULL)
    1.96  					SDL_DestroyTexture(pictures[i].texture);
    1.97 @@ -123,17 +134,19 @@
    1.98  		}
    1.99  	}
   1.100  	
   1.101 -	SDL_Event event;
   1.102 -	int event_pending = 0,should_exit = 0;
   1.103 +	event_pending = 0;
   1.104 +	should_exit = 0;
   1.105  	event_pending = SDL_PollEvent(&event);
   1.106 -	unsigned int current_picture = 0;
   1.107 -	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
   1.108 -	int button_down = 0;
   1.109 -	Uint32 format = 0,access = 0;
   1.110 -	SDL_Rect texture_dimensions = {0,0,0,0};
   1.111 -	SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.112 +	current_picture = 0;
   1.113 +	button_down = 0;
   1.114 +	format = 0,access = 0;
   1.115 +	texture_dimensions.h = 0;
   1.116 +	texture_dimensions.w = 0;
   1.117 +	texture_dimensions.x = 0;
   1.118 +	texture_dimensions.y = 0;
   1.119 +	SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   1.120  	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.121 -	SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   1.122 +	SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   1.123  	next_time = SDL_GetTicks() + TICK_INTERVAL;
   1.124  	while(should_exit == 0) {
   1.125  		event_pending = SDL_PollEvent(&event);
   1.126 @@ -148,9 +161,9 @@
   1.127  				current_picture += 1;
   1.128  				if(current_picture >= num_pictures)
   1.129  					current_picture = 0;
   1.130 -				SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.131 +				SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   1.132  				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.133 -				SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
   1.134 +				SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   1.135  			}
   1.136  			if(event.type == SDL_QUIT)
   1.137  				should_exit = 1;
   1.138 @@ -172,4 +185,6 @@
   1.139  	free(pictures);
   1.140  	//Call SDL_VideoQuit() before quitting.
   1.141  	SDL_VideoQuit();
   1.142 +
   1.143 +	return 0;
   1.144  }