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