test/testshape.c
author Eli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 22:17:52 -0400
changeset 4800 6d4be626225f
parent 4799 a0e096916474
child 4802 f14a8c05f5bb
permissions -rw-r--r--
Same place as before, but optimizing a bit to try to isolate the spot in the program that locks things up.
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@4799
     8
eligottlieb@4799
     9
#define SHAPED_WINDOW_X 150
eligottlieb@4799
    10
#define SHAPED_WINDOW_Y 150
eligottlieb@4799
    11
#define SHAPED_WINDOW_DIMENSION 640
eligottlieb@4799
    12
eligottlieb@4799
    13
int main(int argc,char** argv) {
eligottlieb@4799
    14
        if(argc < 2) {
eligottlieb@4799
    15
        	printf("SDL_Shape requires at least one bitmap file as argument.\n");
eligottlieb@4799
    16
        	exit(-1);
eligottlieb@4799
    17
        }
eligottlieb@4799
    18
	
eligottlieb@4799
    19
	if(SDL_VideoInit(NULL,0) == -1) {
eligottlieb@4799
    20
		printf("Could not initialize SDL video.\n");
eligottlieb@4799
    21
		exit(-2);
eligottlieb@4799
    22
	}
eligottlieb@4799
    23
	
eligottlieb@4799
    24
	Uint8 num_pictures = argc - 1;
eligottlieb@4799
    25
	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
eligottlieb@4799
    26
	int i = 0;
eligottlieb@4799
    27
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
    28
		pictures[i] = NULL;
eligottlieb@4799
    29
	for(i=0;i<num_pictures;i++) {
eligottlieb@4799
    30
		SDL_Surface *original = SDL_LoadBMP(argv[i+1]);
eligottlieb@4799
    31
		if(original == NULL) {
eligottlieb@4799
    32
			int j = 0;
eligottlieb@4799
    33
			for(j=0;j<num_pictures;j++)
eligottlieb@4799
    34
				if(pictures[j] != NULL)
eligottlieb@4799
    35
					SDL_FreeSurface(pictures[j]);
eligottlieb@4799
    36
			free(pictures);
eligottlieb@4799
    37
			SDL_VideoQuit();
eligottlieb@4799
    38
			printf("Could not load surface from named bitmap file.\n");
eligottlieb@4799
    39
			exit(-3);
eligottlieb@4799
    40
		}
eligottlieb@4799
    41
		//THIS CONVERSION ROUTINE IS FRAGILE!  It relies in the fact that only certain portions of the format structure must be filled in to use it.
eligottlieb@4799
    42
		SDL_PixelFormat format = {NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
eligottlieb@4800
    43
		int bpp = 0;
eligottlieb@4800
    44
		SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_RGBA8888,&bpp,&format.Rmask,&format.Gmask,&format.Bmask,&format.Amask);
eligottlieb@4800
    45
		format.BitsPerPixel = bpp;
eligottlieb@4799
    46
		format.BytesPerPixel = format.BitsPerPixel / 8 + (format.BitsPerPixel % 8 > 0 ? 1 : 0);
eligottlieb@4799
    47
		pictures[i] = SDL_ConvertSurface(original,&format,0);
eligottlieb@4799
    48
		//We have no more need of the original now that we have our desired format.
eligottlieb@4799
    49
		SDL_FreeSurface(original);
eligottlieb@4799
    50
		if(pictures[i] == NULL) {
eligottlieb@4799
    51
			int j = 0;
eligottlieb@4799
    52
			for(j=0;j<num_pictures;j++)
eligottlieb@4799
    53
				if(pictures[j] != NULL)
eligottlieb@4799
    54
					SDL_FreeSurface(pictures[j]);
eligottlieb@4799
    55
			free(pictures);
eligottlieb@4799
    56
			SDL_VideoQuit();
eligottlieb@4799
    57
			printf("Could not convert bitmap surface to desired format.\n");
eligottlieb@4799
    58
			exit(-3);
eligottlieb@4799
    59
		}
eligottlieb@4799
    60
		
eligottlieb@4799
    61
		if(SDL_MUSTLOCK(pictures[i]))
eligottlieb@4799
    62
			SDL_LockSurface(pictures[i]);
eligottlieb@4799
    63
			
eligottlieb@4799
    64
		void* pixels = pictures[i]->pixels;
eligottlieb@4799
    65
		unsigned int pitch = pictures[i]->pitch;
eligottlieb@4799
    66
		int y =0,x = 0;
eligottlieb@4799
    67
		for(y=0;y<pictures[i]->h;y++)
eligottlieb@4799
    68
			for(x=0;x<pictures[i]->w;x++) {
eligottlieb@4800
    69
				Uint32* pixel = pixels + y * pitch + x * pictures[i]->format->BytesPerPixel;
eligottlieb@4799
    70
				Uint8 r = 0,g = 0,b = 0;
eligottlieb@4799
    71
				SDL_GetRGB(*pixel,pictures[i]->format,&r,&g,&b);
eligottlieb@4800
    72
				//if(r == g == b == 0xff)
eligottlieb@4800
    73
				//	*pixel = SDL_MapRGBA(pictures[i]->format,r,g,b,0);
eligottlieb@4799
    74
			}
eligottlieb@4799
    75
			
eligottlieb@4799
    76
		if(SDL_MUSTLOCK(pictures[i]))
eligottlieb@4799
    77
			SDL_UnlockSurface(pictures[i]);
eligottlieb@4799
    78
	}
eligottlieb@4799
    79
	
eligottlieb@4799
    80
	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
    81
	if(window == NULL) {
eligottlieb@4799
    82
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    83
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    84
		free(pictures);
eligottlieb@4799
    85
		SDL_VideoQuit();
eligottlieb@4799
    86
		printf("Could not create shaped window for SDL_Shape.\n");
eligottlieb@4799
    87
		exit(-4);
eligottlieb@4799
    88
	}
eligottlieb@4799
    89
	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
eligottlieb@4799
    90
		SDL_DestroyWindow(window);
eligottlieb@4799
    91
		for(i=0;i<num_pictures;i++)
eligottlieb@4799
    92
			SDL_FreeSurface(pictures[i]);
eligottlieb@4799
    93
		free(pictures);
eligottlieb@4799
    94
		SDL_VideoQuit();
eligottlieb@4799
    95
		printf("Could not create rendering context for SDL_Shape window.\n");
eligottlieb@4799
    96
		exit(-5);
eligottlieb@4799
    97
	}
eligottlieb@4799
    98
	
eligottlieb@4799
    99
	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
eligottlieb@4799
   100
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   101
		textures[i] = NULL;
eligottlieb@4799
   102
	for(i=0;i<num_pictures;i++) {
eligottlieb@4799
   103
		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
eligottlieb@4799
   104
		if(textures[i] == NULL) {
eligottlieb@4799
   105
			int j = 0;
eligottlieb@4799
   106
			for(j=0;j<num_pictures;i++)
eligottlieb@4799
   107
				if(textures[i] != NULL)
eligottlieb@4799
   108
					SDL_DestroyTexture(textures[i]);
eligottlieb@4799
   109
			free(textures);
eligottlieb@4799
   110
			for(i=0;i<num_pictures;i++)
eligottlieb@4799
   111
				SDL_FreeSurface(pictures[i]);
eligottlieb@4799
   112
			free(pictures);
eligottlieb@4799
   113
			SDL_DestroyRenderer(window);
eligottlieb@4799
   114
			SDL_DestroyWindow(window);
eligottlieb@4799
   115
			SDL_VideoQuit();
eligottlieb@4799
   116
			printf("Could not create texture for SDL_shape.\n");
eligottlieb@4799
   117
			exit(-6);
eligottlieb@4799
   118
		}
eligottlieb@4799
   119
	}
eligottlieb@4799
   120
	
eligottlieb@4799
   121
	SDL_Event event;
eligottlieb@4800
   122
	int event_pending = 0,should_exit = 0;
eligottlieb@4799
   123
	event_pending = SDL_PollEvent(&event);
eligottlieb@4799
   124
	unsigned int current_picture = 0;
eligottlieb@4799
   125
	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
eligottlieb@4799
   126
	SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4799
   127
	int mouse_down = 0;
eligottlieb@4799
   128
	Uint32 format,access;
eligottlieb@4799
   129
	SDL_Rect texture_dimensions = {0,0,0,0};
eligottlieb@4799
   130
	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4799
   131
	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4800
   132
	while(should_exit == 0) {
eligottlieb@4800
   133
		event_pending = SDL_PollEvent(&event);
eligottlieb@4800
   134
		if(event_pending == 1) {
eligottlieb@4800
   135
			if(event.type == SDL_MOUSEBUTTONDOWN)
eligottlieb@4800
   136
				mouse_down = 1;
eligottlieb@4800
   137
			if(mouse_down && event.type == SDL_MOUSEBUTTONUP) {
eligottlieb@4800
   138
				mouse_down = 0;
eligottlieb@4800
   139
				current_picture += 1;
eligottlieb@4800
   140
				if(current_picture >= num_pictures)
eligottlieb@4800
   141
					current_picture = 0;
eligottlieb@4800
   142
				SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
eligottlieb@4800
   143
				SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
eligottlieb@4800
   144
				SDL_SetWindowShape(window,pictures[current_picture],&mode);
eligottlieb@4800
   145
			}
eligottlieb@4800
   146
			if(event.type == SDL_QUIT)
eligottlieb@4800
   147
				should_exit = 1;
eligottlieb@4800
   148
			event_pending = 0;
eligottlieb@4799
   149
		}
eligottlieb@4799
   150
		
eligottlieb@4799
   151
		SDL_SelectRenderer(window);
eligottlieb@4799
   152
	
eligottlieb@4799
   153
		//Clear render-target to blue.
eligottlieb@4799
   154
		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
eligottlieb@4799
   155
		SDL_RenderClear();
eligottlieb@4799
   156
		
eligottlieb@4799
   157
		//Render the texture.
eligottlieb@4799
   158
		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
eligottlieb@4799
   159
		
eligottlieb@4799
   160
		SDL_RenderPresent();
eligottlieb@4799
   161
	}
eligottlieb@4799
   162
	
eligottlieb@4799
   163
	//Free the textures.
eligottlieb@4799
   164
	for(i=0;i<num_pictures;i++)
eligottlieb@4799
   165
		SDL_DestroyTexture(textures[i]);
eligottlieb@4799
   166
	free(textures);
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@4799
   171
		SDL_FreeSurface(pictures[i]);
eligottlieb@4799
   172
	free(pictures);
eligottlieb@4799
   173
	//Call SDL_VideoQuit() before quitting.
eligottlieb@4799
   174
	SDL_VideoQuit();
eligottlieb@4799
   175
}