Updated test code, updated win32 code a bit (still not complete, but hopefully tonight), and removed the last vestiges of ellipse and polygon drawing support.
authorEli Gottlieb <eligottlieb@gmail.com>
Tue, 06 Jul 2010 22:05:22 -0400
changeset 4785ef8b32ef9793
parent 4784 2878650e0dc9
child 4786 175da36d1342
Updated test code, updated win32 code a bit (still not complete, but hopefully tonight), and removed the last vestiges of ellipse and polygon drawing support.
include/SDL_ellipse.h
include/SDL_poly.h
include/SDL_video.h
src/video/SDL_shape.c
src/video/win32/SDL_win32shape.c
src/video/x11/SDL_x11shape.c
test/testeyes.c
     1.1 --- a/include/SDL_ellipse.h	Fri Jul 02 12:05:47 2010 -0400
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,92 +0,0 @@
     1.4 -/*
     1.5 -    SDL - Simple DirectMedia Layer
     1.6 -    Copyright (C) 2010 Eli Gottlieb
     1.7 -
     1.8 -    This library is free software; you can redistribute it and/or
     1.9 -    modify it under the terms of the GNU Lesser General Public
    1.10 -    License as published by the Free Software Foundation; either
    1.11 -    version 2.1 of the License, or (at your option) any later version.
    1.12 -
    1.13 -    This library is distributed in the hope that it will be useful,
    1.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 -    Lesser General Public License for more details.
    1.17 -
    1.18 -    You should have received a copy of the GNU Lesser General Public
    1.19 -    License along with this library; if not, write to the Free Software
    1.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 -
    1.22 -    Eli Gottlieb
    1.23 -    eligottlieb@gmail.com
    1.24 -*/
    1.25 -
    1.26 -/**
    1.27 - *  \file SDL_ellipse.h
    1.28 - *  
    1.29 - *  Header file for SDL_ellipse definition and management functions.
    1.30 - */
    1.31 -
    1.32 -#ifndef _SDL_ellipse_h
    1.33 -#define _SDL_ellipse_h
    1.34 -
    1.35 -#include "SDL_stdinc.h"
    1.36 -#include "SDL_error.h"
    1.37 -#include "SDL_pixels.h"
    1.38 -#include "SDL_rwops.h"
    1.39 -
    1.40 -#include "begin_code.h"
    1.41 -/* Set up for C function definitions, even when using C++ */
    1.42 -#ifdef __cplusplus
    1.43 -/* *INDENT-OFF* */
    1.44 -extern "C" {
    1.45 -/* *INDENT-ON* */
    1.46 -#endif
    1.47 -
    1.48 -/**
    1.49 - *  \brief  The structure that defines an ellipse.
    1.50 - *
    1.51 - *  \sa SDL_EllipseEmpty
    1.52 - *  \sa SDL_EllipseEquals
    1.53 - *  \sa SDL_EllipsesIntersect
    1.54 - *  \sa SDL_IntersectEllipseAndLine
    1.55 - */
    1.56 -typedef struct SDL_Ellipse {
    1.57 -	int x,y;
    1.58 -	int a,b;
    1.59 -	int r;
    1.60 -} SDL_Ellipse;
    1.61 -
    1.62 -/**
    1.63 - *  \brief Returns true if the ellipse has no area.
    1.64 - */
    1.65 -#define SDL_EllipseEmpty(X)    ((X)->r <= 0)
    1.66 -
    1.67 -/**
    1.68 - *  \brief Returns true if the two ellipses are equal.
    1.69 - */
    1.70 -#define SDL_EllipseEquals(A, B)   (((A)->x == (B)->x) && ((A)->y == (B)->y) && \
    1.71 -                                ((A)->a == (B)->a) && ((A)->b == (B)->b) && ((A)->r == (B)->r))
    1.72 -
    1.73 -/**
    1.74 - *  \brief Determine whether two ellipses intersect.
    1.75 - *  
    1.76 - *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
    1.77 - */
    1.78 -extern DECLSPEC SDL_bool SDLCALL SDL_EllipsesIntersect(const SDL_Ellipse * A,const SDL_Ellipse * B);
    1.79 -
    1.80 -/**
    1.81 - *  \brief Calculate the intersection of an ellipse and line segment.
    1.82 - *  
    1.83 - *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
    1.84 - */
    1.85 -extern DECLSPEC SDL_bool SDLCALL SDL_IntersectEllipseAndLine(const SDL_Ellipse *ellipse,int *X1,int *Y1,int *X2,int *Y2);
    1.86 -
    1.87 -/* Ends C function definitions when using C++ */
    1.88 -#ifdef __cplusplus
    1.89 -/* *INDENT-OFF* */
    1.90 -}
    1.91 -/* *INDENT-ON* */
    1.92 -#endif
    1.93 -#include "close_code.h"
    1.94 -
    1.95 -#endif /* _SDL_ellipse_h */
     2.1 --- a/include/SDL_poly.h	Fri Jul 02 12:05:47 2010 -0400
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,110 +0,0 @@
     2.4 -/*
     2.5 -    SDL - Simple DirectMedia Layer
     2.6 -    Copyright (C) 2010 Eli Gottlieb
     2.7 -
     2.8 -    This library is free software; you can redistribute it and/or
     2.9 -    modify it under the terms of the GNU Lesser General Public
    2.10 -    License as published by the Free Software Foundation; either
    2.11 -    version 2.1 of the License, or (at your option) any later version.
    2.12 -
    2.13 -    This library is distributed in the hope that it will be useful,
    2.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 -    Lesser General Public License for more details.
    2.17 -
    2.18 -    You should have received a copy of the GNU Lesser General Public
    2.19 -    License along with this library; if not, write to the Free Software
    2.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 -
    2.22 -    Eli Gottlieb
    2.23 -    eligottlieb@gmail.com
    2.24 -*/
    2.25 -
    2.26 -/**
    2.27 - *  \file SDL_poly.h
    2.28 - *  
    2.29 - *  Header file for SDL_poly definition and management functions.
    2.30 - */
    2.31 -
    2.32 -#ifndef _SDL_poly_h
    2.33 -#define _SDL_poly_h
    2.34 -
    2.35 -#include "SDL_stdinc.h"
    2.36 -#include "SDL_error.h"
    2.37 -#include "SDL_pixels.h"
    2.38 -#include "SDL_rwops.h"
    2.39 -#include "SDL_rect.h"
    2.40 -
    2.41 -#include "begin_code.h"
    2.42 -/* Set up for C function definitions, even when using C++ */
    2.43 -#ifdef __cplusplus
    2.44 -/* *INDENT-OFF* */
    2.45 -extern "C" {
    2.46 -/* *INDENT-ON* */
    2.47 -#endif
    2.48 -
    2.49 -/**
    2.50 - *  \brief  The structure that defines an polygon.
    2.51 - *
    2.52 - *  \sa SDL_PolyEmpty
    2.53 - *  \sa SDL_PolyEquals
    2.54 - *  \sa SDL_PolysIntersect
    2.55 - *  \sa SDL_IntersectPoly
    2.56 - *  \sa SDL_WrapPoints
    2.57 - *  \sa SDL_IntersectPolyAndLine
    2.58 - */
    2.59 -typedef struct SDL_Poly {
    2.60 -	SDL_Point *vertices;
    2.61 -	int count;
    2.62 -} SDL_Poly;
    2.63 -
    2.64 -/**
    2.65 - *  \brief Returns true if the polygon has no area.
    2.66 - */
    2.67 -#define SDL_PolyEmpty(X)    (((X)->vertices == NULL) || ((X)->count <= 2))
    2.68 -
    2.69 -/**
    2.70 - *  \brief Determine whether two polygons are equal.
    2.71 - *  
    2.72 - *  \return SDL_TRUE if the polygons are equal, SDL_FALSE otherwise.
    2.73 - */
    2.74 -extern DECLSPEC SDL_bool SDLCALL SDL_PolyEquals(const SDL_Poly *A,const SDL_Poly *B);
    2.75 -
    2.76 -/**
    2.77 - *  \brief Determine whether two rectangles intersect.
    2.78 - *  
    2.79 - *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
    2.80 - */
    2.81 -extern DECLSPEC SDL_bool SDLCALL SDL_PolysIntersect(const SDL_Poly * A,const SDL_Poly * B);
    2.82 -
    2.83 -/**
    2.84 - *  \brief Calculate the intersection of two rectangles.
    2.85 - *  
    2.86 - *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
    2.87 - */
    2.88 -extern DECLSPEC SDL_bool SDLCALL SDL_IntersectPoly(const SDL_Poly * A,const SDL_Poly * B,SDL_Poly * result);
    2.89 -
    2.90 -/**
    2.91 - *  \brief Calculate a minimal polygon wrapping a set of points
    2.92 - *
    2.93 - *  \return 0 on success, -1 if the parameters were invalid, and -2 if an insufficient number of points were supplied
    2.94 - *          in the output polygon.
    2.95 - */
    2.96 -extern DECLSPEC int SDLCALL SDL_WrapPoints(const SDL_Point * points,int count,SDL_Poly *result);
    2.97 -
    2.98 -/**
    2.99 - *  \brief Calculate the intersection of a polygon and line segment.
   2.100 - *  
   2.101 - *  \return SDL_TRUE if there is an intersection, SDL_FALSE otherwise.
   2.102 - */
   2.103 -extern DECLSPEC SDL_bool SDLCALL SDL_IntersectPolyAndLine(const SDL_Poly *poly,int *X1,int *Y1,int *X2,int *Y2);
   2.104 -                                                   
   2.105 -/* Ends C function definitions when using C++ */
   2.106 -#ifdef __cplusplus
   2.107 -/* *INDENT-OFF* */
   2.108 -}
   2.109 -/* *INDENT-ON* */
   2.110 -#endif
   2.111 -#include "close_code.h"
   2.112 -
   2.113 -#endif /* _SDL_poly_h */
     3.1 --- a/include/SDL_video.h	Fri Jul 02 12:05:47 2010 -0400
     3.2 +++ b/include/SDL_video.h	Tue Jul 06 22:05:22 2010 -0400
     3.3 @@ -32,8 +32,6 @@
     3.4  #include "SDL_stdinc.h"
     3.5  #include "SDL_pixels.h"
     3.6  #include "SDL_rect.h"
     3.7 -#include "SDL_ellipse.h"
     3.8 -#include "SDL_poly.h"
     3.9  #include "SDL_surface.h"
    3.10  
    3.11  #include "begin_code.h"
     4.1 --- a/src/video/SDL_shape.c	Fri Jul 02 12:05:47 2010 -0400
     4.2 +++ b/src/video/SDL_shape.c	Tue Jul 06 22:05:22 2010 -0400
     4.3 @@ -45,7 +45,7 @@
     4.4  }
     4.5  
     4.6  /* REQUIRES that bitmap point to a w-by-h bitmap with 1bpp. */
     4.7 -void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap) {
     4.8 +void SDL_CalculateShapeBitmap(Uint8 alphacutoff,SDL_Surface *shape,Uint8* bitmap,Uint8 ppb,Uint8 value) {
     4.9  	if(SDL_MUSTLOCK(shape))
    4.10  		SDL_LockSurface(shape);
    4.11  	int x = 0,y = 0;
    4.12 @@ -55,7 +55,7 @@
    4.13  			Uint8 alpha = 0;
    4.14  			SDL_GetRGBA(*(Uint32*)pixel,shape->format,NULL,NULL,NULL,&alpha);
    4.15  			Uint32 bitmap_pixel = y*shape->w + x;
    4.16 -			bitmap[bitmap_pixel / 8] |= (alpha >= alphacutoff ? 1 : 0) << (8 - (bitmap_pixel % 8));
    4.17 +			bitmap[bitmap_pixel / ppb] |= (alpha >= alphacutoff ? value : 0) << ((ppb - 1) - (bitmap_pixel % ppb));
    4.18  		}
    4.19  	if(SDL_MUSTLOCK(shape))
    4.20  		SDL_UnlockSurface(shape);
    4.21 @@ -81,7 +81,7 @@
    4.22  			}
    4.23  		}
    4.24  	}
    4.25 -	//TODO: Platform-specific implementations of SetWindowShape.  X11 is in-progress.
    4.26 +	//TODO: Platform-specific implementations of SetWindowShape.  X11 is finished.  Win32 is in progress.
    4.27  	int result = window->display->device->shape_driver.SetWindowShape(window->shaper,shape,shapeMode);
    4.28  	window->shaper->hasshape = SDL_TRUE;
    4.29  	if(window->shaper->usershownflag & SDL_WINDOW_SHOWN == SDL_WINDOW_SHOWN) {
     5.1 --- a/src/video/win32/SDL_win32shape.c	Fri Jul 02 12:05:47 2010 -0400
     5.2 +++ b/src/video/win32/SDL_win32shape.c	Tue Jul 06 22:05:22 2010 -0400
     5.3 @@ -20,6 +20,65 @@
     5.4      eligottlieb@gmail.com
     5.5  */
     5.6  
     5.7 -#include "SDL_shape.h"
     5.8 +#include <windows.h>
     5.9 +#include "SDL_win32shape.h"
    5.10  
    5.11 -/* Functions implementing shaped windows for Win32 will be implemented when the API is set. */
    5.12 +SDL_WindowShaper* Win32_CreateShaper(SDL_Window * window) {
    5.13 +	SDL_WindowShaper* result = malloc(sizeof(SDL_WindowShaper));
    5.14 +	result->window = window;
    5.15 +	result->alphacutoff = 0;
    5.16 +	result->usershownflag = 0;
    5.17 +	//Put some driver-data here.
    5.18 +	window->shaper = result;
    5.19 +	int resized_properly = X11ResizeWindowShape(window);
    5.20 +	assert(resized_properly == 0);
    5.21 +	return result;
    5.22 +}
    5.23 +
    5.24 +int Win32_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
    5.25 +	assert(shaper != NULL && shape != NULL);
    5.26 +	if(!SDL_ISPIXELFORMAT_ALPHA(SDL_MasksToPixelFormatEnum(shape->format->BitsPerPixel,shape->format->Rmask,shape->format->Gmask,shape->format->Bmask,shape->format->Amask)))
    5.27 +		return -2;
    5.28 +	if(shape->w != shaper->window->w || shape->h != shaper->window->h)
    5.29 +		return -3;
    5.30 +	
    5.31 +	/* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
    5.32 +	/*
    5.33 +	 * Start with empty region 
    5.34 +	 */
    5.35 +	HRGN MaskRegion = CreateRectRgn(0, 0, 0, 0);
    5.36 +	
    5.37 +	unsigned int pitch = shape->pitch;
    5.38 +	unsigned int width = shape->width;
    5.39 +	unsigned int height = shape->height;
    5.40 +	unsigned int dy = pitch - width;
    5.41 +	
    5.42 +	SDL_ShapeData *data = (SDL_ShapeData*)shaper->driverdata;
    5.43 +	/*
    5.44 +	 * Transfer binarized mask image into workbuffer 
    5.45 +	 */
    5.46 +	SDL_CalculateShapeBitmap(shaper->alphacutoff,shape,data->shapebuffer,1,0xff);
    5.47 +	//Move code over to here from AW_windowShape.c
    5.48 +	
    5.49 +}
    5.50 +
    5.51 +int Win32_ResizeWindowShape(SDL_Window *window) {
    5.52 +	SDL_ShapeData* data = window->shaper->driverdata;
    5.53 +	assert(data != NULL);
    5.54 +	
    5.55 +	unsigned int buffersize = window->w * window->h;
    5.56 +	if(data->buffersize != buffersize || data->shapebuffer == NULL) {
    5.57 +		data->buffersize = buffersize;
    5.58 +		if(data->shapebuffer != NULL)
    5.59 +			free(data->shapebuffer);
    5.60 +		data->shapebuffer = malloc(data->buffersize);
    5.61 +		if(data->shapebuffer == NULL) {
    5.62 +			SDL_SetError("Could not allocate memory for shaped-window bitmap.");
    5.63 +			return -1;
    5.64 +		}
    5.65 +	}
    5.66 +	
    5.67 +	window->shaper->usershownflag = window->flags & SDL_WINDOW_SHOWN;
    5.68 +	
    5.69 +	return 0;
    5.70 +}
     6.1 --- a/src/video/x11/SDL_x11shape.c	Fri Jul 02 12:05:47 2010 -0400
     6.2 +++ b/src/video/x11/SDL_x11shape.c	Tue Jul 06 22:05:22 2010 -0400
     6.3 @@ -62,6 +62,7 @@
     6.4  }
     6.5  	
     6.6  int X11_SetWindowShape(SDL_WindowShaper *shaper,SDL_Surface *shape,SDL_WindowShapeMode *shapeMode) {
     6.7 +	assert(shaper != NULL && shape != NULL);
     6.8  	if(!SDL_ISPIXELFORMAT_ALPHA(SDL_MasksToPixelFormatEnum(shape->format->BitsPerPixel,shape->format->Rmask,shape->format->Gmask,shape->format->Bmask,shape->format->Amask)))
     6.9  		return -2;
    6.10  	if(shape->w != shaper->window->w || shape->h != shaper->window->h)
    6.11 @@ -69,8 +70,8 @@
    6.12  	SDL_ShapeData *data = shaper->driverdata;
    6.13  	assert(data != NULL);
    6.14  	
    6.15 -	/* Assume that shaper->alphacutoff already has a value. */
    6.16 -	SDL_CalculateShapeBitmap(shaper->alphacutoff,shape,data->bitmap);
    6.17 +	/* Assume that shaper->alphacutoff already has a value, because SDL_SetWindowShape() should have given it one. */
    6.18 +	SDL_CalculateShapeBitmap(shaper->alphacutoff,shape,data->bitmap,8,1);
    6.19  		
    6.20  	SDL_WindowData *windowdata = (SDL_WindowData*)(shaper->window->driverdata);
    6.21  	Pixmap shapemask = XCreateBitmapFromData(windowdata->videodata->display,windowdata->xwindow,data->bitmap,shaper->window->w,shaper->window->h);
     7.1 --- a/test/testeyes.c	Fri Jul 02 12:05:47 2010 -0400
     7.2 +++ b/test/testeyes.c	Tue Jul 06 22:05:22 2010 -0400
     7.3 @@ -143,7 +143,7 @@
     7.4  		exit(-3);
     7.5  	}
     7.6  	
     7.7 -	SDL_Color bnw_palette[2] = {{0,0,0,0},{255,255,255,255}};
     7.8 +	SDL_Color bnw_palette[2] = {{0,0,0,255},{255,255,255,255}};
     7.9  	SDL_Texture *eyes_texture = SDL_CreateTexture(SDL_PIXELFORMAT_INDEX1LSB,SDL_TEXTUREACCESS_STREAMING,eyes_width,eyes_height);
    7.10  	if(eyes_texture == NULL) {
    7.11  		SDL_DestroyRenderer(window);
    7.12 @@ -162,8 +162,11 @@
    7.13  		memcpy(pixels+pitch*row,eyes_bits+(eyes_width/8)*row,eyes_width/8);
    7.14  	SDL_UnlockTexture(eyes_texture);
    7.15  	
    7.16 -	SDL_Texture *mask_texture = SDL_CreateTexture(SDL_PIXELFORMAT_INDEX1LSB,SDL_TEXTUREACCESS_STREAMING,eyesmask_width,eyesmask_height);
    7.17 -	if(mask_texture == NULL) {
    7.18 +	int bpp = 0;
    7.19 +	Uint32 r = 0,g = 0,b = 0,a = 0;
    7.20 +	SDL_PixelFormatEnumToMasks(SDL_PIXELFORMAT_ARGB4444,&bpp,&r,&g,&b,&a);
    7.21 +	SDL_Surface *mask = SDL_CreateRGBSurface(0,eyesmask_width,eyesmask_height,bpp,r,g,b,a);
    7.22 +	if(mask == NULL) {
    7.23  		SDL_DestroyTexture(eyes_texture);
    7.24  		SDL_DestroyRenderer(window);
    7.25  		SDL_DestroyWindow(window);
    7.26 @@ -171,19 +174,20 @@
    7.27  		printf("Could not create shape mask texture.\n");
    7.28  		exit(-5);
    7.29  	}
    7.30 -	SDL_SetTexturePalette(mask_texture,bnw_palette,0,2);
    7.31  	
    7.32 -	rect.x = rect.y = 0;
    7.33 -	rect.w = eyesmask_width;
    7.34 -	rect.h = eyesmask_height;
    7.35 -	SDL_LockTexture(mask_texture,&rect,1,&pixels,&pitch);
    7.36 -	for(int row = 0;row<eyesmask_height;row++)
    7.37 -		memcpy(pixels+pitch*row,eyesmask_bits+(eyesmask_width/8)*row,eyesmask_width/8);
    7.38 -	SDL_UnlockTexture(mask_texture);
    7.39 +	if(SDL_MUSTLOCK(mask))
    7.40 +		SDL_LockSurface(mask);
    7.41 +	pixels = mask->pixels;
    7.42 +	for(int y=0;y<eyesmask_height;y++)
    7.43 +		for(int x=0;x<eyesmask_width;x++) {
    7.44 +			Uint8 alpha = *(Uint8*)(eyesmask_bits+(eyesmask_width/8)*y+(x/8)) & (1 << (7 - x % 8)) ? 1 : 0;
    7.45 +			*(Uint16*)(pixels+pitch*y+x*bpp/8) = SDL_MapRGBA(mask->format,0,0,0,alpha);
    7.46 +		}
    7.47 +	if(SDL_MUSTLOCK(mask))
    7.48 +		SDL_UnlockSurface(mask);
    7.49  	
    7.50 -	SDL_SelectShapeRenderer(window);
    7.51 -	SDL_RenderCopy(mask_texture,&rect,&rect);
    7.52 -	SDL_RenderPresent();
    7.53 +	SDL_WindowShapeMode mode = {ShapeModeDefault,1};
    7.54 +	SDL_SetWindowShape(window,mask,&mode);
    7.55  	
    7.56  	SDL_Event event;
    7.57  	int event_pending = 0;
    7.58 @@ -203,6 +207,9 @@
    7.59  		event_pending = SDL_PollEvent(&event);
    7.60  	}
    7.61  	
    7.62 +	SDL_FreeSurface(mask);
    7.63 +	SDL_DestroyTexture(eyes_texture);
    7.64 +	SDL_DestroyWindow(window);
    7.65  	//Call SDL_VideoQuit() before quitting.
    7.66  	SDL_VideoQuit();
    7.67  }