src/video/SDL_surface.c
changeset 431 41cadcba32e8
parent 422 b1b9ee41be70
child 441 598b25b9bffe
     1.1 --- a/src/video/SDL_surface.c	Thu Aug 01 05:16:38 2002 +0000
     1.2 +++ b/src/video/SDL_surface.c	Thu Aug 01 23:06:39 2002 +0000
     1.3 @@ -211,6 +211,7 @@
     1.4  	SDL_InvalidateMap(surface->map);
     1.5  	return(0);
     1.6  }
     1.7 +/* This function sets the alpha channel of a surface */
     1.8  int SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value)
     1.9  {
    1.10  	Uint32 oldflags = surface->flags;
    1.11 @@ -269,6 +270,52 @@
    1.12  		SDL_InvalidateMap(surface->map);
    1.13  	return(0);
    1.14  }
    1.15 +int SDL_SetAlphaChannel(SDL_Surface *surface, Uint8 value)
    1.16 +{
    1.17 +	int row, col;
    1.18 +	int offset;
    1.19 +	Uint8 *buf;
    1.20 +
    1.21 +	if ( (surface->format->Amask != 0xFF000000) &&
    1.22 +	     (surface->format->Amask != 0x000000FF) ) {
    1.23 +		SDL_SetError("Unsupported surface alpha mask format");
    1.24 +		return -1;
    1.25 +	}
    1.26 +
    1.27 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    1.28 +	if ( surface->format->Amask == 0xFF000000 ) {
    1.29 +			offset = 3;
    1.30 +	} else {
    1.31 +			offset = 0;
    1.32 +	}
    1.33 +#else
    1.34 +	if ( surface->format->Amask == 0xFF000000 ) {
    1.35 +			offset = 0;
    1.36 +	} else {
    1.37 +			offset = 3;
    1.38 +	}
    1.39 +#endif /* Byte ordering */
    1.40 +
    1.41 +	/* Quickly set the alpha channel of an RGBA or ARGB surface */
    1.42 +	if ( SDL_MUSTLOCK(surface) ) {
    1.43 +		if ( SDL_LockSurface(surface) < 0 ) {
    1.44 +			return -1;
    1.45 +		}
    1.46 +	}
    1.47 +	row = surface->h;
    1.48 +	while (row--) {
    1.49 +		col = surface->w;
    1.50 +		buf = (Uint8 *)surface->pixels + row * surface->pitch + offset;
    1.51 +		while(col--) {
    1.52 +			*buf = value;
    1.53 +			buf += 4;
    1.54 +		}
    1.55 +	}
    1.56 +	if ( SDL_MUSTLOCK(surface) ) {
    1.57 +		SDL_UnlockSurface(surface);
    1.58 +	}
    1.59 +	return 0;
    1.60 +}
    1.61  
    1.62  /*
    1.63   * A function to calculate the intersection of two rectangles:
    1.64 @@ -748,8 +795,13 @@
    1.65  		}
    1.66  	}
    1.67  	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    1.68 -		alpha = surface->format->alpha;
    1.69 -		SDL_SetAlpha(surface, 0, 0);
    1.70 +		/* Copy over the alpha channel to RGBA if requested */
    1.71 +		if ( format->Amask ) {
    1.72 +			surface->flags &= ~SDL_SRCALPHA;
    1.73 +		} else {
    1.74 +			alpha = surface->format->alpha;
    1.75 +			SDL_SetAlpha(surface, 0, 0);
    1.76 +		}
    1.77  	}
    1.78  
    1.79  	/* Copy over the image data */
    1.80 @@ -780,7 +832,11 @@
    1.81  		        SDL_SetAlpha(convert, aflags|(flags&SDL_RLEACCELOK),
    1.82  				alpha);
    1.83  		}
    1.84 -		SDL_SetAlpha(surface, aflags, alpha);
    1.85 +		if ( format->Amask ) {
    1.86 +			surface->flags |= SDL_SRCALPHA;
    1.87 +		} else {
    1.88 +			SDL_SetAlpha(surface, aflags, alpha);
    1.89 +		}
    1.90  	}
    1.91  
    1.92  	/* We're ready to go! */