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