test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 23:36:39 -0400
changeset 4803 a4c1df880690
parent 4802 f14a8c05f5bb
child 4804 b33752266d8f
permissions -rw-r--r--
Added code to make testshape switch shapes on keystrokes and exit on an ESC keystroke.
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@4803
     8
#include <SDL_keysym.h>
eligottlieb@4799
     9
eligottlieb@4799
    10
#define SHAPED_WINDOW_X 150
eligottlieb@4799
    11
#define SHAPED_WINDOW_Y 150
eligottlieb@4799
    12
#define SHAPED_WINDOW_DIMENSION 640
eligottlieb@4799
    13
eligottlieb@4799
    14
int main(int argc,char** argv) {
eligottlieb@4799
    15
        if(argc < 2) {
eligottlieb@4799
    16
        	printf("SDL_Shape requires at least one bitmap file as argument.\n");
eligottlieb@4799
    17
        	exit(-1);
eligottlieb@4799
    18
        }
eligottlieb@4799
    19
	
eligottlieb@4799
    20
	if(SDL_VideoInit(NULL,0) == -1) {
eligottlieb@4799
    21
		printf("Could not initialize SDL video.\n");
eligottlieb@4799
    22
		exit(-2);
eligottlieb@4799
    23
	}
eligottlieb@4799
    24
	
eligottlieb@4799
    25
	Uint8 num_pictures = argc - 1;
eligottlieb@4799
    26
	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
eligottlieb@4799
    27
	int i = 0;
eligottlieb@4799
    28
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
    29
		pictures[i] = NULL;
eligottlieb@4799
    30
	for(i=0;i<num_pictures;i++) {
eligottlieb@4802
    31
		pictures[i] = SDL_LoadBMP(argv[i+1]);
eligottlieb@4802
    32
		if(pictures[i] == NULL) {
eligottlieb@4799
    33
			int j = 0;
eligottlieb@4799
    34
			for(j=0;j<num_pictures;j++)
eligottlieb@4799
    35
				if(pictures[j] != NULL)
eligottlieb@4799
    36
					SDL_FreeSurface(pictures[j]);
eligottlieb@4799
    37
			free(pictures);
eligottlieb@4799
    38
			SDL_VideoQuit();
eligottlieb@4799
    39
			printf("Could not load surface from named bitmap file.\n");
eligottlieb@4799
    40
			exit(-3);
eligottlieb@4799
    41
		}
eligottlieb@4799
    42
	}
eligottlieb@4799
    43
	
eligottlieb@4799
    44
	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
    45
	if(window == NULL) {
eligottlieb@4799
    46
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    47
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    48
		free(pictures);
eligottlieb@4799
    49
		SDL_VideoQuit();
eligottlieb@4799
    50
		printf("Could not create shaped window for SDL_Shape.\n");
eligottlieb@4799
    51
		exit(-4);
eligottlieb@4799
    52
	}
eligottlieb@4799
    53
	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
eligottlieb@4799
    54
		SDL_DestroyWindow(window);
eligottlieb@4799
    55
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    56
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    57
		free(pictures);
eligottlieb@4799
    58
		SDL_VideoQuit();
eligottlieb@4799
    59
		printf("Could not create rendering context for SDL_Shape window.\n");
eligottlieb@4799
    60
		exit(-5);
eligottlieb@4799
    61
	}
eligottlieb@4799
    62
	
eligottlieb@4799
    63
	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
eligottlieb@4799
    64
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
    65
		textures[i] = NULL;
eligottlieb@4799
    66
	for(i=0;i<num_pictures;i++) {
eligottlieb@4799
    67
		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
eligottlieb@4799
    68
		if(textures[i] == NULL) {
eligottlieb@4799
    69
			int j = 0;
eligottlieb@4799
    70
			for(j=0;j<num_pictures;i++)
eligottlieb@4799
    71
				if(textures[i] != NULL)
eligottlieb@4799
    72
					SDL_DestroyTexture(textures[i]);
eligottlieb@4799
    73
			free(textures);
eligottlieb@4799
    74
			for(i=0;i<num_pictures;i++)
eligottlieb@4799
    75
				SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    76
			free(pictures);
eligottlieb@4799
    77
			SDL_DestroyRenderer(window);
eligottlieb@4799
    78
			SDL_DestroyWindow(window);
eligottlieb@4799
    79
			SDL_VideoQuit();
eligottlieb@4799
    80
			printf("Could not create texture for SDL_shape.\n");
eligottlieb@4799
    81
			exit(-6);
eligottlieb@4799
    82
		}
eligottlieb@4799
    83
	}
eligottlieb@4799
    84
	
eligottlieb@4799
    85
	SDL_Event event;
eligottlieb@4800
    86
	int event_pending = 0,should_exit = 0;
eligottlieb@4799
    87
	event_pending = SDL_PollEvent(&event);
eligottlieb@4799
    88
	unsigned int current_picture = 0;
eligottlieb@4799
    89
	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
eligottlieb@4803
    90
	int button_down = 0;
eligottlieb@4802
    91
	Uint32 format = 0,access = 0;
eligottlieb@4799
    92
	SDL_Rect texture_dimensions = {0,0,0,0};
eligottlieb@4799
    93
	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4799
    94
	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4802
    95
	SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4800
    96
	while(should_exit == 0) {
eligottlieb@4800
    97
		event_pending = SDL_PollEvent(&event);
eligottlieb@4800
    98
		if(event_pending == 1) {
eligottlieb@4803
    99
			if(event.type == SDL_KEYDOWN) {
eligottlieb@4803
   100
				button_down = 1;
eligottlieb@4803
   101
				if(event.key.keysym.sym == SDLK_ESCAPE)
eligottlieb@4803
   102
					should_exit = 1;
eligottlieb@4803
   103
			}
eligottlieb@4803
   104
			if(button_down && event.type == SDL_KEYUP) {
eligottlieb@4803
   105
				button_down = 0;
eligottlieb@4800
   106
				current_picture += 1;
eligottlieb@4800
   107
				if(current_picture >= num_pictures)
eligottlieb@4800
   108
					current_picture = 0;
eligottlieb@4800
   109
				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4800
   110
				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4800
   111
				SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4800
   112
			}
eligottlieb@4800
   113
			if(event.type == SDL_QUIT)
eligottlieb@4800
   114
				should_exit = 1;
eligottlieb@4800
   115
			event_pending = 0;
eligottlieb@4799
   116
		}
eligottlieb@4799
   117
		
eligottlieb@4799
   118
		SDL_SelectRenderer(window);
eligottlieb@4799
   119
	
eligottlieb@4799
   120
		//Clear render-target to blue.
eligottlieb@4799
   121
		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
eligottlieb@4799
   122
		SDL_RenderClear();
eligottlieb@4799
   123
		
eligottlieb@4799
   124
		//Render the texture.
eligottlieb@4799
   125
		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
eligottlieb@4799
   126
		
eligottlieb@4799
   127
		SDL_RenderPresent();
eligottlieb@4799
   128
	}
eligottlieb@4799
   129
	
eligottlieb@4799
   130
	//Free the textures.
eligottlieb@4799
   131
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   132
		SDL_DestroyTexture(textures[i]);
eligottlieb@4799
   133
	free(textures);
eligottlieb@4799
   134
	//Destroy the window.
eligottlieb@4799
   135
	SDL_DestroyWindow(window);
eligottlieb@4799
   136
	//Free the original surfaces backing the textures.
eligottlieb@4799
   137
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   138
		SDL_FreeSurface(pictures[i]);
eligottlieb@4799
   139
	free(pictures);
eligottlieb@4799
   140
	//Call SDL_VideoQuit() before quitting.
eligottlieb@4799
   141
	SDL_VideoQuit();
eligottlieb@4799
   142
}