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