test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 21:31:22 -0400
changeset 4799 a0e096916474
child 4800 6d4be626225f
permissions -rw-r--r--
Rewrote test program for shaped windows. It definitely displays recognizable pictures now, but the resizing and shaping functionality isn't behaving correctly, possibly due to a miscalculation of alpha values.
eligottlieb@4799
     1
#include <stdlib.h>
eligottlieb@4799
     2
#include <math.h>
eligottlieb@4799
     3
#include <SDL_events.h>
eligottlieb@4799
     4
#include <SDL_rect.h>
eligottlieb@4799
     5
#include <SDL_pixels.h>
eligottlieb@4799
     6
#include <SDL_video.h>
eligottlieb@4799
     7
#include <SDL_shape.h>
eligottlieb@4799
     8
eligottlieb@4799
     9
#define SHAPED_WINDOW_X 150
eligottlieb@4799
    10
#define SHAPED_WINDOW_Y 150
eligottlieb@4799
    11
#define SHAPED_WINDOW_DIMENSION 640
eligottlieb@4799
    12
eligottlieb@4799
    13
int main(int argc,char** argv) {
eligottlieb@4799
    14
        if(argc < 2) {
eligottlieb@4799
    15
        	printf("SDL_Shape requires at least one bitmap file as argument.\n");
eligottlieb@4799
    16
        	exit(-1);
eligottlieb@4799
    17
        }
eligottlieb@4799
    18
	
eligottlieb@4799
    19
	if(SDL_VideoInit(NULL,0) == -1) {
eligottlieb@4799
    20
		printf("Could not initialize SDL video.\n");
eligottlieb@4799
    21
		exit(-2);
eligottlieb@4799
    22
	}
eligottlieb@4799
    23
	
eligottlieb@4799
    24
	Uint8 num_pictures = argc - 1;
eligottlieb@4799
    25
	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
eligottlieb@4799
    26
	int i = 0;
eligottlieb@4799
    27
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
    28
		pictures[i] = NULL;
eligottlieb@4799
    29
	for(i=0;i<num_pictures;i++) {
eligottlieb@4799
    30
		SDL_Surface *original = SDL_LoadBMP(argv[i+1]);
eligottlieb@4799
    31
		if(original == NULL) {
eligottlieb@4799
    32
			int j = 0;
eligottlieb@4799
    33
			for(j=0;j<num_pictures;j++)
eligottlieb@4799
    34
				if(pictures[j] != NULL)
eligottlieb@4799
    35
					SDL_FreeSurface(pictures[j]);
eligottlieb@4799
    36
			free(pictures);
eligottlieb@4799
    37
			SDL_VideoQuit();
eligottlieb@4799
    38
			printf("Could not load surface from named bitmap file.\n");
eligottlieb@4799
    39
			exit(-3);
eligottlieb@4799
    40
		}
eligottlieb@4799
    41
		//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.
eligottlieb@4799
    42
		SDL_PixelFormat format = {NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
eligottlieb@4799
    43
		SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_RGBA8888,&format.BitsPerPixel,&format.Rmask,&format.Gmask,&format.Bmask,&format.Amask);
eligottlieb@4799
    44
		format.BytesPerPixel = format.BitsPerPixel / 8 + (format.BitsPerPixel % 8 > 0 ? 1 : 0);
eligottlieb@4799
    45
		pictures[i] = SDL_ConvertSurface(original,&format,0);
eligottlieb@4799
    46
		//We have no more need of the original now that we have our desired format.
eligottlieb@4799
    47
		SDL_FreeSurface(original);
eligottlieb@4799
    48
		if(pictures[i] == NULL) {
eligottlieb@4799
    49
			int j = 0;
eligottlieb@4799
    50
			for(j=0;j<num_pictures;j++)
eligottlieb@4799
    51
				if(pictures[j] != NULL)
eligottlieb@4799
    52
					SDL_FreeSurface(pictures[j]);
eligottlieb@4799
    53
			free(pictures);
eligottlieb@4799
    54
			SDL_VideoQuit();
eligottlieb@4799
    55
			printf("Could not convert bitmap surface to desired format.\n");
eligottlieb@4799
    56
			exit(-3);
eligottlieb@4799
    57
		}
eligottlieb@4799
    58
		
eligottlieb@4799
    59
		if(SDL_MUSTLOCK(pictures[i]))
eligottlieb@4799
    60
			SDL_LockSurface(pictures[i]);
eligottlieb@4799
    61
			
eligottlieb@4799
    62
		void* pixels = pictures[i]->pixels;
eligottlieb@4799
    63
		unsigned int pitch = pictures[i]->pitch;
eligottlieb@4799
    64
		int y =0,x = 0;
eligottlieb@4799
    65
		for(y=0;y<pictures[i]->h;y++)
eligottlieb@4799
    66
			for(x=0;x<pictures[i]->w;x++) {
eligottlieb@4799
    67
				Uint32* pixel = pixels+y*pitch+x*pictures[i]->format->BytesPerPixel;
eligottlieb@4799
    68
				Uint8 r = 0,g = 0,b = 0;
eligottlieb@4799
    69
				SDL_GetRGB(*pixel,pictures[i]->format,&r,&g,&b);
eligottlieb@4799
    70
				if(r == g == b == 0x00)
eligottlieb@4799
    71
					*pixel = SDL_MapRGBA(pictures[i]->format,r,g,b,0);
eligottlieb@4799
    72
			}
eligottlieb@4799
    73
			
eligottlieb@4799
    74
		if(SDL_MUSTLOCK(pictures[i]))
eligottlieb@4799
    75
			SDL_UnlockSurface(pictures[i]);
eligottlieb@4799
    76
	}
eligottlieb@4799
    77
	
eligottlieb@4799
    78
	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);
eligottlieb@4799
    79
	if(window == NULL) {
eligottlieb@4799
    80
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    81
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    82
		free(pictures);
eligottlieb@4799
    83
		SDL_VideoQuit();
eligottlieb@4799
    84
		printf("Could not create shaped window for SDL_Shape.\n");
eligottlieb@4799
    85
		exit(-4);
eligottlieb@4799
    86
	}
eligottlieb@4799
    87
	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
eligottlieb@4799
    88
		SDL_DestroyWindow(window);
eligottlieb@4799
    89
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    90
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    91
		free(pictures);
eligottlieb@4799
    92
		SDL_VideoQuit();
eligottlieb@4799
    93
		printf("Could not create rendering context for SDL_Shape window.\n");
eligottlieb@4799
    94
		exit(-5);
eligottlieb@4799
    95
	}
eligottlieb@4799
    96
	
eligottlieb@4799
    97
	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
eligottlieb@4799
    98
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
    99
		textures[i] = NULL;
eligottlieb@4799
   100
	for(i=0;i<num_pictures;i++) {
eligottlieb@4799
   101
		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
eligottlieb@4799
   102
		if(textures[i] == NULL) {
eligottlieb@4799
   103
			int j = 0;
eligottlieb@4799
   104
			for(j=0;j<num_pictures;i++)
eligottlieb@4799
   105
				if(textures[i] != NULL)
eligottlieb@4799
   106
					SDL_DestroyTexture(textures[i]);
eligottlieb@4799
   107
			free(textures);
eligottlieb@4799
   108
			for(i=0;i<num_pictures;i++)
eligottlieb@4799
   109
				SDL_FreeSurface(pictures[i]);
eligottlieb@4799
   110
			free(pictures);
eligottlieb@4799
   111
			SDL_DestroyRenderer(window);
eligottlieb@4799
   112
			SDL_DestroyWindow(window);
eligottlieb@4799
   113
			SDL_VideoQuit();
eligottlieb@4799
   114
			printf("Could not create texture for SDL_shape.\n");
eligottlieb@4799
   115
			exit(-6);
eligottlieb@4799
   116
		}
eligottlieb@4799
   117
	}
eligottlieb@4799
   118
	
eligottlieb@4799
   119
	SDL_Event event;
eligottlieb@4799
   120
	int event_pending = 0;
eligottlieb@4799
   121
	event_pending = SDL_PollEvent(&event);
eligottlieb@4799
   122
	unsigned int current_picture = 0;
eligottlieb@4799
   123
	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
eligottlieb@4799
   124
	SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4799
   125
	int mouse_down = 0;
eligottlieb@4799
   126
	Uint32 format,access;
eligottlieb@4799
   127
	SDL_Rect texture_dimensions = {0,0,0,0};
eligottlieb@4799
   128
	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4799
   129
	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4799
   130
	while(event.type != SDL_QUIT) {
eligottlieb@4799
   131
		if(event.type == SDL_MOUSEBUTTONDOWN)
eligottlieb@4799
   132
			mouse_down = 1;
eligottlieb@4799
   133
		if(mouse_down && event.type == SDL_MOUSEBUTTONUP) {
eligottlieb@4799
   134
			mouse_down = 0;
eligottlieb@4799
   135
			current_picture += 1;
eligottlieb@4799
   136
			if(current_picture >= num_pictures)
eligottlieb@4799
   137
				current_picture = 0;
eligottlieb@4799
   138
			SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4799
   139
			SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4799
   140
			SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4799
   141
		}
eligottlieb@4799
   142
		
eligottlieb@4799
   143
		SDL_SelectRenderer(window);
eligottlieb@4799
   144
	
eligottlieb@4799
   145
		//Clear render-target to blue.
eligottlieb@4799
   146
		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
eligottlieb@4799
   147
		SDL_RenderClear();
eligottlieb@4799
   148
		
eligottlieb@4799
   149
		//Render the texture.
eligottlieb@4799
   150
		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
eligottlieb@4799
   151
		
eligottlieb@4799
   152
		SDL_RenderPresent();
eligottlieb@4799
   153
		event_pending = SDL_PollEvent(&event);
eligottlieb@4799
   154
	}
eligottlieb@4799
   155
	
eligottlieb@4799
   156
	//Free the textures.
eligottlieb@4799
   157
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   158
		SDL_DestroyTexture(textures[i]);
eligottlieb@4799
   159
	free(textures);
eligottlieb@4799
   160
	//Destroy the window.
eligottlieb@4799
   161
	SDL_DestroyWindow(window);
eligottlieb@4799
   162
	//Free the original surfaces backing the textures.
eligottlieb@4799
   163
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   164
		SDL_FreeSurface(pictures[i]);
eligottlieb@4799
   165
	free(pictures);
eligottlieb@4799
   166
	//Call SDL_VideoQuit() before quitting.
eligottlieb@4799
   167
	SDL_VideoQuit();
eligottlieb@4799
   168
}