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