Fixed bug with converting colorkey surface to texture
authorSam Lantinga <slouken@libsdl.org>
Tue, 25 Nov 2008 02:12:19 +0000
changeset 2785fa1095d42a5b
parent 2784 9bbe3bd94be8
child 2786 6bacfecbf27e
Fixed bug with converting colorkey surface to texture
src/video/SDL_surface.c
src/video/SDL_video.c
     1.1 --- a/src/video/SDL_surface.c	Tue Nov 25 00:48:25 2008 +0000
     1.2 +++ b/src/video/SDL_surface.c	Tue Nov 25 02:12:19 2008 +0000
     1.3 @@ -269,6 +269,72 @@
     1.4      return 0;
     1.5  }
     1.6  
     1.7 +/* This is a fairly slow function to switch from colorkey to alpha */
     1.8 +void
     1.9 +SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
    1.10 +{
    1.11 +	int x, y;
    1.12 +
    1.13 +	if (!surface) {
    1.14 +		return;
    1.15 +	}
    1.16 +
    1.17 +	if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
    1.18 +	    !surface->format->Amask) {
    1.19 +		return;
    1.20 +	}
    1.21 +
    1.22 +	SDL_LockSurface(surface);
    1.23 +
    1.24 +	switch (surface->format->BytesPerPixel) {
    1.25 +	case 2:
    1.26 +		{
    1.27 +			Uint16 *row, *spot;
    1.28 +			Uint16 ckey = (Uint16)surface->map->info.colorkey;
    1.29 +			Uint16 mask = (Uint16)(~surface->format->Amask);
    1.30 +
    1.31 +			row = (Uint16 *)surface->pixels;
    1.32 +			for (y = surface->h; y--; ) {
    1.33 +				spot = row;
    1.34 +				for (x = surface->w; x--; ) {
    1.35 +					if (*spot == ckey) {
    1.36 +						*spot &= mask;
    1.37 +					}
    1.38 +					++spot;
    1.39 +				}
    1.40 +				row += surface->pitch / 2;
    1.41 +			}
    1.42 +		}
    1.43 +		break;
    1.44 +	case 3:
    1.45 +		/* FIXME */
    1.46 +		break;
    1.47 +	case 4:
    1.48 +		{
    1.49 +			Uint32 *row, *spot;
    1.50 +			Uint32 ckey = surface->map->info.colorkey;
    1.51 +			Uint32 mask = ~surface->format->Amask;
    1.52 +
    1.53 +			row = (Uint32 *)surface->pixels;
    1.54 +			for (y = surface->h; y--; ) {
    1.55 +				spot = row;
    1.56 +				for (x = surface->w; x--; ) {
    1.57 +					if (*spot == ckey) {
    1.58 +						*spot &= mask;
    1.59 +					}
    1.60 +					++spot;
    1.61 +				}
    1.62 +				row += surface->pitch / 4;
    1.63 +			}
    1.64 +		}
    1.65 +		break;
    1.66 +	}
    1.67 +
    1.68 +	SDL_UnlockSurface(surface);
    1.69 +
    1.70 +	SDL_SetColorKey(surface, 0, 0);
    1.71 +}
    1.72 +
    1.73  int
    1.74  SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
    1.75  {
     2.1 --- a/src/video/SDL_video.c	Tue Nov 25 00:48:25 2008 +0000
     2.2 +++ b/src/video/SDL_video.c	Tue Nov 25 02:12:19 2008 +0000
     2.3 @@ -46,6 +46,9 @@
     2.4  #endif
     2.5  #endif /* SDL_VIDEO_OPENGL */
     2.6  
     2.7 +/* From SDL_surface.c */
     2.8 +extern void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface);
     2.9 +
    2.10  /* Available video drivers */
    2.11  static VideoBootStrap *bootstrap[] = {
    2.12  #if SDL_VIDEO_DRIVER_COCOA
    2.13 @@ -1585,6 +1588,7 @@
    2.14              }
    2.15              dst = SDL_ConvertSurface(surface, dst_fmt, 0);
    2.16              if (dst) {
    2.17 +				SDL_ConvertColorkeyToAlpha(dst);
    2.18                  SDL_UpdateTexture(textureID, NULL, dst->pixels, dst->pitch);
    2.19                  SDL_FreeSurface(dst);
    2.20              }