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