Rewrote test program for shaped windows. It definitely displays recognizable pictures now, but the resizing and shaping functionality isn't behaving correctly, possibly due to a miscalculation of alpha values.
authorEli Gottlieb <eligottlieb@gmail.com>
Sun, 18 Jul 2010 21:31:22 -0400
changeset 4799a0e096916474
parent 4798 980614a70cfc
child 4800 6d4be626225f
Rewrote test program for shaped windows. It definitely displays recognizable pictures now, but the resizing and shaping functionality isn't behaving correctly, possibly due to a miscalculation of alpha values.
src/video/SDL_shape.c
test/Makefile.in
test/README
test/testeyes.c
test/testeyes_bitmap.h
test/testeyes_mask_bitmap.h
test/testshape.c
     1.1 --- a/src/video/SDL_shape.c	Sun Jul 18 15:15:05 2010 -0400
     1.2 +++ b/src/video/SDL_shape.c	Sun Jul 18 21:31:22 2010 -0400
     1.3 @@ -63,14 +63,17 @@
     1.4  	Uint32 bitmap_pixel;
     1.5  	if(SDL_MUSTLOCK(shape))
     1.6  		SDL_LockSurface(shape);
     1.7 -	for(x = 0;x<shape->w;x++)
     1.8 -		for(y = 0;y<shape->h;y++) {
     1.9 -			pixel = (Uint8 *)(shape->pixels) + (y*shape->pitch) + (x*shape->format->BytesPerPixel);
    1.10 +	pixel = (Uint8*)shape->pixels;
    1.11 +	for(y = 0;y<shape->h;y++) {
    1.12 +		pixel = (Uint8 *)(shape->pixels) + y * shape->pitch;
    1.13 +		for(x=0;x<shape->w;x++) {
    1.14  			alpha = 0;
    1.15  			SDL_GetRGBA(*(Uint32*)pixel,shape->format,&r,&g,&b,&alpha);
    1.16  			bitmap_pixel = y*shape->w + x;
    1.17  			bitmap[bitmap_pixel / ppb] |= (alpha >= alphacutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
    1.18 +			pixel += shape->format->BytesPerPixel;
    1.19  		}
    1.20 +	}
    1.21  	if(SDL_MUSTLOCK(shape))
    1.22  		SDL_UnlockSurface(shape);
    1.23  }
     2.1 --- a/test/Makefile.in	Sun Jul 18 15:15:05 2010 -0400
     2.2 +++ b/test/Makefile.in	Sun Jul 18 21:31:22 2010 -0400
     2.3 @@ -7,7 +7,7 @@
     2.4  CFLAGS  = @CFLAGS@
     2.5  LIBS	= @LIBS@
     2.6  
     2.7 -TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testeyes$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmmousetablet$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
     2.8 +TARGETS = checkkeys$(EXE) graywin$(EXE) loopwave$(EXE) testalpha$(EXE) testatomic$(EXE) testaudioinfo$(EXE) testbitmap$(EXE) testblitspeed$(EXE) testcursor$(EXE) testdraw2$(EXE) testdyngles$(EXE) testdyngl$(EXE) testerror$(EXE) testfile$(EXE) testfill$(EXE) testgamma$(EXE) testgl2$(EXE) testgles$(EXE) testgl$(EXE) testhaptic$(EXE) testhread$(EXE) testiconv$(EXE) testime$(EXE) testintersections$(EXE) testjoystick$(EXE) testkeys$(EXE) testloadso$(EXE) testlock$(EXE) testmmousetablet$(EXE) testmultiaudio$(EXE) testoverlay2$(EXE) testoverlay$(EXE) testpalette$(EXE) testplatform$(EXE) testpower$(EXE) testresample$(EXE) testsem$(EXE) testshape$(EXE) testsprite2$(EXE) testsprite$(EXE) testspriteminimal$(EXE) testtimer$(EXE) testver$(EXE) testvidinfo$(EXE) testwin$(EXE) testwm2$(EXE) testwm$(EXE) threadwin$(EXE) torturethread$(EXE)
     2.9  
    2.10  all: Makefile $(TARGETS)
    2.11  
    2.12 @@ -62,9 +62,6 @@
    2.13  testerror$(EXE): $(srcdir)/testerror.c
    2.14  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.15  
    2.16 -testeyes$(EXE): $(srcdir)/testeyes.c
    2.17 -	$(CC) -o $@ $? -std=c99 $(CFLAGS) $(LIBS)
    2.18 -
    2.19  testfile$(EXE): $(srcdir)/testfile.c
    2.20  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.21  
    2.22 @@ -113,6 +110,9 @@
    2.23  testsem$(EXE): $(srcdir)/testsem.c
    2.24  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    2.25  
    2.26 +testshape$(EXE): $(srcdir)/testshape.c
    2.27 +	$(CC) -o $@ $? -std=c99 $(CFLAGS) $(LIBS)
    2.28 +
    2.29  testspriteminimal$(EXE): $(srcdir)/testspriteminimal.c
    2.30  	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @MATHLIB@
    2.31  
     3.1 --- a/test/README	Sun Jul 18 15:15:05 2010 -0400
     3.2 +++ b/test/README	Sun Jul 18 21:31:22 2010 -0400
     3.3 @@ -12,7 +12,6 @@
     3.4  	testcursor	Tests custom mouse cursor
     3.5  	testdyngl	Tests dynamically loading OpenGL library
     3.6  	testerror	Tests multi-threaded error handling
     3.7 -	testeyes	Tests shaped windows
     3.8  	testfile	Tests RWops layer
     3.9  	testgamma	Tests video device gamma ramp
    3.10  	testgl		A very simple example of using OpenGL with SDL
    3.11 @@ -29,6 +28,7 @@
    3.12  	testpalette	Tests palette color cycling
    3.13  	testplatform	Tests types, endianness and cpu capabilities
    3.14  	testsem		Tests SDL's semaphore implementation
    3.15 +	testshape	Tests shaped windows
    3.16  	testsprite	Example of fast sprite movement on the screen
    3.17  	testsprite2	Improved version of testsprite
    3.18  	testtimer	Test the timer facilities
     4.1 --- a/test/testeyes.c	Sun Jul 18 15:15:05 2010 -0400
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,232 +0,0 @@
     4.4 -#include <stdlib.h>
     4.5 -#include <math.h>
     4.6 -#include <SDL_events.h>
     4.7 -#include <SDL_rect.h>
     4.8 -#include <SDL_pixels.h>
     4.9 -#include <SDL_video.h>
    4.10 -#include <SDL_shape.h>
    4.11 -#include "testeyes_bitmap.h"
    4.12 -#include "testeyes_mask_bitmap.h"
    4.13 -
    4.14 -/* The following code for the calculation of pupil positions has been taken and rewritten from the original xeyes.  The
    4.15 -   copyright notice is included as follows, and the code is now under the same license as the rest of this file. */
    4.16 -/*
    4.17 -
    4.18 -Copyright (c) 1991  X Consortium
    4.19 -
    4.20 -Permission is hereby granted, free of charge, to any person obtaining
    4.21 -a copy of this software and associated documentation files (the
    4.22 -"Software"), to deal in the Software without restriction, including
    4.23 -without limitation the rights to use, copy, modify, merge, publish,
    4.24 -distribute, sublicense, and/or sell copies of the Software, and to
    4.25 -permit persons to whom the Software is furnished to do so, subject to
    4.26 -the following conditions:
    4.27 -
    4.28 -The above copyright notice and this permission notice shall be included
    4.29 -in all copies or substantial portions of the Software.
    4.30 -
    4.31 -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    4.32 -OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    4.33 -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    4.34 -IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
    4.35 -OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
    4.36 -ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    4.37 -OTHER DEALINGS IN THE SOFTWARE.
    4.38 -
    4.39 -Except as contained in this notice, the name of the X Consortium shall
    4.40 -not be used in advertising or otherwise to promote the sale, use or
    4.41 -other dealings in this Software without prior written authorization
    4.42 -from the X Consortium.
    4.43 -
    4.44 -*/
    4.45 -# define NUM_EYES	2
    4.46 -# define EYE_X(n)	((n) * 2.0)
    4.47 -# define EYE_Y(n)	(0.0)
    4.48 -# define EYE_THICK	(0.175)	/* thickness of eye rim */
    4.49 -# define EYE_OFFSET	(0.1)	/* padding between eyes */
    4.50 -# define EYE_WIDTH	(2.0 - (EYE_THICK + EYE_OFFSET) * 2)
    4.51 -# define EYE_HEIGHT	EYE_WIDTH
    4.52 -# define EYE_HWIDTH	(EYE_WIDTH / 2.0)
    4.53 -# define EYE_HHEIGHT	(EYE_HEIGHT / 2.0)
    4.54 -# define BALL_WIDTH	(0.3)
    4.55 -# define BALL_HEIGHT	BALL_WIDTH
    4.56 -# define BALL_PAD	(0.05)
    4.57 -# define BALL_DIST	((EYE_WIDTH - BALL_WIDTH) / 2.0 - BALL_PAD)
    4.58 -
    4.59 -SDL_Point computePupil (int num,SDL_Point mouse) {
    4.60 -	double dx = mouse.x - EYE_X(num);
    4.61 -	double dy = mouse.y - EYE_Y(num);
    4.62 -	double cx = 0.0,cy = 0.0;
    4.63 -	if(dx == 0 && dy == 0) {
    4.64 -		cx = EYE_X(num);
    4.65 -		cy = EYE_Y(num);
    4.66 -	}
    4.67 -	else {
    4.68 -		double angle = atan2((double)dy,(double)dx);
    4.69 -		double cosa = cos(angle);
    4.70 -		double sina = sin(angle);
    4.71 -		double hypotenuse = hypot(EYE_HHEIGHT * cosa, EYE_HWIDTH * sina);
    4.72 -		double dist = BALL_DIST * hypot((EYE_HWIDTH * EYE_HHEIGHT) * cosa / hypotenuse,
    4.73 -					        (EYE_HWIDTH * EYE_HHEIGHT) * sina / hypotenuse);
    4.74 -		if(dist > hypot((double)dx,(double)dy)) {
    4.75 -			cx = dx + EYE_X(num);
    4.76 -			cy = dy + EYE_Y(num);
    4.77 -		}
    4.78 -		else {
    4.79 -			cx = dist * cosa + EYE_X(num);
    4.80 -			cy = dist * sina + EYE_Y(num);
    4.81 -		}
    4.82 -	}
    4.83 -	SDL_Point ret = {cx,cy};
    4.84 -	return ret;
    4.85 -}
    4.86 -
    4.87 -/* Here begins the code exclusively and entirely written by Eli Gottlieb in May 2010. */
    4.88 -typedef struct {
    4.89 -	SDL_Rect left;
    4.90 -	SDL_Rect right;
    4.91 -} Pupil_Pair;
    4.92 -typedef struct {
    4.93 -	SDL_Point left;
    4.94 -	SDL_Point right;
    4.95 -} Pupil_Points;
    4.96 -
    4.97 -Pupil_Pair compute_pupil_positions(SDL_Point target) {
    4.98 -	Pupil_Pair result;
    4.99 -	Pupil_Points points;
   4.100 -	points.left = computePupil(0,target);
   4.101 -	points.right = computePupil(1,target);
   4.102 -	result.left.x = points.left.x - BALL_WIDTH / 2.0;
   4.103 -	result.left.y = points.left.y - BALL_HEIGHT / 2.0;
   4.104 -	result.right.x = points.right.x - BALL_WIDTH / 2.0;
   4.105 -	result.right.y = points.right.y - BALL_HEIGHT / 2.0;
   4.106 -	result.left.w = result.right.w = BALL_WIDTH;
   4.107 -	result.left.h = result.left.w = BALL_HEIGHT;
   4.108 -	return result;
   4.109 -}
   4.110 -
   4.111 -void render_eyes(SDL_Window *window,SDL_Texture *eyes_texture,Pupil_Pair pupils) {
   4.112 -	SDL_SelectRenderer(window);
   4.113 -	
   4.114 -	//Clear render-target to blue.
   4.115 -	SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
   4.116 -	SDL_RenderClear();
   4.117 -	
   4.118 -	//Render the whites of the eyes.
   4.119 -	SDL_Rect srcdestrect = {0,0,eyes_width,eyes_height};
   4.120 -	SDL_RenderCopy(eyes_texture,&srcdestrect,&srcdestrect);
   4.121 -	
   4.122 -	//Render the pupils.
   4.123 -	SDL_SetRenderDrawColor(0x00,0x00,0x00,0xff);
   4.124 -	const SDL_Rect eyes[2] = {pupils.left,pupils.right};
   4.125 -	SDL_RenderFillRects((const SDL_Rect**)&eyes,2);
   4.126 -	
   4.127 -	SDL_RenderPresent();
   4.128 -}
   4.129 -
   4.130 -int main(int argc,char** argv) {
   4.131 -	if(SDL_VideoInit(NULL,0) == -1) {
   4.132 -		printf("Could not initialize SDL video.\n");
   4.133 -		exit(-1);
   4.134 -	}
   4.135 -	
   4.136 -	SDL_Window *window = SDL_CreateShapedWindow("Big Brother is watching you.",eyes_x_hot,eyes_y_hot,eyes_width,eyes_height,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
   4.137 -	if(window == NULL) {
   4.138 -		SDL_VideoQuit();
   4.139 -		printf("Could not create shaped window for eyes.\n");
   4.140 -		exit(-2);
   4.141 -	}
   4.142 -	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
   4.143 -		SDL_DestroyWindow(window);
   4.144 -		SDL_VideoQuit();
   4.145 -		printf("Could not create rendering context for SDL_Eyes window.\n");
   4.146 -		exit(-3);
   4.147 -	}
   4.148 -	
   4.149 -	int bpp = 0;
   4.150 -	Uint32 r = 0,g = 0,b = 0,a = 0;
   4.151 -	SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ARGB4444,&bpp,&r,&g,&b,&a);
   4.152 -	SDL_Surface *eyes = SDL_CreateRGBSurface(0,eyes_width,eyes_height,bpp,r,g,b,a);
   4.153 -	if(eyes == NULL) {
   4.154 -		SDL_DestroyRenderer(window);
   4.155 -		SDL_DestroyWindow(window);
   4.156 -		SDL_VideoQuit();
   4.157 -		printf("Could not create eyes surface.\n");
   4.158 -		exit(-4);
   4.159 -	}
   4.160 -	
   4.161 -	void *pixels = NULL;
   4.162 -	int pitch = 0;
   4.163 -	SDL_Rect rect = {0,0,eyes_width,eyes_height};
   4.164 -	if(SDL_MUSTLOCK(eyes))
   4.165 -		SDL_LockSurface(eyes);
   4.166 -	pixels = eyes->pixels;
   4.167 -	pitch = eyes->pitch;
   4.168 -	for(int y=0;y<eyes_height;y++)
   4.169 -		for(int x=0;x<eyes_width;x++) {
   4.170 -			Uint8 brightness = *(Uint8*)(eyes_bits+(eyes_width/8)*y+(x/8)) & (1 << (7 - x % 8)) ? 255 : 0;
   4.171 -			*(Uint16*)(pixels+pitch*y+x*16/8) = SDL_MapRGBA(eyes->format,brightness,brightness,brightness,255);
   4.172 -		}
   4.173 -	if(SDL_MUSTLOCK(eyes))
   4.174 -		SDL_UnlockSurface(eyes);
   4.175 -	SDL_Texture *eyes_texture = SDL_CreateTextureFromSurface(0,eyes);
   4.176 -	if(eyes_texture == NULL) {
   4.177 -		SDL_FreeSurface(eyes);
   4.178 -		SDL_DestroyRenderer(window);
   4.179 -		SDL_DestroyWindow(window);
   4.180 -		SDL_VideoQuit();
   4.181 -		printf("Could not create eyes texture.\n");
   4.182 -		exit(-5);
   4.183 -	}
   4.184 -
   4.185 -	SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ARGB4444,&bpp,&r,&g,&b,&a);
   4.186 -	SDL_Surface *mask = SDL_CreateRGBSurface(0,eyesmask_width,eyesmask_height,bpp,r,g,b,a);
   4.187 -	if(mask == NULL) {
   4.188 -		SDL_DestroyTexture(eyes_texture);
   4.189 -		SDL_FreeSurface(eyes);
   4.190 -		SDL_DestroyRenderer(window);
   4.191 -		SDL_DestroyWindow(window);
   4.192 -		SDL_VideoQuit();
   4.193 -		printf("Could not create shape mask texture.\n");
   4.194 -		exit(-5);
   4.195 -	}
   4.196 -	
   4.197 -	if(SDL_MUSTLOCK(mask))
   4.198 -		SDL_LockSurface(mask);
   4.199 -	pixels = mask->pixels;
   4.200 -	pitch = mask->pitch;
   4.201 -	for(int y=0;y<eyesmask_height;y++)
   4.202 -		for(int x=0;x<eyesmask_width;x++) {
   4.203 -			Uint8 alpha = *(Uint8*)(eyesmask_bits+(eyesmask_width/8)*y+(x/8)) & (1 << (7 - x % 8)) ? 1 : 0;
   4.204 -			*(Uint16*)(pixels+pitch*y+x*bpp/8) = SDL_MapRGBA(mask->format,0,0,0,alpha);
   4.205 -		}
   4.206 -	if(SDL_MUSTLOCK(mask))
   4.207 -		SDL_UnlockSurface(mask);
   4.208 -	
   4.209 -	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
   4.210 -	SDL_SetWindowShape(window,mask,&mode);
   4.211 -	
   4.212 -	SDL_Event event;
   4.213 -	int event_pending = 0;
   4.214 -	event_pending = SDL_PollEvent(&event);
   4.215 -	SDL_Point mouse_position;
   4.216 -	Pupil_Pair pupil_positions;
   4.217 -	SDL_SelectMouse(0);
   4.218 -	SDL_GetMouseState(&mouse_position.x,&mouse_position.y);
   4.219 -	pupil_positions = compute_pupil_positions(mouse_position);
   4.220 -	while(event.type != SDL_QUIT) {
   4.221 -		if(event.type == SDL_MOUSEMOTION) {
   4.222 -			mouse_position.x = event.motion.x;
   4.223 -			mouse_position.y = event.motion.y;
   4.224 -			pupil_positions = compute_pupil_positions(mouse_position);
   4.225 -		}
   4.226 -		render_eyes(window,eyes_texture,pupil_positions);
   4.227 -		event_pending = SDL_PollEvent(&event);
   4.228 -	}
   4.229 -	
   4.230 -	SDL_FreeSurface(mask);
   4.231 -	SDL_DestroyTexture(eyes_texture);
   4.232 -	SDL_DestroyWindow(window);
   4.233 -	//Call SDL_VideoQuit() before quitting.
   4.234 -	SDL_VideoQuit();
   4.235 -}
     5.1 --- a/test/testeyes_bitmap.h	Sun Jul 18 15:15:05 2010 -0400
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,21 +0,0 @@
     5.4 -#define eyes_width 48
     5.5 -#define eyes_height 32
     5.6 -#define eyes_x_hot 0
     5.7 -#define eyes_y_hot 0
     5.8 -static unsigned char eyes_bits[] = {
     5.9 -   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    5.10 -   0x00, 0x78, 0x00, 0x00, 0x1e, 0x00, 0x00, 0xfe, 0x01, 0x80, 0x7f, 0x00,
    5.11 -   0x00, 0x87, 0x03, 0xc0, 0xe1, 0x00, 0x80, 0x01, 0x06, 0x60, 0x80, 0x01,
    5.12 -   0xc0, 0x00, 0x0c, 0x30, 0x00, 0x03, 0x60, 0x00, 0x18, 0x18, 0x00, 0x06,
    5.13 -   0x30, 0x00, 0x30, 0x0c, 0x00, 0x0c, 0x30, 0x00, 0x30, 0x0c, 0x00, 0x0c,
    5.14 -   0x10, 0x00, 0x20, 0x04, 0x00, 0x08, 0x18, 0x00, 0x60, 0x06, 0x00, 0x18,
    5.15 -   0x18, 0x00, 0x60, 0x06, 0x00, 0x18, 0x18, 0x00, 0x60, 0x06, 0x00, 0x18,
    5.16 -   0x18, 0x00, 0x60, 0x06, 0x00, 0x18, 0x18, 0x00, 0x60, 0x06, 0x00, 0x18,
    5.17 -   0x18, 0x00, 0x60, 0x06, 0x00, 0x18, 0x18, 0x80, 0x63, 0x06, 0x00, 0x18,
    5.18 -   0x18, 0x80, 0x63, 0x06, 0xe0, 0x18, 0x18, 0x80, 0x63, 0x06, 0xe0, 0x18,
    5.19 -   0x18, 0x00, 0x60, 0x06, 0xe0, 0x18, 0x18, 0x00, 0x20, 0x06, 0x00, 0x08,
    5.20 -   0x10, 0x00, 0x30, 0x04, 0x00, 0x0c, 0x10, 0x00, 0x10, 0x04, 0x00, 0x04,
    5.21 -   0x30, 0x00, 0x18, 0x0c, 0x00, 0x06, 0x60, 0x00, 0x08, 0x18, 0x00, 0x02,
    5.22 -   0x40, 0x00, 0x0c, 0x10, 0x00, 0x03, 0xc0, 0x00, 0x06, 0x30, 0x80, 0x01,
    5.23 -   0x80, 0x83, 0x03, 0xe0, 0xe0, 0x00, 0x00, 0xff, 0x01, 0xc0, 0x7f, 0x00,
    5.24 -   0x00, 0xfe, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
     6.1 --- a/test/testeyes_mask_bitmap.h	Sun Jul 18 15:15:05 2010 -0400
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,21 +0,0 @@
     6.4 -#define eyesmask_width 48
     6.5 -#define eyesmask_height 32
     6.6 -#define eyesmask_x_hot 0
     6.7 -#define eyesmask_y_hot 0
     6.8 -static unsigned char eyesmask_bits[] = {
     6.9 -   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    6.10 -   0x00, 0x78, 0x00, 0x00, 0x1e, 0x00, 0x00, 0xfe, 0x01, 0x80, 0x7f, 0x00,
    6.11 -   0x00, 0xff, 0x03, 0xc0, 0xff, 0x00, 0x80, 0xff, 0x07, 0xe0, 0xff, 0x01,
    6.12 -   0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x03, 0xe0, 0xff, 0x1f, 0xf8, 0xff, 0x07,
    6.13 -   0xf0, 0xff, 0x3f, 0xfc, 0xff, 0x0f, 0xf0, 0xff, 0x3f, 0xfc, 0xff, 0x0f,
    6.14 -   0xf0, 0xff, 0x3f, 0xfc, 0xff, 0x0f, 0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f,
    6.15 -   0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f, 0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f,
    6.16 -   0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f, 0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f,
    6.17 -   0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f, 0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f,
    6.18 -   0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f, 0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f,
    6.19 -   0xf8, 0xff, 0x7f, 0xfe, 0xff, 0x1f, 0xf8, 0xff, 0x3f, 0xfe, 0xff, 0x0f,
    6.20 -   0xf0, 0xff, 0x3f, 0xfc, 0xff, 0x0f, 0xf0, 0xff, 0x1f, 0xfc, 0xff, 0x07,
    6.21 -   0xf0, 0xff, 0x1f, 0xfc, 0xff, 0x07, 0xe0, 0xff, 0x0f, 0xf8, 0xff, 0x03,
    6.22 -   0xc0, 0xff, 0x0f, 0xf0, 0xff, 0x03, 0xc0, 0xff, 0x07, 0xf0, 0xff, 0x01,
    6.23 -   0x80, 0xff, 0x03, 0xe0, 0xff, 0x00, 0x00, 0xff, 0x01, 0xc0, 0x7f, 0x00,
    6.24 -   0x00, 0xfe, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/testshape.c	Sun Jul 18 21:31:22 2010 -0400
     7.3 @@ -0,0 +1,168 @@
     7.4 +#include <stdlib.h>
     7.5 +#include <math.h>
     7.6 +#include <SDL_events.h>
     7.7 +#include <SDL_rect.h>
     7.8 +#include <SDL_pixels.h>
     7.9 +#include <SDL_video.h>
    7.10 +#include <SDL_shape.h>
    7.11 +
    7.12 +#define SHAPED_WINDOW_X 150
    7.13 +#define SHAPED_WINDOW_Y 150
    7.14 +#define SHAPED_WINDOW_DIMENSION 640
    7.15 +
    7.16 +int main(int argc,char** argv) {
    7.17 +        if(argc < 2) {
    7.18 +        	printf("SDL_Shape requires at least one bitmap file as argument.\n");
    7.19 +        	exit(-1);
    7.20 +        }
    7.21 +	
    7.22 +	if(SDL_VideoInit(NULL,0) == -1) {
    7.23 +		printf("Could not initialize SDL video.\n");
    7.24 +		exit(-2);
    7.25 +	}
    7.26 +	
    7.27 +	Uint8 num_pictures = argc - 1;
    7.28 +	SDL_Surface **pictures = malloc(sizeof(SDL_Surface*)*num_pictures);
    7.29 +	int i = 0;
    7.30 +	for(i=0;i<num_pictures;i++)
    7.31 +		pictures[i] = NULL;
    7.32 +	for(i=0;i<num_pictures;i++) {
    7.33 +		SDL_Surface *original = SDL_LoadBMP(argv[i+1]);
    7.34 +		if(original == NULL) {
    7.35 +			int j = 0;
    7.36 +			for(j=0;j<num_pictures;j++)
    7.37 +				if(pictures[j] != NULL)
    7.38 +					SDL_FreeSurface(pictures[j]);
    7.39 +			free(pictures);
    7.40 +			SDL_VideoQuit();
    7.41 +			printf("Could not load surface from named bitmap file.\n");
    7.42 +			exit(-3);
    7.43 +		}
    7.44 +		//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.
    7.45 +		SDL_PixelFormat format = {NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    7.46 +		SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_RGBA8888,&format.BitsPerPixel,&format.Rmask,&format.Gmask,&format.Bmask,&format.Amask);
    7.47 +		format.BytesPerPixel = format.BitsPerPixel / 8 + (format.BitsPerPixel % 8 > 0 ? 1 : 0);
    7.48 +		pictures[i] = SDL_ConvertSurface(original,&format,0);
    7.49 +		//We have no more need of the original now that we have our desired format.
    7.50 +		SDL_FreeSurface(original);
    7.51 +		if(pictures[i] == NULL) {
    7.52 +			int j = 0;
    7.53 +			for(j=0;j<num_pictures;j++)
    7.54 +				if(pictures[j] != NULL)
    7.55 +					SDL_FreeSurface(pictures[j]);
    7.56 +			free(pictures);
    7.57 +			SDL_VideoQuit();
    7.58 +			printf("Could not convert bitmap surface to desired format.\n");
    7.59 +			exit(-3);
    7.60 +		}
    7.61 +		
    7.62 +		if(SDL_MUSTLOCK(pictures[i]))
    7.63 +			SDL_LockSurface(pictures[i]);
    7.64 +			
    7.65 +		void* pixels = pictures[i]->pixels;
    7.66 +		unsigned int pitch = pictures[i]->pitch;
    7.67 +		int y =0,x = 0;
    7.68 +		for(y=0;y<pictures[i]->h;y++)
    7.69 +			for(x=0;x<pictures[i]->w;x++) {
    7.70 +				Uint32* pixel = pixels+y*pitch+x*pictures[i]->format->BytesPerPixel;
    7.71 +				Uint8 r = 0,g = 0,b = 0;
    7.72 +				SDL_GetRGB(*pixel,pictures[i]->format,&r,&g,&b);
    7.73 +				if(r == g == b == 0x00)
    7.74 +					*pixel = SDL_MapRGBA(pictures[i]->format,r,g,b,0);
    7.75 +			}
    7.76 +			
    7.77 +		if(SDL_MUSTLOCK(pictures[i]))
    7.78 +			SDL_UnlockSurface(pictures[i]);
    7.79 +	}
    7.80 +	
    7.81 +	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);
    7.82 +	if(window == NULL) {
    7.83 +		for(i=0;i<num_pictures;i++)
    7.84 +			SDL_FreeSurface(pictures[i]);
    7.85 +		free(pictures);
    7.86 +		SDL_VideoQuit();
    7.87 +		printf("Could not create shaped window for SDL_Shape.\n");
    7.88 +		exit(-4);
    7.89 +	}
    7.90 +	if(SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTFLIP2) == -1) {
    7.91 +		SDL_DestroyWindow(window);
    7.92 +		for(i=0;i<num_pictures;i++)
    7.93 +			SDL_FreeSurface(pictures[i]);
    7.94 +		free(pictures);
    7.95 +		SDL_VideoQuit();
    7.96 +		printf("Could not create rendering context for SDL_Shape window.\n");
    7.97 +		exit(-5);
    7.98 +	}
    7.99 +	
   7.100 +	SDL_Texture **textures = malloc(sizeof(SDL_Texture*)*num_pictures);
   7.101 +	for(i=0;i<num_pictures;i++)
   7.102 +		textures[i] = NULL;
   7.103 +	for(i=0;i<num_pictures;i++) {
   7.104 +		textures[i] = SDL_CreateTextureFromSurface(0,pictures[i]);
   7.105 +		if(textures[i] == NULL) {
   7.106 +			int j = 0;
   7.107 +			for(j=0;j<num_pictures;i++)
   7.108 +				if(textures[i] != NULL)
   7.109 +					SDL_DestroyTexture(textures[i]);
   7.110 +			free(textures);
   7.111 +			for(i=0;i<num_pictures;i++)
   7.112 +				SDL_FreeSurface(pictures[i]);
   7.113 +			free(pictures);
   7.114 +			SDL_DestroyRenderer(window);
   7.115 +			SDL_DestroyWindow(window);
   7.116 +			SDL_VideoQuit();
   7.117 +			printf("Could not create texture for SDL_shape.\n");
   7.118 +			exit(-6);
   7.119 +		}
   7.120 +	}
   7.121 +	
   7.122 +	SDL_Event event;
   7.123 +	int event_pending = 0;
   7.124 +	event_pending = SDL_PollEvent(&event);
   7.125 +	unsigned int current_picture = 0;
   7.126 +	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
   7.127 +	SDL_SetWindowShape(window,pictures[current_picture],&mode);
   7.128 +	int mouse_down = 0;
   7.129 +	Uint32 format,access;
   7.130 +	SDL_Rect texture_dimensions = {0,0,0,0};
   7.131 +	SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   7.132 +	SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   7.133 +	while(event.type != SDL_QUIT) {
   7.134 +		if(event.type == SDL_MOUSEBUTTONDOWN)
   7.135 +			mouse_down = 1;
   7.136 +		if(mouse_down && event.type == SDL_MOUSEBUTTONUP) {
   7.137 +			mouse_down = 0;
   7.138 +			current_picture += 1;
   7.139 +			if(current_picture >= num_pictures)
   7.140 +				current_picture = 0;
   7.141 +			SDL_QueryTexture(textures[current_picture],&format,&access,&texture_dimensions.w,&texture_dimensions.h);
   7.142 +			SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   7.143 +			SDL_SetWindowShape(window,pictures[current_picture],&mode);
   7.144 +		}
   7.145 +		
   7.146 +		SDL_SelectRenderer(window);
   7.147 +	
   7.148 +		//Clear render-target to blue.
   7.149 +		SDL_SetRenderDrawColor(0x00,0x00,0xff,0xff);
   7.150 +		SDL_RenderClear();
   7.151 +		
   7.152 +		//Render the texture.
   7.153 +		SDL_RenderCopy(textures[current_picture],&texture_dimensions,&texture_dimensions);
   7.154 +		
   7.155 +		SDL_RenderPresent();
   7.156 +		event_pending = SDL_PollEvent(&event);
   7.157 +	}
   7.158 +	
   7.159 +	//Free the textures.
   7.160 +	for(i=0;i<num_pictures;i++)
   7.161 +		SDL_DestroyTexture(textures[i]);
   7.162 +	free(textures);
   7.163 +	//Destroy the window.
   7.164 +	SDL_DestroyWindow(window);
   7.165 +	//Free the original surfaces backing the textures.
   7.166 +	for(i=0;i<num_pictures;i++)
   7.167 +		SDL_FreeSurface(pictures[i]);
   7.168 +	free(pictures);
   7.169 +	//Call SDL_VideoQuit() before quitting.
   7.170 +	SDL_VideoQuit();
   7.171 +}