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