diff src/video/SDL_surface.c @ 2785:fa1095d42a5b

Fixed bug with converting colorkey surface to texture
author Sam Lantinga <slouken@libsdl.org>
date Tue, 25 Nov 2008 02:12:19 +0000
parents 204be4fc2726
children 6bacfecbf27e
line wrap: on
line diff
--- a/src/video/SDL_surface.c	Tue Nov 25 00:48:25 2008 +0000
+++ b/src/video/SDL_surface.c	Tue Nov 25 02:12:19 2008 +0000
@@ -269,6 +269,72 @@
     return 0;
 }
 
+/* This is a fairly slow function to switch from colorkey to alpha */
+void
+SDL_ConvertColorkeyToAlpha(SDL_Surface *surface)
+{
+	int x, y;
+
+	if (!surface) {
+		return;
+	}
+
+	if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
+	    !surface->format->Amask) {
+		return;
+	}
+
+	SDL_LockSurface(surface);
+
+	switch (surface->format->BytesPerPixel) {
+	case 2:
+		{
+			Uint16 *row, *spot;
+			Uint16 ckey = (Uint16)surface->map->info.colorkey;
+			Uint16 mask = (Uint16)(~surface->format->Amask);
+
+			row = (Uint16 *)surface->pixels;
+			for (y = surface->h; y--; ) {
+				spot = row;
+				for (x = surface->w; x--; ) {
+					if (*spot == ckey) {
+						*spot &= mask;
+					}
+					++spot;
+				}
+				row += surface->pitch / 2;
+			}
+		}
+		break;
+	case 3:
+		/* FIXME */
+		break;
+	case 4:
+		{
+			Uint32 *row, *spot;
+			Uint32 ckey = surface->map->info.colorkey;
+			Uint32 mask = ~surface->format->Amask;
+
+			row = (Uint32 *)surface->pixels;
+			for (y = surface->h; y--; ) {
+				spot = row;
+				for (x = surface->w; x--; ) {
+					if (*spot == ckey) {
+						*spot &= mask;
+					}
+					++spot;
+				}
+				row += surface->pitch / 4;
+			}
+		}
+		break;
+	}
+
+	SDL_UnlockSurface(surface);
+
+	SDL_SetColorKey(surface, 0, 0);
+}
+
 int
 SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
 {