Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
authorSam Lantinga <slouken@libsdl.org>
Thu, 01 Aug 2002 23:06:39 +0000
changeset 43141cadcba32e8
parent 430 60effdbf14ee
child 432 80a35d43a58f
Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
docs.html
include/SDL_video.h
src/video/SDL_blit_N.c
src/video/SDL_surface.c
     1.1 --- a/docs.html	Thu Aug 01 05:16:38 2002 +0000
     1.2 +++ b/docs.html	Thu Aug 01 23:06:39 2002 +0000
     1.3 @@ -16,6 +16,7 @@
     1.4  Major changes since SDL 1.0.0:
     1.5  </H2>
     1.6  <UL>
     1.7 +	<LI> 1.2.5: Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
     1.8  	<LI> 1.2.5: Fixed setting OpenGL mode multiple times on Windows
     1.9  	<LI> 1.2.5: Added support for Qtopia on embedded systems (thanks David!)
    1.10  	<LI> 1.2.4: Added initial support for Atari (thanks Patrice!)
     2.1 --- a/include/SDL_video.h	Thu Aug 01 05:16:38 2002 +0000
     2.2 +++ b/include/SDL_video.h	Thu Aug 01 23:06:39 2002 +0000
     2.3 @@ -645,7 +645,7 @@
     2.4   * 	alpha-blend (using the source per-surface alpha value);
     2.5   * 	set destination alpha to opaque.
     2.6   *     SDL_SRCALPHA not set:
     2.7 - * 	copy RGB, set destination alpha to opaque.
     2.8 + * 	copy RGB, set destination alpha to source per-surface alpha value.
     2.9   *     both:
    2.10   * 	if SDL_SRCCOLORKEY set, only copy the pixels matching the
    2.11   * 	source colour key.
     3.1 --- a/src/video/SDL_blit_N.c	Thu Aug 01 05:16:38 2002 +0000
     3.2 +++ b/src/video/SDL_blit_N.c	Thu Aug 01 23:06:39 2002 +0000
     3.3 @@ -1202,7 +1202,7 @@
     3.4  	int srcbpp = srcfmt->BytesPerPixel;
     3.5  	SDL_PixelFormat *dstfmt = info->dst;
     3.6  	int dstbpp = dstfmt->BytesPerPixel;
     3.7 -	unsigned alpha = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
     3.8 +	unsigned alpha = dstfmt->Amask ? srcfmt->alpha : 0;
     3.9  
    3.10  	while ( height-- ) {
    3.11  		DUFFS_LOOP(
    3.12 @@ -1358,7 +1358,7 @@
    3.13  	SDL_PixelFormat *dstfmt = info->dst;
    3.14  	int srcbpp = srcfmt->BytesPerPixel;
    3.15  	int dstbpp = dstfmt->BytesPerPixel;
    3.16 -	unsigned alpha = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
    3.17 +	unsigned alpha = dstfmt->Amask ? srcfmt->alpha : 0;
    3.18  
    3.19  	while ( height-- ) {
    3.20  		DUFFS_LOOP(
     4.1 --- a/src/video/SDL_surface.c	Thu Aug 01 05:16:38 2002 +0000
     4.2 +++ b/src/video/SDL_surface.c	Thu Aug 01 23:06:39 2002 +0000
     4.3 @@ -211,6 +211,7 @@
     4.4  	SDL_InvalidateMap(surface->map);
     4.5  	return(0);
     4.6  }
     4.7 +/* This function sets the alpha channel of a surface */
     4.8  int SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value)
     4.9  {
    4.10  	Uint32 oldflags = surface->flags;
    4.11 @@ -269,6 +270,52 @@
    4.12  		SDL_InvalidateMap(surface->map);
    4.13  	return(0);
    4.14  }
    4.15 +int SDL_SetAlphaChannel(SDL_Surface *surface, Uint8 value)
    4.16 +{
    4.17 +	int row, col;
    4.18 +	int offset;
    4.19 +	Uint8 *buf;
    4.20 +
    4.21 +	if ( (surface->format->Amask != 0xFF000000) &&
    4.22 +	     (surface->format->Amask != 0x000000FF) ) {
    4.23 +		SDL_SetError("Unsupported surface alpha mask format");
    4.24 +		return -1;
    4.25 +	}
    4.26 +
    4.27 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    4.28 +	if ( surface->format->Amask == 0xFF000000 ) {
    4.29 +			offset = 3;
    4.30 +	} else {
    4.31 +			offset = 0;
    4.32 +	}
    4.33 +#else
    4.34 +	if ( surface->format->Amask == 0xFF000000 ) {
    4.35 +			offset = 0;
    4.36 +	} else {
    4.37 +			offset = 3;
    4.38 +	}
    4.39 +#endif /* Byte ordering */
    4.40 +
    4.41 +	/* Quickly set the alpha channel of an RGBA or ARGB surface */
    4.42 +	if ( SDL_MUSTLOCK(surface) ) {
    4.43 +		if ( SDL_LockSurface(surface) < 0 ) {
    4.44 +			return -1;
    4.45 +		}
    4.46 +	}
    4.47 +	row = surface->h;
    4.48 +	while (row--) {
    4.49 +		col = surface->w;
    4.50 +		buf = (Uint8 *)surface->pixels + row * surface->pitch + offset;
    4.51 +		while(col--) {
    4.52 +			*buf = value;
    4.53 +			buf += 4;
    4.54 +		}
    4.55 +	}
    4.56 +	if ( SDL_MUSTLOCK(surface) ) {
    4.57 +		SDL_UnlockSurface(surface);
    4.58 +	}
    4.59 +	return 0;
    4.60 +}
    4.61  
    4.62  /*
    4.63   * A function to calculate the intersection of two rectangles:
    4.64 @@ -748,8 +795,13 @@
    4.65  		}
    4.66  	}
    4.67  	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    4.68 -		alpha = surface->format->alpha;
    4.69 -		SDL_SetAlpha(surface, 0, 0);
    4.70 +		/* Copy over the alpha channel to RGBA if requested */
    4.71 +		if ( format->Amask ) {
    4.72 +			surface->flags &= ~SDL_SRCALPHA;
    4.73 +		} else {
    4.74 +			alpha = surface->format->alpha;
    4.75 +			SDL_SetAlpha(surface, 0, 0);
    4.76 +		}
    4.77  	}
    4.78  
    4.79  	/* Copy over the image data */
    4.80 @@ -780,7 +832,11 @@
    4.81  		        SDL_SetAlpha(convert, aflags|(flags&SDL_RLEACCELOK),
    4.82  				alpha);
    4.83  		}
    4.84 -		SDL_SetAlpha(surface, aflags, alpha);
    4.85 +		if ( format->Amask ) {
    4.86 +			surface->flags |= SDL_SRCALPHA;
    4.87 +		} else {
    4.88 +			SDL_SetAlpha(surface, aflags, alpha);
    4.89 +		}
    4.90  	}
    4.91  
    4.92  	/* We're ready to go! */