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