src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 25 Nov 2008 02:21:53 +0000
changeset 2786 6bacfecbf27e
parent 2785 fa1095d42a5b
child 2787 93764fe8601a
permissions -rw-r--r--
indent
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
    surface->w = width;
slouken@1895
    57
    surface->h = height;
slouken@1895
    58
    surface->pitch = SDL_CalculatePitch(surface);
slouken@1895
    59
    SDL_SetClipRect(surface, NULL);
slouken@0
    60
slouken@1895
    61
    if (surface->format->BitsPerPixel <= 8) {
slouken@1895
    62
        SDL_Palette *palette =
slouken@1895
    63
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
slouken@1895
    64
        if (!palette) {
slouken@1895
    65
            SDL_FreeSurface(surface);
slouken@1895
    66
            return NULL;
slouken@1895
    67
        }
slouken@1895
    68
        if (Rmask || Bmask || Gmask) {
slouken@1895
    69
            const SDL_PixelFormat *format = surface->format;
slouken@0
    70
slouken@1895
    71
            /* create palette according to masks */
slouken@1895
    72
            int i;
slouken@1895
    73
            int Rm = 0, Gm = 0, Bm = 0;
slouken@1895
    74
            int Rw = 0, Gw = 0, Bw = 0;
slouken@0
    75
slouken@1895
    76
            if (Rmask) {
slouken@1895
    77
                Rw = 8 - format->Rloss;
slouken@1895
    78
                for (i = format->Rloss; i > 0; i -= Rw)
slouken@1895
    79
                    Rm |= 1 << i;
slouken@1895
    80
            }
slouken@1895
    81
            if (Gmask) {
slouken@1895
    82
                Gw = 8 - format->Gloss;
slouken@1895
    83
                for (i = format->Gloss; i > 0; i -= Gw)
slouken@1895
    84
                    Gm |= 1 << i;
slouken@1895
    85
            }
slouken@1895
    86
            if (Bmask) {
slouken@1895
    87
                Bw = 8 - format->Bloss;
slouken@1895
    88
                for (i = format->Bloss; i > 0; i -= Bw)
slouken@1895
    89
                    Bm |= 1 << i;
slouken@1895
    90
            }
slouken@1895
    91
            for (i = 0; i < palette->ncolors; ++i) {
slouken@1895
    92
                int r, g, b;
slouken@1895
    93
                r = (i & Rmask) >> format->Rshift;
slouken@1895
    94
                r = (r << format->Rloss) | ((r * Rm) >> Rw);
slouken@1895
    95
                palette->colors[i].r = r;
slouken@0
    96
slouken@1895
    97
                g = (i & Gmask) >> format->Gshift;
slouken@1895
    98
                g = (g << format->Gloss) | ((g * Gm) >> Gw);
slouken@1895
    99
                palette->colors[i].g = g;
slouken@1895
   100
slouken@1895
   101
                b = (i & Bmask) >> format->Bshift;
slouken@1895
   102
                b = (b << format->Bloss) | ((b * Bm) >> Bw);
slouken@1895
   103
                palette->colors[i].b = b;
slouken@1895
   104
            }
slouken@1895
   105
        } else if (palette->ncolors == 2) {
slouken@1895
   106
            /* Create a black and white bitmap palette */
slouken@1895
   107
            palette->colors[0].r = 0xFF;
slouken@1895
   108
            palette->colors[0].g = 0xFF;
slouken@1895
   109
            palette->colors[0].b = 0xFF;
slouken@1895
   110
            palette->colors[1].r = 0x00;
slouken@1895
   111
            palette->colors[1].g = 0x00;
slouken@1895
   112
            palette->colors[1].b = 0x00;
slouken@1895
   113
        }
slouken@1895
   114
        SDL_SetSurfacePalette(surface, palette);
slouken@1895
   115
        SDL_FreePalette(palette);
slouken@1895
   116
    }
slouken@1895
   117
slouken@1895
   118
    /* Get the pixels */
slouken@1895
   119
    if (surface->w && surface->h) {
slouken@1895
   120
        surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1895
   121
        if (!surface->pixels) {
slouken@1895
   122
            SDL_FreeSurface(surface);
slouken@1895
   123
            SDL_OutOfMemory();
slouken@1895
   124
            return NULL;
slouken@1895
   125
        }
slouken@1895
   126
        /* This is important for bitmaps */
slouken@1895
   127
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@1895
   128
    }
slouken@1895
   129
slouken@1895
   130
    /* Allocate an empty mapping */
slouken@1895
   131
    surface->map = SDL_AllocBlitMap();
slouken@1895
   132
    if (!surface->map) {
slouken@1895
   133
        SDL_FreeSurface(surface);
slouken@1895
   134
        return NULL;
slouken@1895
   135
    }
slouken@1895
   136
    SDL_FormatChanged(surface);
slouken@1895
   137
slouken@2267
   138
    /* By default surface with an alpha mask are set up for blending */
slouken@2267
   139
    if (Amask) {
slouken@2267
   140
        SDL_SetSurfaceBlendMode(surface, SDL_TEXTUREBLENDMODE_BLEND);
slouken@2267
   141
    }
slouken@2267
   142
slouken@1895
   143
    /* The surface is ready to go */
slouken@1895
   144
    surface->refcount = 1;
slouken@0
   145
#ifdef CHECK_LEAKS
slouken@1895
   146
    ++surfaces_allocated;
slouken@0
   147
#endif
slouken@1895
   148
    return surface;
slouken@0
   149
}
slouken@1895
   150
slouken@0
   151
/*
slouken@0
   152
 * Create an RGB surface from an existing memory buffer
slouken@0
   153
 */
slouken@1895
   154
SDL_Surface *
slouken@1895
   155
SDL_CreateRGBSurfaceFrom(void *pixels,
slouken@1895
   156
                         int width, int height, int depth, int pitch,
slouken@1895
   157
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1895
   158
                         Uint32 Amask)
slouken@0
   159
{
slouken@1895
   160
    SDL_Surface *surface;
slouken@0
   161
slouken@1895
   162
    surface =
slouken@1895
   163
        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
slouken@1895
   164
    if (surface != NULL) {
slouken@1895
   165
        surface->flags |= SDL_PREALLOC;
slouken@1895
   166
        surface->pixels = pixels;
slouken@1895
   167
        surface->w = width;
slouken@1895
   168
        surface->h = height;
slouken@1895
   169
        surface->pitch = pitch;
slouken@1895
   170
        SDL_SetClipRect(surface, NULL);
slouken@1895
   171
    }
slouken@1895
   172
    return surface;
slouken@0
   173
}
slouken@1895
   174
slouken@1895
   175
static int
slouken@1895
   176
SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
slouken@1895
   177
{
slouken@1895
   178
    SDL_Surface *surface = (SDL_Surface *) userdata;
slouken@1895
   179
slouken@1895
   180
    SDL_FormatChanged(surface);
slouken@1895
   181
slouken@1895
   182
    return 0;
slouken@1895
   183
}
slouken@1895
   184
slouken@1895
   185
int
slouken@1895
   186
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
slouken@1895
   187
{
slouken@1895
   188
    if (!surface || !surface->format) {
slouken@1895
   189
        SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
slouken@1895
   190
        return -1;
slouken@1895
   191
    }
slouken@1895
   192
slouken@1895
   193
    if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
slouken@1895
   194
        SDL_SetError
slouken@1895
   195
            ("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
slouken@1895
   196
        return -1;
slouken@1895
   197
    }
slouken@1895
   198
slouken@1895
   199
    if (surface->format->palette == palette) {
slouken@1895
   200
        return 0;
slouken@1895
   201
    }
slouken@1895
   202
slouken@1895
   203
    if (surface->format->palette) {
slouken@1895
   204
        SDL_DelPaletteWatch(surface->format->palette,
slouken@1895
   205
                            SDL_SurfacePaletteChanged, surface);
slouken@1895
   206
    }
slouken@1895
   207
slouken@1895
   208
    surface->format->palette = palette;
slouken@1895
   209
slouken@1895
   210
    if (surface->format->palette) {
slouken@1895
   211
        SDL_AddPaletteWatch(surface->format->palette,
slouken@1895
   212
                            SDL_SurfacePaletteChanged, surface);
slouken@1895
   213
    }
slouken@1895
   214
    return 0;
slouken@1895
   215
}
slouken@1895
   216
slouken@2267
   217
int
slouken@2267
   218
SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
slouken@0
   219
{
slouken@2267
   220
    int flags;
slouken@0
   221
slouken@2266
   222
    if (!surface) {
slouken@2266
   223
        return -1;
slouken@1895
   224
    }
slouken@0
   225
slouken@2267
   226
    flags = surface->map->info.flags;
slouken@1895
   227
    if (flag) {
slouken@2267
   228
        surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
slouken@1895
   229
    } else {
slouken@2267
   230
        surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
slouken@1895
   231
    }
slouken@2267
   232
    if (surface->map->info.flags != flags) {
slouken@1895
   233
        SDL_InvalidateMap(surface->map);
slouken@1895
   234
    }
slouken@1895
   235
    return 0;
slouken@431
   236
}
slouken@0
   237
slouken@2267
   238
int
slouken@2267
   239
SDL_SetColorKey(SDL_Surface * surface, Uint32 flag, Uint32 key)
slouken@2266
   240
{
slouken@2266
   241
    int flags;
slouken@2266
   242
slouken@2266
   243
    if (!surface) {
slouken@2266
   244
        return -1;
slouken@2266
   245
    }
slouken@2266
   246
slouken@2266
   247
    if (flag & SDL_RLEACCEL) {
slouken@2266
   248
        SDL_SetSurfaceRLE(surface, 1);
slouken@2266
   249
    }
slouken@2266
   250
slouken@2266
   251
    flags = surface->map->info.flags;
slouken@2266
   252
    if (flag) {
slouken@2266
   253
        surface->map->info.flags |= SDL_COPY_COLORKEY;
slouken@2266
   254
        surface->map->info.colorkey = key;
slouken@2266
   255
    } else {
slouken@2266
   256
        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
slouken@2266
   257
    }
slouken@2266
   258
    if (surface->map->info.flags != flags) {
slouken@2266
   259
        SDL_InvalidateMap(surface->map);
slouken@2266
   260
    }
slouken@2267
   261
slouken@2267
   262
    /* Compatibility mode */
slouken@2267
   263
    if (surface->map->info.flags & SDL_COPY_COLORKEY) {
slouken@2267
   264
        surface->flags |= SDL_SRCCOLORKEY;
slouken@2267
   265
    } else {
slouken@2267
   266
        surface->flags &= ~SDL_SRCCOLORKEY;
slouken@2267
   267
    }
slouken@2267
   268
slouken@2266
   269
    return 0;
slouken@2266
   270
}
slouken@2266
   271
slouken@2785
   272
/* This is a fairly slow function to switch from colorkey to alpha */
slouken@2785
   273
void
slouken@2786
   274
SDL_ConvertColorkeyToAlpha(SDL_Surface * surface)
slouken@2785
   275
{
slouken@2786
   276
    int x, y;
slouken@2785
   277
slouken@2786
   278
    if (!surface) {
slouken@2786
   279
        return;
slouken@2786
   280
    }
slouken@2785
   281
slouken@2786
   282
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
slouken@2786
   283
        !surface->format->Amask) {
slouken@2786
   284
        return;
slouken@2786
   285
    }
slouken@2785
   286
slouken@2786
   287
    SDL_LockSurface(surface);
slouken@2785
   288
slouken@2786
   289
    switch (surface->format->BytesPerPixel) {
slouken@2786
   290
    case 2:
slouken@2786
   291
        {
slouken@2786
   292
            Uint16 *row, *spot;
slouken@2786
   293
            Uint16 ckey = (Uint16) surface->map->info.colorkey;
slouken@2786
   294
            Uint16 mask = (Uint16) (~surface->format->Amask);
slouken@2785
   295
slouken@2786
   296
            row = (Uint16 *) surface->pixels;
slouken@2786
   297
            for (y = surface->h; y--;) {
slouken@2786
   298
                spot = row;
slouken@2786
   299
                for (x = surface->w; x--;) {
slouken@2786
   300
                    if (*spot == ckey) {
slouken@2786
   301
                        *spot &= mask;
slouken@2786
   302
                    }
slouken@2786
   303
                    ++spot;
slouken@2786
   304
                }
slouken@2786
   305
                row += surface->pitch / 2;
slouken@2786
   306
            }
slouken@2786
   307
        }
slouken@2786
   308
        break;
slouken@2786
   309
    case 3:
slouken@2786
   310
        /* FIXME */
slouken@2786
   311
        break;
slouken@2786
   312
    case 4:
slouken@2786
   313
        {
slouken@2786
   314
            Uint32 *row, *spot;
slouken@2786
   315
            Uint32 ckey = surface->map->info.colorkey;
slouken@2786
   316
            Uint32 mask = ~surface->format->Amask;
slouken@2785
   317
slouken@2786
   318
            row = (Uint32 *) surface->pixels;
slouken@2786
   319
            for (y = surface->h; y--;) {
slouken@2786
   320
                spot = row;
slouken@2786
   321
                for (x = surface->w; x--;) {
slouken@2786
   322
                    if (*spot == ckey) {
slouken@2786
   323
                        *spot &= mask;
slouken@2786
   324
                    }
slouken@2786
   325
                    ++spot;
slouken@2786
   326
                }
slouken@2786
   327
                row += surface->pitch / 4;
slouken@2786
   328
            }
slouken@2786
   329
        }
slouken@2786
   330
        break;
slouken@2786
   331
    }
slouken@2785
   332
slouken@2786
   333
    SDL_UnlockSurface(surface);
slouken@2785
   334
slouken@2786
   335
    SDL_SetColorKey(surface, 0, 0);
slouken@2785
   336
}
slouken@2785
   337
slouken@2267
   338
int
slouken@2267
   339
SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
slouken@2266
   340
{
slouken@2266
   341
    int flags;
slouken@2266
   342
slouken@2266
   343
    if (!surface) {
slouken@2266
   344
        return -1;
slouken@2266
   345
    }
slouken@2266
   346
slouken@2266
   347
    surface->map->info.r = r;
slouken@2266
   348
    surface->map->info.g = g;
slouken@2266
   349
    surface->map->info.b = b;
slouken@2266
   350
slouken@2266
   351
    flags = surface->map->info.flags;
slouken@2266
   352
    if (r != 0xFF || g != 0xFF || b != 0xFF) {
slouken@2266
   353
        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
slouken@2266
   354
    } else {
slouken@2266
   355
        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
slouken@2266
   356
    }
slouken@2266
   357
    if (surface->map->info.flags != flags) {
slouken@2266
   358
        SDL_InvalidateMap(surface->map);
slouken@2266
   359
    }
slouken@2266
   360
    return 0;
slouken@2266
   361
}
slouken@2266
   362
slouken@2266
   363
slouken@2267
   364
int
slouken@2267
   365
SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
slouken@2266
   366
{
slouken@2266
   367
    if (!surface) {
slouken@2266
   368
        return -1;
slouken@2266
   369
    }
slouken@2266
   370
slouken@2266
   371
    if (r) {
slouken@2266
   372
        *r = surface->map->info.r;
slouken@2266
   373
    }
slouken@2266
   374
    if (g) {
slouken@2266
   375
        *g = surface->map->info.g;
slouken@2266
   376
    }
slouken@2266
   377
    if (b) {
slouken@2266
   378
        *b = surface->map->info.b;
slouken@2266
   379
    }
slouken@2266
   380
    return 0;
slouken@2266
   381
}
slouken@2266
   382
slouken@2267
   383
int
slouken@2267
   384
SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
slouken@2266
   385
{
slouken@2266
   386
    int flags;
slouken@2266
   387
slouken@2266
   388
    if (!surface) {
slouken@2266
   389
        return -1;
slouken@2266
   390
    }
slouken@2266
   391
slouken@2266
   392
    surface->map->info.a = alpha;
slouken@2266
   393
slouken@2266
   394
    flags = surface->map->info.flags;
slouken@2266
   395
    if (alpha != 0xFF) {
slouken@2266
   396
        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
slouken@2266
   397
    } else {
slouken@2266
   398
        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
slouken@2266
   399
    }
slouken@2266
   400
    if (surface->map->info.flags != flags) {
slouken@2266
   401
        SDL_InvalidateMap(surface->map);
slouken@2266
   402
    }
slouken@2266
   403
    return 0;
slouken@2266
   404
}
slouken@2266
   405
slouken@2267
   406
int
slouken@2267
   407
SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
slouken@2266
   408
{
slouken@2266
   409
    if (!surface) {
slouken@2266
   410
        return -1;
slouken@2266
   411
    }
slouken@2266
   412
slouken@2266
   413
    if (alpha) {
slouken@2266
   414
        *alpha = surface->map->info.a;
slouken@2266
   415
    }
slouken@2266
   416
    return 0;
slouken@2266
   417
}
slouken@2266
   418
slouken@2267
   419
int
slouken@2267
   420
SDL_SetSurfaceBlendMode(SDL_Surface * surface, int blendMode)
slouken@2266
   421
{
slouken@2266
   422
    int flags, status;
slouken@2266
   423
slouken@2266
   424
    if (!surface) {
slouken@2266
   425
        return -1;
slouken@2266
   426
    }
slouken@2266
   427
slouken@2266
   428
    status = 0;
slouken@2266
   429
    flags = surface->map->info.flags;
slouken@2267
   430
    surface->map->info.flags &=
slouken@2267
   431
        ~(SDL_COPY_MASK | SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
slouken@2266
   432
    switch (blendMode) {
slouken@2266
   433
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@2266
   434
        break;
slouken@2266
   435
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@2266
   436
        surface->map->info.flags |= SDL_COPY_MASK;
slouken@2266
   437
        break;
slouken@2266
   438
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@2266
   439
        surface->map->info.flags |= SDL_COPY_BLEND;
slouken@2266
   440
        break;
slouken@2266
   441
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@2266
   442
        surface->map->info.flags |= SDL_COPY_ADD;
slouken@2266
   443
        break;
slouken@2266
   444
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@2266
   445
        surface->map->info.flags |= SDL_COPY_MOD;
slouken@2266
   446
        break;
slouken@2266
   447
    default:
slouken@2266
   448
        SDL_Unsupported();
slouken@2266
   449
        status = -1;
slouken@2266
   450
        break;
slouken@2266
   451
    }
slouken@2266
   452
slouken@2266
   453
    if (surface->map->info.flags != flags) {
slouken@2266
   454
        SDL_InvalidateMap(surface->map);
slouken@2266
   455
    }
slouken@2267
   456
slouken@2267
   457
    /* Compatibility mode */
slouken@2267
   458
    if (surface->map->info.flags & SDL_COPY_BLEND) {
slouken@2267
   459
        surface->flags |= SDL_SRCALPHA;
slouken@2267
   460
    } else {
slouken@2267
   461
        surface->flags &= ~SDL_SRCALPHA;
slouken@2267
   462
    }
slouken@2267
   463
slouken@2266
   464
    return status;
slouken@2266
   465
}
slouken@2266
   466
slouken@2267
   467
int
slouken@2267
   468
SDL_GetSurfaceBlendMode(SDL_Surface * surface, int *blendMode)
slouken@2266
   469
{
slouken@2266
   470
    if (!surface) {
slouken@2266
   471
        return -1;
slouken@2266
   472
    }
slouken@2266
   473
slouken@2266
   474
    if (!blendMode) {
slouken@2266
   475
        return 0;
slouken@2266
   476
    }
slouken@2266
   477
slouken@2735
   478
    switch (surface->map->
slouken@2735
   479
            info.flags & (SDL_COPY_MASK | SDL_COPY_BLEND | SDL_COPY_ADD |
slouken@2735
   480
                          SDL_COPY_MOD)) {
slouken@2266
   481
    case SDL_COPY_MASK:
slouken@2267
   482
        *blendMode = SDL_TEXTUREBLENDMODE_MASK;
slouken@2266
   483
        break;
slouken@2266
   484
    case SDL_COPY_BLEND:
slouken@2267
   485
        *blendMode = SDL_TEXTUREBLENDMODE_BLEND;
slouken@2266
   486
        break;
slouken@2266
   487
    case SDL_COPY_ADD:
slouken@2267
   488
        *blendMode = SDL_TEXTUREBLENDMODE_ADD;
slouken@2266
   489
        break;
slouken@2266
   490
    case SDL_COPY_MOD:
slouken@2267
   491
        *blendMode = SDL_TEXTUREBLENDMODE_MOD;
slouken@2266
   492
        break;
slouken@2266
   493
    default:
slouken@2267
   494
        *blendMode = SDL_TEXTUREBLENDMODE_NONE;
slouken@2266
   495
        break;
slouken@2266
   496
    }
slouken@2266
   497
    return 0;
slouken@2266
   498
}
slouken@2266
   499
slouken@2267
   500
int
slouken@2267
   501
SDL_SetSurfaceScaleMode(SDL_Surface * surface, int scaleMode)
slouken@2266
   502
{
slouken@2266
   503
    int flags, status;
slouken@2266
   504
slouken@2266
   505
    if (!surface) {
slouken@2266
   506
        return -1;
slouken@2266
   507
    }
slouken@2266
   508
slouken@2266
   509
    status = 0;
slouken@2266
   510
    flags = surface->map->info.flags;
slouken@2266
   511
    surface->map->info.flags &= ~(SDL_COPY_NEAREST);
slouken@2266
   512
    switch (scaleMode) {
slouken@2266
   513
    case SDL_TEXTURESCALEMODE_NONE:
slouken@2266
   514
        break;
slouken@2266
   515
    case SDL_TEXTURESCALEMODE_FAST:
slouken@2266
   516
        surface->map->info.flags |= SDL_COPY_NEAREST;
slouken@2266
   517
        break;
slouken@2266
   518
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@2266
   519
    case SDL_TEXTURESCALEMODE_BEST:
slouken@2266
   520
        SDL_Unsupported();
slouken@2266
   521
        surface->map->info.flags |= SDL_COPY_NEAREST;
slouken@2266
   522
        status = -1;
slouken@2266
   523
        break;
slouken@2266
   524
    default:
slouken@2266
   525
        SDL_Unsupported();
slouken@2266
   526
        status = -1;
slouken@2266
   527
        break;
slouken@2266
   528
    }
slouken@2266
   529
slouken@2266
   530
    if (surface->map->info.flags != flags) {
slouken@2266
   531
        SDL_InvalidateMap(surface->map);
slouken@2266
   532
    }
slouken@2266
   533
    return status;
slouken@2266
   534
}
slouken@2266
   535
slouken@2267
   536
int
slouken@2267
   537
SDL_GetSurfaceScaleMode(SDL_Surface * surface, int *scaleMode)
slouken@2266
   538
{
slouken@2266
   539
    if (!surface) {
slouken@2266
   540
        return -1;
slouken@2266
   541
    }
slouken@2266
   542
slouken@2266
   543
    if (!scaleMode) {
slouken@2266
   544
        return 0;
slouken@2266
   545
    }
slouken@2266
   546
slouken@2267
   547
    switch (surface->map->info.flags & (SDL_COPY_NEAREST)) {
slouken@2267
   548
    case SDL_COPY_NEAREST:
slouken@2267
   549
        *scaleMode = SDL_TEXTURESCALEMODE_FAST;
slouken@2266
   550
        break;
slouken@2266
   551
    default:
slouken@2267
   552
        *scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@2266
   553
        break;
slouken@2266
   554
    }
slouken@2266
   555
    return 0;
slouken@2266
   556
}
slouken@2266
   557
slouken@1895
   558
SDL_bool
slouken@1895
   559
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   560
{
slouken@1895
   561
    SDL_Rect full_rect;
slouken@0
   562
slouken@1895
   563
    /* Don't do anything if there's no surface to act on */
slouken@1895
   564
    if (!surface) {
slouken@1895
   565
        return SDL_FALSE;
slouken@1895
   566
    }
slouken@0
   567
slouken@1895
   568
    /* Set up the full surface rectangle */
slouken@1895
   569
    full_rect.x = 0;
slouken@1895
   570
    full_rect.y = 0;
slouken@1895
   571
    full_rect.w = surface->w;
slouken@1895
   572
    full_rect.h = surface->h;
slouken@0
   573
slouken@1895
   574
    /* Set the clipping rectangle */
slouken@1895
   575
    if (!rect) {
slouken@1895
   576
        surface->clip_rect = full_rect;
slouken@1895
   577
        return 1;
slouken@1895
   578
    }
slouken@1895
   579
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   580
}
slouken@1895
   581
slouken@1895
   582
void
slouken@1895
   583
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   584
{
slouken@1895
   585
    if (surface && rect) {
slouken@1895
   586
        *rect = surface->clip_rect;
slouken@1895
   587
    }
slouken@0
   588
}
slouken@1895
   589
slouken@0
   590
/* 
slouken@0
   591
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   592
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   593
 * verification.  The lower part is a pointer to a low level
slouken@0
   594
 * accelerated blitting function.
slouken@0
   595
 *
slouken@0
   596
 * These parts are separated out and each used internally by this 
slouken@0
   597
 * library in the optimimum places.  They are exported so that if
slouken@0
   598
 * you know exactly what you are doing, you can optimize your code
slouken@0
   599
 * by calling the one(s) you need.
slouken@0
   600
 */
slouken@1895
   601
int
slouken@1895
   602
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   603
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   604
{
slouken@1895
   605
    /* Check to make sure the blit mapping is valid */
slouken@1895
   606
    if ((src->map->dst != dst) ||
slouken@1895
   607
        (src->map->dst->format_version != src->map->format_version)) {
slouken@1895
   608
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   609
            return (-1);
slouken@1895
   610
        }
bob@2328
   611
        /* just here for debugging */
bob@2329
   612
/*         printf */
bob@2329
   613
/*             ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
bob@2329
   614
/*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
bob@2329
   615
/*              dst->map->info.flags, src->map->blit); */
slouken@1895
   616
    }
slouken@2257
   617
    return (src->map->blit(src, srcrect, dst, dstrect));
slouken@0
   618
}
slouken@0
   619
slouken@0
   620
slouken@1895
   621
int
slouken@1895
   622
SDL_UpperBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   623
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   624
{
slouken@1895
   625
    SDL_Rect fulldst;
slouken@1895
   626
    int srcx, srcy, w, h;
slouken@0
   627
slouken@1895
   628
    /* Make sure the surfaces aren't locked */
slouken@1895
   629
    if (!src || !dst) {
slouken@1895
   630
        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   631
        return (-1);
slouken@1895
   632
    }
slouken@1895
   633
    if (src->locked || dst->locked) {
slouken@1895
   634
        SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   635
        return (-1);
slouken@1895
   636
    }
slouken@0
   637
slouken@1895
   638
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   639
    if (dstrect == NULL) {
slouken@1895
   640
        fulldst.x = fulldst.y = 0;
slouken@1895
   641
        dstrect = &fulldst;
slouken@1895
   642
    }
slouken@0
   643
slouken@1895
   644
    /* clip the source rectangle to the source surface */
slouken@1895
   645
    if (srcrect) {
slouken@1895
   646
        int maxw, maxh;
slouken@0
   647
slouken@1895
   648
        srcx = srcrect->x;
slouken@1895
   649
        w = srcrect->w;
slouken@1895
   650
        if (srcx < 0) {
slouken@1895
   651
            w += srcx;
slouken@1895
   652
            dstrect->x -= srcx;
slouken@1895
   653
            srcx = 0;
slouken@1895
   654
        }
slouken@1895
   655
        maxw = src->w - srcx;
slouken@1895
   656
        if (maxw < w)
slouken@1895
   657
            w = maxw;
slouken@0
   658
slouken@1895
   659
        srcy = srcrect->y;
slouken@1895
   660
        h = srcrect->h;
slouken@1895
   661
        if (srcy < 0) {
slouken@1895
   662
            h += srcy;
slouken@1895
   663
            dstrect->y -= srcy;
slouken@1895
   664
            srcy = 0;
slouken@1895
   665
        }
slouken@1895
   666
        maxh = src->h - srcy;
slouken@1895
   667
        if (maxh < h)
slouken@1895
   668
            h = maxh;
slouken@0
   669
slouken@1895
   670
    } else {
slouken@1895
   671
        srcx = srcy = 0;
slouken@1895
   672
        w = src->w;
slouken@1895
   673
        h = src->h;
slouken@1895
   674
    }
slouken@0
   675
slouken@1895
   676
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   677
    {
slouken@1895
   678
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   679
        int dx, dy;
slouken@0
   680
slouken@1895
   681
        dx = clip->x - dstrect->x;
slouken@1895
   682
        if (dx > 0) {
slouken@1895
   683
            w -= dx;
slouken@1895
   684
            dstrect->x += dx;
slouken@1895
   685
            srcx += dx;
slouken@1895
   686
        }
slouken@1895
   687
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   688
        if (dx > 0)
slouken@1895
   689
            w -= dx;
slouken@1895
   690
slouken@1895
   691
        dy = clip->y - dstrect->y;
slouken@1895
   692
        if (dy > 0) {
slouken@1895
   693
            h -= dy;
slouken@1895
   694
            dstrect->y += dy;
slouken@1895
   695
            srcy += dy;
slouken@1895
   696
        }
slouken@1895
   697
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   698
        if (dy > 0)
slouken@1895
   699
            h -= dy;
slouken@1895
   700
    }
slouken@1895
   701
slouken@1895
   702
    if (w > 0 && h > 0) {
slouken@1895
   703
        SDL_Rect sr;
slouken@1895
   704
        sr.x = srcx;
slouken@1895
   705
        sr.y = srcy;
slouken@1895
   706
        sr.w = dstrect->w = w;
slouken@1895
   707
        sr.h = dstrect->h = h;
slouken@1895
   708
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   709
    }
slouken@1895
   710
    dstrect->w = dstrect->h = 0;
slouken@1895
   711
    return 0;
slouken@0
   712
}
slouken@0
   713
slouken@0
   714
/*
slouken@0
   715
 * Lock a surface to directly access the pixels
slouken@0
   716
 */
slouken@1895
   717
int
slouken@1895
   718
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   719
{
slouken@1895
   720
    if (!surface->locked) {
slouken@1895
   721
        /* Perform the lock */
slouken@1895
   722
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   723
            SDL_UnRLESurface(surface, 1);
slouken@1895
   724
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   725
        }
slouken@1895
   726
    }
slouken@0
   727
slouken@1895
   728
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   729
    ++surface->locked;
slouken@0
   730
slouken@1895
   731
    /* Ready to go.. */
slouken@1895
   732
    return (0);
slouken@0
   733
}
slouken@1895
   734
slouken@0
   735
/*
slouken@0
   736
 * Unlock a previously locked surface
slouken@0
   737
 */
slouken@1895
   738
void
slouken@1895
   739
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   740
{
slouken@1895
   741
    /* Only perform an unlock if we are locked */
slouken@1895
   742
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   743
        return;
slouken@1895
   744
    }
slouken@0
   745
slouken@1895
   746
    /* Update RLE encoded surface with new data */
slouken@1895
   747
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   748
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   749
        SDL_RLESurface(surface);
slouken@1895
   750
    }
slouken@0
   751
}
slouken@0
   752
slouken@0
   753
/* 
slouken@0
   754
 * Convert a surface into the specified pixel format.
slouken@0
   755
 */
slouken@1895
   756
SDL_Surface *
slouken@1895
   757
SDL_ConvertSurface(SDL_Surface * surface,
slouken@1895
   758
                   SDL_PixelFormat * format, Uint32 flags)
slouken@0
   759
{
slouken@1895
   760
    SDL_Surface *convert;
slouken@2266
   761
    Uint32 copy_flags;
slouken@1895
   762
    SDL_Rect bounds;
slouken@0
   763
slouken@1895
   764
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   765
    if (format->palette != NULL) {
slouken@1895
   766
        int i;
slouken@1895
   767
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   768
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   769
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   770
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   771
                break;
slouken@1895
   772
        }
slouken@1895
   773
        if (i == format->palette->ncolors) {
slouken@1895
   774
            SDL_SetError("Empty destination palette");
slouken@1895
   775
            return (NULL);
slouken@1895
   776
        }
slouken@1895
   777
    }
slouken@0
   778
slouken@1895
   779
    /* Create a new surface with the desired format */
slouken@2266
   780
    convert = SDL_CreateRGBSurface(0, surface->w, surface->h,
slouken@1895
   781
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   782
                                   format->Gmask, format->Bmask,
slouken@1895
   783
                                   format->Amask);
slouken@1895
   784
    if (convert == NULL) {
slouken@1895
   785
        return (NULL);
slouken@1895
   786
    }
slouken@264
   787
slouken@1895
   788
    /* Copy the palette if any */
slouken@1895
   789
    if (format->palette && convert->format->palette) {
slouken@1895
   790
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   791
                   format->palette->colors,
slouken@1895
   792
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   793
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   794
    }
slouken@0
   795
slouken@2266
   796
    /* Save the original copy flags */
slouken@2266
   797
    copy_flags = surface->map->info.flags;
slouken@2266
   798
    surface->map->info.flags = 0;
slouken@0
   799
slouken@1895
   800
    /* Copy over the image data */
slouken@1895
   801
    bounds.x = 0;
slouken@1895
   802
    bounds.y = 0;
slouken@1895
   803
    bounds.w = surface->w;
slouken@1895
   804
    bounds.h = surface->h;
slouken@1895
   805
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   806
slouken@1895
   807
    /* Clean up the original surface, and update converted surface */
slouken@2266
   808
    SDL_SetClipRect(convert, &surface->clip_rect);
slouken@2266
   809
    if (copy_flags & SDL_COPY_COLORKEY) {
slouken@2266
   810
        Uint8 keyR, keyG, keyB, keyA;
slouken@2266
   811
slouken@2267
   812
        SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
slouken@2267
   813
                    &keyG, &keyB, &keyA);
slouken@2266
   814
        SDL_SetColorKey(convert, 1,
slouken@2266
   815
                        SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
slouken@1895
   816
    }
slouken@2266
   817
    convert->map->info.r = surface->map->info.r;
slouken@2266
   818
    convert->map->info.g = surface->map->info.g;
slouken@2266
   819
    convert->map->info.b = surface->map->info.b;
slouken@2266
   820
    convert->map->info.a = surface->map->info.a;
slouken@2266
   821
    convert->map->info.flags = copy_flags;
slouken@2266
   822
    surface->map->info.flags = copy_flags;
slouken@0
   823
slouken@2266
   824
    /* Enable alpha blending by default if the new surface has an
slouken@2266
   825
     * alpha channel or alpha modulation */
slouken@2266
   826
    if (format->Amask || (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
slouken@2266
   827
        SDL_SetSurfaceBlendMode(convert, SDL_TEXTUREBLENDMODE_BLEND);
slouken@1895
   828
    }
slouken@0
   829
slouken@1895
   830
    /* We're ready to go! */
slouken@1895
   831
    return (convert);
slouken@0
   832
}
slouken@0
   833
slouken@0
   834
/*
slouken@0
   835
 * Free a surface created by the above function.
slouken@0
   836
 */
slouken@1895
   837
void
slouken@1895
   838
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   839
{
slouken@1895
   840
    if (surface == NULL) {
slouken@1895
   841
        return;
slouken@1895
   842
    }
slouken@1895
   843
    if (--surface->refcount > 0) {
slouken@1895
   844
        return;
slouken@1895
   845
    }
slouken@1895
   846
    while (surface->locked > 0) {
slouken@1895
   847
        SDL_UnlockSurface(surface);
slouken@1895
   848
    }
slouken@1895
   849
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   850
        SDL_UnRLESurface(surface, 0);
slouken@1895
   851
    }
slouken@1895
   852
    if (surface->format) {
slouken@1895
   853
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
   854
        SDL_FreeFormat(surface->format);
slouken@1895
   855
        surface->format = NULL;
slouken@1895
   856
    }
slouken@1895
   857
    if (surface->map != NULL) {
slouken@1895
   858
        SDL_FreeBlitMap(surface->map);
slouken@1895
   859
        surface->map = NULL;
slouken@1895
   860
    }
slouken@1895
   861
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
   862
        SDL_free(surface->pixels);
slouken@1895
   863
    }
slouken@1895
   864
    SDL_free(surface);
slouken@0
   865
#ifdef CHECK_LEAKS
slouken@1895
   866
    --surfaces_allocated;
slouken@0
   867
#endif
slouken@0
   868
}
slouken@1895
   869
slouken@1895
   870
/* vi: set ts=4 sw=4 expandtab: */