src/video/SDL_surface.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 6137 4720145f848b
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

also fix a changelog date.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_video.h"
slouken@0
    25
#include "SDL_sysvideo.h"
slouken@0
    26
#include "SDL_cursor_c.h"
slouken@0
    27
#include "SDL_blit.h"
slouken@0
    28
#include "SDL_RLEaccel_c.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@0
    30
#include "SDL_leaks.h"
slouken@0
    31
icculus@1251
    32
slouken@0
    33
/* Public routines */
slouken@0
    34
/*
slouken@0
    35
 * Create an empty RGB surface of the appropriate depth
slouken@0
    36
 */
slouken@0
    37
SDL_Surface * SDL_CreateRGBSurface (Uint32 flags,
slouken@0
    38
			int width, int height, int depth,
slouken@0
    39
			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
    40
{
slouken@0
    41
	SDL_VideoDevice *video = current_video;
slouken@0
    42
	SDL_VideoDevice *this  = current_video;
slouken@0
    43
	SDL_Surface *screen;
slouken@0
    44
	SDL_Surface *surface;
slouken@0
    45
slouken@940
    46
	/* Make sure the size requested doesn't overflow our datatypes */
slouken@940
    47
	/* Next time I write a library like SDL, I'll use int for size. :) */
slouken@1017
    48
	if ( width >= 16384 || height >= 65536 ) {
slouken@940
    49
		SDL_SetError("Width or height is too large");
slouken@940
    50
		return(NULL);
slouken@940
    51
	}
slouken@940
    52
slouken@0
    53
	/* Check to see if we desire the surface in video memory */
slouken@0
    54
	if ( video ) {
slouken@0
    55
		screen = SDL_PublicSurface;
slouken@0
    56
	} else {
slouken@0
    57
		screen = NULL;
slouken@0
    58
	}
slouken@0
    59
	if ( screen && ((screen->flags&SDL_HWSURFACE) == SDL_HWSURFACE) ) {
slouken@0
    60
		if ( (flags&(SDL_SRCCOLORKEY|SDL_SRCALPHA)) != 0 ) {
slouken@0
    61
			flags |= SDL_HWSURFACE;
slouken@0
    62
		}
slouken@0
    63
		if ( (flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@0
    64
			if ( ! current_video->info.blit_hw_CC ) {
slouken@0
    65
				flags &= ~SDL_HWSURFACE;
slouken@0
    66
			}
slouken@0
    67
		}
slouken@0
    68
		if ( (flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
slouken@0
    69
			if ( ! current_video->info.blit_hw_A ) {
slouken@0
    70
				flags &= ~SDL_HWSURFACE;
slouken@0
    71
			}
slouken@0
    72
		}
slouken@0
    73
	} else {
slouken@0
    74
		flags &= ~SDL_HWSURFACE;
slouken@0
    75
	}
slouken@0
    76
slouken@0
    77
	/* Allocate the surface */
slouken@1336
    78
	surface = (SDL_Surface *)SDL_malloc(sizeof(*surface));
slouken@0
    79
	if ( surface == NULL ) {
slouken@0
    80
		SDL_OutOfMemory();
slouken@0
    81
		return(NULL);
slouken@0
    82
	}
slouken@0
    83
	surface->flags = SDL_SWSURFACE;
slouken@0
    84
	if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
icculus@1052
    85
		if ((Amask) && (video->displayformatalphapixel))
icculus@1052
    86
		{
icculus@1052
    87
			depth = video->displayformatalphapixel->BitsPerPixel;
icculus@1052
    88
			Rmask = video->displayformatalphapixel->Rmask;
icculus@1052
    89
			Gmask = video->displayformatalphapixel->Gmask;
icculus@1052
    90
			Bmask = video->displayformatalphapixel->Bmask;
icculus@1052
    91
			Amask = video->displayformatalphapixel->Amask;
icculus@1052
    92
		}
icculus@1052
    93
		else
icculus@1052
    94
		{
icculus@1052
    95
			depth = screen->format->BitsPerPixel;
icculus@1052
    96
			Rmask = screen->format->Rmask;
icculus@1052
    97
			Gmask = screen->format->Gmask;
icculus@1052
    98
			Bmask = screen->format->Bmask;
icculus@1052
    99
			Amask = screen->format->Amask;
icculus@1052
   100
		}
slouken@0
   101
	}
slouken@0
   102
	surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
slouken@0
   103
	if ( surface->format == NULL ) {
slouken@1336
   104
		SDL_free(surface);
slouken@0
   105
		return(NULL);
slouken@0
   106
	}
slouken@0
   107
	if ( Amask ) {
slouken@0
   108
		surface->flags |= SDL_SRCALPHA;
slouken@0
   109
	}
slouken@0
   110
	surface->w = width;
slouken@0
   111
	surface->h = height;
slouken@0
   112
	surface->pitch = SDL_CalculatePitch(surface);
slouken@0
   113
	surface->pixels = NULL;
slouken@0
   114
	surface->offset = 0;
slouken@0
   115
	surface->hwdata = NULL;
slouken@0
   116
	surface->locked = 0;
slouken@0
   117
	surface->map = NULL;
slouken@441
   118
	surface->unused1 = 0;
slouken@0
   119
	SDL_SetClipRect(surface, NULL);
slouken@845
   120
	SDL_FormatChanged(surface);
slouken@0
   121
slouken@0
   122
	/* Get the pixels */
slouken@0
   123
	if ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) || 
slouken@0
   124
				(video->AllocHWSurface(this, surface) < 0) ) {
slouken@0
   125
		if ( surface->w && surface->h ) {
slouken@1336
   126
			surface->pixels = SDL_malloc(surface->h*surface->pitch);
slouken@0
   127
			if ( surface->pixels == NULL ) {
slouken@0
   128
				SDL_FreeSurface(surface);
slouken@0
   129
				SDL_OutOfMemory();
slouken@0
   130
				return(NULL);
slouken@0
   131
			}
slouken@0
   132
			/* This is important for bitmaps */
slouken@1336
   133
			SDL_memset(surface->pixels, 0, surface->h*surface->pitch);
slouken@0
   134
		}
slouken@0
   135
	}
slouken@0
   136
slouken@0
   137
	/* Allocate an empty mapping */
slouken@0
   138
	surface->map = SDL_AllocBlitMap();
slouken@0
   139
	if ( surface->map == NULL ) {
slouken@0
   140
		SDL_FreeSurface(surface);
slouken@0
   141
		return(NULL);
slouken@0
   142
	}
slouken@0
   143
slouken@0
   144
	/* The surface is ready to go */
slouken@0
   145
	surface->refcount = 1;
slouken@0
   146
#ifdef CHECK_LEAKS
slouken@0
   147
	++surfaces_allocated;
slouken@0
   148
#endif
slouken@0
   149
	return(surface);
slouken@0
   150
}
slouken@0
   151
/*
slouken@0
   152
 * Create an RGB surface from an existing memory buffer
slouken@0
   153
 */
slouken@0
   154
SDL_Surface * SDL_CreateRGBSurfaceFrom (void *pixels,
slouken@0
   155
			int width, int height, int depth, int pitch,
slouken@0
   156
			Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
   157
{
slouken@0
   158
	SDL_Surface *surface;
slouken@0
   159
slouken@0
   160
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, depth,
slouken@0
   161
	                               Rmask, Gmask, Bmask, Amask);
slouken@0
   162
	if ( surface != NULL ) {
slouken@0
   163
		surface->flags |= SDL_PREALLOC;
slouken@0
   164
		surface->pixels = pixels;
slouken@0
   165
		surface->w = width;
slouken@0
   166
		surface->h = height;
slouken@0
   167
		surface->pitch = pitch;
slouken@0
   168
		SDL_SetClipRect(surface, NULL);
slouken@0
   169
	}
slouken@0
   170
	return(surface);
slouken@0
   171
}
slouken@0
   172
/*
slouken@0
   173
 * Set the color key in a blittable surface
slouken@0
   174
 */
slouken@0
   175
int SDL_SetColorKey (SDL_Surface *surface, Uint32 flag, Uint32 key)
slouken@0
   176
{
slouken@0
   177
	/* Sanity check the flag as it gets passed in */
slouken@0
   178
	if ( flag & SDL_SRCCOLORKEY ) {
slouken@0
   179
		if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) {
slouken@0
   180
			flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@0
   181
		} else {
slouken@0
   182
			flag = SDL_SRCCOLORKEY;
slouken@0
   183
		}
slouken@0
   184
	} else {
slouken@0
   185
		flag = 0;
slouken@0
   186
	}
slouken@0
   187
slouken@0
   188
	/* Optimize away operations that don't change anything */
slouken@0
   189
	if ( (flag == (surface->flags & (SDL_SRCCOLORKEY|SDL_RLEACCELOK))) &&
slouken@0
   190
	     (key == surface->format->colorkey) ) {
slouken@0
   191
		return(0);
slouken@0
   192
	}
slouken@0
   193
slouken@0
   194
	/* UnRLE surfaces before we change the colorkey */
slouken@0
   195
	if ( surface->flags & SDL_RLEACCEL ) {
slouken@0
   196
	        SDL_UnRLESurface(surface, 1);
slouken@0
   197
	}
slouken@0
   198
slouken@0
   199
	if ( flag ) {
slouken@0
   200
		SDL_VideoDevice *video = current_video;
slouken@0
   201
		SDL_VideoDevice *this  = current_video;
slouken@0
   202
slouken@0
   203
slouken@0
   204
		surface->flags |= SDL_SRCCOLORKEY;
slouken@0
   205
		surface->format->colorkey = key;
slouken@0
   206
		if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
slouken@0
   207
			if ( (video->SetHWColorKey == NULL) ||
slouken@0
   208
			     (video->SetHWColorKey(this, surface, key) < 0) ) {
slouken@0
   209
				surface->flags &= ~SDL_HWACCEL;
slouken@0
   210
			}
slouken@0
   211
		}
slouken@0
   212
		if ( flag & SDL_RLEACCELOK ) {
slouken@0
   213
			surface->flags |= SDL_RLEACCELOK;
slouken@0
   214
		} else {
slouken@0
   215
			surface->flags &= ~SDL_RLEACCELOK;
slouken@0
   216
		}
slouken@0
   217
	} else {
slouken@0
   218
		surface->flags &= ~(SDL_SRCCOLORKEY|SDL_RLEACCELOK);
slouken@0
   219
		surface->format->colorkey = 0;
slouken@0
   220
	}
slouken@0
   221
	SDL_InvalidateMap(surface->map);
slouken@0
   222
	return(0);
slouken@0
   223
}
slouken@431
   224
/* This function sets the alpha channel of a surface */
slouken@0
   225
int SDL_SetAlpha (SDL_Surface *surface, Uint32 flag, Uint8 value)
slouken@0
   226
{
slouken@0
   227
	Uint32 oldflags = surface->flags;
slouken@0
   228
	Uint32 oldalpha = surface->format->alpha;
slouken@0
   229
slouken@0
   230
	/* Sanity check the flag as it gets passed in */
slouken@0
   231
	if ( flag & SDL_SRCALPHA ) {
slouken@0
   232
		if ( flag & (SDL_RLEACCEL|SDL_RLEACCELOK) ) {
slouken@0
   233
			flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@0
   234
		} else {
slouken@0
   235
			flag = SDL_SRCALPHA;
slouken@0
   236
		}
slouken@0
   237
	} else {
slouken@0
   238
		flag = 0;
slouken@0
   239
	}
slouken@0
   240
slouken@0
   241
	/* Optimize away operations that don't change anything */
slouken@0
   242
	if ( (flag == (surface->flags & (SDL_SRCALPHA|SDL_RLEACCELOK))) &&
slouken@0
   243
	     (!flag || value == oldalpha) ) {
slouken@0
   244
		return(0);
slouken@0
   245
	}
slouken@0
   246
slouken@0
   247
	if(!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
slouken@0
   248
		SDL_UnRLESurface(surface, 1);
slouken@0
   249
slouken@0
   250
	if ( flag ) {
slouken@0
   251
		SDL_VideoDevice *video = current_video;
slouken@0
   252
		SDL_VideoDevice *this  = current_video;
slouken@0
   253
slouken@0
   254
		surface->flags |= SDL_SRCALPHA;
slouken@0
   255
		surface->format->alpha = value;
slouken@0
   256
		if ( (surface->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
slouken@0
   257
			if ( (video->SetHWAlpha == NULL) ||
slouken@0
   258
			     (video->SetHWAlpha(this, surface, value) < 0) ) {
slouken@0
   259
				surface->flags &= ~SDL_HWACCEL;
slouken@0
   260
			}
slouken@0
   261
		}
slouken@0
   262
		if ( flag & SDL_RLEACCELOK ) {
slouken@0
   263
		        surface->flags |= SDL_RLEACCELOK;
slouken@0
   264
		} else {
slouken@0
   265
		        surface->flags &= ~SDL_RLEACCELOK;
slouken@0
   266
		}
slouken@0
   267
	} else {
slouken@0
   268
		surface->flags &= ~SDL_SRCALPHA;
slouken@0
   269
		surface->format->alpha = SDL_ALPHA_OPAQUE;
slouken@0
   270
	}
slouken@0
   271
	/*
slouken@0
   272
	 * The representation for software surfaces is independent of
slouken@0
   273
	 * per-surface alpha, so no need to invalidate the blit mapping
slouken@0
   274
	 * if just the alpha value was changed. (If either is 255, we still
slouken@0
   275
	 * need to invalidate.)
slouken@0
   276
	 */
slouken@0
   277
	if((surface->flags & SDL_HWACCEL) == SDL_HWACCEL
slouken@0
   278
	   || oldflags != surface->flags
slouken@0
   279
	   || (((oldalpha + 1) ^ (value + 1)) & 0x100))
slouken@0
   280
		SDL_InvalidateMap(surface->map);
slouken@0
   281
	return(0);
slouken@0
   282
}
slouken@431
   283
int SDL_SetAlphaChannel(SDL_Surface *surface, Uint8 value)
slouken@431
   284
{
slouken@431
   285
	int row, col;
slouken@431
   286
	int offset;
slouken@431
   287
	Uint8 *buf;
slouken@431
   288
slouken@431
   289
	if ( (surface->format->Amask != 0xFF000000) &&
slouken@431
   290
	     (surface->format->Amask != 0x000000FF) ) {
slouken@431
   291
		SDL_SetError("Unsupported surface alpha mask format");
slouken@431
   292
		return -1;
slouken@431
   293
	}
slouken@431
   294
slouken@431
   295
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@431
   296
	if ( surface->format->Amask == 0xFF000000 ) {
slouken@431
   297
			offset = 3;
slouken@431
   298
	} else {
slouken@431
   299
			offset = 0;
slouken@431
   300
	}
slouken@431
   301
#else
slouken@431
   302
	if ( surface->format->Amask == 0xFF000000 ) {
slouken@431
   303
			offset = 0;
slouken@431
   304
	} else {
slouken@431
   305
			offset = 3;
slouken@431
   306
	}
slouken@431
   307
#endif /* Byte ordering */
slouken@431
   308
slouken@431
   309
	/* Quickly set the alpha channel of an RGBA or ARGB surface */
slouken@431
   310
	if ( SDL_MUSTLOCK(surface) ) {
slouken@431
   311
		if ( SDL_LockSurface(surface) < 0 ) {
slouken@431
   312
			return -1;
slouken@431
   313
		}
slouken@431
   314
	}
slouken@431
   315
	row = surface->h;
slouken@431
   316
	while (row--) {
slouken@431
   317
		col = surface->w;
slouken@431
   318
		buf = (Uint8 *)surface->pixels + row * surface->pitch + offset;
slouken@431
   319
		while(col--) {
slouken@431
   320
			*buf = value;
slouken@431
   321
			buf += 4;
slouken@431
   322
		}
slouken@431
   323
	}
slouken@431
   324
	if ( SDL_MUSTLOCK(surface) ) {
slouken@431
   325
		SDL_UnlockSurface(surface);
slouken@431
   326
	}
slouken@431
   327
	return 0;
slouken@431
   328
}
slouken@0
   329
slouken@0
   330
/*
slouken@0
   331
 * A function to calculate the intersection of two rectangles:
slouken@0
   332
 * return true if the rectangles intersect, false otherwise
slouken@0
   333
 */
slouken@0
   334
static __inline__
slouken@130
   335
SDL_bool SDL_IntersectRect(const SDL_Rect *A, const SDL_Rect *B, SDL_Rect *intersection)
slouken@0
   336
{
slouken@0
   337
	int Amin, Amax, Bmin, Bmax;
slouken@0
   338
slouken@0
   339
	/* Horizontal intersection */
slouken@0
   340
	Amin = A->x;
slouken@0
   341
	Amax = Amin + A->w;
slouken@0
   342
	Bmin = B->x;
slouken@0
   343
	Bmax = Bmin + B->w;
slouken@0
   344
	if(Bmin > Amin)
slouken@0
   345
	        Amin = Bmin;
slouken@0
   346
	intersection->x = Amin;
slouken@0
   347
	if(Bmax < Amax)
slouken@0
   348
	        Amax = Bmax;
slouken@0
   349
	intersection->w = Amax - Amin > 0 ? Amax - Amin : 0;
slouken@0
   350
slouken@0
   351
	/* Vertical intersection */
slouken@0
   352
	Amin = A->y;
slouken@0
   353
	Amax = Amin + A->h;
slouken@0
   354
	Bmin = B->y;
slouken@0
   355
	Bmax = Bmin + B->h;
slouken@0
   356
	if(Bmin > Amin)
slouken@0
   357
	        Amin = Bmin;
slouken@0
   358
	intersection->y = Amin;
slouken@0
   359
	if(Bmax < Amax)
slouken@0
   360
	        Amax = Bmax;
slouken@0
   361
	intersection->h = Amax - Amin > 0 ? Amax - Amin : 0;
slouken@0
   362
slouken@0
   363
	return (intersection->w && intersection->h);
slouken@0
   364
}
slouken@0
   365
/*
slouken@0
   366
 * Set the clipping rectangle for a blittable surface
slouken@0
   367
 */
slouken@130
   368
SDL_bool SDL_SetClipRect(SDL_Surface *surface, const SDL_Rect *rect)
slouken@0
   369
{
slouken@0
   370
	SDL_Rect full_rect;
slouken@0
   371
slouken@0
   372
	/* Don't do anything if there's no surface to act on */
slouken@0
   373
	if ( ! surface ) {
slouken@0
   374
		return SDL_FALSE;
slouken@0
   375
	}
slouken@0
   376
slouken@0
   377
	/* Set up the full surface rectangle */
slouken@0
   378
	full_rect.x = 0;
slouken@0
   379
	full_rect.y = 0;
slouken@0
   380
	full_rect.w = surface->w;
slouken@0
   381
	full_rect.h = surface->h;
slouken@0
   382
slouken@0
   383
	/* Set the clipping rectangle */
slouken@0
   384
	if ( ! rect ) {
slouken@0
   385
		surface->clip_rect = full_rect;
slouken@0
   386
		return 1;
slouken@0
   387
	}
slouken@0
   388
	return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   389
}
slouken@0
   390
void SDL_GetClipRect(SDL_Surface *surface, SDL_Rect *rect)
slouken@0
   391
{
slouken@0
   392
	if ( surface && rect ) {
slouken@0
   393
		*rect = surface->clip_rect;
slouken@0
   394
	}
slouken@0
   395
}
slouken@0
   396
/* 
slouken@0
   397
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   398
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   399
 * verification.  The lower part is a pointer to a low level
slouken@0
   400
 * accelerated blitting function.
slouken@0
   401
 *
slouken@0
   402
 * These parts are separated out and each used internally by this 
slouken@0
   403
 * library in the optimimum places.  They are exported so that if
slouken@0
   404
 * you know exactly what you are doing, you can optimize your code
slouken@0
   405
 * by calling the one(s) you need.
slouken@0
   406
 */
slouken@0
   407
int SDL_LowerBlit (SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   408
				SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   409
{
slouken@0
   410
	SDL_blit do_blit;
slouken@462
   411
	SDL_Rect hw_srcrect;
slouken@462
   412
	SDL_Rect hw_dstrect;
slouken@0
   413
slouken@0
   414
	/* Check to make sure the blit mapping is valid */
slouken@0
   415
	if ( (src->map->dst != dst) ||
slouken@0
   416
             (src->map->dst->format_version != src->map->format_version) ) {
slouken@0
   417
		if ( SDL_MapSurface(src, dst) < 0 ) {
slouken@0
   418
			return(-1);
slouken@0
   419
		}
slouken@0
   420
	}
slouken@0
   421
slouken@0
   422
	/* Figure out which blitter to use */
slouken@0
   423
	if ( (src->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
slouken@462
   424
		if ( src == SDL_VideoSurface ) {
slouken@519
   425
			hw_srcrect = *srcrect;
slouken@462
   426
			hw_srcrect.x += current_video->offset_x;
slouken@462
   427
			hw_srcrect.y += current_video->offset_y;
slouken@462
   428
			srcrect = &hw_srcrect;
slouken@462
   429
		}
slouken@462
   430
		if ( dst == SDL_VideoSurface ) {
slouken@462
   431
			hw_dstrect = *dstrect;
slouken@462
   432
			hw_dstrect.x += current_video->offset_x;
slouken@462
   433
			hw_dstrect.y += current_video->offset_y;
slouken@462
   434
			dstrect = &hw_dstrect;
slouken@462
   435
		}
slouken@0
   436
		do_blit = src->map->hw_blit;
slouken@0
   437
	} else {
slouken@0
   438
		do_blit = src->map->sw_blit;
slouken@0
   439
	}
slouken@0
   440
	return(do_blit(src, srcrect, dst, dstrect));
slouken@0
   441
}
slouken@0
   442
slouken@0
   443
slouken@0
   444
int SDL_UpperBlit (SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   445
		   SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   446
{
slouken@0
   447
        SDL_Rect fulldst;
slouken@0
   448
	int srcx, srcy, w, h;
slouken@0
   449
slouken@0
   450
	/* Make sure the surfaces aren't locked */
slouken@0
   451
	if ( ! src || ! dst ) {
slouken@0
   452
		SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@0
   453
		return(-1);
slouken@0
   454
	}
slouken@0
   455
	if ( src->locked || dst->locked ) {
slouken@0
   456
		SDL_SetError("Surfaces must not be locked during blit");
slouken@0
   457
		return(-1);
slouken@0
   458
	}
slouken@0
   459
slouken@0
   460
	/* If the destination rectangle is NULL, use the entire dest surface */
slouken@0
   461
	if ( dstrect == NULL ) {
slouken@0
   462
	        fulldst.x = fulldst.y = 0;
slouken@0
   463
		dstrect = &fulldst;
slouken@0
   464
	}
slouken@0
   465
slouken@0
   466
	/* clip the source rectangle to the source surface */
slouken@0
   467
	if(srcrect) {
slouken@0
   468
	        int maxw, maxh;
slouken@0
   469
	
slouken@0
   470
		srcx = srcrect->x;
slouken@0
   471
		w = srcrect->w;
slouken@0
   472
		if(srcx < 0) {
slouken@0
   473
		        w += srcx;
slouken@0
   474
			dstrect->x -= srcx;
slouken@0
   475
			srcx = 0;
slouken@0
   476
		}
slouken@0
   477
		maxw = src->w - srcx;
slouken@0
   478
		if(maxw < w)
slouken@0
   479
			w = maxw;
slouken@0
   480
slouken@0
   481
		srcy = srcrect->y;
slouken@0
   482
		h = srcrect->h;
slouken@0
   483
		if(srcy < 0) {
slouken@0
   484
		        h += srcy;
slouken@0
   485
			dstrect->y -= srcy;
slouken@0
   486
			srcy = 0;
slouken@0
   487
		}
slouken@0
   488
		maxh = src->h - srcy;
slouken@0
   489
		if(maxh < h)
slouken@0
   490
			h = maxh;
slouken@0
   491
	    
slouken@0
   492
	} else {
slouken@0
   493
	        srcx = srcy = 0;
slouken@0
   494
		w = src->w;
slouken@0
   495
		h = src->h;
slouken@0
   496
	}
slouken@0
   497
slouken@0
   498
	/* clip the destination rectangle against the clip rectangle */
slouken@0
   499
	{
slouken@0
   500
	        SDL_Rect *clip = &dst->clip_rect;
slouken@0
   501
		int dx, dy;
slouken@0
   502
slouken@0
   503
		dx = clip->x - dstrect->x;
slouken@0
   504
		if(dx > 0) {
slouken@0
   505
			w -= dx;
slouken@0
   506
			dstrect->x += dx;
slouken@0
   507
			srcx += dx;
slouken@0
   508
		}
slouken@0
   509
		dx = dstrect->x + w - clip->x - clip->w;
slouken@0
   510
		if(dx > 0)
slouken@0
   511
			w -= dx;
slouken@0
   512
slouken@0
   513
		dy = clip->y - dstrect->y;
slouken@0
   514
		if(dy > 0) {
slouken@0
   515
			h -= dy;
slouken@0
   516
			dstrect->y += dy;
slouken@0
   517
			srcy += dy;
slouken@0
   518
		}
slouken@0
   519
		dy = dstrect->y + h - clip->y - clip->h;
slouken@0
   520
		if(dy > 0)
slouken@0
   521
			h -= dy;
slouken@0
   522
	}
slouken@0
   523
slouken@0
   524
	if(w > 0 && h > 0) {
slouken@0
   525
	        SDL_Rect sr;
slouken@0
   526
	        sr.x = srcx;
slouken@0
   527
		sr.y = srcy;
slouken@0
   528
		sr.w = dstrect->w = w;
slouken@0
   529
		sr.h = dstrect->h = h;
slouken@0
   530
		return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@0
   531
	}
slouken@0
   532
	dstrect->w = dstrect->h = 0;
slouken@0
   533
	return 0;
slouken@0
   534
}
slouken@0
   535
slouken@532
   536
static int SDL_FillRect1(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
slouken@532
   537
{
slouken@532
   538
	/* FIXME: We have to worry about packing order.. *sigh* */
slouken@532
   539
	SDL_SetError("1-bpp rect fill not yet implemented");
slouken@532
   540
	return -1;
slouken@532
   541
}
slouken@532
   542
slouken@532
   543
static int SDL_FillRect4(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
slouken@532
   544
{
slouken@532
   545
	/* FIXME: We have to worry about packing order.. *sigh* */
slouken@532
   546
	SDL_SetError("4-bpp rect fill not yet implemented");
slouken@532
   547
	return -1;
slouken@532
   548
}
slouken@532
   549
slouken@0
   550
/* 
slouken@0
   551
 * This function performs a fast fill of the given rectangle with 'color'
slouken@0
   552
 */
slouken@0
   553
int SDL_FillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
slouken@0
   554
{
slouken@0
   555
	SDL_VideoDevice *video = current_video;
slouken@0
   556
	SDL_VideoDevice *this  = current_video;
slouken@0
   557
	int x, y;
slouken@0
   558
	Uint8 *row;
slouken@0
   559
slouken@532
   560
	/* This function doesn't work on surfaces < 8 bpp */
slouken@532
   561
	if ( dst->format->BitsPerPixel < 8 ) {
slouken@532
   562
		switch(dst->format->BitsPerPixel) {
slouken@532
   563
		    case 1:
slouken@532
   564
			return SDL_FillRect1(dst, dstrect, color);
slouken@532
   565
			break;
slouken@532
   566
		    case 4:
slouken@532
   567
			return SDL_FillRect4(dst, dstrect, color);
slouken@532
   568
			break;
slouken@532
   569
		    default:
slouken@532
   570
			SDL_SetError("Fill rect on unsupported surface format");
slouken@532
   571
			return(-1);
slouken@532
   572
			break;
slouken@532
   573
		}
slouken@532
   574
	}
slouken@532
   575
slouken@0
   576
	/* If 'dstrect' == NULL, then fill the whole surface */
slouken@0
   577
	if ( dstrect ) {
slouken@0
   578
		/* Perform clipping */
slouken@0
   579
		if ( !SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect) ) {
slouken@0
   580
			return(0);
slouken@0
   581
		}
slouken@0
   582
	} else {
slouken@0
   583
		dstrect = &dst->clip_rect;
slouken@0
   584
	}
slouken@0
   585
slouken@0
   586
	/* Check for hardware acceleration */
slouken@0
   587
	if ( ((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) &&
slouken@0
   588
					video->info.blit_fill ) {
slouken@462
   589
		SDL_Rect hw_rect;
slouken@462
   590
		if ( dst == SDL_VideoSurface ) {
slouken@462
   591
			hw_rect = *dstrect;
slouken@462
   592
			hw_rect.x += current_video->offset_x;
slouken@462
   593
			hw_rect.y += current_video->offset_y;
slouken@462
   594
			dstrect = &hw_rect;
slouken@462
   595
		}
slouken@0
   596
		return(video->FillHWRect(this, dst, dstrect, color));
slouken@0
   597
	}
slouken@0
   598
slouken@0
   599
	/* Perform software fill */
slouken@0
   600
	if ( SDL_LockSurface(dst) != 0 ) {
slouken@0
   601
		return(-1);
slouken@0
   602
	}
slouken@0
   603
	row = (Uint8 *)dst->pixels+dstrect->y*dst->pitch+
slouken@0
   604
			dstrect->x*dst->format->BytesPerPixel;
slouken@0
   605
	if ( dst->format->palette || (color == 0) ) {
slouken@0
   606
		x = dstrect->w*dst->format->BytesPerPixel;
slouken@1456
   607
		if ( !color && !((uintptr_t)row&3) && !(x&3) && !(dst->pitch&3) ) {
slouken@0
   608
			int n = x >> 2;
slouken@0
   609
			for ( y=dstrect->h; y; --y ) {
slouken@0
   610
				SDL_memset4(row, 0, n);
slouken@0
   611
				row += dst->pitch;
slouken@0
   612
			}
slouken@0
   613
		} else {
slouken@0
   614
#ifdef __powerpc__
slouken@0
   615
			/*
slouken@1336
   616
			 * SDL_memset() on PPC (both glibc and codewarrior) uses
slouken@0
   617
			 * the dcbz (Data Cache Block Zero) instruction, which
slouken@0
   618
			 * causes an alignment exception if the destination is
slouken@0
   619
			 * uncachable, so only use it on software surfaces
slouken@0
   620
			 */
slouken@0
   621
			if((dst->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
slouken@0
   622
				if(dstrect->w >= 8) {
slouken@0
   623
					/*
slouken@0
   624
					 * 64-bit stores are probably most
slouken@0
   625
					 * efficient to uncached video memory
slouken@0
   626
					 */
slouken@0
   627
					double fill;
slouken@1336
   628
					SDL_memset(&fill, color, (sizeof fill));
slouken@0
   629
					for(y = dstrect->h; y; y--) {
slouken@0
   630
						Uint8 *d = row;
slouken@0
   631
						unsigned n = x;
slouken@0
   632
						unsigned nn;
slouken@0
   633
						Uint8 c = color;
slouken@0
   634
						double f = fill;
slouken@0
   635
						while((unsigned long)d
slouken@0
   636
						      & (sizeof(double) - 1)) {
slouken@0
   637
							*d++ = c;
slouken@0
   638
							n--;
slouken@0
   639
						}
slouken@0
   640
						nn = n / (sizeof(double) * 4);
slouken@0
   641
						while(nn) {
slouken@0
   642
							((double *)d)[0] = f;
slouken@0
   643
							((double *)d)[1] = f;
slouken@0
   644
							((double *)d)[2] = f;
slouken@0
   645
							((double *)d)[3] = f;
slouken@0
   646
							d += 4*sizeof(double);
slouken@0
   647
							nn--;
slouken@0
   648
						}
slouken@0
   649
						n &= ~(sizeof(double) * 4 - 1);
slouken@0
   650
						nn = n / sizeof(double);
slouken@0
   651
						while(nn) {
slouken@0
   652
							*(double *)d = f;
slouken@0
   653
							d += sizeof(double);
slouken@0
   654
							nn--;
slouken@0
   655
						}
slouken@0
   656
						n &= ~(sizeof(double) - 1);
slouken@0
   657
						while(n) {
slouken@0
   658
							*d++ = c;
slouken@0
   659
							n--;
slouken@0
   660
						}
slouken@0
   661
						row += dst->pitch;
slouken@0
   662
					}
slouken@0
   663
				} else {
slouken@0
   664
					/* narrow boxes */
slouken@0
   665
					for(y = dstrect->h; y; y--) {
slouken@0
   666
						Uint8 *d = row;
slouken@0
   667
						Uint8 c = color;
slouken@0
   668
						int n = x;
slouken@0
   669
						while(n) {
slouken@0
   670
							*d++ = c;
slouken@0
   671
							n--;
slouken@0
   672
						}
slouken@0
   673
						row += dst->pitch;
slouken@0
   674
					}
slouken@0
   675
				}
slouken@0
   676
			} else
slouken@0
   677
#endif /* __powerpc__ */
slouken@0
   678
			{
slouken@0
   679
				for(y = dstrect->h; y; y--) {
slouken@1336
   680
					SDL_memset(row, color, x);
slouken@0
   681
					row += dst->pitch;
slouken@0
   682
				}
slouken@0
   683
			}
slouken@0
   684
		}
slouken@0
   685
	} else {
slouken@0
   686
		switch (dst->format->BytesPerPixel) {
slouken@0
   687
		    case 2:
slouken@0
   688
			for ( y=dstrect->h; y; --y ) {
slouken@0
   689
				Uint16 *pixels = (Uint16 *)row;
slouken@1428
   690
				Uint16 c = (Uint16)color;
slouken@0
   691
				Uint32 cc = (Uint32)c << 16 | c;
slouken@0
   692
				int n = dstrect->w;
slouken@1456
   693
				if((uintptr_t)pixels & 3) {
slouken@0
   694
					*pixels++ = c;
slouken@0
   695
					n--;
slouken@0
   696
				}
slouken@0
   697
				if(n >> 1)
slouken@0
   698
					SDL_memset4(pixels, cc, n >> 1);
slouken@0
   699
				if(n & 1)
slouken@0
   700
					pixels[n - 1] = c;
slouken@0
   701
				row += dst->pitch;
slouken@0
   702
			}
slouken@0
   703
			break;
slouken@0
   704
slouken@0
   705
		    case 3:
icculus@1155
   706
			#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@0
   707
				color <<= 8;
icculus@1155
   708
			#endif
slouken@0
   709
			for ( y=dstrect->h; y; --y ) {
slouken@0
   710
				Uint8 *pixels = row;
slouken@0
   711
				for ( x=dstrect->w; x; --x ) {
slouken@1336
   712
					SDL_memcpy(pixels, &color, 3);
slouken@0
   713
					pixels += 3;
slouken@0
   714
				}
slouken@0
   715
				row += dst->pitch;
slouken@0
   716
			}
slouken@0
   717
			break;
slouken@0
   718
slouken@0
   719
		    case 4:
slouken@0
   720
			for(y = dstrect->h; y; --y) {
slouken@0
   721
				SDL_memset4(row, color, dstrect->w);
slouken@0
   722
				row += dst->pitch;
slouken@0
   723
			}
slouken@0
   724
			break;
slouken@0
   725
		}
slouken@0
   726
	}
slouken@0
   727
	SDL_UnlockSurface(dst);
slouken@0
   728
slouken@0
   729
	/* We're done! */
slouken@0
   730
	return(0);
slouken@0
   731
}
slouken@0
   732
slouken@0
   733
/*
slouken@0
   734
 * Lock a surface to directly access the pixels
slouken@0
   735
 */
slouken@0
   736
int SDL_LockSurface (SDL_Surface *surface)
slouken@0
   737
{
slouken@0
   738
	if ( ! surface->locked ) {
slouken@0
   739
		/* Perform the lock */
slouken@0
   740
		if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
   741
			SDL_VideoDevice *video = current_video;
slouken@0
   742
			SDL_VideoDevice *this  = current_video;
slouken@0
   743
			if ( video->LockHWSurface(this, surface) < 0 ) {
slouken@0
   744
				return(-1);
slouken@0
   745
			}
slouken@0
   746
		}
slouken@0
   747
		if ( surface->flags & SDL_RLEACCEL ) {
slouken@0
   748
			SDL_UnRLESurface(surface, 1);
slouken@0
   749
			surface->flags |= SDL_RLEACCEL;	/* save accel'd state */
slouken@0
   750
		}
slouken@0
   751
		/* This needs to be done here in case pixels changes value */
slouken@0
   752
		surface->pixels = (Uint8 *)surface->pixels + surface->offset;
slouken@0
   753
	}
slouken@0
   754
slouken@0
   755
	/* Increment the surface lock count, for recursive locks */
slouken@0
   756
	++surface->locked;
slouken@0
   757
slouken@0
   758
	/* Ready to go.. */
slouken@0
   759
	return(0);
slouken@0
   760
}
slouken@0
   761
/*
slouken@0
   762
 * Unlock a previously locked surface
slouken@0
   763
 */
slouken@0
   764
void SDL_UnlockSurface (SDL_Surface *surface)
slouken@0
   765
{
slouken@0
   766
	/* Only perform an unlock if we are locked */
slouken@0
   767
	if ( ! surface->locked || (--surface->locked > 0) ) {
slouken@0
   768
		return;
slouken@0
   769
	}
slouken@0
   770
slouken@0
   771
	/* Perform the unlock */
slouken@0
   772
	surface->pixels = (Uint8 *)surface->pixels - surface->offset;
slouken@0
   773
slouken@0
   774
	/* Unlock hardware or accelerated surfaces */
slouken@0
   775
	if ( surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT) ) {
slouken@0
   776
		SDL_VideoDevice *video = current_video;
slouken@0
   777
		SDL_VideoDevice *this  = current_video;
slouken@0
   778
		video->UnlockHWSurface(this, surface);
slouken@0
   779
	} else {
slouken@0
   780
		/* Update RLE encoded surface with new data */
slouken@0
   781
		if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
   782
		        surface->flags &= ~SDL_RLEACCEL; /* stop lying */
slouken@0
   783
			SDL_RLESurface(surface);
slouken@0
   784
		}
slouken@0
   785
	}
slouken@0
   786
}
slouken@0
   787
slouken@0
   788
/* 
slouken@0
   789
 * Convert a surface into the specified pixel format.
slouken@0
   790
 */
slouken@0
   791
SDL_Surface * SDL_ConvertSurface (SDL_Surface *surface,
slouken@0
   792
					SDL_PixelFormat *format, Uint32 flags)
slouken@0
   793
{
slouken@0
   794
	SDL_Surface *convert;
slouken@0
   795
	Uint32 colorkey = 0;
slouken@0
   796
	Uint8 alpha = 0;
slouken@0
   797
	Uint32 surface_flags;
slouken@0
   798
	SDL_Rect bounds;
slouken@0
   799
slouken@0
   800
	/* Check for empty destination palette! (results in empty image) */
slouken@0
   801
	if ( format->palette != NULL ) {
slouken@0
   802
		int i;
slouken@0
   803
		for ( i=0; i<format->palette->ncolors; ++i ) {
slouken@0
   804
			if ( (format->palette->colors[i].r != 0) ||
slouken@0
   805
			     (format->palette->colors[i].g != 0) ||
slouken@0
   806
			     (format->palette->colors[i].b != 0) )
slouken@0
   807
				break;
slouken@0
   808
		}
slouken@0
   809
		if ( i == format->palette->ncolors ) {
slouken@0
   810
			SDL_SetError("Empty destination palette");
slouken@0
   811
			return(NULL);
slouken@0
   812
		}
slouken@0
   813
	}
slouken@0
   814
slouken@264
   815
	/* Only create hw surfaces with alpha channel if hw alpha blits
slouken@264
   816
	   are supported */
slouken@264
   817
	if(format->Amask != 0 && (flags & SDL_HWSURFACE)) {
slouken@264
   818
		const SDL_VideoInfo *vi = SDL_GetVideoInfo();
slouken@264
   819
		if(!vi || !vi->blit_hw_A)
slouken@264
   820
			flags &= ~SDL_HWSURFACE;
slouken@264
   821
	}
slouken@264
   822
slouken@0
   823
	/* Create a new surface with the desired format */
slouken@0
   824
	convert = SDL_CreateRGBSurface(flags,
slouken@0
   825
				surface->w, surface->h, format->BitsPerPixel,
slouken@0
   826
		format->Rmask, format->Gmask, format->Bmask, format->Amask);
slouken@0
   827
	if ( convert == NULL ) {
slouken@0
   828
		return(NULL);
slouken@0
   829
	}
slouken@0
   830
slouken@0
   831
	/* Copy the palette if any */
slouken@0
   832
	if ( format->palette && convert->format->palette ) {
slouken@1336
   833
		SDL_memcpy(convert->format->palette->colors,
slouken@0
   834
				format->palette->colors,
slouken@0
   835
				format->palette->ncolors*sizeof(SDL_Color));
slouken@0
   836
		convert->format->palette->ncolors = format->palette->ncolors;
slouken@0
   837
	}
slouken@0
   838
slouken@0
   839
	/* Save the original surface color key and alpha */
slouken@0
   840
	surface_flags = surface->flags;
slouken@0
   841
	if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@0
   842
		/* Convert colourkeyed surfaces to RGBA if requested */
slouken@0
   843
		if((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY
slouken@0
   844
		   && format->Amask) {
slouken@0
   845
			surface_flags &= ~SDL_SRCCOLORKEY;
slouken@0
   846
		} else {
slouken@0
   847
			colorkey = surface->format->colorkey;
slouken@0
   848
			SDL_SetColorKey(surface, 0, 0);
slouken@0
   849
		}
slouken@0
   850
	}
slouken@0
   851
	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
slouken@431
   852
		/* Copy over the alpha channel to RGBA if requested */
slouken@431
   853
		if ( format->Amask ) {
slouken@431
   854
			surface->flags &= ~SDL_SRCALPHA;
slouken@431
   855
		} else {
slouken@431
   856
			alpha = surface->format->alpha;
slouken@431
   857
			SDL_SetAlpha(surface, 0, 0);
slouken@431
   858
		}
slouken@0
   859
	}
slouken@0
   860
slouken@0
   861
	/* Copy over the image data */
slouken@0
   862
	bounds.x = 0;
slouken@0
   863
	bounds.y = 0;
slouken@0
   864
	bounds.w = surface->w;
slouken@0
   865
	bounds.h = surface->h;
slouken@0
   866
	SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   867
slouken@0
   868
	/* Clean up the original surface, and update converted surface */
slouken@0
   869
	if ( convert != NULL ) {
slouken@0
   870
		SDL_SetClipRect(convert, &surface->clip_rect);
slouken@0
   871
	}
slouken@0
   872
	if ( (surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@0
   873
		Uint32 cflags = surface_flags&(SDL_SRCCOLORKEY|SDL_RLEACCELOK);
slouken@0
   874
		if ( convert != NULL ) {
slouken@0
   875
			Uint8 keyR, keyG, keyB;
slouken@0
   876
slouken@0
   877
			SDL_GetRGB(colorkey,surface->format,&keyR,&keyG,&keyB);
slouken@0
   878
			SDL_SetColorKey(convert, cflags|(flags&SDL_RLEACCELOK),
slouken@0
   879
				SDL_MapRGB(convert->format, keyR, keyG, keyB));
slouken@0
   880
		}
slouken@0
   881
		SDL_SetColorKey(surface, cflags, colorkey);
slouken@0
   882
	}
slouken@0
   883
	if ( (surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
slouken@0
   884
		Uint32 aflags = surface_flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
slouken@0
   885
		if ( convert != NULL ) {
slouken@0
   886
		        SDL_SetAlpha(convert, aflags|(flags&SDL_RLEACCELOK),
slouken@0
   887
				alpha);
slouken@0
   888
		}
slouken@431
   889
		if ( format->Amask ) {
slouken@431
   890
			surface->flags |= SDL_SRCALPHA;
slouken@431
   891
		} else {
slouken@431
   892
			SDL_SetAlpha(surface, aflags, alpha);
slouken@431
   893
		}
slouken@0
   894
	}
slouken@0
   895
slouken@0
   896
	/* We're ready to go! */
slouken@0
   897
	return(convert);
slouken@0
   898
}
slouken@0
   899
slouken@0
   900
/*
slouken@0
   901
 * Free a surface created by the above function.
slouken@0
   902
 */
slouken@0
   903
void SDL_FreeSurface (SDL_Surface *surface)
slouken@0
   904
{
slouken@0
   905
	/* Free anything that's not NULL, and not the screen surface */
slouken@0
   906
	if ((surface == NULL) ||
slouken@0
   907
	    (current_video &&
slouken@0
   908
	    ((surface == SDL_ShadowSurface)||(surface == SDL_VideoSurface)))) {
slouken@0
   909
		return;
slouken@0
   910
	}
slouken@0
   911
	if ( --surface->refcount > 0 ) {
slouken@0
   912
		return;
slouken@0
   913
	}
slouken@915
   914
	while ( surface->locked > 0 ) {
slouken@915
   915
		SDL_UnlockSurface(surface);
slouken@915
   916
	}
slouken@0
   917
	if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
slouken@0
   918
	        SDL_UnRLESurface(surface, 0);
slouken@0
   919
	}
slouken@0
   920
	if ( surface->format ) {
slouken@0
   921
		SDL_FreeFormat(surface->format);
slouken@0
   922
		surface->format = NULL;
slouken@0
   923
	}
slouken@0
   924
	if ( surface->map != NULL ) {
slouken@0
   925
		SDL_FreeBlitMap(surface->map);
slouken@0
   926
		surface->map = NULL;
slouken@0
   927
	}
slouken@422
   928
	if ( surface->hwdata ) {
slouken@0
   929
		SDL_VideoDevice *video = current_video;
slouken@0
   930
		SDL_VideoDevice *this  = current_video;
slouken@0
   931
		video->FreeHWSurface(this, surface);
slouken@0
   932
	}
slouken@0
   933
	if ( surface->pixels &&
slouken@0
   934
	     ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC) ) {
slouken@1336
   935
		SDL_free(surface->pixels);
slouken@0
   936
	}
slouken@1336
   937
	SDL_free(surface);
slouken@0
   938
#ifdef CHECK_LEAKS
slouken@0
   939
	--surfaces_allocated;
slouken@0
   940
#endif
slouken@0
   941
}