src/video/SDL_surface.c
changeset 1895 c121d94672cb
parent 1456 84de7511f79f
child 1920 8a162bfdc838
     1.1 --- a/src/video/SDL_surface.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/video/SDL_surface.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -22,8 +22,8 @@
     1.4  #include "SDL_config.h"
     1.5  
     1.6  #include "SDL_video.h"
     1.7 +#include "SDL_compat.h"
     1.8  #include "SDL_sysvideo.h"
     1.9 -#include "SDL_cursor_c.h"
    1.10  #include "SDL_blit.h"
    1.11  #include "SDL_RLEaccel_c.h"
    1.12  #include "SDL_pixels_c.h"
    1.13 @@ -34,365 +34,414 @@
    1.14  /*
    1.15   * Create an empty RGB surface of the appropriate depth
    1.16   */
    1.17 -SDL_Surface * SDL_CreateRGBSurface (Uint32 flags,
    1.18 -			int width, int height, int depth,
    1.19 -			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
    1.20 +SDL_Surface *
    1.21 +SDL_CreateRGBSurface(Uint32 flags,
    1.22 +                     int width, int height, int depth,
    1.23 +                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
    1.24  {
    1.25 -	SDL_VideoDevice *video = current_video;
    1.26 -	SDL_VideoDevice *this  = current_video;
    1.27 -	SDL_Surface *screen;
    1.28 -	SDL_Surface *surface;
    1.29 +    SDL_Surface *surface;
    1.30  
    1.31 -	/* Make sure the size requested doesn't overflow our datatypes */
    1.32 -	/* Next time I write a library like SDL, I'll use int for size. :) */
    1.33 -	if ( width >= 16384 || height >= 65536 ) {
    1.34 -		SDL_SetError("Width or height is too large");
    1.35 -		return(NULL);
    1.36 -	}
    1.37 +    /* Allocate the surface */
    1.38 +    surface = (SDL_Surface *) SDL_malloc(sizeof(*surface));
    1.39 +    if (surface == NULL) {
    1.40 +        SDL_OutOfMemory();
    1.41 +        return NULL;
    1.42 +    }
    1.43 +    SDL_zerop(surface);
    1.44  
    1.45 -	/* Check to see if we desire the surface in video memory */
    1.46 -	if ( video ) {
    1.47 -		screen = SDL_PublicSurface;
    1.48 -	} else {
    1.49 -		screen = NULL;
    1.50 -	}
    1.51 -	if ( screen && ((screen->flags&SDL_HWSURFACE) == SDL_HWSURFACE) ) {
    1.52 -		if ( (flags&(SDL_SRCCOLORKEY|SDL_SRCALPHA)) != 0 ) {
    1.53 -			flags |= SDL_HWSURFACE;
    1.54 -		}
    1.55 -		if ( (flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
    1.56 -			if ( ! current_video->info.blit_hw_CC ) {
    1.57 -				flags &= ~SDL_HWSURFACE;
    1.58 -			}
    1.59 -		}
    1.60 -		if ( (flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
    1.61 -			if ( ! current_video->info.blit_hw_A ) {
    1.62 -				flags &= ~SDL_HWSURFACE;
    1.63 -			}
    1.64 -		}
    1.65 -	} else {
    1.66 -		flags &= ~SDL_HWSURFACE;
    1.67 -	}
    1.68 +    surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
    1.69 +    if (!surface->format) {
    1.70 +        SDL_FreeSurface(surface);
    1.71 +        return NULL;
    1.72 +    }
    1.73 +    if (Amask) {
    1.74 +        surface->flags |= SDL_SRCALPHA;
    1.75 +    }
    1.76 +    surface->w = width;
    1.77 +    surface->h = height;
    1.78 +    surface->pitch = SDL_CalculatePitch(surface);
    1.79 +    SDL_SetClipRect(surface, NULL);
    1.80  
    1.81 -	/* Allocate the surface */
    1.82 -	surface = (SDL_Surface *)SDL_malloc(sizeof(*surface));
    1.83 -	if ( surface == NULL ) {
    1.84 -		SDL_OutOfMemory();
    1.85 -		return(NULL);
    1.86 -	}
    1.87 -	surface->flags = SDL_SWSURFACE;
    1.88 -	if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
    1.89 -		if ((Amask) && (video->displayformatalphapixel))
    1.90 -		{
    1.91 -			depth = video->displayformatalphapixel->BitsPerPixel;
    1.92 -			Rmask = video->displayformatalphapixel->Rmask;
    1.93 -			Gmask = video->displayformatalphapixel->Gmask;
    1.94 -			Bmask = video->displayformatalphapixel->Bmask;
    1.95 -			Amask = video->displayformatalphapixel->Amask;
    1.96 -		}
    1.97 -		else
    1.98 -		{
    1.99 -			depth = screen->format->BitsPerPixel;
   1.100 -			Rmask = screen->format->Rmask;
   1.101 -			Gmask = screen->format->Gmask;
   1.102 -			Bmask = screen->format->Bmask;
   1.103 -			Amask = screen->format->Amask;
   1.104 -		}
   1.105 -	}
   1.106 -	surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
   1.107 -	if ( surface->format == NULL ) {
   1.108 -		SDL_free(surface);
   1.109 -		return(NULL);
   1.110 -	}
   1.111 -	if ( Amask ) {
   1.112 -		surface->flags |= SDL_SRCALPHA;
   1.113 -	}
   1.114 -	surface->w = width;
   1.115 -	surface->h = height;
   1.116 -	surface->pitch = SDL_CalculatePitch(surface);
   1.117 -	surface->pixels = NULL;
   1.118 -	surface->offset = 0;
   1.119 -	surface->hwdata = NULL;
   1.120 -	surface->locked = 0;
   1.121 -	surface->map = NULL;
   1.122 -	surface->unused1 = 0;
   1.123 -	SDL_SetClipRect(surface, NULL);
   1.124 -	SDL_FormatChanged(surface);
   1.125 +    if (surface->format->BitsPerPixel <= 8) {
   1.126 +        SDL_Palette *palette =
   1.127 +            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
   1.128 +        if (!palette) {
   1.129 +            SDL_FreeSurface(surface);
   1.130 +            return NULL;
   1.131 +        }
   1.132 +        if (Rmask || Bmask || Gmask) {
   1.133 +            const SDL_PixelFormat *format = surface->format;
   1.134  
   1.135 -	/* Get the pixels */
   1.136 -	if ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) || 
   1.137 -				(video->AllocHWSurface(this, surface) < 0) ) {
   1.138 -		if ( surface->w && surface->h ) {
   1.139 -			surface->pixels = SDL_malloc(surface->h*surface->pitch);
   1.140 -			if ( surface->pixels == NULL ) {
   1.141 -				SDL_FreeSurface(surface);
   1.142 -				SDL_OutOfMemory();
   1.143 -				return(NULL);
   1.144 -			}
   1.145 -			/* This is important for bitmaps */
   1.146 -			SDL_memset(surface->pixels, 0, surface->h*surface->pitch);
   1.147 -		}
   1.148 -	}
   1.149 +            /* create palette according to masks */
   1.150 +            int i;
   1.151 +            int Rm = 0, Gm = 0, Bm = 0;
   1.152 +            int Rw = 0, Gw = 0, Bw = 0;
   1.153  
   1.154 -	/* Allocate an empty mapping */
   1.155 -	surface->map = SDL_AllocBlitMap();
   1.156 -	if ( surface->map == NULL ) {
   1.157 -		SDL_FreeSurface(surface);
   1.158 -		return(NULL);
   1.159 -	}
   1.160 +            if (Rmask) {
   1.161 +                Rw = 8 - format->Rloss;
   1.162 +                for (i = format->Rloss; i > 0; i -= Rw)
   1.163 +                    Rm |= 1 << i;
   1.164 +            }
   1.165 +            if (Gmask) {
   1.166 +                Gw = 8 - format->Gloss;
   1.167 +                for (i = format->Gloss; i > 0; i -= Gw)
   1.168 +                    Gm |= 1 << i;
   1.169 +            }
   1.170 +            if (Bmask) {
   1.171 +                Bw = 8 - format->Bloss;
   1.172 +                for (i = format->Bloss; i > 0; i -= Bw)
   1.173 +                    Bm |= 1 << i;
   1.174 +            }
   1.175 +            for (i = 0; i < palette->ncolors; ++i) {
   1.176 +                int r, g, b;
   1.177 +                r = (i & Rmask) >> format->Rshift;
   1.178 +                r = (r << format->Rloss) | ((r * Rm) >> Rw);
   1.179 +                palette->colors[i].r = r;
   1.180  
   1.181 -	/* The surface is ready to go */
   1.182 -	surface->refcount = 1;
   1.183 +                g = (i & Gmask) >> format->Gshift;
   1.184 +                g = (g << format->Gloss) | ((g * Gm) >> Gw);
   1.185 +                palette->colors[i].g = g;
   1.186 +
   1.187 +                b = (i & Bmask) >> format->Bshift;
   1.188 +                b = (b << format->Bloss) | ((b * Bm) >> Bw);
   1.189 +                palette->colors[i].b = b;
   1.190 +            }
   1.191 +        } else if (palette->ncolors == 2) {
   1.192 +            /* Create a black and white bitmap palette */
   1.193 +            palette->colors[0].r = 0xFF;
   1.194 +            palette->colors[0].g = 0xFF;
   1.195 +            palette->colors[0].b = 0xFF;
   1.196 +            palette->colors[1].r = 0x00;
   1.197 +            palette->colors[1].g = 0x00;
   1.198 +            palette->colors[1].b = 0x00;
   1.199 +        }
   1.200 +        SDL_SetSurfacePalette(surface, palette);
   1.201 +        SDL_FreePalette(palette);
   1.202 +    }
   1.203 +
   1.204 +    /* Get the pixels */
   1.205 +    if (surface->w && surface->h) {
   1.206 +        surface->pixels = SDL_malloc(surface->h * surface->pitch);
   1.207 +        if (!surface->pixels) {
   1.208 +            SDL_FreeSurface(surface);
   1.209 +            SDL_OutOfMemory();
   1.210 +            return NULL;
   1.211 +        }
   1.212 +        /* This is important for bitmaps */
   1.213 +        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
   1.214 +    }
   1.215 +
   1.216 +    /* Allocate an empty mapping */
   1.217 +    surface->map = SDL_AllocBlitMap();
   1.218 +    if (!surface->map) {
   1.219 +        SDL_FreeSurface(surface);
   1.220 +        return NULL;
   1.221 +    }
   1.222 +    SDL_FormatChanged(surface);
   1.223 +
   1.224 +    /* The surface is ready to go */
   1.225 +    surface->refcount = 1;
   1.226  #ifdef CHECK_LEAKS
   1.227 -	++surfaces_allocated;
   1.228 +    ++surfaces_allocated;
   1.229  #endif
   1.230 -	return(surface);
   1.231 +    return surface;
   1.232  }
   1.233 +
   1.234  /*
   1.235   * Create an RGB surface from an existing memory buffer
   1.236   */
   1.237 -SDL_Surface * SDL_CreateRGBSurfaceFrom (void *pixels,
   1.238 -			int width, int height, int depth, int pitch,
   1.239 -			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
   1.240 +SDL_Surface *
   1.241 +SDL_CreateRGBSurfaceFrom(void *pixels,
   1.242 +                         int width, int height, int depth, int pitch,
   1.243 +                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
   1.244 +                         Uint32 Amask)
   1.245  {
   1.246 -	SDL_Surface *surface;
   1.247 +    SDL_Surface *surface;
   1.248  
   1.249 -	surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, depth,
   1.250 -	                               Rmask, Gmask, Bmask, Amask);
   1.251 -	if ( surface != NULL ) {
   1.252 -		surface->flags |= SDL_PREALLOC;
   1.253 -		surface->pixels = pixels;
   1.254 -		surface->w = width;
   1.255 -		surface->h = height;
   1.256 -		surface->pitch = pitch;
   1.257 -		SDL_SetClipRect(surface, NULL);
   1.258 -	}
   1.259 -	return(surface);
   1.260 +    surface =
   1.261 +        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
   1.262 +    if (surface != NULL) {
   1.263 +        surface->flags |= SDL_PREALLOC;
   1.264 +        surface->pixels = pixels;
   1.265 +        surface->w = width;
   1.266 +        surface->h = height;
   1.267 +        surface->pitch = pitch;
   1.268 +        SDL_SetClipRect(surface, NULL);
   1.269 +    }
   1.270 +    return surface;
   1.271  }
   1.272 +
   1.273 +SDL_Surface *
   1.274 +SDL_CreateRGBSurfaceFromTexture(SDL_TextureID textureID)
   1.275 +{
   1.276 +    SDL_Surface *surface;
   1.277 +    Uint32 format;
   1.278 +    int w, h;
   1.279 +    int bpp;
   1.280 +    Uint32 Rmask, Gmask, Bmask, Amask;
   1.281 +    void *pixels;
   1.282 +    int pitch;
   1.283 +
   1.284 +    if (SDL_QueryTexture(textureID, &format, NULL, &w, &h) < 0) {
   1.285 +        return NULL;
   1.286 +    }
   1.287 +
   1.288 +    if (!SDL_PixelFormatEnumToMasks
   1.289 +        (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
   1.290 +        SDL_SetError("Unknown texture format");
   1.291 +        return NULL;
   1.292 +    }
   1.293 +
   1.294 +    if (SDL_QueryTexturePixels(textureID, &pixels, &pitch) == 0) {
   1.295 +        surface =
   1.296 +            SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch, Rmask, Gmask,
   1.297 +                                     Bmask, Amask);
   1.298 +    } else {
   1.299 +        surface =
   1.300 +            SDL_CreateRGBSurface(0, 0, 0, bpp, Rmask, Gmask, Bmask, Amask);
   1.301 +        if (surface) {
   1.302 +            surface->flags |= SDL_HWSURFACE;
   1.303 +            surface->w = w;
   1.304 +            surface->h = h;
   1.305 +            surface->pitch = SDL_CalculatePitch(surface);
   1.306 +            SDL_SetClipRect(surface, NULL);
   1.307 +        }
   1.308 +    }
   1.309 +    if (surface) {
   1.310 +        surface->textureID = textureID;
   1.311 +    }
   1.312 +
   1.313 +    return surface;
   1.314 +}
   1.315 +
   1.316 +static int
   1.317 +SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
   1.318 +{
   1.319 +    SDL_Surface *surface = (SDL_Surface *) userdata;
   1.320 +
   1.321 +    if (surface->textureID) {
   1.322 +        if (SDL_SetTexturePalette
   1.323 +            (surface->textureID, palette->colors, 0, palette->ncolors) < 0) {
   1.324 +            SDL_GetTexturePalette(surface->textureID, palette->colors, 0,
   1.325 +                                  palette->ncolors);
   1.326 +            return -1;
   1.327 +        }
   1.328 +    }
   1.329 +    SDL_FormatChanged(surface);
   1.330 +
   1.331 +    return 0;
   1.332 +}
   1.333 +
   1.334 +int
   1.335 +SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
   1.336 +{
   1.337 +    if (!surface || !surface->format) {
   1.338 +        SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
   1.339 +        return -1;
   1.340 +    }
   1.341 +
   1.342 +    if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
   1.343 +        SDL_SetError
   1.344 +            ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
   1.345 +        return -1;
   1.346 +    }
   1.347 +
   1.348 +    if (surface->format->palette == palette) {
   1.349 +        return 0;
   1.350 +    }
   1.351 +
   1.352 +    if (surface->format->palette) {
   1.353 +        SDL_DelPaletteWatch(surface->format->palette,
   1.354 +                            SDL_SurfacePaletteChanged, surface);
   1.355 +    }
   1.356 +
   1.357 +    surface->format->palette = palette;
   1.358 +
   1.359 +    if (surface->format->palette) {
   1.360 +        SDL_AddPaletteWatch(surface->format->palette,
   1.361 +                            SDL_SurfacePaletteChanged, surface);
   1.362 +    }
   1.363 +    return 0;
   1.364 +}
   1.365 +
   1.366  /*
   1.367   * Set the color key in a blittable surface
   1.368   */
   1.369 -int SDL_SetColorKey (SDL_Surface *surface, Uint32 flag, Uint32 key)
   1.370 +int
   1.371 +SDL_SetColorKey(SDL_Surface * surface, Uint32 flag, Uint32 key)
   1.372  {
   1.373 -	/* Sanity check the flag as it gets passed in */
   1.374 -	if ( flag & SDL_SRCCOLORKEY ) {
   1.375 -		if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) {
   1.376 -			flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   1.377 -		} else {
   1.378 -			flag = SDL_SRCCOLORKEY;
   1.379 -		}
   1.380 -	} else {
   1.381 -		flag = 0;
   1.382 -	}
   1.383 +    /* Sanity check the flag as it gets passed in */
   1.384 +    if (flag & SDL_SRCCOLORKEY) {
   1.385 +        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
   1.386 +            flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   1.387 +        } else {
   1.388 +            flag = SDL_SRCCOLORKEY;
   1.389 +        }
   1.390 +    } else {
   1.391 +        flag = 0;
   1.392 +    }
   1.393  
   1.394 -	/* Optimize away operations that don't change anything */
   1.395 -	if ( (flag == (surface->flags & (SDL_SRCCOLORKEY|SDL_RLEACCELOK))) &&
   1.396 -	     (key == surface->format->colorkey) ) {
   1.397 -		return(0);
   1.398 -	}
   1.399 +    /* Optimize away operations that don't change anything */
   1.400 +    if ((flag == (surface->flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK))) &&
   1.401 +        (key == surface->format->colorkey)) {
   1.402 +        return (0);
   1.403 +    }
   1.404  
   1.405 -	/* UnRLE surfaces before we change the colorkey */
   1.406 -	if ( surface->flags & SDL_RLEACCEL ) {
   1.407 -	        SDL_UnRLESurface(surface, 1);
   1.408 -	}
   1.409 +    /* UnRLE surfaces before we change the colorkey */
   1.410 +    if (surface->flags & SDL_RLEACCEL) {
   1.411 +        SDL_UnRLESurface(surface, 1);
   1.412 +    }
   1.413  
   1.414 -	if ( flag ) {
   1.415 -		SDL_VideoDevice *video = current_video;
   1.416 -		SDL_VideoDevice *this  = current_video;
   1.417 +    if (flag) {
   1.418 +        surface->flags |= SDL_SRCCOLORKEY;
   1.419 +        surface->format->colorkey = key;
   1.420 +        if (flag & SDL_RLEACCELOK) {
   1.421 +            surface->flags |= SDL_RLEACCELOK;
   1.422 +        } else {
   1.423 +            surface->flags &= ~SDL_RLEACCELOK;
   1.424 +        }
   1.425 +    } else {
   1.426 +        surface->flags &= ~(SDL_SRCCOLORKEY | SDL_RLEACCELOK);
   1.427 +        surface->format->colorkey = 0;
   1.428 +    }
   1.429 +    SDL_InvalidateMap(surface->map);
   1.430 +    return (0);
   1.431 +}
   1.432  
   1.433 +/* This function sets the alpha channel of a surface */
   1.434 +int
   1.435 +SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
   1.436 +{
   1.437 +    Uint32 oldflags = surface->flags;
   1.438 +    Uint32 oldalpha = surface->format->alpha;
   1.439  
   1.440 -		surface->flags |= SDL_SRCCOLORKEY;
   1.441 -		surface->format->colorkey = key;
   1.442 -		if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
   1.443 -			if ( (video->SetHWColorKey == NULL) ||
   1.444 -			     (video->SetHWColorKey(this, surface, key) < 0) ) {
   1.445 -				surface->flags &= ~SDL_HWACCEL;
   1.446 -			}
   1.447 -		}
   1.448 -		if ( flag & SDL_RLEACCELOK ) {
   1.449 -			surface->flags |= SDL_RLEACCELOK;
   1.450 -		} else {
   1.451 -			surface->flags &= ~SDL_RLEACCELOK;
   1.452 -		}
   1.453 -	} else {
   1.454 -		surface->flags &= ~(SDL_SRCCOLORKEY|SDL_RLEACCELOK);
   1.455 -		surface->format->colorkey = 0;
   1.456 -	}
   1.457 -	SDL_InvalidateMap(surface->map);
   1.458 -	return(0);
   1.459 +    /* Sanity check the flag as it gets passed in */
   1.460 +    if (flag & SDL_SRCALPHA) {
   1.461 +        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
   1.462 +            flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.463 +        } else {
   1.464 +            flag = SDL_SRCALPHA;
   1.465 +        }
   1.466 +    } else {
   1.467 +        flag = 0;
   1.468 +    }
   1.469 +
   1.470 +    /* Optimize away operations that don't change anything */
   1.471 +    if ((flag == (surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK))) &&
   1.472 +        (!flag || value == oldalpha)) {
   1.473 +        return (0);
   1.474 +    }
   1.475 +
   1.476 +    if (!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
   1.477 +        SDL_UnRLESurface(surface, 1);
   1.478 +
   1.479 +    if (flag) {
   1.480 +        surface->flags |= SDL_SRCALPHA;
   1.481 +        surface->format->alpha = value;
   1.482 +        if (flag & SDL_RLEACCELOK) {
   1.483 +            surface->flags |= SDL_RLEACCELOK;
   1.484 +        } else {
   1.485 +            surface->flags &= ~SDL_RLEACCELOK;
   1.486 +        }
   1.487 +    } else {
   1.488 +        surface->flags &= ~SDL_SRCALPHA;
   1.489 +        surface->format->alpha = SDL_ALPHA_OPAQUE;
   1.490 +    }
   1.491 +    /*
   1.492 +     * The representation for software surfaces is independent of
   1.493 +     * per-surface alpha, so no need to invalidate the blit mapping
   1.494 +     * if just the alpha value was changed. (If either is 255, we still
   1.495 +     * need to invalidate.)
   1.496 +     */
   1.497 +    if (oldflags != surface->flags
   1.498 +        || (((oldalpha + 1) ^ (value + 1)) & 0x100)) {
   1.499 +        SDL_InvalidateMap(surface->map);
   1.500 +    }
   1.501 +    return (0);
   1.502  }
   1.503 -/* This function sets the alpha channel of a surface */
   1.504 -int SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value)
   1.505 +
   1.506 +int
   1.507 +SDL_SetAlphaChannel(SDL_Surface * surface, Uint8 value)
   1.508  {
   1.509 -	Uint32 oldflags = surface->flags;
   1.510 -	Uint32 oldalpha = surface->format->alpha;
   1.511 +    int row, col;
   1.512 +    int offset;
   1.513 +    Uint8 *buf;
   1.514  
   1.515 -	/* Sanity check the flag as it gets passed in */
   1.516 -	if ( flag & SDL_SRCALPHA ) {
   1.517 -		if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) {
   1.518 -			flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
   1.519 -		} else {
   1.520 -			flag = SDL_SRCALPHA;
   1.521 -		}
   1.522 -	} else {
   1.523 -		flag = 0;
   1.524 -	}
   1.525 -
   1.526 -	/* Optimize away operations that don't change anything */
   1.527 -	if ( (flag == (surface->flags & (SDL_SRCALPHA|SDL_RLEACCELOK))) &&
   1.528 -	     (!flag || value == oldalpha) ) {
   1.529 -		return(0);
   1.530 -	}
   1.531 -
   1.532 -	if(!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
   1.533 -		SDL_UnRLESurface(surface, 1);
   1.534 -
   1.535 -	if ( flag ) {
   1.536 -		SDL_VideoDevice *video = current_video;
   1.537 -		SDL_VideoDevice *this  = current_video;
   1.538 -
   1.539 -		surface->flags |= SDL_SRCALPHA;
   1.540 -		surface->format->alpha = value;
   1.541 -		if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
   1.542 -			if ( (video->SetHWAlpha == NULL) ||
   1.543 -			     (video->SetHWAlpha(this, surface, value) < 0) ) {
   1.544 -				surface->flags &= ~SDL_HWACCEL;
   1.545 -			}
   1.546 -		}
   1.547 -		if ( flag & SDL_RLEACCELOK ) {
   1.548 -		        surface->flags |= SDL_RLEACCELOK;
   1.549 -		} else {
   1.550 -		        surface->flags &= ~SDL_RLEACCELOK;
   1.551 -		}
   1.552 -	} else {
   1.553 -		surface->flags &= ~SDL_SRCALPHA;
   1.554 -		surface->format->alpha = SDL_ALPHA_OPAQUE;
   1.555 -	}
   1.556 -	/*
   1.557 -	 * The representation for software surfaces is independent of
   1.558 -	 * per-surface alpha, so no need to invalidate the blit mapping
   1.559 -	 * if just the alpha value was changed. (If either is 255, we still
   1.560 -	 * need to invalidate.)
   1.561 -	 */
   1.562 -	if((surface->flags & SDL_HWACCEL) == SDL_HWACCEL
   1.563 -	   || oldflags != surface->flags
   1.564 -	   || (((oldalpha + 1) ^ (value + 1)) & 0x100))
   1.565 -		SDL_InvalidateMap(surface->map);
   1.566 -	return(0);
   1.567 -}
   1.568 -int SDL_SetAlphaChannel(SDL_Surface *surface, Uint8 value)
   1.569 -{
   1.570 -	int row, col;
   1.571 -	int offset;
   1.572 -	Uint8 *buf;
   1.573 -
   1.574 -	if ( (surface->format->Amask != 0xFF000000) &&
   1.575 -	     (surface->format->Amask != 0x000000FF) ) {
   1.576 -		SDL_SetError("Unsupported surface alpha mask format");
   1.577 -		return -1;
   1.578 -	}
   1.579 -
   1.580 +    if ((surface->format->Amask != 0xFF000000) &&
   1.581 +        (surface->format->Amask != 0x000000FF)) {
   1.582 +        SDL_SetError("Unsupported surface alpha mask format");
   1.583 +        return -1;
   1.584 +    }
   1.585  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.586 -	if ( surface->format->Amask == 0xFF000000 ) {
   1.587 -			offset = 3;
   1.588 -	} else {
   1.589 -			offset = 0;
   1.590 -	}
   1.591 +    if (surface->format->Amask == 0xFF000000) {
   1.592 +        offset = 3;
   1.593 +    } else {
   1.594 +        offset = 0;
   1.595 +    }
   1.596  #else
   1.597 -	if ( surface->format->Amask == 0xFF000000 ) {
   1.598 -			offset = 0;
   1.599 -	} else {
   1.600 -			offset = 3;
   1.601 -	}
   1.602 +    if (surface->format->Amask == 0xFF000000) {
   1.603 +        offset = 0;
   1.604 +    } else {
   1.605 +        offset = 3;
   1.606 +    }
   1.607  #endif /* Byte ordering */
   1.608  
   1.609 -	/* Quickly set the alpha channel of an RGBA or ARGB surface */
   1.610 -	if ( SDL_MUSTLOCK(surface) ) {
   1.611 -		if ( SDL_LockSurface(surface) < 0 ) {
   1.612 -			return -1;
   1.613 -		}
   1.614 -	}
   1.615 -	row = surface->h;
   1.616 -	while (row--) {
   1.617 -		col = surface->w;
   1.618 -		buf = (Uint8 *)surface->pixels + row * surface->pitch + offset;
   1.619 -		while(col--) {
   1.620 -			*buf = value;
   1.621 -			buf += 4;
   1.622 -		}
   1.623 -	}
   1.624 -	if ( SDL_MUSTLOCK(surface) ) {
   1.625 -		SDL_UnlockSurface(surface);
   1.626 -	}
   1.627 -	return 0;
   1.628 +    /* Quickly set the alpha channel of an RGBA or ARGB surface */
   1.629 +    if (SDL_MUSTLOCK(surface)) {
   1.630 +        if (SDL_LockSurface(surface) < 0) {
   1.631 +            return -1;
   1.632 +        }
   1.633 +    }
   1.634 +    row = surface->h;
   1.635 +    while (row--) {
   1.636 +        col = surface->w;
   1.637 +        buf = (Uint8 *) surface->pixels + row * surface->pitch + offset;
   1.638 +        while (col--) {
   1.639 +            *buf = value;
   1.640 +            buf += 4;
   1.641 +        }
   1.642 +    }
   1.643 +    if (SDL_MUSTLOCK(surface)) {
   1.644 +        SDL_UnlockSurface(surface);
   1.645 +    }
   1.646 +    return 0;
   1.647  }
   1.648  
   1.649  /*
   1.650 - * A function to calculate the intersection of two rectangles:
   1.651 - * return true if the rectangles intersect, false otherwise
   1.652 - */
   1.653 -static __inline__
   1.654 -SDL_bool SDL_IntersectRect(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *intersection)
   1.655 -{
   1.656 -	int Amin, Amax, Bmin, Bmax;
   1.657 -
   1.658 -	/* Horizontal intersection */
   1.659 -	Amin = A->x;
   1.660 -	Amax = Amin + A->w;
   1.661 -	Bmin = B->x;
   1.662 -	Bmax = Bmin + B->w;
   1.663 -	if(Bmin > Amin)
   1.664 -	        Amin = Bmin;
   1.665 -	intersection->x = Amin;
   1.666 -	if(Bmax < Amax)
   1.667 -	        Amax = Bmax;
   1.668 -	intersection->w = Amax - Amin > 0 ? Amax - Amin : 0;
   1.669 -
   1.670 -	/* Vertical intersection */
   1.671 -	Amin = A->y;
   1.672 -	Amax = Amin + A->h;
   1.673 -	Bmin = B->y;
   1.674 -	Bmax = Bmin + B->h;
   1.675 -	if(Bmin > Amin)
   1.676 -	        Amin = Bmin;
   1.677 -	intersection->y = Amin;
   1.678 -	if(Bmax < Amax)
   1.679 -	        Amax = Bmax;
   1.680 -	intersection->h = Amax - Amin > 0 ? Amax - Amin : 0;
   1.681 -
   1.682 -	return (intersection->w && intersection->h);
   1.683 -}
   1.684 -/*
   1.685   * Set the clipping rectangle for a blittable surface
   1.686   */
   1.687 -SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
   1.688 +SDL_bool
   1.689 +SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
   1.690  {
   1.691 -	SDL_Rect full_rect;
   1.692 +    SDL_Rect full_rect;
   1.693  
   1.694 -	/* Don't do anything if there's no surface to act on */
   1.695 -	if ( ! surface ) {
   1.696 -		return SDL_FALSE;
   1.697 -	}
   1.698 +    /* Don't do anything if there's no surface to act on */
   1.699 +    if (!surface) {
   1.700 +        return SDL_FALSE;
   1.701 +    }
   1.702  
   1.703 -	/* Set up the full surface rectangle */
   1.704 -	full_rect.x = 0;
   1.705 -	full_rect.y = 0;
   1.706 -	full_rect.w = surface->w;
   1.707 -	full_rect.h = surface->h;
   1.708 +    /* Set up the full surface rectangle */
   1.709 +    full_rect.x = 0;
   1.710 +    full_rect.y = 0;
   1.711 +    full_rect.w = surface->w;
   1.712 +    full_rect.h = surface->h;
   1.713  
   1.714 -	/* Set the clipping rectangle */
   1.715 -	if ( ! rect ) {
   1.716 -		surface->clip_rect = full_rect;
   1.717 -		return 1;
   1.718 -	}
   1.719 -	return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
   1.720 +    /* Set the clipping rectangle */
   1.721 +    if (!rect) {
   1.722 +        surface->clip_rect = full_rect;
   1.723 +        return 1;
   1.724 +    }
   1.725 +    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
   1.726  }
   1.727 -void SDL_GetClipRect(SDL_Surface *surface, SDL_Rect *rect)
   1.728 +
   1.729 +void
   1.730 +SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
   1.731  {
   1.732 -	if ( surface && rect ) {
   1.733 -		*rect = surface->clip_rect;
   1.734 -	}
   1.735 +    if (surface && rect) {
   1.736 +        *rect = surface->clip_rect;
   1.737 +    }
   1.738  }
   1.739 +
   1.740  /* 
   1.741   * Set up a blit between two surfaces -- split into three parts:
   1.742   * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
   1.743 @@ -404,538 +453,494 @@
   1.744   * you know exactly what you are doing, you can optimize your code
   1.745   * by calling the one(s) you need.
   1.746   */
   1.747 -int SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect,
   1.748 -				SDL_Surface *dst, SDL_Rect *dstrect)
   1.749 +int
   1.750 +SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
   1.751 +              SDL_Surface * dst, SDL_Rect * dstrect)
   1.752  {
   1.753 -	SDL_blit do_blit;
   1.754 -	SDL_Rect hw_srcrect;
   1.755 -	SDL_Rect hw_dstrect;
   1.756 -
   1.757 -	/* Check to make sure the blit mapping is valid */
   1.758 -	if ( (src->map->dst != dst) ||
   1.759 -             (src->map->dst->format_version != src->map->format_version) ) {
   1.760 -		if ( SDL_MapSurface(src, dst) < 0 ) {
   1.761 -			return(-1);
   1.762 -		}
   1.763 -	}
   1.764 -
   1.765 -	/* Figure out which blitter to use */
   1.766 -	if ( (src->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
   1.767 -		if ( src == SDL_VideoSurface ) {
   1.768 -			hw_srcrect = *srcrect;
   1.769 -			hw_srcrect.x += current_video->offset_x;
   1.770 -			hw_srcrect.y += current_video->offset_y;
   1.771 -			srcrect = &hw_srcrect;
   1.772 -		}
   1.773 -		if ( dst == SDL_VideoSurface ) {
   1.774 -			hw_dstrect = *dstrect;
   1.775 -			hw_dstrect.x += current_video->offset_x;
   1.776 -			hw_dstrect.y += current_video->offset_y;
   1.777 -			dstrect = &hw_dstrect;
   1.778 -		}
   1.779 -		do_blit = src->map->hw_blit;
   1.780 -	} else {
   1.781 -		do_blit = src->map->sw_blit;
   1.782 -	}
   1.783 -	return(do_blit(src, srcrect, dst, dstrect));
   1.784 +    /* Check to make sure the blit mapping is valid */
   1.785 +    if ((src->map->dst != dst) ||
   1.786 +        (src->map->dst->format_version != src->map->format_version)) {
   1.787 +        if (SDL_MapSurface(src, dst) < 0) {
   1.788 +            return (-1);
   1.789 +        }
   1.790 +    }
   1.791 +    return (src->map->sw_blit(src, srcrect, dst, dstrect));
   1.792  }
   1.793  
   1.794  
   1.795 -int SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect,
   1.796 -		   SDL_Surface *dst, SDL_Rect *dstrect)
   1.797 +int
   1.798 +SDL_UpperBlit(SDL_Surface * src, SDL_Rect * srcrect,
   1.799 +              SDL_Surface * dst, SDL_Rect * dstrect)
   1.800  {
   1.801 -        SDL_Rect fulldst;
   1.802 -	int srcx, srcy, w, h;
   1.803 +    SDL_Rect fulldst;
   1.804 +    int srcx, srcy, w, h;
   1.805  
   1.806 -	/* Make sure the surfaces aren't locked */
   1.807 -	if ( ! src || ! dst ) {
   1.808 -		SDL_SetError("SDL_UpperBlit: passed a NULL surface");
   1.809 -		return(-1);
   1.810 -	}
   1.811 -	if ( src->locked || dst->locked ) {
   1.812 -		SDL_SetError("Surfaces must not be locked during blit");
   1.813 -		return(-1);
   1.814 -	}
   1.815 +    /* Make sure the surfaces aren't locked */
   1.816 +    if (!src || !dst) {
   1.817 +        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
   1.818 +        return (-1);
   1.819 +    }
   1.820 +    if (src->locked || dst->locked) {
   1.821 +        SDL_SetError("Surfaces must not be locked during blit");
   1.822 +        return (-1);
   1.823 +    }
   1.824  
   1.825 -	/* If the destination rectangle is NULL, use the entire dest surface */
   1.826 -	if ( dstrect == NULL ) {
   1.827 -	        fulldst.x = fulldst.y = 0;
   1.828 -		dstrect = &fulldst;
   1.829 -	}
   1.830 +    /* If the destination rectangle is NULL, use the entire dest surface */
   1.831 +    if (dstrect == NULL) {
   1.832 +        fulldst.x = fulldst.y = 0;
   1.833 +        dstrect = &fulldst;
   1.834 +    }
   1.835  
   1.836 -	/* clip the source rectangle to the source surface */
   1.837 -	if(srcrect) {
   1.838 -	        int maxw, maxh;
   1.839 -	
   1.840 -		srcx = srcrect->x;
   1.841 -		w = srcrect->w;
   1.842 -		if(srcx < 0) {
   1.843 -		        w += srcx;
   1.844 -			dstrect->x -= srcx;
   1.845 -			srcx = 0;
   1.846 -		}
   1.847 -		maxw = src->w - srcx;
   1.848 -		if(maxw < w)
   1.849 -			w = maxw;
   1.850 +    /* clip the source rectangle to the source surface */
   1.851 +    if (srcrect) {
   1.852 +        int maxw, maxh;
   1.853  
   1.854 -		srcy = srcrect->y;
   1.855 -		h = srcrect->h;
   1.856 -		if(srcy < 0) {
   1.857 -		        h += srcy;
   1.858 -			dstrect->y -= srcy;
   1.859 -			srcy = 0;
   1.860 -		}
   1.861 -		maxh = src->h - srcy;
   1.862 -		if(maxh < h)
   1.863 -			h = maxh;
   1.864 -	    
   1.865 -	} else {
   1.866 -	        srcx = srcy = 0;
   1.867 -		w = src->w;
   1.868 -		h = src->h;
   1.869 -	}
   1.870 +        srcx = srcrect->x;
   1.871 +        w = srcrect->w;
   1.872 +        if (srcx < 0) {
   1.873 +            w += srcx;
   1.874 +            dstrect->x -= srcx;
   1.875 +            srcx = 0;
   1.876 +        }
   1.877 +        maxw = src->w - srcx;
   1.878 +        if (maxw < w)
   1.879 +            w = maxw;
   1.880  
   1.881 -	/* clip the destination rectangle against the clip rectangle */
   1.882 -	{
   1.883 -	        SDL_Rect *clip = &dst->clip_rect;
   1.884 -		int dx, dy;
   1.885 +        srcy = srcrect->y;
   1.886 +        h = srcrect->h;
   1.887 +        if (srcy < 0) {
   1.888 +            h += srcy;
   1.889 +            dstrect->y -= srcy;
   1.890 +            srcy = 0;
   1.891 +        }
   1.892 +        maxh = src->h - srcy;
   1.893 +        if (maxh < h)
   1.894 +            h = maxh;
   1.895  
   1.896 -		dx = clip->x - dstrect->x;
   1.897 -		if(dx > 0) {
   1.898 -			w -= dx;
   1.899 -			dstrect->x += dx;
   1.900 -			srcx += dx;
   1.901 -		}
   1.902 -		dx = dstrect->x + w - clip->x - clip->w;
   1.903 -		if(dx > 0)
   1.904 -			w -= dx;
   1.905 +    } else {
   1.906 +        srcx = srcy = 0;
   1.907 +        w = src->w;
   1.908 +        h = src->h;
   1.909 +    }
   1.910  
   1.911 -		dy = clip->y - dstrect->y;
   1.912 -		if(dy > 0) {
   1.913 -			h -= dy;
   1.914 -			dstrect->y += dy;
   1.915 -			srcy += dy;
   1.916 -		}
   1.917 -		dy = dstrect->y + h - clip->y - clip->h;
   1.918 -		if(dy > 0)
   1.919 -			h -= dy;
   1.920 -	}
   1.921 +    /* clip the destination rectangle against the clip rectangle */
   1.922 +    {
   1.923 +        SDL_Rect *clip = &dst->clip_rect;
   1.924 +        int dx, dy;
   1.925  
   1.926 -	if(w > 0 && h > 0) {
   1.927 -	        SDL_Rect sr;
   1.928 -	        sr.x = srcx;
   1.929 -		sr.y = srcy;
   1.930 -		sr.w = dstrect->w = w;
   1.931 -		sr.h = dstrect->h = h;
   1.932 -		return SDL_LowerBlit(src, &sr, dst, dstrect);
   1.933 -	}
   1.934 -	dstrect->w = dstrect->h = 0;
   1.935 -	return 0;
   1.936 +        dx = clip->x - dstrect->x;
   1.937 +        if (dx > 0) {
   1.938 +            w -= dx;
   1.939 +            dstrect->x += dx;
   1.940 +            srcx += dx;
   1.941 +        }
   1.942 +        dx = dstrect->x + w - clip->x - clip->w;
   1.943 +        if (dx > 0)
   1.944 +            w -= dx;
   1.945 +
   1.946 +        dy = clip->y - dstrect->y;
   1.947 +        if (dy > 0) {
   1.948 +            h -= dy;
   1.949 +            dstrect->y += dy;
   1.950 +            srcy += dy;
   1.951 +        }
   1.952 +        dy = dstrect->y + h - clip->y - clip->h;
   1.953 +        if (dy > 0)
   1.954 +            h -= dy;
   1.955 +    }
   1.956 +
   1.957 +    if (w > 0 && h > 0) {
   1.958 +        SDL_Rect sr;
   1.959 +        sr.x = srcx;
   1.960 +        sr.y = srcy;
   1.961 +        sr.w = dstrect->w = w;
   1.962 +        sr.h = dstrect->h = h;
   1.963 +        return SDL_LowerBlit(src, &sr, dst, dstrect);
   1.964 +    }
   1.965 +    dstrect->w = dstrect->h = 0;
   1.966 +    return 0;
   1.967  }
   1.968  
   1.969 -static int SDL_FillRect1(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
   1.970 +static int
   1.971 +SDL_FillRect1(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
   1.972  {
   1.973 -	/* FIXME: We have to worry about packing order.. *sigh* */
   1.974 -	SDL_SetError("1-bpp rect fill not yet implemented");
   1.975 -	return -1;
   1.976 +    /* FIXME: We have to worry about packing order.. *sigh* */
   1.977 +    SDL_SetError("1-bpp rect fill not yet implemented");
   1.978 +    return -1;
   1.979  }
   1.980  
   1.981 -static int SDL_FillRect4(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
   1.982 +static int
   1.983 +SDL_FillRect4(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
   1.984  {
   1.985 -	/* FIXME: We have to worry about packing order.. *sigh* */
   1.986 -	SDL_SetError("4-bpp rect fill not yet implemented");
   1.987 -	return -1;
   1.988 +    /* FIXME: We have to worry about packing order.. *sigh* */
   1.989 +    SDL_SetError("4-bpp rect fill not yet implemented");
   1.990 +    return -1;
   1.991  }
   1.992  
   1.993  /* 
   1.994   * This function performs a fast fill of the given rectangle with 'color'
   1.995   */
   1.996 -int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
   1.997 +int
   1.998 +SDL_FillRect(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
   1.999  {
  1.1000 -	SDL_VideoDevice *video = current_video;
  1.1001 -	SDL_VideoDevice *this  = current_video;
  1.1002 -	int x, y;
  1.1003 -	Uint8 *row;
  1.1004 +    int x, y;
  1.1005 +    Uint8 *row;
  1.1006  
  1.1007 -	/* This function doesn't work on surfaces < 8 bpp */
  1.1008 -	if ( dst->format->BitsPerPixel < 8 ) {
  1.1009 -		switch(dst->format->BitsPerPixel) {
  1.1010 -		    case 1:
  1.1011 -			return SDL_FillRect1(dst, dstrect, color);
  1.1012 -			break;
  1.1013 -		    case 4:
  1.1014 -			return SDL_FillRect4(dst, dstrect, color);
  1.1015 -			break;
  1.1016 -		    default:
  1.1017 -			SDL_SetError("Fill rect on unsupported surface format");
  1.1018 -			return(-1);
  1.1019 -			break;
  1.1020 -		}
  1.1021 -	}
  1.1022 +    /* This function doesn't work on surfaces < 8 bpp */
  1.1023 +    if (dst->format->BitsPerPixel < 8) {
  1.1024 +        switch (dst->format->BitsPerPixel) {
  1.1025 +        case 1:
  1.1026 +            return SDL_FillRect1(dst, dstrect, color);
  1.1027 +            break;
  1.1028 +        case 4:
  1.1029 +            return SDL_FillRect4(dst, dstrect, color);
  1.1030 +            break;
  1.1031 +        default:
  1.1032 +            SDL_SetError("Fill rect on unsupported surface format");
  1.1033 +            return (-1);
  1.1034 +            break;
  1.1035 +        }
  1.1036 +    }
  1.1037  
  1.1038 -	/* If 'dstrect' == NULL, then fill the whole surface */
  1.1039 -	if ( dstrect ) {
  1.1040 -		/* Perform clipping */
  1.1041 -		if ( !SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect) ) {
  1.1042 -			return(0);
  1.1043 -		}
  1.1044 -	} else {
  1.1045 -		dstrect = &dst->clip_rect;
  1.1046 -	}
  1.1047 +    /* If 'dstrect' == NULL, then fill the whole surface */
  1.1048 +    if (dstrect) {
  1.1049 +        /* Perform clipping */
  1.1050 +        if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
  1.1051 +            return (0);
  1.1052 +        }
  1.1053 +    } else {
  1.1054 +        dstrect = &dst->clip_rect;
  1.1055 +    }
  1.1056  
  1.1057 -	/* Check for hardware acceleration */
  1.1058 -	if ( ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) &&
  1.1059 -					video->info.blit_fill ) {
  1.1060 -		SDL_Rect hw_rect;
  1.1061 -		if ( dst == SDL_VideoSurface ) {
  1.1062 -			hw_rect = *dstrect;
  1.1063 -			hw_rect.x += current_video->offset_x;
  1.1064 -			hw_rect.y += current_video->offset_y;
  1.1065 -			dstrect = &hw_rect;
  1.1066 -		}
  1.1067 -		return(video->FillHWRect(this, dst, dstrect, color));
  1.1068 -	}
  1.1069 +    /* Perform software fill */
  1.1070 +    if (SDL_LockSurface(dst) != 0) {
  1.1071 +        return (-1);
  1.1072 +    }
  1.1073 +    row = (Uint8 *) dst->pixels + dstrect->y * dst->pitch +
  1.1074 +        dstrect->x * dst->format->BytesPerPixel;
  1.1075 +    if (dst->format->palette || (color == 0)) {
  1.1076 +        x = dstrect->w * dst->format->BytesPerPixel;
  1.1077 +        if (!color && !((uintptr_t) row & 3) && !(x & 3)
  1.1078 +            && !(dst->pitch & 3)) {
  1.1079 +            int n = x >> 2;
  1.1080 +            for (y = dstrect->h; y; --y) {
  1.1081 +                SDL_memset4(row, 0, n);
  1.1082 +                row += dst->pitch;
  1.1083 +            }
  1.1084 +        } else {
  1.1085 +#ifdef __powerpc__
  1.1086 +            /*
  1.1087 +             * SDL_memset() on PPC (both glibc and codewarrior) uses
  1.1088 +             * the dcbz (Data Cache Block Zero) instruction, which
  1.1089 +             * causes an alignment exception if the destination is
  1.1090 +             * uncachable, so only use it on software surfaces
  1.1091 +             */
  1.1092 +            if (dst->flags & SDL_HWSURFACE) {
  1.1093 +                if (dstrect->w >= 8) {
  1.1094 +                    /*
  1.1095 +                     * 64-bit stores are probably most
  1.1096 +                     * efficient to uncached video memory
  1.1097 +                     */
  1.1098 +                    double fill;
  1.1099 +                    SDL_memset(&fill, color, (sizeof fill));
  1.1100 +                    for (y = dstrect->h; y; y--) {
  1.1101 +                        Uint8 *d = row;
  1.1102 +                        unsigned n = x;
  1.1103 +                        unsigned nn;
  1.1104 +                        Uint8 c = color;
  1.1105 +                        double f = fill;
  1.1106 +                        while ((unsigned long) d & (sizeof(double) - 1)) {
  1.1107 +                            *d++ = c;
  1.1108 +                            n--;
  1.1109 +                        }
  1.1110 +                        nn = n / (sizeof(double) * 4);
  1.1111 +                        while (nn) {
  1.1112 +                            ((double *) d)[0] = f;
  1.1113 +                            ((double *) d)[1] = f;
  1.1114 +                            ((double *) d)[2] = f;
  1.1115 +                            ((double *) d)[3] = f;
  1.1116 +                            d += 4 * sizeof(double);
  1.1117 +                            nn--;
  1.1118 +                        }
  1.1119 +                        n &= ~(sizeof(double) * 4 - 1);
  1.1120 +                        nn = n / sizeof(double);
  1.1121 +                        while (nn) {
  1.1122 +                            *(double *) d = f;
  1.1123 +                            d += sizeof(double);
  1.1124 +                            nn--;
  1.1125 +                        }
  1.1126 +                        n &= ~(sizeof(double) - 1);
  1.1127 +                        while (n) {
  1.1128 +                            *d++ = c;
  1.1129 +                            n--;
  1.1130 +                        }
  1.1131 +                        row += dst->pitch;
  1.1132 +                    }
  1.1133 +                } else {
  1.1134 +                    /* narrow boxes */
  1.1135 +                    for (y = dstrect->h; y; y--) {
  1.1136 +                        Uint8 *d = row;
  1.1137 +                        Uint8 c = color;
  1.1138 +                        int n = x;
  1.1139 +                        while (n) {
  1.1140 +                            *d++ = c;
  1.1141 +                            n--;
  1.1142 +                        }
  1.1143 +                        row += dst->pitch;
  1.1144 +                    }
  1.1145 +                }
  1.1146 +            } else
  1.1147 +#endif /* __powerpc__ */
  1.1148 +            {
  1.1149 +                for (y = dstrect->h; y; y--) {
  1.1150 +                    SDL_memset(row, color, x);
  1.1151 +                    row += dst->pitch;
  1.1152 +                }
  1.1153 +            }
  1.1154 +        }
  1.1155 +    } else {
  1.1156 +        switch (dst->format->BytesPerPixel) {
  1.1157 +        case 2:
  1.1158 +            for (y = dstrect->h; y; --y) {
  1.1159 +                Uint16 *pixels = (Uint16 *) row;
  1.1160 +                Uint16 c = (Uint16) color;
  1.1161 +                Uint32 cc = (Uint32) c << 16 | c;
  1.1162 +                int n = dstrect->w;
  1.1163 +                if ((uintptr_t) pixels & 3) {
  1.1164 +                    *pixels++ = c;
  1.1165 +                    n--;
  1.1166 +                }
  1.1167 +                if (n >> 1)
  1.1168 +                    SDL_memset4(pixels, cc, n >> 1);
  1.1169 +                if (n & 1)
  1.1170 +                    pixels[n - 1] = c;
  1.1171 +                row += dst->pitch;
  1.1172 +            }
  1.1173 +            break;
  1.1174  
  1.1175 -	/* Perform software fill */
  1.1176 -	if ( SDL_LockSurface(dst) != 0 ) {
  1.1177 -		return(-1);
  1.1178 -	}
  1.1179 -	row = (Uint8 *)dst->pixels+dstrect->y*dst->pitch+
  1.1180 -			dstrect->x*dst->format->BytesPerPixel;
  1.1181 -	if ( dst->format->palette || (color == 0) ) {
  1.1182 -		x = dstrect->w*dst->format->BytesPerPixel;
  1.1183 -		if ( !color && !((uintptr_t)row&3) && !(x&3) && !(dst->pitch&3) ) {
  1.1184 -			int n = x >> 2;
  1.1185 -			for ( y=dstrect->h; y; --y ) {
  1.1186 -				SDL_memset4(row, 0, n);
  1.1187 -				row += dst->pitch;
  1.1188 -			}
  1.1189 -		} else {
  1.1190 -#ifdef __powerpc__
  1.1191 -			/*
  1.1192 -			 * SDL_memset() on PPC (both glibc and codewarrior) uses
  1.1193 -			 * the dcbz (Data Cache Block Zero) instruction, which
  1.1194 -			 * causes an alignment exception if the destination is
  1.1195 -			 * uncachable, so only use it on software surfaces
  1.1196 -			 */
  1.1197 -			if((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
  1.1198 -				if(dstrect->w >= 8) {
  1.1199 -					/*
  1.1200 -					 * 64-bit stores are probably most
  1.1201 -					 * efficient to uncached video memory
  1.1202 -					 */
  1.1203 -					double fill;
  1.1204 -					SDL_memset(&fill, color, (sizeof fill));
  1.1205 -					for(y = dstrect->h; y; y--) {
  1.1206 -						Uint8 *d = row;
  1.1207 -						unsigned n = x;
  1.1208 -						unsigned nn;
  1.1209 -						Uint8 c = color;
  1.1210 -						double f = fill;
  1.1211 -						while((unsigned long)d
  1.1212 -						      & (sizeof(double) - 1)) {
  1.1213 -							*d++ = c;
  1.1214 -							n--;
  1.1215 -						}
  1.1216 -						nn = n / (sizeof(double) * 4);
  1.1217 -						while(nn) {
  1.1218 -							((double *)d)[0] = f;
  1.1219 -							((double *)d)[1] = f;
  1.1220 -							((double *)d)[2] = f;
  1.1221 -							((double *)d)[3] = f;
  1.1222 -							d += 4*sizeof(double);
  1.1223 -							nn--;
  1.1224 -						}
  1.1225 -						n &= ~(sizeof(double) * 4 - 1);
  1.1226 -						nn = n / sizeof(double);
  1.1227 -						while(nn) {
  1.1228 -							*(double *)d = f;
  1.1229 -							d += sizeof(double);
  1.1230 -							nn--;
  1.1231 -						}
  1.1232 -						n &= ~(sizeof(double) - 1);
  1.1233 -						while(n) {
  1.1234 -							*d++ = c;
  1.1235 -							n--;
  1.1236 -						}
  1.1237 -						row += dst->pitch;
  1.1238 -					}
  1.1239 -				} else {
  1.1240 -					/* narrow boxes */
  1.1241 -					for(y = dstrect->h; y; y--) {
  1.1242 -						Uint8 *d = row;
  1.1243 -						Uint8 c = color;
  1.1244 -						int n = x;
  1.1245 -						while(n) {
  1.1246 -							*d++ = c;
  1.1247 -							n--;
  1.1248 -						}
  1.1249 -						row += dst->pitch;
  1.1250 -					}
  1.1251 -				}
  1.1252 -			} else
  1.1253 -#endif /* __powerpc__ */
  1.1254 -			{
  1.1255 -				for(y = dstrect->h; y; y--) {
  1.1256 -					SDL_memset(row, color, x);
  1.1257 -					row += dst->pitch;
  1.1258 -				}
  1.1259 -			}
  1.1260 -		}
  1.1261 -	} else {
  1.1262 -		switch (dst->format->BytesPerPixel) {
  1.1263 -		    case 2:
  1.1264 -			for ( y=dstrect->h; y; --y ) {
  1.1265 -				Uint16 *pixels = (Uint16 *)row;
  1.1266 -				Uint16 c = (Uint16)color;
  1.1267 -				Uint32 cc = (Uint32)c << 16 | c;
  1.1268 -				int n = dstrect->w;
  1.1269 -				if((uintptr_t)pixels & 3) {
  1.1270 -					*pixels++ = c;
  1.1271 -					n--;
  1.1272 -				}
  1.1273 -				if(n >> 1)
  1.1274 -					SDL_memset4(pixels, cc, n >> 1);
  1.1275 -				if(n & 1)
  1.1276 -					pixels[n - 1] = c;
  1.1277 -				row += dst->pitch;
  1.1278 -			}
  1.1279 -			break;
  1.1280 +        case 3:
  1.1281 +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1.1282 +            color <<= 8;
  1.1283 +#endif
  1.1284 +            for (y = dstrect->h; y; --y) {
  1.1285 +                Uint8 *pixels = row;
  1.1286 +                for (x = dstrect->w; x; --x) {
  1.1287 +                    SDL_memcpy(pixels, &color, 3);
  1.1288 +                    pixels += 3;
  1.1289 +                }
  1.1290 +                row += dst->pitch;
  1.1291 +            }
  1.1292 +            break;
  1.1293  
  1.1294 -		    case 3:
  1.1295 -			#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1.1296 -				color <<= 8;
  1.1297 -			#endif
  1.1298 -			for ( y=dstrect->h; y; --y ) {
  1.1299 -				Uint8 *pixels = row;
  1.1300 -				for ( x=dstrect->w; x; --x ) {
  1.1301 -					SDL_memcpy(pixels, &color, 3);
  1.1302 -					pixels += 3;
  1.1303 -				}
  1.1304 -				row += dst->pitch;
  1.1305 -			}
  1.1306 -			break;
  1.1307 +        case 4:
  1.1308 +            for (y = dstrect->h; y; --y) {
  1.1309 +                SDL_memset4(row, color, dstrect->w);
  1.1310 +                row += dst->pitch;
  1.1311 +            }
  1.1312 +            break;
  1.1313 +        }
  1.1314 +    }
  1.1315 +    SDL_UnlockSurface(dst);
  1.1316  
  1.1317 -		    case 4:
  1.1318 -			for(y = dstrect->h; y; --y) {
  1.1319 -				SDL_memset4(row, color, dstrect->w);
  1.1320 -				row += dst->pitch;
  1.1321 -			}
  1.1322 -			break;
  1.1323 -		}
  1.1324 -	}
  1.1325 -	SDL_UnlockSurface(dst);
  1.1326 -
  1.1327 -	/* We're done! */
  1.1328 -	return(0);
  1.1329 +    /* We're done! */
  1.1330 +    return (0);
  1.1331  }
  1.1332  
  1.1333  /*
  1.1334   * Lock a surface to directly access the pixels
  1.1335   */
  1.1336 -int SDL_LockSurface (SDL_Surface *surface)
  1.1337 +int
  1.1338 +SDL_LockSurface(SDL_Surface * surface)
  1.1339  {
  1.1340 -	if ( ! surface->locked ) {
  1.1341 -		/* Perform the lock */
  1.1342 -		if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
  1.1343 -			SDL_VideoDevice *video = current_video;
  1.1344 -			SDL_VideoDevice *this  = current_video;
  1.1345 -			if ( video->LockHWSurface(this, surface) < 0 ) {
  1.1346 -				return(-1);
  1.1347 -			}
  1.1348 -		}
  1.1349 -		if ( surface->flags & SDL_RLEACCEL ) {
  1.1350 -			SDL_UnRLESurface(surface, 1);
  1.1351 -			surface->flags |= SDL_RLEACCEL;	/* save accel'd state */
  1.1352 -		}
  1.1353 -		/* This needs to be done here in case pixels changes value */
  1.1354 -		surface->pixels = (Uint8 *)surface->pixels + surface->offset;
  1.1355 -	}
  1.1356 +    if (!surface->locked) {
  1.1357 +        /* Perform the lock */
  1.1358 +        if (surface->flags & SDL_HWSURFACE) {
  1.1359 +            if (SDL_LockTexture
  1.1360 +                (surface->textureID, NULL, 1, &surface->pixels,
  1.1361 +                 &surface->pitch) < 0) {
  1.1362 +                return (-1);
  1.1363 +            }
  1.1364 +        }
  1.1365 +        if (surface->flags & SDL_RLEACCEL) {
  1.1366 +            SDL_UnRLESurface(surface, 1);
  1.1367 +            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
  1.1368 +        }
  1.1369 +    }
  1.1370  
  1.1371 -	/* Increment the surface lock count, for recursive locks */
  1.1372 -	++surface->locked;
  1.1373 +    /* Increment the surface lock count, for recursive locks */
  1.1374 +    ++surface->locked;
  1.1375  
  1.1376 -	/* Ready to go.. */
  1.1377 -	return(0);
  1.1378 +    /* Ready to go.. */
  1.1379 +    return (0);
  1.1380  }
  1.1381 +
  1.1382  /*
  1.1383   * Unlock a previously locked surface
  1.1384   */
  1.1385 -void SDL_UnlockSurface (SDL_Surface *surface)
  1.1386 +void
  1.1387 +SDL_UnlockSurface(SDL_Surface * surface)
  1.1388  {
  1.1389 -	/* Only perform an unlock if we are locked */
  1.1390 -	if ( ! surface->locked || (--surface->locked > 0) ) {
  1.1391 -		return;
  1.1392 -	}
  1.1393 +    /* Only perform an unlock if we are locked */
  1.1394 +    if (!surface->locked || (--surface->locked > 0)) {
  1.1395 +        return;
  1.1396 +    }
  1.1397  
  1.1398 -	/* Perform the unlock */
  1.1399 -	surface->pixels = (Uint8 *)surface->pixels - surface->offset;
  1.1400 +    /* Unlock hardware or accelerated surfaces */
  1.1401 +    if (surface->flags & SDL_HWSURFACE) {
  1.1402 +        SDL_UnlockTexture(surface->textureID);
  1.1403 +    }
  1.1404  
  1.1405 -	/* Unlock hardware or accelerated surfaces */
  1.1406 -	if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
  1.1407 -		SDL_VideoDevice *video = current_video;
  1.1408 -		SDL_VideoDevice *this  = current_video;
  1.1409 -		video->UnlockHWSurface(this, surface);
  1.1410 -	} else {
  1.1411 -		/* Update RLE encoded surface with new data */
  1.1412 -		if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
  1.1413 -		        surface->flags &= ~SDL_RLEACCEL; /* stop lying */
  1.1414 -			SDL_RLESurface(surface);
  1.1415 -		}
  1.1416 -	}
  1.1417 +    /* Update RLE encoded surface with new data */
  1.1418 +    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
  1.1419 +        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
  1.1420 +        SDL_RLESurface(surface);
  1.1421 +    }
  1.1422  }
  1.1423  
  1.1424  /* 
  1.1425   * Convert a surface into the specified pixel format.
  1.1426   */
  1.1427 -SDL_Surface * SDL_ConvertSurface (SDL_Surface *surface,
  1.1428 -					SDL_PixelFormat *format, Uint32 flags)
  1.1429 +SDL_Surface *
  1.1430 +SDL_ConvertSurface(SDL_Surface * surface,
  1.1431 +                   SDL_PixelFormat * format, Uint32 flags)
  1.1432  {
  1.1433 -	SDL_Surface *convert;
  1.1434 -	Uint32 colorkey = 0;
  1.1435 -	Uint8 alpha = 0;
  1.1436 -	Uint32 surface_flags;
  1.1437 -	SDL_Rect bounds;
  1.1438 +    SDL_Surface *convert;
  1.1439 +    Uint32 colorkey = 0;
  1.1440 +    Uint8 alpha = 0;
  1.1441 +    Uint32 surface_flags;
  1.1442 +    SDL_Rect bounds;
  1.1443  
  1.1444 -	/* Check for empty destination palette! (results in empty image) */
  1.1445 -	if ( format->palette != NULL ) {
  1.1446 -		int i;
  1.1447 -		for ( i=0; i<format->palette->ncolors; ++i ) {
  1.1448 -			if ( (format->palette->colors[i].r != 0) ||
  1.1449 -			     (format->palette->colors[i].g != 0) ||
  1.1450 -			     (format->palette->colors[i].b != 0) )
  1.1451 -				break;
  1.1452 -		}
  1.1453 -		if ( i == format->palette->ncolors ) {
  1.1454 -			SDL_SetError("Empty destination palette");
  1.1455 -			return(NULL);
  1.1456 -		}
  1.1457 -	}
  1.1458 +    /* Check for empty destination palette! (results in empty image) */
  1.1459 +    if (format->palette != NULL) {
  1.1460 +        int i;
  1.1461 +        for (i = 0; i < format->palette->ncolors; ++i) {
  1.1462 +            if ((format->palette->colors[i].r != 0xFF) ||
  1.1463 +                (format->palette->colors[i].g != 0xFF) ||
  1.1464 +                (format->palette->colors[i].b != 0xFF))
  1.1465 +                break;
  1.1466 +        }
  1.1467 +        if (i == format->palette->ncolors) {
  1.1468 +            SDL_SetError("Empty destination palette");
  1.1469 +            return (NULL);
  1.1470 +        }
  1.1471 +    }
  1.1472  
  1.1473 -	/* Only create hw surfaces with alpha channel if hw alpha blits
  1.1474 -	   are supported */
  1.1475 -	if(format->Amask != 0 && (flags & SDL_HWSURFACE)) {
  1.1476 -		const SDL_VideoInfo *vi = SDL_GetVideoInfo();
  1.1477 -		if(!vi || !vi->blit_hw_A)
  1.1478 -			flags &= ~SDL_HWSURFACE;
  1.1479 -	}
  1.1480 +    /* Create a new surface with the desired format */
  1.1481 +    convert = SDL_CreateRGBSurface(flags,
  1.1482 +                                   surface->w, surface->h,
  1.1483 +                                   format->BitsPerPixel, format->Rmask,
  1.1484 +                                   format->Gmask, format->Bmask,
  1.1485 +                                   format->Amask);
  1.1486 +    if (convert == NULL) {
  1.1487 +        return (NULL);
  1.1488 +    }
  1.1489  
  1.1490 -	/* Create a new surface with the desired format */
  1.1491 -	convert = SDL_CreateRGBSurface(flags,
  1.1492 -				surface->w, surface->h, format->BitsPerPixel,
  1.1493 -		format->Rmask, format->Gmask, format->Bmask, format->Amask);
  1.1494 -	if ( convert == NULL ) {
  1.1495 -		return(NULL);
  1.1496 -	}
  1.1497 +    /* Copy the palette if any */
  1.1498 +    if (format->palette && convert->format->palette) {
  1.1499 +        SDL_memcpy(convert->format->palette->colors,
  1.1500 +                   format->palette->colors,
  1.1501 +                   format->palette->ncolors * sizeof(SDL_Color));
  1.1502 +        convert->format->palette->ncolors = format->palette->ncolors;
  1.1503 +    }
  1.1504  
  1.1505 -	/* Copy the palette if any */
  1.1506 -	if ( format->palette && convert->format->palette ) {
  1.1507 -		SDL_memcpy(convert->format->palette->colors,
  1.1508 -				format->palette->colors,
  1.1509 -				format->palette->ncolors*sizeof(SDL_Color));
  1.1510 -		convert->format->palette->ncolors = format->palette->ncolors;
  1.1511 -	}
  1.1512 +    /* Save the original surface color key and alpha */
  1.1513 +    surface_flags = surface->flags;
  1.1514 +    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
  1.1515 +        /* Convert colourkeyed surfaces to RGBA if requested */
  1.1516 +        if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
  1.1517 +            surface_flags &= ~SDL_SRCCOLORKEY;
  1.1518 +        } else {
  1.1519 +            colorkey = surface->format->colorkey;
  1.1520 +            SDL_SetColorKey(surface, 0, 0);
  1.1521 +        }
  1.1522 +    }
  1.1523 +    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1.1524 +        /* Copy over the alpha channel to RGBA if requested */
  1.1525 +        if (format->Amask) {
  1.1526 +            surface->flags &= ~SDL_SRCALPHA;
  1.1527 +        } else {
  1.1528 +            alpha = surface->format->alpha;
  1.1529 +            SDL_SetAlpha(surface, 0, 0);
  1.1530 +        }
  1.1531 +    }
  1.1532  
  1.1533 -	/* Save the original surface color key and alpha */
  1.1534 -	surface_flags = surface->flags;
  1.1535 -	if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
  1.1536 -		/* Convert colourkeyed surfaces to RGBA if requested */
  1.1537 -		if((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY
  1.1538 -		   && format->Amask) {
  1.1539 -			surface_flags &= ~SDL_SRCCOLORKEY;
  1.1540 -		} else {
  1.1541 -			colorkey = surface->format->colorkey;
  1.1542 -			SDL_SetColorKey(surface, 0, 0);
  1.1543 -		}
  1.1544 -	}
  1.1545 -	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
  1.1546 -		/* Copy over the alpha channel to RGBA if requested */
  1.1547 -		if ( format->Amask ) {
  1.1548 -			surface->flags &= ~SDL_SRCALPHA;
  1.1549 -		} else {
  1.1550 -			alpha = surface->format->alpha;
  1.1551 -			SDL_SetAlpha(surface, 0, 0);
  1.1552 -		}
  1.1553 -	}
  1.1554 +    /* Copy over the image data */
  1.1555 +    bounds.x = 0;
  1.1556 +    bounds.y = 0;
  1.1557 +    bounds.w = surface->w;
  1.1558 +    bounds.h = surface->h;
  1.1559 +    SDL_LowerBlit(surface, &bounds, convert, &bounds);
  1.1560  
  1.1561 -	/* Copy over the image data */
  1.1562 -	bounds.x = 0;
  1.1563 -	bounds.y = 0;
  1.1564 -	bounds.w = surface->w;
  1.1565 -	bounds.h = surface->h;
  1.1566 -	SDL_LowerBlit(surface, &bounds, convert, &bounds);
  1.1567 +    /* Clean up the original surface, and update converted surface */
  1.1568 +    if (convert != NULL) {
  1.1569 +        SDL_SetClipRect(convert, &surface->clip_rect);
  1.1570 +    }
  1.1571 +    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
  1.1572 +        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
  1.1573 +        if (convert != NULL) {
  1.1574 +            Uint8 keyR, keyG, keyB;
  1.1575  
  1.1576 -	/* Clean up the original surface, and update converted surface */
  1.1577 -	if ( convert != NULL ) {
  1.1578 -		SDL_SetClipRect(convert, &surface->clip_rect);
  1.1579 -	}
  1.1580 -	if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
  1.1581 -		Uint32 cflags = surface_flags&(SDL_SRCCOLORKEY|SDL_RLEACCELOK);
  1.1582 -		if ( convert != NULL ) {
  1.1583 -			Uint8 keyR, keyG, keyB;
  1.1584 +            SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
  1.1585 +            SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
  1.1586 +                            SDL_MapRGB(convert->format, keyR, keyG, keyB));
  1.1587 +        }
  1.1588 +        SDL_SetColorKey(surface, cflags, colorkey);
  1.1589 +    }
  1.1590 +    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
  1.1591 +        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
  1.1592 +        if (convert != NULL) {
  1.1593 +            SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
  1.1594 +        }
  1.1595 +        if (format->Amask) {
  1.1596 +            surface->flags |= SDL_SRCALPHA;
  1.1597 +        } else {
  1.1598 +            SDL_SetAlpha(surface, aflags, alpha);
  1.1599 +        }
  1.1600 +    }
  1.1601  
  1.1602 -			SDL_GetRGB(colorkey,surface->format,&keyR,&keyG,&keyB);
  1.1603 -			SDL_SetColorKey(convert, cflags|(flags&SDL_RLEACCELOK),
  1.1604 -				SDL_MapRGB(convert->format, keyR, keyG, keyB));
  1.1605 -		}
  1.1606 -		SDL_SetColorKey(surface, cflags, colorkey);
  1.1607 -	}
  1.1608 -	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
  1.1609 -		Uint32 aflags = surface_flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
  1.1610 -		if ( convert != NULL ) {
  1.1611 -		        SDL_SetAlpha(convert, aflags|(flags&SDL_RLEACCELOK),
  1.1612 -				alpha);
  1.1613 -		}
  1.1614 -		if ( format->Amask ) {
  1.1615 -			surface->flags |= SDL_SRCALPHA;
  1.1616 -		} else {
  1.1617 -			SDL_SetAlpha(surface, aflags, alpha);
  1.1618 -		}
  1.1619 -	}
  1.1620 -
  1.1621 -	/* We're ready to go! */
  1.1622 -	return(convert);
  1.1623 +    /* We're ready to go! */
  1.1624 +    return (convert);
  1.1625  }
  1.1626  
  1.1627  /*
  1.1628   * Free a surface created by the above function.
  1.1629   */
  1.1630 -void SDL_FreeSurface (SDL_Surface *surface)
  1.1631 +void
  1.1632 +SDL_FreeSurface(SDL_Surface * surface)
  1.1633  {
  1.1634 -	/* Free anything that's not NULL, and not the screen surface */
  1.1635 -	if ((surface == NULL) ||
  1.1636 -	    (current_video &&
  1.1637 -	    ((surface == SDL_ShadowSurface)||(surface == SDL_VideoSurface)))) {
  1.1638 -		return;
  1.1639 -	}
  1.1640 -	if ( --surface->refcount > 0 ) {
  1.1641 -		return;
  1.1642 -	}
  1.1643 -	while ( surface->locked > 0 ) {
  1.1644 -		SDL_UnlockSurface(surface);
  1.1645 -	}
  1.1646 -	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
  1.1647 -	        SDL_UnRLESurface(surface, 0);
  1.1648 -	}
  1.1649 -	if ( surface->format ) {
  1.1650 -		SDL_FreeFormat(surface->format);
  1.1651 -		surface->format = NULL;
  1.1652 -	}
  1.1653 -	if ( surface->map != NULL ) {
  1.1654 -		SDL_FreeBlitMap(surface->map);
  1.1655 -		surface->map = NULL;
  1.1656 -	}
  1.1657 -	if ( surface->hwdata ) {
  1.1658 -		SDL_VideoDevice *video = current_video;
  1.1659 -		SDL_VideoDevice *this  = current_video;
  1.1660 -		video->FreeHWSurface(this, surface);
  1.1661 -	}
  1.1662 -	if ( surface->pixels &&
  1.1663 -	     ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC) ) {
  1.1664 -		SDL_free(surface->pixels);
  1.1665 -	}
  1.1666 -	SDL_free(surface);
  1.1667 +    if (surface == NULL) {
  1.1668 +        return;
  1.1669 +    }
  1.1670 +    if (--surface->refcount > 0) {
  1.1671 +        return;
  1.1672 +    }
  1.1673 +    while (surface->locked > 0) {
  1.1674 +        SDL_UnlockSurface(surface);
  1.1675 +    }
  1.1676 +    if (surface->flags & SDL_RLEACCEL) {
  1.1677 +        SDL_UnRLESurface(surface, 0);
  1.1678 +    }
  1.1679 +    if (surface->format) {
  1.1680 +        SDL_SetSurfacePalette(surface, NULL);
  1.1681 +        SDL_FreeFormat(surface->format);
  1.1682 +        surface->format = NULL;
  1.1683 +    }
  1.1684 +    if (surface->map != NULL) {
  1.1685 +        SDL_FreeBlitMap(surface->map);
  1.1686 +        surface->map = NULL;
  1.1687 +    }
  1.1688 +    /* Should we destroy the texture too?
  1.1689 +       if (surface->textureID) {
  1.1690 +       SDL_DestroyTexture(surface->textureID);
  1.1691 +       }
  1.1692 +     */
  1.1693 +    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
  1.1694 +        SDL_free(surface->pixels);
  1.1695 +    }
  1.1696 +    SDL_free(surface);
  1.1697  #ifdef CHECK_LEAKS
  1.1698 -	--surfaces_allocated;
  1.1699 +    --surfaces_allocated;
  1.1700  #endif
  1.1701  }
  1.1702 +
  1.1703 +/* vi: set ts=4 sw=4 expandtab: */