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