test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Fri, 23 Jul 2010 01:48:42 -0400
changeset 4808 2ae79ed78a5a
parent 4805 de3b3455f8ec
child 4809 329708ffe2a7
permissions -rw-r--r--
More work on color-key mode.
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@4805
     9
#include <SDL_timer.h>
eligottlieb@4799
    10
eligottlieb@4799
    11
#define SHAPED_WINDOW_X 150
eligottlieb@4799
    12
#define SHAPED_WINDOW_Y 150
eligottlieb@4799
    13
#define SHAPED_WINDOW_DIMENSION 640
eligottlieb@4799
    14
eligottlieb@4805
    15
#define TICK_INTERVAL 18
eligottlieb@4805
    16
eligottlieb@4808
    17
typedef struct LoadedPicture {
eligottlieb@4808
    18
	SDL_Surface *surface;
eligottlieb@4808
    19
	SDL_Texture *texture;
eligottlieb@4808
    20
	SDL_WindowShapeMode mode;
eligottlieb@4808
    21
} LoadedPicture;
eligottlieb@4808
    22
eligottlieb@4804
    23
void render(SDL_Window* window,SDL_Texture *texture,SDL_Rect texture_dimensions) {
eligottlieb@4804
    24
	SDL_SelectRenderer(window);
eligottlieb@4804
    25
	
eligottlieb@4804
    26
	//Clear render-target to blue.
eligottlieb@4804
    27
	SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
eligottlieb@4804
    28
	SDL_RenderClear();
eligottlieb@4804
    29
	
eligottlieb@4804
    30
	//Render the texture.
eligottlieb@4804
    31
	SDL_RenderCopy(texture,&texture_dimensions,&texture_dimensions);
eligottlieb@4804
    32
	
eligottlieb@4804
    33
	SDL_RenderPresent();
eligottlieb@4804
    34
}
eligottlieb@4804
    35
eligottlieb@4805
    36
static Uint32 next_time;
eligottlieb@4805
    37
eligottlieb@4805
    38
Uint32 time_left() {
eligottlieb@4805
    39
	Uint32 now = SDL_GetTicks();
eligottlieb@4805
    40
	if(next_time <= now)
eligottlieb@4805
    41
		return 0;
eligottlieb@4805
    42
	else
eligottlieb@4805
    43
		return next_time - now;
eligottlieb@4805
    44
}
eligottlieb@4805
    45
eligottlieb@4799
    46
int main(int argc,char** argv) {
eligottlieb@4799
    47
        if(argc < 2) {
eligottlieb@4799
    48
        	printf("SDL_Shape requires at least one bitmap file as argument.\n");
eligottlieb@4799
    49
        	exit(-1);
eligottlieb@4799
    50
        }
eligottlieb@4799
    51
	
eligottlieb@4799
    52
	if(SDL_VideoInit(NULL,0) == -1) {
eligottlieb@4799
    53
		printf("Could not initialize SDL video.\n");
eligottlieb@4799
    54
		exit(-2);
eligottlieb@4799
    55
	}
eligottlieb@4799
    56
	
eligottlieb@4799
    57
	Uint8 num_pictures = argc - 1;
eligottlieb@4808
    58
	LoadedPicture* pictures = malloc(sizeof(LoadedPicture)*num_pictures);
eligottlieb@4799
    59
	int i = 0;
eligottlieb@4799
    60
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
    61
		pictures[i].surface = NULL;
eligottlieb@4799
    62
	for(i=0;i<num_pictures;i++) {
eligottlieb@4808
    63
		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
eligottlieb@4808
    64
		if(pictures[i].surface == NULL) {
eligottlieb@4799
    65
			int j = 0;
eligottlieb@4799
    66
			for(j=0;j<num_pictures;j++)
eligottlieb@4808
    67
				if(pictures[j].surface != NULL)
eligottlieb@4808
    68
					SDL_FreeSurface(pictures[j].surface);
eligottlieb@4799
    69
			free(pictures);
eligottlieb@4799
    70
			SDL_VideoQuit();
eligottlieb@4799
    71
			printf("Could not load surface from named bitmap file.\n");
eligottlieb@4799
    72
			exit(-3);
eligottlieb@4799
    73
		}
eligottlieb@4808
    74
		SDL_PixelFormat* format = pictures[i].surface->format;
eligottlieb@4808
    75
		Uint32 format_enum = SDL_MasksToPixelFormatEnum (format->BitsPerPixel,format->Rmask,format->Gmask, format->Bmask,format->Amask);
eligottlieb@4808
    76
		if(SDL_ISPIXELFORMAT_ALPHA(format_enum)) {
eligottlieb@4808
    77
			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
eligottlieb@4808
    78
			pictures[i].mode.parameters.binarizationCutoff = 1;
eligottlieb@4808
    79
		}
eligottlieb@4808
    80
		else {
eligottlieb@4808
    81
			pictures[i].mode.mode = ShapeModeColorKey;
eligottlieb@4808
    82
			SDL_Color black = {0,0,0,0xff};
eligottlieb@4808
    83
			pictures[i].mode.parameters.colorKey = black;
eligottlieb@4808
    84
		}
eligottlieb@4799
    85
	}
eligottlieb@4799
    86
	
eligottlieb@4799
    87
	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
    88
	if(window == NULL) {
eligottlieb@4799
    89
		for(i=0;i<num_pictures;i++)
eligottlieb@4808
    90
			SDL_FreeSurface(pictures[i].surface);
eligottlieb@4799
    91
		free(pictures);
eligottlieb@4799
    92
		SDL_VideoQuit();
eligottlieb@4799
    93
		printf("Could not create shaped window for SDL_Shape.\n");
eligottlieb@4799
    94
		exit(-4);
eligottlieb@4799
    95
	}
eligottlieb@4799
    96
	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
eligottlieb@4799
    97
		SDL_DestroyWindow(window);
eligottlieb@4799
    98
		for(i=0;i<num_pictures;i++)
eligottlieb@4808
    99
			SDL_FreeSurface(pictures[i].surface);
eligottlieb@4799
   100
		free(pictures);
eligottlieb@4799
   101
		SDL_VideoQuit();
eligottlieb@4799
   102
		printf("Could not create rendering context for SDL_Shape window.\n");
eligottlieb@4799
   103
		exit(-5);
eligottlieb@4799
   104
	}
eligottlieb@4799
   105
	
eligottlieb@4799
   106
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
   107
		pictures[i].texture = NULL;
eligottlieb@4799
   108
	for(i=0;i<num_pictures;i++) {
eligottlieb@4808
   109
		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
eligottlieb@4808
   110
		if(pictures[i].texture == NULL) {
eligottlieb@4799
   111
			int j = 0;
eligottlieb@4799
   112
			for(j=0;j<num_pictures;i++)
eligottlieb@4808
   113
				if(pictures[i].texture != NULL)
eligottlieb@4808
   114
					SDL_DestroyTexture(pictures[i].texture);
eligottlieb@4799
   115
			for(i=0;i<num_pictures;i++)
eligottlieb@4808
   116
				SDL_FreeSurface(pictures[i].surface);
eligottlieb@4799
   117
			free(pictures);
eligottlieb@4799
   118
			SDL_DestroyRenderer(window);
eligottlieb@4799
   119
			SDL_DestroyWindow(window);
eligottlieb@4799
   120
			SDL_VideoQuit();
eligottlieb@4799
   121
			printf("Could not create texture for SDL_shape.\n");
eligottlieb@4799
   122
			exit(-6);
eligottlieb@4799
   123
		}
eligottlieb@4799
   124
	}
eligottlieb@4799
   125
	
eligottlieb@4799
   126
	SDL_Event event;
eligottlieb@4800
   127
	int event_pending = 0,should_exit = 0;
eligottlieb@4799
   128
	event_pending = SDL_PollEvent(&event);
eligottlieb@4799
   129
	unsigned int current_picture = 0;
eligottlieb@4799
   130
	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
eligottlieb@4803
   131
	int button_down = 0;
eligottlieb@4802
   132
	Uint32 format = 0,access = 0;
eligottlieb@4799
   133
	SDL_Rect texture_dimensions = {0,0,0,0};
eligottlieb@4808
   134
	SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4799
   135
	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4808
   136
	SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
eligottlieb@4805
   137
	next_time = SDL_GetTicks() + TICK_INTERVAL;
eligottlieb@4800
   138
	while(should_exit == 0) {
eligottlieb@4800
   139
		event_pending = SDL_PollEvent(&event);
eligottlieb@4800
   140
		if(event_pending == 1) {
eligottlieb@4803
   141
			if(event.type == SDL_KEYDOWN) {
eligottlieb@4803
   142
				button_down = 1;
eligottlieb@4803
   143
				if(event.key.keysym.sym == SDLK_ESCAPE)
eligottlieb@4803
   144
					should_exit = 1;
eligottlieb@4803
   145
			}
eligottlieb@4803
   146
			if(button_down && event.type == SDL_KEYUP) {
eligottlieb@4803
   147
				button_down = 0;
eligottlieb@4800
   148
				current_picture += 1;
eligottlieb@4800
   149
				if(current_picture >= num_pictures)
eligottlieb@4800
   150
					current_picture = 0;
eligottlieb@4808
   151
				SDL_QueryTexture(pictures[current_picture].texture,&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4800
   152
				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4808
   153
				SDL_SetWindowShape(window,pictures[current_picture].surface,&mode);
eligottlieb@4800
   154
			}
eligottlieb@4800
   155
			if(event.type == SDL_QUIT)
eligottlieb@4800
   156
				should_exit = 1;
eligottlieb@4800
   157
			event_pending = 0;
eligottlieb@4799
   158
		}
eligottlieb@4808
   159
		render(window,pictures[current_picture].texture,texture_dimensions);
eligottlieb@4805
   160
		SDL_Delay(time_left());
eligottlieb@4805
   161
		next_time += TICK_INTERVAL;
eligottlieb@4799
   162
	}
eligottlieb@4799
   163
	
eligottlieb@4799
   164
	//Free the textures.
eligottlieb@4799
   165
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
   166
		SDL_DestroyTexture(pictures[i].texture);
eligottlieb@4799
   167
	//Destroy the window.
eligottlieb@4799
   168
	SDL_DestroyWindow(window);
eligottlieb@4799
   169
	//Free the original surfaces backing the textures.
eligottlieb@4799
   170
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
   171
		SDL_FreeSurface(pictures[i].surface);
eligottlieb@4799
   172
	free(pictures);
eligottlieb@4799
   173
	//Call SDL_VideoQuit() before quitting.
eligottlieb@4799
   174
	SDL_VideoQuit();
eligottlieb@4799
   175
}