src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Aug 2007 00:54:53 +0000
changeset 2257 340942cfda48
parent 2253 6d99edd791bf
child 2260 202ddfd1cfb1
permissions -rw-r--r--
Moved the colorkey and per-surface alpha into the blit info,
in preparation for support for general color channel modulation.

Removed and consolidated some data in the blit info.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_video.h"
slouken@1895
    25
#include "SDL_compat.h"
slouken@0
    26
#include "SDL_sysvideo.h"
slouken@0
    27
#include "SDL_blit.h"
slouken@0
    28
#include "SDL_RLEaccel_c.h"
slouken@0
    29
#include "SDL_pixels_c.h"
slouken@0
    30
#include "SDL_leaks.h"
slouken@0
    31
icculus@1251
    32
slouken@0
    33
/* Public routines */
slouken@0
    34
/*
slouken@0
    35
 * Create an empty RGB surface of the appropriate depth
slouken@0
    36
 */
slouken@1895
    37
SDL_Surface *
slouken@1895
    38
SDL_CreateRGBSurface(Uint32 flags,
slouken@1895
    39
                     int width, int height, int depth,
slouken@1895
    40
                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
    41
{
slouken@1895
    42
    SDL_Surface *surface;
slouken@0
    43
slouken@1895
    44
    /* Allocate the surface */
slouken@1920
    45
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
slouken@1895
    46
    if (surface == NULL) {
slouken@1895
    47
        SDL_OutOfMemory();
slouken@1895
    48
        return NULL;
slouken@1895
    49
    }
slouken@940
    50
slouken@1895
    51
    surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
slouken@1895
    52
    if (!surface->format) {
slouken@1895
    53
        SDL_FreeSurface(surface);
slouken@1895
    54
        return NULL;
slouken@1895
    55
    }
slouken@1895
    56
    if (Amask) {
slouken@1895
    57
        surface->flags |= SDL_SRCALPHA;
slouken@1895
    58
    }
slouken@1895
    59
    surface->w = width;
slouken@1895
    60
    surface->h = height;
slouken@1895
    61
    surface->pitch = SDL_CalculatePitch(surface);
slouken@1895
    62
    SDL_SetClipRect(surface, NULL);
slouken@0
    63
slouken@1895
    64
    if (surface->format->BitsPerPixel <= 8) {
slouken@1895
    65
        SDL_Palette *palette =
slouken@1895
    66
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
slouken@1895
    67
        if (!palette) {
slouken@1895
    68
            SDL_FreeSurface(surface);
slouken@1895
    69
            return NULL;
slouken@1895
    70
        }
slouken@1895
    71
        if (Rmask || Bmask || Gmask) {
slouken@1895
    72
            const SDL_PixelFormat *format = surface->format;
slouken@0
    73
slouken@1895
    74
            /* create palette according to masks */
slouken@1895
    75
            int i;
slouken@1895
    76
            int Rm = 0, Gm = 0, Bm = 0;
slouken@1895
    77
            int Rw = 0, Gw = 0, Bw = 0;
slouken@0
    78
slouken@1895
    79
            if (Rmask) {
slouken@1895
    80
                Rw = 8 - format->Rloss;
slouken@1895
    81
                for (i = format->Rloss; i > 0; i -= Rw)
slouken@1895
    82
                    Rm |= 1 << i;
slouken@1895
    83
            }
slouken@1895
    84
            if (Gmask) {
slouken@1895
    85
                Gw = 8 - format->Gloss;
slouken@1895
    86
                for (i = format->Gloss; i > 0; i -= Gw)
slouken@1895
    87
                    Gm |= 1 << i;
slouken@1895
    88
            }
slouken@1895
    89
            if (Bmask) {
slouken@1895
    90
                Bw = 8 - format->Bloss;
slouken@1895
    91
                for (i = format->Bloss; i > 0; i -= Bw)
slouken@1895
    92
                    Bm |= 1 << i;
slouken@1895
    93
            }
slouken@1895
    94
            for (i = 0; i < palette->ncolors; ++i) {
slouken@1895
    95
                int r, g, b;
slouken@1895
    96
                r = (i & Rmask) >> format->Rshift;
slouken@1895
    97
                r = (r << format->Rloss) | ((r * Rm) >> Rw);
slouken@1895
    98
                palette->colors[i].r = r;
slouken@0
    99
slouken@1895
   100
                g = (i & Gmask) >> format->Gshift;
slouken@1895
   101
                g = (g << format->Gloss) | ((g * Gm) >> Gw);
slouken@1895
   102
                palette->colors[i].g = g;
slouken@1895
   103
slouken@1895
   104
                b = (i & Bmask) >> format->Bshift;
slouken@1895
   105
                b = (b << format->Bloss) | ((b * Bm) >> Bw);
slouken@1895
   106
                palette->colors[i].b = b;
slouken@1895
   107
            }
slouken@1895
   108
        } else if (palette->ncolors == 2) {
slouken@1895
   109
            /* Create a black and white bitmap palette */
slouken@1895
   110
            palette->colors[0].r = 0xFF;
slouken@1895
   111
            palette->colors[0].g = 0xFF;
slouken@1895
   112
            palette->colors[0].b = 0xFF;
slouken@1895
   113
            palette->colors[1].r = 0x00;
slouken@1895
   114
            palette->colors[1].g = 0x00;
slouken@1895
   115
            palette->colors[1].b = 0x00;
slouken@1895
   116
        }
slouken@1895
   117
        SDL_SetSurfacePalette(surface, palette);
slouken@1895
   118
        SDL_FreePalette(palette);
slouken@1895
   119
    }
slouken@1895
   120
slouken@1895
   121
    /* Get the pixels */
slouken@1895
   122
    if (surface->w && surface->h) {
slouken@1895
   123
        surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
   124
        if (!surface->pixels) {
slouken@1895
   125
            SDL_FreeSurface(surface);
slouken@1895
   126
            SDL_OutOfMemory();
slouken@1895
   127
            return NULL;
slouken@1895
   128
        }
slouken@1895
   129
        /* This is important for bitmaps */
slouken@1895
   130
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@1895
   131
    }
slouken@1895
   132
slouken@1895
   133
    /* Allocate an empty mapping */
slouken@1895
   134
    surface->map = SDL_AllocBlitMap();
slouken@1895
   135
    if (!surface->map) {
slouken@1895
   136
        SDL_FreeSurface(surface);
slouken@1895
   137
        return NULL;
slouken@1895
   138
    }
slouken@1895
   139
    SDL_FormatChanged(surface);
slouken@1895
   140
slouken@1895
   141
    /* The surface is ready to go */
slouken@1895
   142
    surface->refcount = 1;
slouken@0
   143
#ifdef CHECK_LEAKS
slouken@1895
   144
    ++surfaces_allocated;
slouken@0
   145
#endif
slouken@1895
   146
    return surface;
slouken@0
   147
}
slouken@1895
   148
slouken@0
   149
/*
slouken@0
   150
 * Create an RGB surface from an existing memory buffer
slouken@0
   151
 */
slouken@1895
   152
SDL_Surface *
slouken@1895
   153
SDL_CreateRGBSurfaceFrom(void *pixels,
slouken@1895
   154
                         int width, int height, int depth, int pitch,
slouken@1895
   155
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   156
                         Uint32 Amask)
slouken@0
   157
{
slouken@1895
   158
    SDL_Surface *surface;
slouken@0
   159
slouken@1895
   160
    surface =
slouken@1895
   161
        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
slouken@1895
   162
    if (surface != NULL) {
slouken@1895
   163
        surface->flags |= SDL_PREALLOC;
slouken@1895
   164
        surface->pixels = pixels;
slouken@1895
   165
        surface->w = width;
slouken@1895
   166
        surface->h = height;
slouken@1895
   167
        surface->pitch = pitch;
slouken@1895
   168
        SDL_SetClipRect(surface, NULL);
slouken@1895
   169
    }
slouken@1895
   170
    return surface;
slouken@0
   171
}
slouken@1895
   172
slouken@1895
   173
static int
slouken@1895
   174
SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1895
   175
{
slouken@1895
   176
    SDL_Surface *surface = (SDL_Surface *) userdata;
slouken@1895
   177
slouken@1895
   178
    SDL_FormatChanged(surface);
slouken@1895
   179
slouken@1895
   180
    return 0;
slouken@1895
   181
}
slouken@1895
   182
slouken@1895
   183
int
slouken@1895
   184
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
slouken@1895
   185
{
slouken@1895
   186
    if (!surface || !surface->format) {
slouken@1895
   187
        SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
slouken@1895
   188
        return -1;
slouken@1895
   189
    }
slouken@1895
   190
slouken@1895
   191
    if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
slouken@1895
   192
        SDL_SetError
slouken@1895
   193
            ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
slouken@1895
   194
        return -1;
slouken@1895
   195
    }
slouken@1895
   196
slouken@1895
   197
    if (surface->format->palette == palette) {
slouken@1895
   198
        return 0;
slouken@1895
   199
    }
slouken@1895
   200
slouken@1895
   201
    if (surface->format->palette) {
slouken@1895
   202
        SDL_DelPaletteWatch(surface->format->palette,
slouken@1895
   203
                            SDL_SurfacePaletteChanged, surface);
slouken@1895
   204
    }
slouken@1895
   205
slouken@1895
   206
    surface->format->palette = palette;
slouken@1895
   207
slouken@1895
   208
    if (surface->format->palette) {
slouken@1895
   209
        SDL_AddPaletteWatch(surface->format->palette,
slouken@1895
   210
                            SDL_SurfacePaletteChanged, surface);
slouken@1895
   211
    }
slouken@1895
   212
    return 0;
slouken@1895
   213
}
slouken@1895
   214
slouken@0
   215
/*
slouken@0
   216
 * Set the color key in a blittable surface
slouken@0
   217
 */
slouken@1895
   218
int
slouken@1895
   219
SDL_SetColorKey(SDL_Surface * surface, Uint32 flag, Uint32 key)
slouken@0
   220
{
slouken@1895
   221
    /* Sanity check the flag as it gets passed in */
slouken@1895
   222
    if (flag & SDL_SRCCOLORKEY) {
slouken@1895
   223
        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
slouken@1895
   224
            flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1895
   225
        } else {
slouken@1895
   226
            flag = SDL_SRCCOLORKEY;
slouken@1895
   227
        }
slouken@1895
   228
    } else {
slouken@1895
   229
        flag = 0;
slouken@1895
   230
    }
slouken@0
   231
slouken@1895
   232
    /* Optimize away operations that don't change anything */
slouken@1895
   233
    if ((flag == (surface->flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK))) &&
slouken@2257
   234
        (key == surface->map->ckey)) {
slouken@1895
   235
        return (0);
slouken@1895
   236
    }
slouken@0
   237
slouken@1895
   238
    /* UnRLE surfaces before we change the colorkey */
slouken@1895
   239
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   240
        SDL_UnRLESurface(surface, 1);
slouken@1895
   241
    }
slouken@0
   242
slouken@1895
   243
    if (flag) {
slouken@1895
   244
        surface->flags |= SDL_SRCCOLORKEY;
slouken@2257
   245
        surface->map->ckey = key;
slouken@1895
   246
        if (flag & SDL_RLEACCELOK) {
slouken@1895
   247
            surface->flags |= SDL_RLEACCELOK;
slouken@1895
   248
        } else {
slouken@1895
   249
            surface->flags &= ~SDL_RLEACCELOK;
slouken@1895
   250
        }
slouken@1895
   251
    } else {
slouken@1895
   252
        surface->flags &= ~(SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@2257
   253
        surface->map->ckey = 0;
slouken@1895
   254
    }
slouken@1895
   255
    SDL_InvalidateMap(surface->map);
slouken@1895
   256
    return (0);
slouken@1895
   257
}
slouken@0
   258
slouken@1895
   259
/* This function sets the alpha channel of a surface */
slouken@1895
   260
int
slouken@1895
   261
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
slouken@1895
   262
{
slouken@1895
   263
    Uint32 oldflags = surface->flags;
slouken@2257
   264
    Uint32 oldalpha = (surface->map->cmod >> 24);
slouken@0
   265
slouken@1895
   266
    /* Sanity check the flag as it gets passed in */
slouken@1895
   267
    if (flag & SDL_SRCALPHA) {
slouken@1895
   268
        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
slouken@1895
   269
            flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1895
   270
        } else {
slouken@1895
   271
            flag = SDL_SRCALPHA;
slouken@1895
   272
        }
slouken@1895
   273
    } else {
slouken@1895
   274
        flag = 0;
slouken@1895
   275
    }
slouken@1895
   276
slouken@1895
   277
    /* Optimize away operations that don't change anything */
slouken@1895
   278
    if ((flag == (surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK))) &&
slouken@1895
   279
        (!flag || value == oldalpha)) {
slouken@1895
   280
        return (0);
slouken@1895
   281
    }
slouken@1895
   282
slouken@1895
   283
    if (!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
slouken@1895
   284
        SDL_UnRLESurface(surface, 1);
slouken@1895
   285
slouken@1895
   286
    if (flag) {
slouken@1895
   287
        surface->flags |= SDL_SRCALPHA;
slouken@2257
   288
        surface->map->cmod &= 0x00FFFFFF;
slouken@2257
   289
        surface->map->cmod |= ((Uint32)value << 24);
slouken@1895
   290
        if (flag & SDL_RLEACCELOK) {
slouken@1895
   291
            surface->flags |= SDL_RLEACCELOK;
slouken@1895
   292
        } else {
slouken@1895
   293
            surface->flags &= ~SDL_RLEACCELOK;
slouken@1895
   294
        }
slouken@1895
   295
    } else {
slouken@1895
   296
        surface->flags &= ~SDL_SRCALPHA;
slouken@2257
   297
        surface->map->cmod |= 0xFF000000;
slouken@1895
   298
    }
slouken@1895
   299
    /*
slouken@1895
   300
     * The representation for software surfaces is independent of
slouken@1895
   301
     * per-surface alpha, so no need to invalidate the blit mapping
slouken@1895
   302
     * if just the alpha value was changed. (If either is 255, we still
slouken@1895
   303
     * need to invalidate.)
slouken@1895
   304
     */
slouken@1895
   305
    if (oldflags != surface->flags
slouken@1895
   306
        || (((oldalpha + 1) ^ (value + 1)) & 0x100)) {
slouken@1895
   307
        SDL_InvalidateMap(surface->map);
slouken@1895
   308
    }
slouken@1895
   309
    return (0);
slouken@0
   310
}
slouken@1895
   311
slouken@1895
   312
int
slouken@1895
   313
SDL_SetAlphaChannel(SDL_Surface * surface, Uint8 value)
slouken@0
   314
{
slouken@1895
   315
    int row, col;
slouken@1895
   316
    int offset;
slouken@1895
   317
    Uint8 *buf;
slouken@0
   318
slouken@1895
   319
    if ((surface->format->Amask != 0xFF000000) &&
slouken@1895
   320
        (surface->format->Amask != 0x000000FF)) {
slouken@1895
   321
        SDL_SetError("Unsupported surface alpha mask format");
slouken@1895
   322
        return -1;
slouken@1895
   323
    }
slouken@431
   324
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@1895
   325
    if (surface->format->Amask == 0xFF000000) {
slouken@1895
   326
        offset = 3;
slouken@1895
   327
    } else {
slouken@1895
   328
        offset = 0;
slouken@1895
   329
    }
slouken@431
   330
#else
slouken@1895
   331
    if (surface->format->Amask == 0xFF000000) {
slouken@1895
   332
        offset = 0;
slouken@1895
   333
    } else {
slouken@1895
   334
        offset = 3;
slouken@1895
   335
    }
slouken@431
   336
#endif /* Byte ordering */
slouken@431
   337
slouken@1895
   338
    /* Quickly set the alpha channel of an RGBA or ARGB surface */
slouken@1895
   339
    if (SDL_MUSTLOCK(surface)) {
slouken@1895
   340
        if (SDL_LockSurface(surface) < 0) {
slouken@1895
   341
            return -1;
slouken@1895
   342
        }
slouken@1895
   343
    }
slouken@1895
   344
    row = surface->h;
slouken@1895
   345
    while (row--) {
slouken@1895
   346
        col = surface->w;
slouken@1895
   347
        buf = (Uint8 *) surface->pixels + row * surface->pitch + offset;
slouken@1895
   348
        while (col--) {
slouken@1895
   349
            *buf = value;
slouken@1895
   350
            buf += 4;
slouken@1895
   351
        }
slouken@1895
   352
    }
slouken@1895
   353
    if (SDL_MUSTLOCK(surface)) {
slouken@1895
   354
        SDL_UnlockSurface(surface);
slouken@1895
   355
    }
slouken@1895
   356
    return 0;
slouken@431
   357
}
slouken@0
   358
slouken@0
   359
/*
slouken@0
   360
 * Set the clipping rectangle for a blittable surface
slouken@0
   361
 */
slouken@1895
   362
SDL_bool
slouken@1895
   363
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   364
{
slouken@1895
   365
    SDL_Rect full_rect;
slouken@0
   366
slouken@1895
   367
    /* Don't do anything if there's no surface to act on */
slouken@1895
   368
    if (!surface) {
slouken@1895
   369
        return SDL_FALSE;
slouken@1895
   370
    }
slouken@0
   371
slouken@1895
   372
    /* Set up the full surface rectangle */
slouken@1895
   373
    full_rect.x = 0;
slouken@1895
   374
    full_rect.y = 0;
slouken@1895
   375
    full_rect.w = surface->w;
slouken@1895
   376
    full_rect.h = surface->h;
slouken@0
   377
slouken@1895
   378
    /* Set the clipping rectangle */
slouken@1895
   379
    if (!rect) {
slouken@1895
   380
        surface->clip_rect = full_rect;
slouken@1895
   381
        return 1;
slouken@1895
   382
    }
slouken@1895
   383
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   384
}
slouken@1895
   385
slouken@1895
   386
void
slouken@1895
   387
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   388
{
slouken@1895
   389
    if (surface && rect) {
slouken@1895
   390
        *rect = surface->clip_rect;
slouken@1895
   391
    }
slouken@0
   392
}
slouken@1895
   393
slouken@0
   394
/* 
slouken@0
   395
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   396
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   397
 * verification.  The lower part is a pointer to a low level
slouken@0
   398
 * accelerated blitting function.
slouken@0
   399
 *
slouken@0
   400
 * These parts are separated out and each used internally by this 
slouken@0
   401
 * library in the optimimum places.  They are exported so that if
slouken@0
   402
 * you know exactly what you are doing, you can optimize your code
slouken@0
   403
 * by calling the one(s) you need.
slouken@0
   404
 */
slouken@1895
   405
int
slouken@1895
   406
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   407
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   408
{
slouken@1895
   409
    /* Check to make sure the blit mapping is valid */
slouken@1895
   410
    if ((src->map->dst != dst) ||
slouken@1895
   411
        (src->map->dst->format_version != src->map->format_version)) {
slouken@1895
   412
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   413
            return (-1);
slouken@1895
   414
        }
slouken@1895
   415
    }
slouken@2257
   416
    return (src->map->blit(src, srcrect, dst, dstrect));
slouken@0
   417
}
slouken@0
   418
slouken@0
   419
slouken@1895
   420
int
slouken@1895
   421
SDL_UpperBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   422
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   423
{
slouken@1895
   424
    SDL_Rect fulldst;
slouken@1895
   425
    int srcx, srcy, w, h;
slouken@0
   426
slouken@1895
   427
    /* Make sure the surfaces aren't locked */
slouken@1895
   428
    if (!src || !dst) {
slouken@1895
   429
        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   430
        return (-1);
slouken@1895
   431
    }
slouken@1895
   432
    if (src->locked || dst->locked) {
slouken@1895
   433
        SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   434
        return (-1);
slouken@1895
   435
    }
slouken@0
   436
slouken@1895
   437
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   438
    if (dstrect == NULL) {
slouken@1895
   439
        fulldst.x = fulldst.y = 0;
slouken@1895
   440
        dstrect = &fulldst;
slouken@1895
   441
    }
slouken@0
   442
slouken@1895
   443
    /* clip the source rectangle to the source surface */
slouken@1895
   444
    if (srcrect) {
slouken@1895
   445
        int maxw, maxh;
slouken@0
   446
slouken@1895
   447
        srcx = srcrect->x;
slouken@1895
   448
        w = srcrect->w;
slouken@1895
   449
        if (srcx < 0) {
slouken@1895
   450
            w += srcx;
slouken@1895
   451
            dstrect->x -= srcx;
slouken@1895
   452
            srcx = 0;
slouken@1895
   453
        }
slouken@1895
   454
        maxw = src->w - srcx;
slouken@1895
   455
        if (maxw < w)
slouken@1895
   456
            w = maxw;
slouken@0
   457
slouken@1895
   458
        srcy = srcrect->y;
slouken@1895
   459
        h = srcrect->h;
slouken@1895
   460
        if (srcy < 0) {
slouken@1895
   461
            h += srcy;
slouken@1895
   462
            dstrect->y -= srcy;
slouken@1895
   463
            srcy = 0;
slouken@1895
   464
        }
slouken@1895
   465
        maxh = src->h - srcy;
slouken@1895
   466
        if (maxh < h)
slouken@1895
   467
            h = maxh;
slouken@0
   468
slouken@1895
   469
    } else {
slouken@1895
   470
        srcx = srcy = 0;
slouken@1895
   471
        w = src->w;
slouken@1895
   472
        h = src->h;
slouken@1895
   473
    }
slouken@0
   474
slouken@1895
   475
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   476
    {
slouken@1895
   477
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   478
        int dx, dy;
slouken@0
   479
slouken@1895
   480
        dx = clip->x - dstrect->x;
slouken@1895
   481
        if (dx > 0) {
slouken@1895
   482
            w -= dx;
slouken@1895
   483
            dstrect->x += dx;
slouken@1895
   484
            srcx += dx;
slouken@1895
   485
        }
slouken@1895
   486
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   487
        if (dx > 0)
slouken@1895
   488
            w -= dx;
slouken@1895
   489
slouken@1895
   490
        dy = clip->y - dstrect->y;
slouken@1895
   491
        if (dy > 0) {
slouken@1895
   492
            h -= dy;
slouken@1895
   493
            dstrect->y += dy;
slouken@1895
   494
            srcy += dy;
slouken@1895
   495
        }
slouken@1895
   496
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   497
        if (dy > 0)
slouken@1895
   498
            h -= dy;
slouken@1895
   499
    }
slouken@1895
   500
slouken@1895
   501
    if (w > 0 && h > 0) {
slouken@1895
   502
        SDL_Rect sr;
slouken@1895
   503
        sr.x = srcx;
slouken@1895
   504
        sr.y = srcy;
slouken@1895
   505
        sr.w = dstrect->w = w;
slouken@1895
   506
        sr.h = dstrect->h = h;
slouken@1895
   507
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   508
    }
slouken@1895
   509
    dstrect->w = dstrect->h = 0;
slouken@1895
   510
    return 0;
slouken@0
   511
}
slouken@0
   512
slouken@0
   513
/*
slouken@0
   514
 * Lock a surface to directly access the pixels
slouken@0
   515
 */
slouken@1895
   516
int
slouken@1895
   517
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   518
{
slouken@1895
   519
    if (!surface->locked) {
slouken@1895
   520
        /* Perform the lock */
slouken@1895
   521
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   522
            SDL_UnRLESurface(surface, 1);
slouken@1895
   523
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   524
        }
slouken@1895
   525
    }
slouken@0
   526
slouken@1895
   527
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   528
    ++surface->locked;
slouken@0
   529
slouken@1895
   530
    /* Ready to go.. */
slouken@1895
   531
    return (0);
slouken@0
   532
}
slouken@1895
   533
slouken@0
   534
/*
slouken@0
   535
 * Unlock a previously locked surface
slouken@0
   536
 */
slouken@1895
   537
void
slouken@1895
   538
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   539
{
slouken@1895
   540
    /* Only perform an unlock if we are locked */
slouken@1895
   541
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   542
        return;
slouken@1895
   543
    }
slouken@0
   544
slouken@1895
   545
    /* Update RLE encoded surface with new data */
slouken@1895
   546
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   547
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   548
        SDL_RLESurface(surface);
slouken@1895
   549
    }
slouken@0
   550
}
slouken@0
   551
slouken@0
   552
/* 
slouken@0
   553
 * Convert a surface into the specified pixel format.
slouken@0
   554
 */
slouken@1895
   555
SDL_Surface *
slouken@1895
   556
SDL_ConvertSurface(SDL_Surface * surface,
slouken@1895
   557
                   SDL_PixelFormat * format, Uint32 flags)
slouken@0
   558
{
slouken@1895
   559
    SDL_Surface *convert;
slouken@1895
   560
    Uint32 colorkey = 0;
slouken@1895
   561
    Uint8 alpha = 0;
slouken@1895
   562
    Uint32 surface_flags;
slouken@1895
   563
    SDL_Rect bounds;
slouken@0
   564
slouken@1895
   565
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   566
    if (format->palette != NULL) {
slouken@1895
   567
        int i;
slouken@1895
   568
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   569
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   570
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   571
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   572
                break;
slouken@1895
   573
        }
slouken@1895
   574
        if (i == format->palette->ncolors) {
slouken@1895
   575
            SDL_SetError("Empty destination palette");
slouken@1895
   576
            return (NULL);
slouken@1895
   577
        }
slouken@1895
   578
    }
slouken@0
   579
slouken@1895
   580
    /* Create a new surface with the desired format */
slouken@1895
   581
    convert = SDL_CreateRGBSurface(flags,
slouken@1895
   582
                                   surface->w, surface->h,
slouken@1895
   583
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   584
                                   format->Gmask, format->Bmask,
slouken@1895
   585
                                   format->Amask);
slouken@1895
   586
    if (convert == NULL) {
slouken@1895
   587
        return (NULL);
slouken@1895
   588
    }
slouken@264
   589
slouken@1895
   590
    /* Copy the palette if any */
slouken@1895
   591
    if (format->palette && convert->format->palette) {
slouken@1895
   592
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   593
                   format->palette->colors,
slouken@1895
   594
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   595
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   596
    }
slouken@0
   597
slouken@1895
   598
    /* Save the original surface color key and alpha */
slouken@1895
   599
    surface_flags = surface->flags;
slouken@1895
   600
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   601
        /* Convert colourkeyed surfaces to RGBA if requested */
slouken@1895
   602
        if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
slouken@1895
   603
            surface_flags &= ~SDL_SRCCOLORKEY;
slouken@1895
   604
        } else {
slouken@2257
   605
            colorkey = surface->map->ckey;
slouken@1895
   606
            SDL_SetColorKey(surface, 0, 0);
slouken@1895
   607
        }
slouken@1895
   608
    }
slouken@1895
   609
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   610
        /* Copy over the alpha channel to RGBA if requested */
slouken@1895
   611
        if (format->Amask) {
slouken@1895
   612
            surface->flags &= ~SDL_SRCALPHA;
slouken@1895
   613
        } else {
slouken@2257
   614
            alpha = (Uint8)(surface->map->cmod >> 24);
slouken@1895
   615
            SDL_SetAlpha(surface, 0, 0);
slouken@1895
   616
        }
slouken@1895
   617
    }
slouken@0
   618
slouken@1895
   619
    /* Copy over the image data */
slouken@1895
   620
    bounds.x = 0;
slouken@1895
   621
    bounds.y = 0;
slouken@1895
   622
    bounds.w = surface->w;
slouken@1895
   623
    bounds.h = surface->h;
slouken@1895
   624
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   625
slouken@1895
   626
    /* Clean up the original surface, and update converted surface */
slouken@1895
   627
    if (convert != NULL) {
slouken@1895
   628
        SDL_SetClipRect(convert, &surface->clip_rect);
slouken@1895
   629
    }
slouken@1895
   630
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
   631
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1895
   632
        if (convert != NULL) {
slouken@1895
   633
            Uint8 keyR, keyG, keyB;
slouken@0
   634
slouken@1895
   635
            SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
slouken@1895
   636
            SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
slouken@1895
   637
                            SDL_MapRGB(convert->format, keyR, keyG, keyB));
slouken@1895
   638
        }
slouken@1895
   639
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1895
   640
    }
slouken@1895
   641
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1895
   642
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1895
   643
        if (convert != NULL) {
slouken@1895
   644
            SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
slouken@1895
   645
        }
slouken@1895
   646
        if (format->Amask) {
slouken@1895
   647
            surface->flags |= SDL_SRCALPHA;
slouken@1895
   648
        } else {
slouken@1895
   649
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1895
   650
        }
slouken@1895
   651
    }
slouken@0
   652
slouken@1895
   653
    /* We're ready to go! */
slouken@1895
   654
    return (convert);
slouken@0
   655
}
slouken@0
   656
slouken@0
   657
/*
slouken@0
   658
 * Free a surface created by the above function.
slouken@0
   659
 */
slouken@1895
   660
void
slouken@1895
   661
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   662
{
slouken@1895
   663
    if (surface == NULL) {
slouken@1895
   664
        return;
slouken@1895
   665
    }
slouken@1895
   666
    if (--surface->refcount > 0) {
slouken@1895
   667
        return;
slouken@1895
   668
    }
slouken@1895
   669
    while (surface->locked > 0) {
slouken@1895
   670
        SDL_UnlockSurface(surface);
slouken@1895
   671
    }
slouken@1895
   672
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   673
        SDL_UnRLESurface(surface, 0);
slouken@1895
   674
    }
slouken@1895
   675
    if (surface->format) {
slouken@1895
   676
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
   677
        SDL_FreeFormat(surface->format);
slouken@1895
   678
        surface->format = NULL;
slouken@1895
   679
    }
slouken@1895
   680
    if (surface->map != NULL) {
slouken@1895
   681
        SDL_FreeBlitMap(surface->map);
slouken@1895
   682
        surface->map = NULL;
slouken@1895
   683
    }
slouken@1895
   684
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
   685
        SDL_free(surface->pixels);
slouken@1895
   686
    }
slouken@1895
   687
    SDL_free(surface);
slouken@0
   688
#ifdef CHECK_LEAKS
slouken@1895
   689
    --surfaces_allocated;
slouken@0
   690
#endif
slouken@0
   691
}
slouken@1895
   692
slouken@1895
   693
/* vi: set ts=4 sw=4 expandtab: */