src/video/SDL_surface.c
author Ken Rogoway <ken@rogoway.com>
Mon, 14 Feb 2011 11:50:18 -0600
changeset 5296 48067bfc300c
parent 5288 d4381f3b0d1e
child 5375 16877f74123c
permissions -rw-r--r--
Software scaling support. Not very fast, but it seems to work.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@1895
    25
#include "SDL_compat.h"
slouken@0
    26
#include "SDL_sysvideo.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
icculus@1251
    31
slouken@0
    32
/* Public routines */
slouken@0
    33
/*
slouken@0
    34
 * Create an empty RGB surface of the appropriate depth
slouken@0
    35
 */
slouken@1895
    36
SDL_Surface *
slouken@1895
    37
SDL_CreateRGBSurface(Uint32 flags,
slouken@1895
    38
                     int width, int height, int depth,
slouken@1895
    39
                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
    40
{
slouken@1895
    41
    SDL_Surface *surface;
slouken@5288
    42
    Uint32 format;
slouken@0
    43
slouken@2807
    44
    /* The flags are no longer used, make the compiler happy */
slouken@4461
    45
    (void)flags;
slouken@2807
    46
slouken@5288
    47
    /* Get the pixel format */
slouken@5288
    48
    format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
slouken@5288
    49
    if (format == SDL_PIXELFORMAT_UNKNOWN) {
slouken@5288
    50
        SDL_SetError("Unknown pixel format");
slouken@5288
    51
        return NULL;
slouken@5288
    52
    }
slouken@5288
    53
slouken@1895
    54
    /* Allocate the surface */
slouken@1920
    55
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
slouken@1895
    56
    if (surface == NULL) {
slouken@1895
    57
        SDL_OutOfMemory();
slouken@1895
    58
        return NULL;
slouken@1895
    59
    }
slouken@940
    60
slouken@5288
    61
    surface->format = SDL_AllocFormat(format);
slouken@1895
    62
    if (!surface->format) {
slouken@1895
    63
        SDL_FreeSurface(surface);
slouken@1895
    64
        return NULL;
slouken@1895
    65
    }
slouken@1895
    66
    surface->w = width;
slouken@1895
    67
    surface->h = height;
slouken@1895
    68
    surface->pitch = SDL_CalculatePitch(surface);
slouken@1895
    69
    SDL_SetClipRect(surface, NULL);
slouken@0
    70
slouken@5288
    71
    if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
slouken@1895
    72
        SDL_Palette *palette =
slouken@1895
    73
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
slouken@1895
    74
        if (!palette) {
slouken@1895
    75
            SDL_FreeSurface(surface);
slouken@1895
    76
            return NULL;
slouken@1895
    77
        }
slouken@1895
    78
        if (Rmask || Bmask || Gmask) {
slouken@1895
    79
            const SDL_PixelFormat *format = surface->format;
slouken@0
    80
slouken@1895
    81
            /* create palette according to masks */
slouken@1895
    82
            int i;
slouken@1895
    83
            int Rm = 0, Gm = 0, Bm = 0;
slouken@1895
    84
            int Rw = 0, Gw = 0, Bw = 0;
slouken@0
    85
slouken@1895
    86
            if (Rmask) {
slouken@1895
    87
                Rw = 8 - format->Rloss;
slouken@1895
    88
                for (i = format->Rloss; i > 0; i -= Rw)
slouken@1895
    89
                    Rm |= 1 << i;
slouken@1895
    90
            }
slouken@1895
    91
            if (Gmask) {
slouken@1895
    92
                Gw = 8 - format->Gloss;
slouken@1895
    93
                for (i = format->Gloss; i > 0; i -= Gw)
slouken@1895
    94
                    Gm |= 1 << i;
slouken@1895
    95
            }
slouken@1895
    96
            if (Bmask) {
slouken@1895
    97
                Bw = 8 - format->Bloss;
slouken@1895
    98
                for (i = format->Bloss; i > 0; i -= Bw)
slouken@1895
    99
                    Bm |= 1 << i;
slouken@1895
   100
            }
slouken@1895
   101
            for (i = 0; i < palette->ncolors; ++i) {
slouken@1895
   102
                int r, g, b;
slouken@1895
   103
                r = (i & Rmask) >> format->Rshift;
slouken@1895
   104
                r = (r << format->Rloss) | ((r * Rm) >> Rw);
slouken@1895
   105
                palette->colors[i].r = r;
slouken@0
   106
slouken@1895
   107
                g = (i & Gmask) >> format->Gshift;
slouken@1895
   108
                g = (g << format->Gloss) | ((g * Gm) >> Gw);
slouken@1895
   109
                palette->colors[i].g = g;
slouken@1895
   110
slouken@1895
   111
                b = (i & Bmask) >> format->Bshift;
slouken@1895
   112
                b = (b << format->Bloss) | ((b * Bm) >> Bw);
slouken@1895
   113
                palette->colors[i].b = b;
slouken@1895
   114
            }
slouken@1895
   115
        } else if (palette->ncolors == 2) {
slouken@1895
   116
            /* Create a black and white bitmap palette */
slouken@1895
   117
            palette->colors[0].r = 0xFF;
slouken@1895
   118
            palette->colors[0].g = 0xFF;
slouken@1895
   119
            palette->colors[0].b = 0xFF;
slouken@1895
   120
            palette->colors[1].r = 0x00;
slouken@1895
   121
            palette->colors[1].g = 0x00;
slouken@1895
   122
            palette->colors[1].b = 0x00;
slouken@1895
   123
        }
slouken@1895
   124
        SDL_SetSurfacePalette(surface, palette);
slouken@1895
   125
        SDL_FreePalette(palette);
slouken@1895
   126
    }
slouken@1895
   127
slouken@1895
   128
    /* Get the pixels */
slouken@1895
   129
    if (surface->w && surface->h) {
slouken@1895
   130
        surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
   131
        if (!surface->pixels) {
slouken@1895
   132
            SDL_FreeSurface(surface);
slouken@1895
   133
            SDL_OutOfMemory();
slouken@1895
   134
            return NULL;
slouken@1895
   135
        }
slouken@1895
   136
        /* This is important for bitmaps */
slouken@1895
   137
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@1895
   138
    }
slouken@1895
   139
slouken@1895
   140
    /* Allocate an empty mapping */
slouken@1895
   141
    surface->map = SDL_AllocBlitMap();
slouken@1895
   142
    if (!surface->map) {
slouken@1895
   143
        SDL_FreeSurface(surface);
slouken@1895
   144
        return NULL;
slouken@1895
   145
    }
slouken@1895
   146
slouken@2267
   147
    /* By default surface with an alpha mask are set up for blending */
slouken@2267
   148
    if (Amask) {
slouken@2884
   149
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2267
   150
    }
slouken@2267
   151
slouken@1895
   152
    /* The surface is ready to go */
slouken@1895
   153
    surface->refcount = 1;
slouken@1895
   154
    return surface;
slouken@0
   155
}
slouken@1895
   156
slouken@0
   157
/*
slouken@0
   158
 * Create an RGB surface from an existing memory buffer
slouken@0
   159
 */
slouken@1895
   160
SDL_Surface *
slouken@1895
   161
SDL_CreateRGBSurfaceFrom(void *pixels,
slouken@1895
   162
                         int width, int height, int depth, int pitch,
slouken@1895
   163
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   164
                         Uint32 Amask)
slouken@0
   165
{
slouken@1895
   166
    SDL_Surface *surface;
slouken@0
   167
slouken@1895
   168
    surface =
slouken@1895
   169
        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
slouken@1895
   170
    if (surface != NULL) {
slouken@1895
   171
        surface->flags |= SDL_PREALLOC;
slouken@1895
   172
        surface->pixels = pixels;
slouken@1895
   173
        surface->w = width;
slouken@1895
   174
        surface->h = height;
slouken@1895
   175
        surface->pitch = pitch;
slouken@1895
   176
        SDL_SetClipRect(surface, NULL);
slouken@1895
   177
    }
slouken@1895
   178
    return surface;
slouken@0
   179
}
slouken@1895
   180
slouken@1895
   181
int
slouken@1895
   182
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
slouken@1895
   183
{
slouken@5288
   184
    if (!surface) {
slouken@1895
   185
        SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
slouken@1895
   186
        return -1;
slouken@1895
   187
    }
slouken@5288
   188
    return SDL_SetPixelFormatPalette(surface->format, palette);
slouken@1895
   189
}
slouken@1895
   190
slouken@2267
   191
int
slouken@2267
   192
SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
slouken@0
   193
{
slouken@2267
   194
    int flags;
slouken@0
   195
slouken@2266
   196
    if (!surface) {
slouken@2266
   197
        return -1;
slouken@1895
   198
    }
slouken@0
   199
slouken@2267
   200
    flags = surface->map->info.flags;
slouken@1895
   201
    if (flag) {
slouken@2267
   202
        surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
slouken@1895
   203
    } else {
slouken@2267
   204
        surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
slouken@1895
   205
    }
slouken@2267
   206
    if (surface->map->info.flags != flags) {
slouken@1895
   207
        SDL_InvalidateMap(surface->map);
slouken@1895
   208
    }
slouken@1895
   209
    return 0;
slouken@431
   210
}
slouken@0
   211
slouken@2267
   212
int
slouken@3560
   213
SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
slouken@2266
   214
{
slouken@2266
   215
    int flags;
slouken@2266
   216
slouken@2266
   217
    if (!surface) {
slouken@2266
   218
        return -1;
slouken@2266
   219
    }
slouken@2266
   220
slouken@2266
   221
    if (flag & SDL_RLEACCEL) {
slouken@2266
   222
        SDL_SetSurfaceRLE(surface, 1);
slouken@2266
   223
    }
slouken@2266
   224
slouken@2266
   225
    flags = surface->map->info.flags;
slouken@2266
   226
    if (flag) {
slouken@2266
   227
        surface->map->info.flags |= SDL_COPY_COLORKEY;
slouken@2266
   228
        surface->map->info.colorkey = key;
slouken@2266
   229
    } else {
slouken@2266
   230
        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
slouken@2266
   231
    }
slouken@2266
   232
    if (surface->map->info.flags != flags) {
slouken@2266
   233
        SDL_InvalidateMap(surface->map);
slouken@2266
   234
    }
slouken@2267
   235
slouken@2267
   236
    /* Compatibility mode */
slouken@2267
   237
    if (surface->map->info.flags & SDL_COPY_COLORKEY) {
slouken@2267
   238
        surface->flags |= SDL_SRCCOLORKEY;
slouken@2267
   239
    } else {
slouken@2267
   240
        surface->flags &= ~SDL_SRCCOLORKEY;
slouken@2267
   241
    }
slouken@2267
   242
slouken@2266
   243
    return 0;
slouken@2266
   244
}
slouken@2266
   245
slouken@3103
   246
int
slouken@3103
   247
SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
slouken@3103
   248
{
slouken@3103
   249
    if (!surface) {
slouken@3103
   250
        return -1;
slouken@3103
   251
    }
slouken@3103
   252
slouken@3103
   253
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
slouken@3103
   254
        return -1;
slouken@3103
   255
    }
slouken@3103
   256
slouken@3103
   257
    if (key) {
slouken@3103
   258
        *key = surface->map->info.colorkey;
slouken@3103
   259
    }
slouken@3103
   260
    return 0;
slouken@3103
   261
}
slouken@3103
   262
slouken@2785
   263
/* This is a fairly slow function to switch from colorkey to alpha */
slouken@2787
   264
static void
slouken@2786
   265
SDL_ConvertColorkeyToAlpha(SDL_Surface * surface)
slouken@2785
   266
{
slouken@2786
   267
    int x, y;
slouken@2785
   268
slouken@2786
   269
    if (!surface) {
slouken@2786
   270
        return;
slouken@2786
   271
    }
slouken@2785
   272
slouken@2786
   273
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
slouken@2786
   274
        !surface->format->Amask) {
slouken@2786
   275
        return;
slouken@2786
   276
    }
slouken@2785
   277
slouken@2786
   278
    SDL_LockSurface(surface);
slouken@2785
   279
slouken@2786
   280
    switch (surface->format->BytesPerPixel) {
slouken@2786
   281
    case 2:
slouken@2786
   282
        {
slouken@2786
   283
            Uint16 *row, *spot;
slouken@2786
   284
            Uint16 ckey = (Uint16) surface->map->info.colorkey;
slouken@2786
   285
            Uint16 mask = (Uint16) (~surface->format->Amask);
slouken@2785
   286
slouken@2786
   287
            row = (Uint16 *) surface->pixels;
slouken@2786
   288
            for (y = surface->h; y--;) {
slouken@2786
   289
                spot = row;
slouken@2786
   290
                for (x = surface->w; x--;) {
slouken@2786
   291
                    if (*spot == ckey) {
slouken@2786
   292
                        *spot &= mask;
slouken@2786
   293
                    }
slouken@2786
   294
                    ++spot;
slouken@2786
   295
                }
slouken@2786
   296
                row += surface->pitch / 2;
slouken@2786
   297
            }
slouken@2786
   298
        }
slouken@2786
   299
        break;
slouken@2786
   300
    case 3:
slouken@2786
   301
        /* FIXME */
slouken@2786
   302
        break;
slouken@2786
   303
    case 4:
slouken@2786
   304
        {
slouken@2786
   305
            Uint32 *row, *spot;
slouken@2786
   306
            Uint32 ckey = surface->map->info.colorkey;
slouken@2786
   307
            Uint32 mask = ~surface->format->Amask;
slouken@2785
   308
slouken@2786
   309
            row = (Uint32 *) surface->pixels;
slouken@2786
   310
            for (y = surface->h; y--;) {
slouken@2786
   311
                spot = row;
slouken@2786
   312
                for (x = surface->w; x--;) {
slouken@2786
   313
                    if (*spot == ckey) {
slouken@2786
   314
                        *spot &= mask;
slouken@2786
   315
                    }
slouken@2786
   316
                    ++spot;
slouken@2786
   317
                }
slouken@2786
   318
                row += surface->pitch / 4;
slouken@2786
   319
            }
slouken@2786
   320
        }
slouken@2786
   321
        break;
slouken@2786
   322
    }
slouken@2785
   323
slouken@2786
   324
    SDL_UnlockSurface(surface);
slouken@2785
   325
slouken@2786
   326
    SDL_SetColorKey(surface, 0, 0);
slouken@2884
   327
    SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
slouken@2785
   328
}
slouken@2785
   329
slouken@2267
   330
int
slouken@2267
   331
SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
slouken@2266
   332
{
slouken@2266
   333
    int flags;
slouken@2266
   334
slouken@2266
   335
    if (!surface) {
slouken@2266
   336
        return -1;
slouken@2266
   337
    }
slouken@2266
   338
slouken@2266
   339
    surface->map->info.r = r;
slouken@2266
   340
    surface->map->info.g = g;
slouken@2266
   341
    surface->map->info.b = b;
slouken@2266
   342
slouken@2266
   343
    flags = surface->map->info.flags;
slouken@2266
   344
    if (r != 0xFF || g != 0xFF || b != 0xFF) {
slouken@2266
   345
        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
slouken@2266
   346
    } else {
slouken@2266
   347
        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
slouken@2266
   348
    }
slouken@2266
   349
    if (surface->map->info.flags != flags) {
slouken@2266
   350
        SDL_InvalidateMap(surface->map);
slouken@2266
   351
    }
slouken@2266
   352
    return 0;
slouken@2266
   353
}
slouken@2266
   354
slouken@2266
   355
slouken@2267
   356
int
slouken@2267
   357
SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
slouken@2266
   358
{
slouken@2266
   359
    if (!surface) {
slouken@2266
   360
        return -1;
slouken@2266
   361
    }
slouken@2266
   362
slouken@2266
   363
    if (r) {
slouken@2266
   364
        *r = surface->map->info.r;
slouken@2266
   365
    }
slouken@2266
   366
    if (g) {
slouken@2266
   367
        *g = surface->map->info.g;
slouken@2266
   368
    }
slouken@2266
   369
    if (b) {
slouken@2266
   370
        *b = surface->map->info.b;
slouken@2266
   371
    }
slouken@2266
   372
    return 0;
slouken@2266
   373
}
slouken@2266
   374
slouken@2267
   375
int
slouken@2267
   376
SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
slouken@2266
   377
{
slouken@2266
   378
    int flags;
slouken@2266
   379
slouken@2266
   380
    if (!surface) {
slouken@2266
   381
        return -1;
slouken@2266
   382
    }
slouken@2266
   383
slouken@2266
   384
    surface->map->info.a = alpha;
slouken@2266
   385
slouken@2266
   386
    flags = surface->map->info.flags;
slouken@2266
   387
    if (alpha != 0xFF) {
slouken@2266
   388
        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
slouken@2266
   389
    } else {
slouken@2266
   390
        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
slouken@2266
   391
    }
slouken@2266
   392
    if (surface->map->info.flags != flags) {
slouken@2266
   393
        SDL_InvalidateMap(surface->map);
slouken@2266
   394
    }
slouken@2266
   395
    return 0;
slouken@2266
   396
}
slouken@2266
   397
slouken@2267
   398
int
slouken@2267
   399
SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
slouken@2266
   400
{
slouken@2266
   401
    if (!surface) {
slouken@2266
   402
        return -1;
slouken@2266
   403
    }
slouken@2266
   404
slouken@2266
   405
    if (alpha) {
slouken@2266
   406
        *alpha = surface->map->info.a;
slouken@2266
   407
    }
slouken@2266
   408
    return 0;
slouken@2266
   409
}
slouken@2266
   410
slouken@2267
   411
int
slouken@4929
   412
SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
slouken@2266
   413
{
slouken@2266
   414
    int flags, status;
slouken@2266
   415
slouken@2266
   416
    if (!surface) {
slouken@2266
   417
        return -1;
slouken@2266
   418
    }
slouken@2266
   419
slouken@2266
   420
    status = 0;
slouken@2266
   421
    flags = surface->map->info.flags;
slouken@5184
   422
    surface->map->info.flags &=
slouken@5184
   423
        ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
slouken@2266
   424
    switch (blendMode) {
slouken@2884
   425
    case SDL_BLENDMODE_NONE:
slouken@2266
   426
        break;
slouken@2884
   427
    case SDL_BLENDMODE_BLEND:
slouken@2266
   428
        surface->map->info.flags |= SDL_COPY_BLEND;
slouken@2266
   429
        break;
slouken@2884
   430
    case SDL_BLENDMODE_ADD:
slouken@2266
   431
        surface->map->info.flags |= SDL_COPY_ADD;
slouken@2266
   432
        break;
slouken@5184
   433
    case SDL_BLENDMODE_MOD:
slouken@5184
   434
        surface->map->info.flags |= SDL_COPY_MOD;
slouken@5184
   435
        break;
slouken@2266
   436
    default:
slouken@2266
   437
        SDL_Unsupported();
slouken@2266
   438
        status = -1;
slouken@2266
   439
        break;
slouken@2266
   440
    }
slouken@2266
   441
slouken@2266
   442
    if (surface->map->info.flags != flags) {
slouken@2266
   443
        SDL_InvalidateMap(surface->map);
slouken@2266
   444
    }
slouken@2267
   445
slouken@2267
   446
    /* Compatibility mode */
slouken@2267
   447
    if (surface->map->info.flags & SDL_COPY_BLEND) {
slouken@2267
   448
        surface->flags |= SDL_SRCALPHA;
slouken@2267
   449
    } else {
slouken@2267
   450
        surface->flags &= ~SDL_SRCALPHA;
slouken@2267
   451
    }
slouken@2267
   452
slouken@2266
   453
    return status;
slouken@2266
   454
}
slouken@2266
   455
slouken@2267
   456
int
slouken@4929
   457
SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
slouken@2266
   458
{
slouken@2266
   459
    if (!surface) {
slouken@2266
   460
        return -1;
slouken@2266
   461
    }
slouken@2266
   462
slouken@2266
   463
    if (!blendMode) {
slouken@2266
   464
        return 0;
slouken@2266
   465
    }
slouken@2266
   466
slouken@5184
   467
    switch (surface->map->
slouken@5184
   468
            info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
slouken@2266
   469
    case SDL_COPY_BLEND:
slouken@2884
   470
        *blendMode = SDL_BLENDMODE_BLEND;
slouken@2266
   471
        break;
slouken@2266
   472
    case SDL_COPY_ADD:
slouken@2884
   473
        *blendMode = SDL_BLENDMODE_ADD;
slouken@2266
   474
        break;
slouken@5184
   475
    case SDL_COPY_MOD:
slouken@5184
   476
        *blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   477
        break;
slouken@2266
   478
    default:
slouken@2884
   479
        *blendMode = SDL_BLENDMODE_NONE;
slouken@2266
   480
        break;
slouken@2266
   481
    }
slouken@2266
   482
    return 0;
slouken@2266
   483
}
slouken@2266
   484
slouken@1895
   485
SDL_bool
slouken@1895
   486
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   487
{
slouken@1895
   488
    SDL_Rect full_rect;
slouken@0
   489
slouken@1895
   490
    /* Don't do anything if there's no surface to act on */
slouken@1895
   491
    if (!surface) {
slouken@1895
   492
        return SDL_FALSE;
slouken@1895
   493
    }
slouken@0
   494
slouken@1895
   495
    /* Set up the full surface rectangle */
slouken@1895
   496
    full_rect.x = 0;
slouken@1895
   497
    full_rect.y = 0;
slouken@1895
   498
    full_rect.w = surface->w;
slouken@1895
   499
    full_rect.h = surface->h;
slouken@0
   500
slouken@1895
   501
    /* Set the clipping rectangle */
slouken@1895
   502
    if (!rect) {
slouken@1895
   503
        surface->clip_rect = full_rect;
slouken@4966
   504
        return SDL_TRUE;
slouken@1895
   505
    }
slouken@1895
   506
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   507
}
slouken@1895
   508
slouken@1895
   509
void
slouken@1895
   510
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   511
{
slouken@1895
   512
    if (surface && rect) {
slouken@1895
   513
        *rect = surface->clip_rect;
slouken@1895
   514
    }
slouken@0
   515
}
slouken@1895
   516
slouken@0
   517
/* 
slouken@0
   518
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   519
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   520
 * verification.  The lower part is a pointer to a low level
slouken@0
   521
 * accelerated blitting function.
slouken@0
   522
 *
slouken@0
   523
 * These parts are separated out and each used internally by this 
slouken@0
   524
 * library in the optimimum places.  They are exported so that if
slouken@0
   525
 * you know exactly what you are doing, you can optimize your code
slouken@0
   526
 * by calling the one(s) you need.
slouken@0
   527
 */
slouken@1895
   528
int
slouken@1895
   529
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   530
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   531
{
slouken@1895
   532
    /* Check to make sure the blit mapping is valid */
slouken@1895
   533
    if ((src->map->dst != dst) ||
slouken@5288
   534
        (dst->format->palette &&
slouken@5288
   535
         src->map->palette_version != dst->format->palette->version)) {
slouken@1895
   536
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   537
            return (-1);
slouken@1895
   538
        }
bob@2328
   539
        /* just here for debugging */
bob@2329
   540
/*         printf */
bob@2329
   541
/*             ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
bob@2329
   542
/*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
bob@2329
   543
/*              dst->map->info.flags, src->map->blit); */
slouken@1895
   544
    }
slouken@2257
   545
    return (src->map->blit(src, srcrect, dst, dstrect));
slouken@0
   546
}
slouken@0
   547
slouken@0
   548
slouken@1895
   549
int
slouken@4949
   550
SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
slouken@1895
   551
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   552
{
slouken@1895
   553
    SDL_Rect fulldst;
slouken@1895
   554
    int srcx, srcy, w, h;
slouken@0
   555
slouken@1895
   556
    /* Make sure the surfaces aren't locked */
slouken@1895
   557
    if (!src || !dst) {
slouken@1895
   558
        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   559
        return (-1);
slouken@1895
   560
    }
slouken@1895
   561
    if (src->locked || dst->locked) {
slouken@1895
   562
        SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   563
        return (-1);
slouken@1895
   564
    }
slouken@0
   565
slouken@1895
   566
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   567
    if (dstrect == NULL) {
slouken@1895
   568
        fulldst.x = fulldst.y = 0;
slouken@1895
   569
        dstrect = &fulldst;
slouken@1895
   570
    }
slouken@0
   571
slouken@1895
   572
    /* clip the source rectangle to the source surface */
slouken@1895
   573
    if (srcrect) {
slouken@1895
   574
        int maxw, maxh;
slouken@0
   575
slouken@1895
   576
        srcx = srcrect->x;
slouken@1895
   577
        w = srcrect->w;
slouken@1895
   578
        if (srcx < 0) {
slouken@1895
   579
            w += srcx;
slouken@1895
   580
            dstrect->x -= srcx;
slouken@1895
   581
            srcx = 0;
slouken@1895
   582
        }
slouken@1895
   583
        maxw = src->w - srcx;
slouken@1895
   584
        if (maxw < w)
slouken@1895
   585
            w = maxw;
slouken@0
   586
slouken@1895
   587
        srcy = srcrect->y;
slouken@1895
   588
        h = srcrect->h;
slouken@1895
   589
        if (srcy < 0) {
slouken@1895
   590
            h += srcy;
slouken@1895
   591
            dstrect->y -= srcy;
slouken@1895
   592
            srcy = 0;
slouken@1895
   593
        }
slouken@1895
   594
        maxh = src->h - srcy;
slouken@1895
   595
        if (maxh < h)
slouken@1895
   596
            h = maxh;
slouken@0
   597
slouken@1895
   598
    } else {
slouken@1895
   599
        srcx = srcy = 0;
slouken@1895
   600
        w = src->w;
slouken@1895
   601
        h = src->h;
slouken@1895
   602
    }
slouken@0
   603
slouken@1895
   604
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   605
    {
slouken@1895
   606
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   607
        int dx, dy;
slouken@0
   608
slouken@1895
   609
        dx = clip->x - dstrect->x;
slouken@1895
   610
        if (dx > 0) {
slouken@1895
   611
            w -= dx;
slouken@1895
   612
            dstrect->x += dx;
slouken@1895
   613
            srcx += dx;
slouken@1895
   614
        }
slouken@1895
   615
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   616
        if (dx > 0)
slouken@1895
   617
            w -= dx;
slouken@1895
   618
slouken@1895
   619
        dy = clip->y - dstrect->y;
slouken@1895
   620
        if (dy > 0) {
slouken@1895
   621
            h -= dy;
slouken@1895
   622
            dstrect->y += dy;
slouken@1895
   623
            srcy += dy;
slouken@1895
   624
        }
slouken@1895
   625
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   626
        if (dy > 0)
slouken@1895
   627
            h -= dy;
slouken@1895
   628
    }
slouken@1895
   629
slouken@1895
   630
    if (w > 0 && h > 0) {
slouken@1895
   631
        SDL_Rect sr;
slouken@1895
   632
        sr.x = srcx;
slouken@1895
   633
        sr.y = srcy;
slouken@1895
   634
        sr.w = dstrect->w = w;
slouken@1895
   635
        sr.h = dstrect->h = h;
slouken@1895
   636
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   637
    }
slouken@1895
   638
    dstrect->w = dstrect->h = 0;
slouken@1895
   639
    return 0;
slouken@0
   640
}
slouken@0
   641
slouken@0
   642
/*
ken@5296
   643
 * Scale and blit a surface 
ken@5296
   644
*/
ken@5296
   645
int
ken@5296
   646
SDL_BlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
ken@5296
   647
               SDL_Surface * dst, const SDL_Rect * dstrect)
ken@5296
   648
{
ken@5296
   649
    /* Save off the original dst width, height */
ken@5296
   650
    int dstW = dstrect->w;
ken@5296
   651
    int dstH = dstrect->h;
ken@5296
   652
    SDL_Rect final_dst = *dstrect;
ken@5296
   653
    SDL_Rect final_src = *srcrect;
ken@5296
   654
ken@5296
   655
    /* Clip the dst surface to the dstrect */
ken@5296
   656
    SDL_SetClipRect( dst, &final_dst );
ken@5296
   657
ken@5296
   658
    /* If the dest was clipped to a zero sized rect then exit */
ken@5296
   659
    if ( dst->clip_rect.w <= 0 || dst->clip_rect.h <= 0 ) {
ken@5296
   660
        return -1;
ken@5296
   661
    }
ken@5296
   662
ken@5296
   663
    /* Did the dst width change? */
ken@5296
   664
    if ( dstW != dst->clip_rect.w ) {
ken@5296
   665
        /* scale the src width appropriately */
ken@5296
   666
        final_src.w = final_src.w * dst->clip_rect.w / dstW;
ken@5296
   667
    }
ken@5296
   668
ken@5296
   669
    /* Did the dst height change? */
ken@5296
   670
    if ( dstH != dst->clip_rect.h ) {
ken@5296
   671
        /* scale the src width appropriately */
ken@5296
   672
        final_src.h = final_src.h * dst->clip_rect.h / dstH;
ken@5296
   673
    }
ken@5296
   674
ken@5296
   675
    /* Clip the src surface to the srcrect */
ken@5296
   676
    SDL_SetClipRect( src, &final_src );
ken@5296
   677
ken@5296
   678
    src->map->info.flags |= SDL_COPY_NEAREST;
ken@5296
   679
ken@5296
   680
    if ( src->format->format == dst->format->format && !SDL_ISPIXELFORMAT_INDEXED(src->format->format) ) {
ken@5296
   681
        return SDL_SoftStretch( src, &final_src, dst, &final_dst );
ken@5296
   682
    } else {
ken@5296
   683
        return SDL_LowerBlit( src, &final_src, dst, &final_dst );
ken@5296
   684
    }
ken@5296
   685
}
ken@5296
   686
ken@5296
   687
/*
slouken@0
   688
 * Lock a surface to directly access the pixels
slouken@0
   689
 */
slouken@1895
   690
int
slouken@1895
   691
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   692
{
slouken@1895
   693
    if (!surface->locked) {
slouken@1895
   694
        /* Perform the lock */
slouken@1895
   695
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   696
            SDL_UnRLESurface(surface, 1);
slouken@1895
   697
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   698
        }
slouken@1895
   699
    }
slouken@0
   700
slouken@1895
   701
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   702
    ++surface->locked;
slouken@0
   703
slouken@1895
   704
    /* Ready to go.. */
slouken@1895
   705
    return (0);
slouken@0
   706
}
slouken@1895
   707
slouken@0
   708
/*
slouken@0
   709
 * Unlock a previously locked surface
slouken@0
   710
 */
slouken@1895
   711
void
slouken@1895
   712
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   713
{
slouken@1895
   714
    /* Only perform an unlock if we are locked */
slouken@1895
   715
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   716
        return;
slouken@1895
   717
    }
slouken@0
   718
slouken@1895
   719
    /* Update RLE encoded surface with new data */
slouken@1895
   720
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   721
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   722
        SDL_RLESurface(surface);
slouken@1895
   723
    }
slouken@0
   724
}
slouken@0
   725
slouken@0
   726
/* 
slouken@0
   727
 * Convert a surface into the specified pixel format.
slouken@0
   728
 */
slouken@1895
   729
SDL_Surface *
slouken@2807
   730
SDL_ConvertSurface(SDL_Surface * surface, SDL_PixelFormat * format,
slouken@2807
   731
                   Uint32 flags)
slouken@0
   732
{
slouken@1895
   733
    SDL_Surface *convert;
slouken@2266
   734
    Uint32 copy_flags;
slouken@1895
   735
    SDL_Rect bounds;
slouken@0
   736
slouken@1895
   737
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   738
    if (format->palette != NULL) {
slouken@1895
   739
        int i;
slouken@1895
   740
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   741
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   742
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   743
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   744
                break;
slouken@1895
   745
        }
slouken@1895
   746
        if (i == format->palette->ncolors) {
slouken@1895
   747
            SDL_SetError("Empty destination palette");
slouken@1895
   748
            return (NULL);
slouken@1895
   749
        }
slouken@1895
   750
    }
slouken@0
   751
slouken@1895
   752
    /* Create a new surface with the desired format */
slouken@2807
   753
    convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
slouken@1895
   754
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   755
                                   format->Gmask, format->Bmask,
slouken@1895
   756
                                   format->Amask);
slouken@1895
   757
    if (convert == NULL) {
slouken@1895
   758
        return (NULL);
slouken@1895
   759
    }
slouken@264
   760
slouken@1895
   761
    /* Copy the palette if any */
slouken@1895
   762
    if (format->palette && convert->format->palette) {
slouken@1895
   763
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   764
                   format->palette->colors,
slouken@1895
   765
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   766
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   767
    }
slouken@0
   768
slouken@2266
   769
    /* Save the original copy flags */
slouken@2266
   770
    copy_flags = surface->map->info.flags;
slouken@2266
   771
    surface->map->info.flags = 0;
slouken@0
   772
slouken@1895
   773
    /* Copy over the image data */
slouken@1895
   774
    bounds.x = 0;
slouken@1895
   775
    bounds.y = 0;
slouken@1895
   776
    bounds.w = surface->w;
slouken@1895
   777
    bounds.h = surface->h;
slouken@1895
   778
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   779
slouken@1895
   780
    /* Clean up the original surface, and update converted surface */
slouken@2824
   781
    convert->map->info.r = surface->map->info.r;
slouken@2824
   782
    convert->map->info.g = surface->map->info.g;
slouken@2824
   783
    convert->map->info.b = surface->map->info.b;
slouken@2824
   784
    convert->map->info.a = surface->map->info.a;
slouken@2824
   785
    convert->map->info.flags =
slouken@2824
   786
        (copy_flags &
slouken@2824
   787
         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
slouken@2824
   788
           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
slouken@2824
   789
           SDL_COPY_RLE_ALPHAKEY));
slouken@2824
   790
    surface->map->info.flags = copy_flags;
slouken@2266
   791
    if (copy_flags & SDL_COPY_COLORKEY) {
slouken@2266
   792
        Uint8 keyR, keyG, keyB, keyA;
slouken@2266
   793
slouken@2267
   794
        SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
slouken@2267
   795
                    &keyG, &keyB, &keyA);
slouken@2266
   796
        SDL_SetColorKey(convert, 1,
slouken@2266
   797
                        SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
slouken@2824
   798
        /* This is needed when converting for 3D texture upload */
slouken@2787
   799
        SDL_ConvertColorkeyToAlpha(convert);
slouken@1895
   800
    }
slouken@2824
   801
    SDL_SetClipRect(convert, &surface->clip_rect);
slouken@0
   802
slouken@2266
   803
    /* Enable alpha blending by default if the new surface has an
slouken@2266
   804
     * alpha channel or alpha modulation */
slouken@2824
   805
    if ((surface->format->Amask && format->Amask) ||
slouken@4491
   806
        (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
slouken@2884
   807
        SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
slouken@1895
   808
    }
slouken@2824
   809
    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
slouken@2824
   810
        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
slouken@2824
   811
    }
slouken@0
   812
slouken@1895
   813
    /* We're ready to go! */
slouken@1895
   814
    return (convert);
slouken@0
   815
}
slouken@0
   816
slouken@0
   817
/*
slouken@3434
   818
 * Create a surface on the stack for quick blit operations
slouken@3434
   819
 */
slouken@3434
   820
static __inline__ SDL_bool
slouken@3434
   821
SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
slouken@3434
   822
                         void * pixels, int pitch, SDL_Surface * surface, 
slouken@3434
   823
                         SDL_PixelFormat * format, SDL_BlitMap * blitmap)
slouken@3434
   824
{
slouken@5288
   825
    if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
slouken@5288
   826
        SDL_SetError("Indexed pixel formats not supported");
slouken@3434
   827
        return SDL_FALSE;
slouken@3434
   828
    }
slouken@5288
   829
    if (SDL_InitFormat(format, pixel_format) < 0) {
slouken@3434
   830
        return SDL_FALSE;
slouken@3434
   831
    }
slouken@3434
   832
slouken@3434
   833
    SDL_zerop(surface);
slouken@3434
   834
    surface->flags = SDL_PREALLOC;
slouken@5288
   835
    surface->format = format;
slouken@3434
   836
    surface->pixels = pixels;
slouken@3434
   837
    surface->w = width;
slouken@3434
   838
    surface->h = height;
slouken@3434
   839
    surface->pitch = pitch;
slouken@3434
   840
    /* We don't actually need to set up the clip rect for our purposes */
slouken@3434
   841
    /*SDL_SetClipRect(surface, NULL);*/
slouken@3434
   842
slouken@3434
   843
    /* Allocate an empty mapping */
slouken@3434
   844
    SDL_zerop(blitmap);
slouken@3434
   845
    blitmap->info.r = 0xFF;
slouken@3434
   846
    blitmap->info.g = 0xFF;
slouken@3434
   847
    blitmap->info.b = 0xFF;
slouken@3434
   848
    blitmap->info.a = 0xFF;
slouken@3434
   849
    surface->map = blitmap;
slouken@3434
   850
slouken@3434
   851
    /* The surface is ready to go */
slouken@3434
   852
    surface->refcount = 1;
slouken@3434
   853
    return SDL_TRUE;
slouken@3434
   854
}
slouken@3434
   855
slouken@3434
   856
/*
slouken@3434
   857
 * Copy a block of pixels of one format to another format
slouken@3434
   858
 */
slouken@3434
   859
int SDL_ConvertPixels(int width, int height,
slouken@3434
   860
                      Uint32 src_format, const void * src, int src_pitch,
slouken@3434
   861
                      Uint32 dst_format, void * dst, int dst_pitch)
slouken@3434
   862
{
slouken@3434
   863
    SDL_Surface src_surface, dst_surface;
slouken@3434
   864
    SDL_PixelFormat src_fmt, dst_fmt;
slouken@3434
   865
    SDL_BlitMap src_blitmap, dst_blitmap;
slouken@3434
   866
    SDL_Rect rect;
slouken@3434
   867
slouken@3434
   868
    /* Fast path for same format copy */
slouken@3434
   869
    if (src_format == dst_format) {
slouken@3434
   870
        int bpp;
slouken@3434
   871
slouken@3434
   872
        if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
slouken@3434
   873
            switch (src_format) {
slouken@3434
   874
            case SDL_PIXELFORMAT_YV12:
slouken@3434
   875
            case SDL_PIXELFORMAT_IYUV:
slouken@3434
   876
            case SDL_PIXELFORMAT_YUY2:
slouken@3434
   877
            case SDL_PIXELFORMAT_UYVY:
slouken@3434
   878
            case SDL_PIXELFORMAT_YVYU:
slouken@3434
   879
                bpp = 2;
slouken@3434
   880
            default:
slouken@3434
   881
                SDL_SetError("Unknown FOURCC pixel format");
slouken@3434
   882
                return -1;
slouken@3434
   883
            }
slouken@3434
   884
        } else {
slouken@3434
   885
            bpp = SDL_BYTESPERPIXEL(src_format);
slouken@3434
   886
        }
slouken@3434
   887
        width *= bpp;
slouken@3434
   888
slouken@3434
   889
        while (height-- > 0) {
slouken@3434
   890
            SDL_memcpy(dst, src, width);
slouken@3434
   891
            src = (Uint8*)src + src_pitch;
slouken@3434
   892
            dst = (Uint8*)dst + dst_pitch;
slouken@3434
   893
        }
slouken@3434
   894
        return SDL_TRUE;
slouken@3434
   895
    }
slouken@3434
   896
slouken@3434
   897
    if (!SDL_CreateSurfaceOnStack(width, height, src_format, (void*)src,
slouken@3434
   898
                                  src_pitch,
slouken@3434
   899
                                  &src_surface, &src_fmt, &src_blitmap)) {
slouken@3434
   900
        return -1;
slouken@3434
   901
    }
slouken@3434
   902
    if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
slouken@3434
   903
                                  &dst_surface, &dst_fmt, &dst_blitmap)) {
slouken@3434
   904
        return -1;
slouken@3434
   905
    }
slouken@3434
   906
slouken@3434
   907
    /* Set up the rect and go! */
slouken@3434
   908
    rect.x = 0;
slouken@3434
   909
    rect.y = 0;
slouken@3434
   910
    rect.w = width;
slouken@3436
   911
    rect.h = height;
slouken@3434
   912
    return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
slouken@3434
   913
}
slouken@3434
   914
slouken@3434
   915
/*
slouken@0
   916
 * Free a surface created by the above function.
slouken@0
   917
 */
slouken@1895
   918
void
slouken@1895
   919
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   920
{
slouken@1895
   921
    if (surface == NULL) {
slouken@1895
   922
        return;
slouken@1895
   923
    }
slouken@5288
   924
    if (surface->flags & SDL_DONTFREE) {
slouken@5288
   925
        return;
slouken@5288
   926
    }
slouken@1895
   927
    if (--surface->refcount > 0) {
slouken@1895
   928
        return;
slouken@1895
   929
    }
slouken@1895
   930
    while (surface->locked > 0) {
slouken@1895
   931
        SDL_UnlockSurface(surface);
slouken@1895
   932
    }
slouken@1895
   933
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   934
        SDL_UnRLESurface(surface, 0);
slouken@1895
   935
    }
slouken@1895
   936
    if (surface->format) {
slouken@1895
   937
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
   938
        SDL_FreeFormat(surface->format);
slouken@1895
   939
        surface->format = NULL;
slouken@1895
   940
    }
slouken@1895
   941
    if (surface->map != NULL) {
slouken@1895
   942
        SDL_FreeBlitMap(surface->map);
slouken@1895
   943
        surface->map = NULL;
slouken@1895
   944
    }
slouken@1895
   945
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
   946
        SDL_free(surface->pixels);
slouken@1895
   947
    }
slouken@1895
   948
    SDL_free(surface);
slouken@0
   949
}
slouken@1895
   950
slouken@1895
   951
/* vi: set ts=4 sw=4 expandtab: */