test/testshape.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 12:19:46 -0800
changeset 5142 c8e049de174c
parent 5123 dc0dfdd58f27
child 5147 ad50b3db78bd
permissions -rw-r--r--
Making the API simpler, the renderer present semantics are always having a backbuffer and then discarding it. This is best for hardware accelerated rendering.
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
egottlieb@4849
    11
#define TICK_INTERVAL 1000/10
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() {
egottlieb@4849
    35
    Uint32 now = SDL_GetTicks();
egottlieb@4849
    36
    if(next_time <= now)
egottlieb@4849
    37
        return 0;
eligottlieb@4805
    38
	else
egottlieb@4849
    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@4817
    46
	SDL_PixelFormat* format = NULL;
eligottlieb@4816
    47
	SDL_Window *window;
eligottlieb@4816
    48
	SDL_Color black = {0,0,0,0xff};
eligottlieb@4816
    49
	SDL_Event event;
eligottlieb@4816
    50
	int event_pending = 0;
eligottlieb@4816
    51
	int should_exit = 0;
eligottlieb@4816
    52
	unsigned int current_picture;
eligottlieb@4816
    53
	int button_down;
eligottlieb@4817
    54
	Uint32 pixelFormat = 0;
eligottlieb@4817
    55
	int access = 0;
eligottlieb@4816
    56
	SDL_Rect texture_dimensions;;
eligottlieb@4816
    57
eligottlieb@4816
    58
	if(argc < 2) {
slouken@5032
    59
    	printf("SDL_Shape requires at least one bitmap file as argument.\n");
slouken@5032
    60
    	exit(-1);
slouken@5032
    61
    }
eligottlieb@4799
    62
	
slouken@5123
    63
	if(SDL_VideoInit(NULL) == -1) {
eligottlieb@4799
    64
		printf("Could not initialize SDL video.\n");
eligottlieb@4799
    65
		exit(-2);
eligottlieb@4799
    66
	}
eligottlieb@4799
    67
	
eligottlieb@4816
    68
	num_pictures = argc - 1;
eligottlieb@4816
    69
	pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
eligottlieb@4799
    70
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
    71
		pictures[i].surface = NULL;
eligottlieb@4799
    72
	for(i=0;i<num_pictures;i++) {
eligottlieb@4808
    73
		pictures[i].surface = SDL_LoadBMP(argv[i+1]);
eligottlieb@4808
    74
		if(pictures[i].surface == NULL) {
eligottlieb@4816
    75
			j = 0;
eligottlieb@4799
    76
			for(j=0;j<num_pictures;j++)
eligottlieb@4808
    77
				if(pictures[j].surface != NULL)
eligottlieb@4808
    78
					SDL_FreeSurface(pictures[j].surface);
eligottlieb@4799
    79
			free(pictures);
eligottlieb@4799
    80
			SDL_VideoQuit();
eligottlieb@4799
    81
			printf("Could not load surface from named bitmap file.\n");
eligottlieb@4799
    82
			exit(-3);
eligottlieb@4799
    83
		}
eligottlieb@4816
    84
eligottlieb@4816
    85
		format = pictures[i].surface->format;
eligottlieb@4817
    86
		if(format->Amask != 0) {
eligottlieb@4808
    87
			pictures[i].mode.mode = ShapeModeBinarizeAlpha;
egottlieb@4857
    88
			pictures[i].mode.parameters.binarizationCutoff = 255;
eligottlieb@4808
    89
		}
eligottlieb@4808
    90
		else {
eligottlieb@4808
    91
			pictures[i].mode.mode = ShapeModeColorKey;
eligottlieb@4808
    92
			pictures[i].mode.parameters.colorKey = black;
eligottlieb@4808
    93
		}
eligottlieb@4799
    94
	}
eligottlieb@4799
    95
	
eligottlieb@4816
    96
	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
    97
	if(window == NULL) {
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 shaped window for SDL_Shape.\n");
eligottlieb@4799
   103
		exit(-4);
eligottlieb@4799
   104
	}
slouken@5142
   105
	if(SDL_CreateRenderer(window,-1,0) == -1) {
eligottlieb@4799
   106
		SDL_DestroyWindow(window);
eligottlieb@4799
   107
		for(i=0;i<num_pictures;i++)
eligottlieb@4808
   108
			SDL_FreeSurface(pictures[i].surface);
eligottlieb@4799
   109
		free(pictures);
eligottlieb@4799
   110
		SDL_VideoQuit();
eligottlieb@4799
   111
		printf("Could not create rendering context for SDL_Shape window.\n");
eligottlieb@4799
   112
		exit(-5);
eligottlieb@4799
   113
	}
eligottlieb@4799
   114
	
eligottlieb@4799
   115
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
   116
		pictures[i].texture = NULL;
eligottlieb@4799
   117
	for(i=0;i<num_pictures;i++) {
eligottlieb@4808
   118
		pictures[i].texture = SDL_CreateTextureFromSurface(0,pictures[i].surface);
eligottlieb@4808
   119
		if(pictures[i].texture == NULL) {
eligottlieb@4816
   120
			j = 0;
eligottlieb@4799
   121
			for(j=0;j<num_pictures;i++)
eligottlieb@4808
   122
				if(pictures[i].texture != NULL)
eligottlieb@4808
   123
					SDL_DestroyTexture(pictures[i].texture);
eligottlieb@4799
   124
			for(i=0;i<num_pictures;i++)
eligottlieb@4808
   125
				SDL_FreeSurface(pictures[i].surface);
eligottlieb@4799
   126
			free(pictures);
eligottlieb@4799
   127
			SDL_DestroyRenderer(window);
eligottlieb@4799
   128
			SDL_DestroyWindow(window);
eligottlieb@4799
   129
			SDL_VideoQuit();
eligottlieb@4799
   130
			printf("Could not create texture for SDL_shape.\n");
eligottlieb@4799
   131
			exit(-6);
eligottlieb@4799
   132
		}
eligottlieb@4799
   133
	}
eligottlieb@4799
   134
	
eligottlieb@4816
   135
	event_pending = 0;
eligottlieb@4816
   136
	should_exit = 0;
eligottlieb@4799
   137
	event_pending = SDL_PollEvent(&event);
eligottlieb@4816
   138
	current_picture = 0;
eligottlieb@4816
   139
	button_down = 0;
eligottlieb@4816
   140
	texture_dimensions.h = 0;
eligottlieb@4816
   141
	texture_dimensions.w = 0;
eligottlieb@4816
   142
	texture_dimensions.x = 0;
eligottlieb@4816
   143
	texture_dimensions.y = 0;
eligottlieb@4816
   144
	SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4799
   145
	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4816
   146
	SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
eligottlieb@4805
   147
	next_time = SDL_GetTicks() + TICK_INTERVAL;
eligottlieb@4800
   148
	while(should_exit == 0) {
eligottlieb@4800
   149
		event_pending = SDL_PollEvent(&event);
eligottlieb@4800
   150
		if(event_pending == 1) {
eligottlieb@4803
   151
			if(event.type == SDL_KEYDOWN) {
eligottlieb@4803
   152
				button_down = 1;
eligottlieb@4803
   153
				if(event.key.keysym.sym == SDLK_ESCAPE)
eligottlieb@4803
   154
					should_exit = 1;
eligottlieb@4803
   155
			}
eligottlieb@4803
   156
			if(button_down && event.type == SDL_KEYUP) {
eligottlieb@4803
   157
				button_down = 0;
eligottlieb@4800
   158
				current_picture += 1;
eligottlieb@4800
   159
				if(current_picture >= num_pictures)
eligottlieb@4800
   160
					current_picture = 0;
eligottlieb@4816
   161
				SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4800
   162
				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4816
   163
				SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
eligottlieb@4800
   164
			}
eligottlieb@4800
   165
			if(event.type == SDL_QUIT)
eligottlieb@4800
   166
				should_exit = 1;
eligottlieb@4800
   167
			event_pending = 0;
eligottlieb@4799
   168
		}
eligottlieb@4808
   169
		render(window,pictures[current_picture].texture,texture_dimensions);
eligottlieb@4805
   170
		SDL_Delay(time_left());
eligottlieb@4805
   171
		next_time += TICK_INTERVAL;
eligottlieb@4799
   172
	}
eligottlieb@4799
   173
	
eligottlieb@4799
   174
	//Free the textures.
eligottlieb@4799
   175
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
   176
		SDL_DestroyTexture(pictures[i].texture);
eligottlieb@4799
   177
	//Destroy the window.
eligottlieb@4799
   178
	SDL_DestroyWindow(window);
eligottlieb@4799
   179
	//Free the original surfaces backing the textures.
eligottlieb@4799
   180
	for(i=0;i<num_pictures;i++)
eligottlieb@4808
   181
		SDL_FreeSurface(pictures[i].surface);
eligottlieb@4799
   182
	free(pictures);
eligottlieb@4799
   183
	//Call SDL_VideoQuit() before quitting.
eligottlieb@4799
   184
	SDL_VideoQuit();
eligottlieb@4816
   185
eligottlieb@4816
   186
	return 0;
eligottlieb@4799
   187
}