test/testshape.c
changeset 4799 a0e096916474
child 4800 6d4be626225f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testshape.c	Sun Jul 18 21:31:22 2010 -0400
     1.3 @@ -0,0 +1,168 @@
     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 +
    1.12 +#define SHAPED_WINDOW_X 150
    1.13 +#define SHAPED_WINDOW_Y 150
    1.14 +#define SHAPED_WINDOW_DIMENSION 640
    1.15 +
    1.16 +int main(int argc,char** argv) {
    1.17 +        if(argc < 2) {
    1.18 +        	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    1.19 +        	exit(-1);
    1.20 +        }
    1.21 +	
    1.22 +	if(SDL_VideoInit(NULL,0) == -1) {
    1.23 +		printf("Could not initialize SDL video.\n");
    1.24 +		exit(-2);
    1.25 +	}
    1.26 +	
    1.27 +	Uint8 num_pictures = argc - 1;
    1.28 +	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
    1.29 +	int i = 0;
    1.30 +	for(i=0;i<num_pictures;i++)
    1.31 +		pictures[i] = NULL;
    1.32 +	for(i=0;i<num_pictures;i++) {
    1.33 +		SDL_Surface *original = SDL_LoadBMP(argv[i+1]);
    1.34 +		if(original == NULL) {
    1.35 +			int j = 0;
    1.36 +			for(j=0;j<num_pictures;j++)
    1.37 +				if(pictures[j] != NULL)
    1.38 +					SDL_FreeSurface(pictures[j]);
    1.39 +			free(pictures);
    1.40 +			SDL_VideoQuit();
    1.41 +			printf("Could not load surface from named bitmap file.\n");
    1.42 +			exit(-3);
    1.43 +		}
    1.44 +		//THIS CONVERSION ROUTINE IS FRAGILE!  It relies in the fact that only certain portions of the format structure must be filled in to use it.
    1.45 +		SDL_PixelFormat format = {NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    1.46 +		SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_RGBA8888,&format.BitsPerPixel,&format.Rmask,&format.Gmask,&format.Bmask,&format.Amask);
    1.47 +		format.BytesPerPixel = format.BitsPerPixel / 8 + (format.BitsPerPixel % 8 > 0 ? 1 : 0);
    1.48 +		pictures[i] = SDL_ConvertSurface(original,&format,0);
    1.49 +		//We have no more need of the original now that we have our desired format.
    1.50 +		SDL_FreeSurface(original);
    1.51 +		if(pictures[i] == NULL) {
    1.52 +			int j = 0;
    1.53 +			for(j=0;j<num_pictures;j++)
    1.54 +				if(pictures[j] != NULL)
    1.55 +					SDL_FreeSurface(pictures[j]);
    1.56 +			free(pictures);
    1.57 +			SDL_VideoQuit();
    1.58 +			printf("Could not convert bitmap surface to desired format.\n");
    1.59 +			exit(-3);
    1.60 +		}
    1.61 +		
    1.62 +		if(SDL_MUSTLOCK(pictures[i]))
    1.63 +			SDL_LockSurface(pictures[i]);
    1.64 +			
    1.65 +		void* pixels = pictures[i]->pixels;
    1.66 +		unsigned int pitch = pictures[i]->pitch;
    1.67 +		int y =0,x = 0;
    1.68 +		for(y=0;y<pictures[i]->h;y++)
    1.69 +			for(x=0;x<pictures[i]->w;x++) {
    1.70 +				Uint32* pixel = pixels+y*pitch+x*pictures[i]->format->BytesPerPixel;
    1.71 +				Uint8 r = 0,g = 0,b = 0;
    1.72 +				SDL_GetRGB(*pixel,pictures[i]->format,&r,&g,&b);
    1.73 +				if(r == g == b == 0x00)
    1.74 +					*pixel = SDL_MapRGBA(pictures[i]->format,r,g,b,0);
    1.75 +			}
    1.76 +			
    1.77 +		if(SDL_MUSTLOCK(pictures[i]))
    1.78 +			SDL_UnlockSurface(pictures[i]);
    1.79 +	}
    1.80 +	
    1.81 +	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.82 +	if(window == NULL) {
    1.83 +		for(i=0;i<num_pictures;i++)
    1.84 +			SDL_FreeSurface(pictures[i]);
    1.85 +		free(pictures);
    1.86 +		SDL_VideoQuit();
    1.87 +		printf("Could not create shaped window for SDL_Shape.\n");
    1.88 +		exit(-4);
    1.89 +	}
    1.90 +	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    1.91 +		SDL_DestroyWindow(window);
    1.92 +		for(i=0;i<num_pictures;i++)
    1.93 +			SDL_FreeSurface(pictures[i]);
    1.94 +		free(pictures);
    1.95 +		SDL_VideoQuit();
    1.96 +		printf("Could not create rendering context for SDL_Shape window.\n");
    1.97 +		exit(-5);
    1.98 +	}
    1.99 +	
   1.100 +	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
   1.101 +	for(i=0;i<num_pictures;i++)
   1.102 +		textures[i] = NULL;
   1.103 +	for(i=0;i<num_pictures;i++) {
   1.104 +		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
   1.105 +		if(textures[i] == NULL) {
   1.106 +			int j = 0;
   1.107 +			for(j=0;j<num_pictures;i++)
   1.108 +				if(textures[i] != NULL)
   1.109 +					SDL_DestroyTexture(textures[i]);
   1.110 +			free(textures);
   1.111 +			for(i=0;i<num_pictures;i++)
   1.112 +				SDL_FreeSurface(pictures[i]);
   1.113 +			free(pictures);
   1.114 +			SDL_DestroyRenderer(window);
   1.115 +			SDL_DestroyWindow(window);
   1.116 +			SDL_VideoQuit();
   1.117 +			printf("Could not create texture for SDL_shape.\n");
   1.118 +			exit(-6);
   1.119 +		}
   1.120 +	}
   1.121 +	
   1.122 +	SDL_Event event;
   1.123 +	int event_pending = 0;
   1.124 +	event_pending = SDL_PollEvent(&event);
   1.125 +	unsigned int current_picture = 0;
   1.126 +	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
   1.127 +	SDL_SetWindowShape(window,pictures[current_picture],&mode);
   1.128 +	int mouse_down = 0;
   1.129 +	Uint32 format,access;
   1.130 +	SDL_Rect texture_dimensions = {0,0,0,0};
   1.131 +	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.132 +	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.133 +	while(event.type != SDL_QUIT) {
   1.134 +		if(event.type == SDL_MOUSEBUTTONDOWN)
   1.135 +			mouse_down = 1;
   1.136 +		if(mouse_down && event.type == SDL_MOUSEBUTTONUP) {
   1.137 +			mouse_down = 0;
   1.138 +			current_picture += 1;
   1.139 +			if(current_picture >= num_pictures)
   1.140 +				current_picture = 0;
   1.141 +			SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   1.142 +			SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   1.143 +			SDL_SetWindowShape(window,pictures[current_picture],&mode);
   1.144 +		}
   1.145 +		
   1.146 +		SDL_SelectRenderer(window);
   1.147 +	
   1.148 +		//Clear render-target to blue.
   1.149 +		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
   1.150 +		SDL_RenderClear();
   1.151 +		
   1.152 +		//Render the texture.
   1.153 +		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
   1.154 +		
   1.155 +		SDL_RenderPresent();
   1.156 +		event_pending = SDL_PollEvent(&event);
   1.157 +	}
   1.158 +	
   1.159 +	//Free the textures.
   1.160 +	for(i=0;i<num_pictures;i++)
   1.161 +		SDL_DestroyTexture(textures[i]);
   1.162 +	free(textures);
   1.163 +	//Destroy the window.
   1.164 +	SDL_DestroyWindow(window);
   1.165 +	//Free the original surfaces backing the textures.
   1.166 +	for(i=0;i<num_pictures;i++)
   1.167 +		SDL_FreeSurface(pictures[i]);
   1.168 +	free(pictures);
   1.169 +	//Call SDL_VideoQuit() before quitting.
   1.170 +	SDL_VideoQuit();
   1.171 +}