test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 23:05:40 -0400
changeset 4802 f14a8c05f5bb
parent 4800 6d4be626225f
child 4803 a4c1df880690
permissions -rw-r--r--
Minor bugfixes. testshape now draws a shaped window with bizarre, pixellated gashes of transparency across it, and in doing so seems to hog a system resource and slow the rest of the video system down.
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@4802
    30
		pictures[i] = SDL_LoadBMP(argv[i+1]);
eligottlieb@4802
    31
		if(pictures[i] == 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
	}
eligottlieb@4799
    42
	
eligottlieb@4799
    43
	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
    44
	if(window == NULL) {
eligottlieb@4799
    45
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    46
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    47
		free(pictures);
eligottlieb@4799
    48
		SDL_VideoQuit();
eligottlieb@4799
    49
		printf("Could not create shaped window for SDL_Shape.\n");
eligottlieb@4799
    50
		exit(-4);
eligottlieb@4799
    51
	}
eligottlieb@4799
    52
	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
eligottlieb@4799
    53
		SDL_DestroyWindow(window);
eligottlieb@4799
    54
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    55
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    56
		free(pictures);
eligottlieb@4799
    57
		SDL_VideoQuit();
eligottlieb@4799
    58
		printf("Could not create rendering context for SDL_Shape window.\n");
eligottlieb@4799
    59
		exit(-5);
eligottlieb@4799
    60
	}
eligottlieb@4799
    61
	
eligottlieb@4799
    62
	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
eligottlieb@4799
    63
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
    64
		textures[i] = NULL;
eligottlieb@4799
    65
	for(i=0;i<num_pictures;i++) {
eligottlieb@4799
    66
		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
eligottlieb@4799
    67
		if(textures[i] == NULL) {
eligottlieb@4799
    68
			int j = 0;
eligottlieb@4799
    69
			for(j=0;j<num_pictures;i++)
eligottlieb@4799
    70
				if(textures[i] != NULL)
eligottlieb@4799
    71
					SDL_DestroyTexture(textures[i]);
eligottlieb@4799
    72
			free(textures);
eligottlieb@4799
    73
			for(i=0;i<num_pictures;i++)
eligottlieb@4799
    74
				SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    75
			free(pictures);
eligottlieb@4799
    76
			SDL_DestroyRenderer(window);
eligottlieb@4799
    77
			SDL_DestroyWindow(window);
eligottlieb@4799
    78
			SDL_VideoQuit();
eligottlieb@4799
    79
			printf("Could not create texture for SDL_shape.\n");
eligottlieb@4799
    80
			exit(-6);
eligottlieb@4799
    81
		}
eligottlieb@4799
    82
	}
eligottlieb@4799
    83
	
eligottlieb@4799
    84
	SDL_Event event;
eligottlieb@4800
    85
	int event_pending = 0,should_exit = 0;
eligottlieb@4799
    86
	event_pending = SDL_PollEvent(&event);
eligottlieb@4799
    87
	unsigned int current_picture = 0;
eligottlieb@4799
    88
	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
eligottlieb@4799
    89
	int mouse_down = 0;
eligottlieb@4802
    90
	Uint32 format = 0,access = 0;
eligottlieb@4799
    91
	SDL_Rect texture_dimensions = {0,0,0,0};
eligottlieb@4799
    92
	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4799
    93
	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4802
    94
	SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4800
    95
	while(should_exit == 0) {
eligottlieb@4800
    96
		event_pending = SDL_PollEvent(&event);
eligottlieb@4800
    97
		if(event_pending == 1) {
eligottlieb@4800
    98
			if(event.type == SDL_MOUSEBUTTONDOWN)
eligottlieb@4800
    99
				mouse_down = 1;
eligottlieb@4800
   100
			if(mouse_down && event.type == SDL_MOUSEBUTTONUP) {
eligottlieb@4800
   101
				mouse_down = 0;
eligottlieb@4800
   102
				current_picture += 1;
eligottlieb@4800
   103
				if(current_picture >= num_pictures)
eligottlieb@4800
   104
					current_picture = 0;
eligottlieb@4800
   105
				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4800
   106
				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4800
   107
				SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4800
   108
			}
eligottlieb@4800
   109
			if(event.type == SDL_QUIT)
eligottlieb@4800
   110
				should_exit = 1;
eligottlieb@4800
   111
			event_pending = 0;
eligottlieb@4799
   112
		}
eligottlieb@4799
   113
		
eligottlieb@4799
   114
		SDL_SelectRenderer(window);
eligottlieb@4799
   115
	
eligottlieb@4799
   116
		//Clear render-target to blue.
eligottlieb@4799
   117
		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
eligottlieb@4799
   118
		SDL_RenderClear();
eligottlieb@4799
   119
		
eligottlieb@4799
   120
		//Render the texture.
eligottlieb@4799
   121
		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
eligottlieb@4799
   122
		
eligottlieb@4799
   123
		SDL_RenderPresent();
eligottlieb@4799
   124
	}
eligottlieb@4799
   125
	
eligottlieb@4799
   126
	//Free the textures.
eligottlieb@4799
   127
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   128
		SDL_DestroyTexture(textures[i]);
eligottlieb@4799
   129
	free(textures);
eligottlieb@4799
   130
	//Destroy the window.
eligottlieb@4799
   131
	SDL_DestroyWindow(window);
eligottlieb@4799
   132
	//Free the original surfaces backing the textures.
eligottlieb@4799
   133
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   134
		SDL_FreeSurface(pictures[i]);
eligottlieb@4799
   135
	free(pictures);
eligottlieb@4799
   136
	//Call SDL_VideoQuit() before quitting.
eligottlieb@4799
   137
	SDL_VideoQuit();
eligottlieb@4799
   138
}