19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_sysvideo.h"
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_cursor_c.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_blit.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_RLEaccel_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_pixels_c.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_leaks.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Public routines */
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Create an empty RGB surface of the appropriate depth
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface * SDL_CreateRGBSurface (Uint32 flags,
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int width, int height, int depth,
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *video = current_video;
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this  = current_video;
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *screen;
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *surface;
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Make sure the size requested doesn't overflow our datatypes */
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Next time I write a library like SDL, I'll use int for size. :) */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( width >= 16384 || height >= 65536 ) {
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Width or height is too large");
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if we desire the surface in video memory */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( video ) {
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen = SDL_PublicSurface;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		screen = NULL;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen && ((screen->flags&SDL_HWSURFACE) == SDL_HWSURFACE) ) {
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (flags&(SDL_SRCCOLORKEY|SDL_SRCALPHA)) != 0 ) {
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flags |= SDL_HWSURFACE;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ! current_video->info.blit_hw_CC ) {
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				flags &= ~SDL_HWSURFACE;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ! current_video->info.blit_hw_A ) {
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				flags &= ~SDL_HWSURFACE;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flags &= ~SDL_HWSURFACE;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the surface */
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface = (SDL_Surface *)SDL_malloc(sizeof(*surface));
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface == NULL ) {
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->flags = SDL_SWSURFACE;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((Amask) && (video->displayformatalphapixel))
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			depth = video->displayformatalphapixel->BitsPerPixel;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Rmask = video->displayformatalphapixel->Rmask;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Gmask = video->displayformatalphapixel->Gmask;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Bmask = video->displayformatalphapixel->Bmask;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Amask = video->displayformatalphapixel->Amask;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			depth = screen->format->BitsPerPixel;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Rmask = screen->format->Rmask;
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Gmask = screen->format->Gmask;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Bmask = screen->format->Bmask;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Amask = screen->format->Amask;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->format == NULL ) {
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(surface);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( Amask ) {
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->flags |= SDL_SRCALPHA;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->w = width;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->h = height;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->pitch = SDL_CalculatePitch(surface);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->pixels = NULL;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->offset = 0;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->hwdata = NULL;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->locked = 0;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->map = NULL;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->unused1 = 0;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetClipRect(surface, NULL);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_FormatChanged(surface);
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the pixels */
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) ||
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				(video->AllocHWSurface(this, surface) < 0) ) {
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( surface->w && surface->h ) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface->pixels = SDL_malloc(surface->h*surface->pitch);
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( surface->pixels == NULL ) {
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_FreeSurface(surface);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_OutOfMemory();
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(NULL);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* This is important for bitmaps */
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memset(surface->pixels, 0, surface->h*surface->pitch);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate an empty mapping */
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->map = SDL_AllocBlitMap();
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->map == NULL ) {
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeSurface(surface);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The surface is ready to go */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->refcount = 1;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef CHECK_LEAKS
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++surfaces_allocated;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(surface);
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Create an RGB surface from an existing memory buffer
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface * SDL_CreateRGBSurfaceFrom (void *pixels,
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int width, int height, int depth, int pitch,
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *surface;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, depth,
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                               Rmask, Gmask, Bmask, Amask);
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface != NULL ) {
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->flags |= SDL_PREALLOC;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->pixels = pixels;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->w = width;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->h = height;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->pitch = pitch;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetClipRect(surface, NULL);
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(surface);
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Set the color key in a blittable surface
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SetColorKey (SDL_Surface *surface, Uint32 flag, Uint32 key)
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Sanity check the flag as it gets passed in */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flag & SDL_SRCCOLORKEY ) {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flag = SDL_SRCCOLORKEY;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flag = 0;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Optimize away operations that don't change anything */
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (flag == (surface->flags & (SDL_SRCCOLORKEY|SDL_RLEACCELOK))) &&
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (key == surface->format->colorkey) ) {
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* UnRLE surfaces before we change the colorkey */
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->flags & SDL_RLEACCEL ) {
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        SDL_UnRLESurface(surface, 1);
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flag ) {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *video = current_video;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *this  = current_video;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->flags |= SDL_SRCCOLORKEY;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->format->colorkey = key;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (video->SetHWColorKey == NULL) ||
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (video->SetHWColorKey(this, surface, key) < 0) ) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				surface->flags &= ~SDL_HWACCEL;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( flag & SDL_RLEACCELOK ) {
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface->flags |= SDL_RLEACCELOK;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface->flags &= ~SDL_RLEACCELOK;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->flags &= ~(SDL_SRCCOLORKEY|SDL_RLEACCELOK);
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->format->colorkey = 0;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_InvalidateMap(surface->map);
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function sets the alpha channel of a surface */
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value)
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 oldflags = surface->flags;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 oldalpha = surface->format->alpha;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Sanity check the flag as it gets passed in */
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flag & SDL_SRCALPHA ) {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) {
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flag = SDL_SRCALPHA;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		flag = 0;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Optimize away operations that don't change anything */
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (flag == (surface->flags & (SDL_SRCALPHA|SDL_RLEACCELOK))) &&
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (!flag || value == oldalpha) ) {
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnRLESurface(surface, 1);
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( flag ) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *video = current_video;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *this  = current_video;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->flags |= SDL_SRCALPHA;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->format->alpha = value;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (video->SetHWAlpha == NULL) ||
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (video->SetHWAlpha(this, surface, value) < 0) ) {
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				surface->flags &= ~SDL_HWACCEL;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( flag & SDL_RLEACCELOK ) {
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        surface->flags |= SDL_RLEACCELOK;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        surface->flags &= ~SDL_RLEACCELOK;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->flags &= ~SDL_SRCALPHA;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->format->alpha = SDL_ALPHA_OPAQUE;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * The representation for software surfaces is independent of
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * per-surface alpha, so no need to invalidate the blit mapping
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * if just the alpha value was changed. (If either is 255, we still
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * need to invalidate.)
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if((surface->flags & SDL_HWACCEL) == SDL_HWACCEL
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   || oldflags != surface->flags
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   || (((oldalpha + 1) ^ (value + 1)) & 0x100))
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_InvalidateMap(surface->map);
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SetAlphaChannel(SDL_Surface *surface, Uint8 value)
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int row, col;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int offset;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *buf;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (surface->format->Amask != 0xFF000000) &&
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (surface->format->Amask != 0x000000FF) ) {
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unsupported surface alpha mask format");
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_BYTEORDER == SDL_LIL_ENDIAN
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->format->Amask == 0xFF000000 ) {
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			offset = 3;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			offset = 0;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->format->Amask == 0xFF000000 ) {
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			offset = 0;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			offset = 3;
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* Byte ordering */
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Quickly set the alpha channel of an RGBA or ARGB surface */
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_MUSTLOCK(surface) ) {
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_LockSurface(surface) < 0 ) {
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return -1;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	row = surface->h;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (row--) {
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		col = surface->w;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		buf = (Uint8 *)surface->pixels + row * surface->pitch + offset;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		while(col--) {
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*buf = value;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			buf += 4;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_MUSTLOCK(surface) ) {
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnlockSurface(surface);
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * A function to calculate the intersection of two rectangles:
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * return true if the rectangles intersect, false otherwise
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic __inline__
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_bool SDL_IntersectRect(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *intersection)
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int Amin, Amax, Bmin, Bmax;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Horizontal intersection */
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Amin = A->x;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Amax = Amin + A->w;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Bmin = B->x;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Bmax = Bmin + B->w;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(Bmin > Amin)
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        Amin = Bmin;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	intersection->x = Amin;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(Bmax < Amax)
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        Amax = Bmax;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	intersection->w = Amax - Amin > 0 ? Amax - Amin : 0;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Vertical intersection */
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Amin = A->y;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Amax = Amin + A->h;
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Bmin = B->y;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Bmax = Bmin + B->h;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(Bmin > Amin)
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        Amin = Bmin;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	intersection->y = Amin;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(Bmax < Amax)
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        Amax = Bmax;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	intersection->h = Amax - Amin > 0 ? Amax - Amin : 0;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return (intersection->w && intersection->h);
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Set the clipping rectangle for a blittable surface
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect full_rect;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't do anything if there's no surface to act on */
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! surface ) {
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_FALSE;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the full surface rectangle */
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	full_rect.x = 0;
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	full_rect.y = 0;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	full_rect.w = surface->w;
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	full_rect.h = surface->h;
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the clipping rectangle */
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! rect ) {
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->clip_rect = full_rect;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 1;
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_GetClipRect(SDL_Surface *surface, SDL_Rect *rect)
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface && rect ) {
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*rect = surface->clip_rect;
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Set up a blit between two surfaces -- split into three parts:
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * The upper part, SDL_UpperBlit(), performs clipping and rectangle
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * verification.  The lower part is a pointer to a low level
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * accelerated blitting function.
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * These parts are separated out and each used internally by this
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * library in the optimimum places.  They are exported so that if
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * you know exactly what you are doing, you can optimize your code
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * by calling the one(s) you need.
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect,
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_Surface *dst, SDL_Rect *dstrect)
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_blit do_blit;
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect hw_srcrect;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect hw_dstrect;
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to make sure the blit mapping is valid */
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (src->map->dst != dst) ||
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             (src->map->dst->format_version != src->map->format_version) ) {
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_MapSurface(src, dst) < 0 ) {
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Figure out which blitter to use */
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (src->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( src == SDL_VideoSurface ) {
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_srcrect = *srcrect;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_srcrect.x += current_video->offset_x;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_srcrect.y += current_video->offset_y;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcrect = &hw_srcrect;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( dst == SDL_VideoSurface ) {
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_dstrect = *dstrect;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_dstrect.x += current_video->offset_x;
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_dstrect.y += current_video->offset_y;
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dstrect = &hw_dstrect;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		do_blit = src->map->hw_blit;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		do_blit = src->map->sw_blit;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(do_blit(src, srcrect, dst, dstrect));
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect,
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   SDL_Surface *dst, SDL_Rect *dstrect)
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_Rect fulldst;
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int srcx, srcy, w, h;
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Make sure the surfaces aren't locked */
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! src || ! dst ) {
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("SDL_UpperBlit: passed a NULL surface");
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( src->locked || dst->locked ) {
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Surfaces must not be locked during blit");
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If the destination rectangle is NULL, use the entire dest surface */
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dstrect == NULL ) {
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        fulldst.x = fulldst.y = 0;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dstrect = &fulldst;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* clip the source rectangle to the source surface */
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(srcrect) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        int maxw, maxh;
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		srcx = srcrect->x;
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		w = srcrect->w;
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(srcx < 0) {
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        w += srcx;
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dstrect->x -= srcx;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcx = 0;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		maxw = src->w - srcx;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(maxw < w)
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			w = maxw;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		srcy = srcrect->y;
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		h = srcrect->h;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(srcy < 0) {
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        h += srcy;
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dstrect->y -= srcy;
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcy = 0;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		maxh = src->h - srcy;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(maxh < h)
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			h = maxh;
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        srcx = srcy = 0;
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		w = src->w;
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		h = src->h;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* clip the destination rectangle against the clip rectangle */
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        SDL_Rect *clip = &dst->clip_rect;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int dx, dy;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dx = clip->x - dstrect->x;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(dx > 0) {
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			w -= dx;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dstrect->x += dx;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcx += dx;
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dx = dstrect->x + w - clip->x - clip->w;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(dx > 0)
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			w -= dx;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dy = clip->y - dstrect->y;
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(dy > 0) {
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			h -= dy;
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dstrect->y += dy;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcy += dy;
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dy = dstrect->y + h - clip->y - clip->h;
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(dy > 0)
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			h -= dy;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(w > 0 && h > 0) {
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        SDL_Rect sr;
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        sr.x = srcx;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sr.y = srcy;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sr.w = dstrect->w = w;
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		sr.h = dstrect->h = h;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return SDL_LowerBlit(src, &sr, dst, dstrect);
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstrect->w = dstrect->h = 0;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_FillRect1(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* FIXME: We have to worry about packing order.. *sigh* */
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("1-bpp rect fill not yet implemented");
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_FillRect4(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* FIXME: We have to worry about packing order.. *sigh* */
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError("4-bpp rect fill not yet implemented");
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function performs a fast fill of the given rectangle with 'color'
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *video = current_video;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_VideoDevice *this  = current_video;
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x, y;
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *row;
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* This function doesn't work on surfaces < 8 bpp */
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst->format->BitsPerPixel < 8 ) {
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch(dst->format->BitsPerPixel) {
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 1:
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_FillRect1(dst, dstrect, color);
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 4:
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return SDL_FillRect4(dst, dstrect, color);
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    default:
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Fill rect on unsupported surface format");
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(-1);
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If 'dstrect' == NULL, then fill the whole surface */
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dstrect ) {
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Perform clipping */
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect) ) {
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(0);
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		dstrect = &dst->clip_rect;
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check for hardware acceleration */
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) &&
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					video->info.blit_fill ) {
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Rect hw_rect;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( dst == SDL_VideoSurface ) {
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_rect = *dstrect;
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_rect.x += current_video->offset_x;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			hw_rect.y += current_video->offset_y;
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dstrect = &hw_rect;
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(video->FillHWRect(this, dst, dstrect, color));
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Perform software fill */
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_LockSurface(dst) != 0 ) {
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	row = (Uint8 *)dst->pixels+dstrect->y*dst->pitch+
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			dstrect->x*dst->format->BytesPerPixel;
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( dst->format->palette || (color == 0) ) {
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x = dstrect->w*dst->format->BytesPerPixel;
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( !color && !((uintptr_t)row&3) && !(x&3) && !(dst->pitch&3) ) {
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			int n = x >> 2;
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( y=dstrect->h; y; --y ) {
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_memset4(row, 0, n);
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				row += dst->pitch;
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __powerpc__
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/*
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 * SDL_memset() on PPC (both glibc and codewarrior) uses
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 * the dcbz (Data Cache Block Zero) instruction, which
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 * causes an alignment exception if the destination is
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 * uncachable, so only use it on software surfaces
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 */
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if(dstrect->w >= 8) {
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/*
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					 * 64-bit stores are probably most
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					 * efficient to uncached video memory
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					 */
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					double fill;
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_memset(&fill, color, (sizeof fill));
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					for(y = dstrect->h; y; y--) {
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Uint8 *d = row;
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						unsigned n = x;
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						unsigned nn;
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Uint8 c = color;
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						double f = fill;
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						while((unsigned long)d
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						      & (sizeof(double) - 1)) {
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							*d++ = c;
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							n--;
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						nn = n / (sizeof(double) * 4);
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						while(nn) {
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							((double *)d)[0] = f;
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							((double *)d)[1] = f;
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							((double *)d)[2] = f;
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							((double *)d)[3] = f;
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							d += 4*sizeof(double);
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							nn--;
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						n &= ~(sizeof(double) * 4 - 1);
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						nn = n / sizeof(double);
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						while(nn) {
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							*(double *)d = f;
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							d += sizeof(double);
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							nn--;
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						n &= ~(sizeof(double) - 1);
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						while(n) {
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							*d++ = c;
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							n--;
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						row += dst->pitch;
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				} else {
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* narrow boxes */
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					for(y = dstrect->h; y; y--) {
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Uint8 *d = row;
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						Uint8 c = color;
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						int n = x;
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						while(n) {
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							*d++ = c;
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							n--;
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						row += dst->pitch;
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					}
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* __powerpc__ */
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for(y = dstrect->h; y; y--) {
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_memset(row, color, x);
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					row += dst->pitch;
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch (dst->format->BytesPerPixel) {
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 2:
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( y=dstrect->h; y; --y ) {
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint16 *pixels = (Uint16 *)row;
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint16 c = (Uint16)color;
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint32 cc = (Uint32)c << 16 | c;
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				int n = dstrect->w;
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if((uintptr_t)pixels & 3) {
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					*pixels++ = c;
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					n--;
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if(n >> 1)
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_memset4(pixels, cc, n >> 1);
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if(n & 1)
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					pixels[n - 1] = c;
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				row += dst->pitch;
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 3:
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			#if SDL_BYTEORDER == SDL_BIG_ENDIAN
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				color <<= 8;
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			#endif
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( y=dstrect->h; y; --y ) {
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				Uint8 *pixels = row;
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				for ( x=dstrect->w; x; --x ) {
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_memcpy(pixels, &color, 3);
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					pixels += 3;
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				row += dst->pitch;
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    case 4:
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for(y = dstrect->h; y; --y) {
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_memset4(row, color, dstrect->w);
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				row += dst->pitch;
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_UnlockSurface(dst);
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're done! */
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Lock a surface to directly access the pixels
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_LockSurface (SDL_Surface *surface)
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! surface->locked ) {
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Perform the lock */
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_VideoDevice *video = current_video;
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_VideoDevice *this  = current_video;
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( video->LockHWSurface(this, surface) < 0 ) {
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				return(-1);
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( surface->flags & SDL_RLEACCEL ) {
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_UnRLESurface(surface, 1);
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface->flags |= SDL_RLEACCEL;	/* save accel'd state */
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* This needs to be done here in case pixels changes value */
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->pixels = (Uint8 *)surface->pixels + surface->offset;
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Increment the surface lock count, for recursive locks */
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++surface->locked;
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Ready to go.. */
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Unlock a previously locked surface
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_UnlockSurface (SDL_Surface *surface)
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Only perform an unlock if we are locked */
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! surface->locked || (--surface->locked > 0) ) {
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Perform the unlock */
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface->pixels = (Uint8 *)surface->pixels - surface->offset;
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Unlock hardware or accelerated surfaces */
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *video = current_video;
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *this  = current_video;
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video->UnlockHWSurface(this, surface);
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Update RLE encoded surface with new data */
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        surface->flags &= ~SDL_RLEACCEL; /* stop lying */
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_RLESurface(surface);
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Convert a surface into the specified pixel format.
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface * SDL_ConvertSurface (SDL_Surface *surface,
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_PixelFormat *format, Uint32 flags)
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *convert;
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 colorkey = 0;
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 alpha = 0;
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 surface_flags;
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect bounds;
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check for empty destination palette! (results in empty image) */
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format->palette != NULL ) {
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		int i;
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<format->palette->ncolors; ++i ) {
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( (format->palette->colors[i].r != 0) ||
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (format->palette->colors[i].g != 0) ||
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			     (format->palette->colors[i].b != 0) )
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( i == format->palette->ncolors ) {
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError("Empty destination palette");
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(NULL);
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Only create hw surfaces with alpha channel if hw alpha blits
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   are supported */
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if(format->Amask != 0 && (flags & SDL_HWSURFACE)) {
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		const SDL_VideoInfo *vi = SDL_GetVideoInfo();
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if(!vi || !vi->blit_hw_A)
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			flags &= ~SDL_HWSURFACE;
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create a new surface with the desired format */
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	convert = SDL_CreateRGBSurface(flags,
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				surface->w, surface->h, format->BitsPerPixel,
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		format->Rmask, format->Gmask, format->Bmask, format->Amask);
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( convert == NULL ) {
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Copy the palette if any */
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format->palette && convert->format->palette ) {
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memcpy(convert->format->palette->colors,
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format->palette->colors,
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format->palette->ncolors*sizeof(SDL_Color));
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		convert->format->palette->ncolors = format->palette->ncolors;
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Save the original surface color key and alpha */
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	surface_flags = surface->flags;
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Convert colourkeyed surfaces to RGBA if requested */
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   && format->Amask) {
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface_flags &= ~SDL_SRCCOLORKEY;
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			colorkey = surface->format->colorkey;
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetColorKey(surface, 0, 0);
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Copy over the alpha channel to RGBA if requested */
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( format->Amask ) {
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface->flags &= ~SDL_SRCALPHA;
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			alpha = surface->format->alpha;
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetAlpha(surface, 0, 0);
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Copy over the image data */
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.x = 0;
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.y = 0;
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.w = surface->w;
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bounds.h = surface->h;
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_LowerBlit(surface, &bounds, convert, &bounds);
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Clean up the original surface, and update converted surface */
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( convert != NULL ) {
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetClipRect(convert, &surface->clip_rect);
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint32 cflags = surface_flags&(SDL_SRCCOLORKEY|SDL_RLEACCELOK);
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( convert != NULL ) {
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Uint8 keyR, keyG, keyB;
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_GetRGB(colorkey,surface->format,&keyR,&keyG,&keyB);
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetColorKey(convert, cflags|(flags&SDL_RLEACCELOK),
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_MapRGB(convert->format, keyR, keyG, keyB));
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetColorKey(surface, cflags, colorkey);
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint32 aflags = surface_flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( convert != NULL ) {
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		        SDL_SetAlpha(convert, aflags|(flags&SDL_RLEACCELOK),
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				alpha);
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( format->Amask ) {
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			surface->flags |= SDL_SRCALPHA;
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetAlpha(surface, aflags, alpha);
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to go! */
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(convert);
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Free a surface created by the above function.
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_FreeSurface (SDL_Surface *surface)
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Free anything that's not NULL, and not the screen surface */
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((surface == NULL) ||
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    (current_video &&
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    ((surface == SDL_ShadowSurface)||(surface == SDL_VideoSurface)))) {
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( --surface->refcount > 0 ) {
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( surface->locked > 0 ) {
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnlockSurface(surface);
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        SDL_UnRLESurface(surface, 0);
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->format ) {
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeFormat(surface->format);
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->format = NULL;
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->map != NULL ) {
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeBlitMap(surface->map);
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		surface->map = NULL;
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->hwdata ) {
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *video = current_video;
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_VideoDevice *this  = current_video;
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		video->FreeHWSurface(this, surface);
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( surface->pixels &&
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC) ) {
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(surface->pixels);
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(surface);
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef CHECK_LEAKS
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	--surfaces_allocated;
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
942