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