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