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