src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 Aug 2007 01:44:21 +0000
changeset 2266 e61ad15a205f
parent 2260 202ddfd1cfb1
child 2267 c785543d1843
permissions -rw-r--r--
More work in progress integrating SDL_Surface and the new SDL_Texture API
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@2266
   215
int SDL_SetSurfaceRLE(SDL_Surface *surface, int flag)
slouken@0
   216
{
slouken@2266
   217
    Uint32 flags;
slouken@0
   218
slouken@2266
   219
    if (!surface) {
slouken@2266
   220
        return -1;
slouken@1895
   221
    }
slouken@0
   222
slouken@1895
   223
    if (flag) {
slouken@2266
   224
        surface->flags |= SDL_RLEACCELOK;
slouken@1895
   225
    } else {
slouken@2266
   226
        surface->flags &= ~SDL_RLEACCELOK;
slouken@1895
   227
    }
slouken@2266
   228
    if (surface->flags != flags) {
slouken@1895
   229
        SDL_InvalidateMap(surface->map);
slouken@1895
   230
    }
slouken@1895
   231
    return 0;
slouken@431
   232
}
slouken@0
   233
slouken@2266
   234
int SDL_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
slouken@2266
   235
{
slouken@2266
   236
    int flags;
slouken@2266
   237
slouken@2266
   238
    if (!surface) {
slouken@2266
   239
        return -1;
slouken@2266
   240
    }
slouken@2266
   241
slouken@2266
   242
    if (flag & SDL_RLEACCEL) {
slouken@2266
   243
        SDL_SetSurfaceRLE(surface, 1);
slouken@2266
   244
    }
slouken@2266
   245
slouken@2266
   246
    flags = surface->map->info.flags;
slouken@2266
   247
    if (flag) {
slouken@2266
   248
        surface->map->info.flags |= SDL_COPY_COLORKEY;
slouken@2266
   249
        surface->map->info.colorkey = key;
slouken@2266
   250
    } else {
slouken@2266
   251
        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
slouken@2266
   252
    }
slouken@2266
   253
    if (surface->map->info.flags != flags) {
slouken@2266
   254
        SDL_InvalidateMap(surface->map);
slouken@2266
   255
    }
slouken@2266
   256
    return 0;
slouken@2266
   257
}
slouken@2266
   258
slouken@2266
   259
int SDL_SetSurfaceColorMod(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
slouken@2266
   260
{
slouken@2266
   261
    int flags;
slouken@2266
   262
slouken@2266
   263
    if (!surface) {
slouken@2266
   264
        return -1;
slouken@2266
   265
    }
slouken@2266
   266
slouken@2266
   267
    surface->map->info.r = r;
slouken@2266
   268
    surface->map->info.g = g;
slouken@2266
   269
    surface->map->info.b = b;
slouken@2266
   270
slouken@2266
   271
    flags = surface->map->info.flags;
slouken@2266
   272
    if (r != 0xFF || g != 0xFF || b != 0xFF) {
slouken@2266
   273
        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
slouken@2266
   274
    } else {
slouken@2266
   275
        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
slouken@2266
   276
    }
slouken@2266
   277
    if (surface->map->info.flags != flags) {
slouken@2266
   278
        SDL_InvalidateMap(surface->map);
slouken@2266
   279
    }
slouken@2266
   280
    return 0;
slouken@2266
   281
}
slouken@2266
   282
slouken@2266
   283
slouken@2266
   284
int SDL_GetSurfaceColorMod(SDL_Surface *surface, Uint8 * r, Uint8 * g, Uint8 * b)
slouken@2266
   285
{
slouken@2266
   286
    if (!surface) {
slouken@2266
   287
        return -1;
slouken@2266
   288
    }
slouken@2266
   289
slouken@2266
   290
    if (r) {
slouken@2266
   291
        *r = surface->map->info.r;
slouken@2266
   292
    }
slouken@2266
   293
    if (g) {
slouken@2266
   294
        *g = surface->map->info.g;
slouken@2266
   295
    }
slouken@2266
   296
    if (b) {
slouken@2266
   297
        *b = surface->map->info.b;
slouken@2266
   298
    }
slouken@2266
   299
    return 0;
slouken@2266
   300
}
slouken@2266
   301
slouken@2266
   302
int SDL_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
slouken@2266
   303
{
slouken@2266
   304
    int flags;
slouken@2266
   305
slouken@2266
   306
    if (!surface) {
slouken@2266
   307
        return -1;
slouken@2266
   308
    }
slouken@2266
   309
slouken@2266
   310
    surface->map->info.a = alpha;
slouken@2266
   311
slouken@2266
   312
    flags = surface->map->info.flags;
slouken@2266
   313
    if (alpha != 0xFF) {
slouken@2266
   314
        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
slouken@2266
   315
    } else {
slouken@2266
   316
        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
slouken@2266
   317
    }
slouken@2266
   318
    if (surface->map->info.flags != flags) {
slouken@2266
   319
        SDL_InvalidateMap(surface->map);
slouken@2266
   320
    }
slouken@2266
   321
    return 0;
slouken@2266
   322
}
slouken@2266
   323
slouken@2266
   324
int SDL_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 * alpha)
slouken@2266
   325
{
slouken@2266
   326
    if (!surface) {
slouken@2266
   327
        return -1;
slouken@2266
   328
    }
slouken@2266
   329
slouken@2266
   330
    if (alpha) {
slouken@2266
   331
        *alpha = surface->map->info.a;
slouken@2266
   332
    }
slouken@2266
   333
    return 0;
slouken@2266
   334
}
slouken@2266
   335
slouken@2266
   336
int SDL_SetSurfaceBlendMode(SDL_Surface *surface, int blendMode)
slouken@2266
   337
{
slouken@2266
   338
    int flags, status;
slouken@2266
   339
slouken@2266
   340
    if (!surface) {
slouken@2266
   341
        return -1;
slouken@2266
   342
    }
slouken@2266
   343
slouken@2266
   344
    status = 0;
slouken@2266
   345
    flags = surface->map->info.flags;
slouken@2266
   346
    surface->map->info.flags &= ~(SDL_COPY_MASK|SDL_COPY_BLEND|SDL_COPY_ADD|SDL_COPY_MOD);
slouken@2266
   347
    switch (blendMode) {
slouken@2266
   348
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@2266
   349
        break;
slouken@2266
   350
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@2266
   351
        surface->map->info.flags |= SDL_COPY_MASK;
slouken@2266
   352
        break;
slouken@2266
   353
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@2266
   354
        surface->map->info.flags |= SDL_COPY_BLEND;
slouken@2266
   355
        break;
slouken@2266
   356
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@2266
   357
        surface->map->info.flags |= SDL_COPY_ADD;
slouken@2266
   358
        break;
slouken@2266
   359
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@2266
   360
        surface->map->info.flags |= SDL_COPY_MOD;
slouken@2266
   361
        break;
slouken@2266
   362
    default:
slouken@2266
   363
        SDL_Unsupported();
slouken@2266
   364
        status = -1;
slouken@2266
   365
        break;
slouken@2266
   366
    }
slouken@2266
   367
slouken@2266
   368
    if (surface->map->info.flags != flags) {
slouken@2266
   369
        SDL_InvalidateMap(surface->map);
slouken@2266
   370
    }
slouken@2266
   371
    return status;
slouken@2266
   372
}
slouken@2266
   373
slouken@2266
   374
int SDL_GetSurfaceBlendMode(SDL_Surface *surface, int *blendMode)
slouken@2266
   375
{
slouken@2266
   376
    if (!surface) {
slouken@2266
   377
        return -1;
slouken@2266
   378
    }
slouken@2266
   379
slouken@2266
   380
    if (!blendMode) {
slouken@2266
   381
        return 0;
slouken@2266
   382
    }
slouken@2266
   383
slouken@2266
   384
    switch(surface->map->info.flags & (SDL_COPY_MASK|SDL_COPY_BLEND|SDL_COPY_ADD|SDL_COPY_MOD)) {
slouken@2266
   385
    case SDL_COPY_MASK:
slouken@2266
   386
        *blendMode = SDL_TEXTUREBLENDMODE_MASK:
slouken@2266
   387
        break;
slouken@2266
   388
    case SDL_COPY_BLEND:
slouken@2266
   389
        *blendMode = SDL_TEXTUREBLENDMODE_BLEND:
slouken@2266
   390
        break;
slouken@2266
   391
    case SDL_COPY_ADD:
slouken@2266
   392
        *blendMode = SDL_TEXTUREBLENDMODE_ADD:
slouken@2266
   393
        break;
slouken@2266
   394
    case SDL_COPY_MOD:
slouken@2266
   395
        *blendMode = SDL_TEXTUREBLENDMODE_MOD:
slouken@2266
   396
        break;
slouken@2266
   397
    default:
slouken@2266
   398
        *blendMode = SDL_TEXTUREBLENDMODE_NONE:
slouken@2266
   399
        break;
slouken@2266
   400
    }
slouken@2266
   401
    return 0;
slouken@2266
   402
}
slouken@2266
   403
slouken@2266
   404
int SDL_SetSurfaceScaleMode(SDL_Surface *surface, int scaleMode)
slouken@2266
   405
{
slouken@2266
   406
    int flags, status;
slouken@2266
   407
slouken@2266
   408
    if (!surface) {
slouken@2266
   409
        return -1;
slouken@2266
   410
    }
slouken@2266
   411
slouken@2266
   412
    status = 0;
slouken@2266
   413
    flags = surface->map->info.flags;
slouken@2266
   414
    surface->map->info.flags &= ~(SDL_COPY_NEAREST);
slouken@2266
   415
    switch (scaleMode) {
slouken@2266
   416
    case SDL_TEXTURESCALEMODE_NONE:
slouken@2266
   417
        break;
slouken@2266
   418
    case SDL_TEXTURESCALEMODE_FAST:
slouken@2266
   419
        surface->map->info.flags |= SDL_COPY_NEAREST;
slouken@2266
   420
        break;
slouken@2266
   421
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@2266
   422
    case SDL_TEXTURESCALEMODE_BEST:
slouken@2266
   423
        SDL_Unsupported();
slouken@2266
   424
        surface->map->info.flags |= SDL_COPY_NEAREST;
slouken@2266
   425
        status = -1;
slouken@2266
   426
        break;
slouken@2266
   427
    default:
slouken@2266
   428
        SDL_Unsupported();
slouken@2266
   429
        status = -1;
slouken@2266
   430
        break;
slouken@2266
   431
    }
slouken@2266
   432
slouken@2266
   433
    if (surface->map->info.flags != flags) {
slouken@2266
   434
        SDL_InvalidateMap(surface->map);
slouken@2266
   435
    }
slouken@2266
   436
    return status;
slouken@2266
   437
}
slouken@2266
   438
slouken@2266
   439
int SDL_GetSurfaceScaleMode(SDL_Surface *surface, int *scaleMode)
slouken@2266
   440
{
slouken@2266
   441
    if (!surface) {
slouken@2266
   442
        return -1;
slouken@2266
   443
    }
slouken@2266
   444
slouken@2266
   445
    if (!scaleMode) {
slouken@2266
   446
        return 0;
slouken@2266
   447
    }
slouken@2266
   448
slouken@2266
   449
    switch(surface->map->info.flags & (SDL_COPY_LINEAR)) {
slouken@2266
   450
    case SDL_COPY_LINEAR:
slouken@2266
   451
        *scaleMode = SDL_TEXTURESCALEMODE_FAST:
slouken@2266
   452
        break;
slouken@2266
   453
    default:
slouken@2266
   454
        *scaleMode = SDL_TEXTURESCALEMODE_NONE:
slouken@2266
   455
        break;
slouken@2266
   456
    }
slouken@2266
   457
    return 0;
slouken@2266
   458
}
slouken@2266
   459
slouken@1895
   460
SDL_bool
slouken@1895
   461
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   462
{
slouken@1895
   463
    SDL_Rect full_rect;
slouken@0
   464
slouken@1895
   465
    /* Don't do anything if there's no surface to act on */
slouken@1895
   466
    if (!surface) {
slouken@1895
   467
        return SDL_FALSE;
slouken@1895
   468
    }
slouken@0
   469
slouken@1895
   470
    /* Set up the full surface rectangle */
slouken@1895
   471
    full_rect.x = 0;
slouken@1895
   472
    full_rect.y = 0;
slouken@1895
   473
    full_rect.w = surface->w;
slouken@1895
   474
    full_rect.h = surface->h;
slouken@0
   475
slouken@1895
   476
    /* Set the clipping rectangle */
slouken@1895
   477
    if (!rect) {
slouken@1895
   478
        surface->clip_rect = full_rect;
slouken@1895
   479
        return 1;
slouken@1895
   480
    }
slouken@1895
   481
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   482
}
slouken@1895
   483
slouken@1895
   484
void
slouken@1895
   485
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   486
{
slouken@1895
   487
    if (surface && rect) {
slouken@1895
   488
        *rect = surface->clip_rect;
slouken@1895
   489
    }
slouken@0
   490
}
slouken@1895
   491
slouken@0
   492
/* 
slouken@0
   493
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   494
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   495
 * verification.  The lower part is a pointer to a low level
slouken@0
   496
 * accelerated blitting function.
slouken@0
   497
 *
slouken@0
   498
 * These parts are separated out and each used internally by this 
slouken@0
   499
 * library in the optimimum places.  They are exported so that if
slouken@0
   500
 * you know exactly what you are doing, you can optimize your code
slouken@0
   501
 * by calling the one(s) you need.
slouken@0
   502
 */
slouken@1895
   503
int
slouken@1895
   504
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   505
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   506
{
slouken@1895
   507
    /* Check to make sure the blit mapping is valid */
slouken@1895
   508
    if ((src->map->dst != dst) ||
slouken@1895
   509
        (src->map->dst->format_version != src->map->format_version)) {
slouken@1895
   510
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1895
   511
            return (-1);
slouken@1895
   512
        }
slouken@1895
   513
    }
slouken@2257
   514
    return (src->map->blit(src, srcrect, dst, dstrect));
slouken@0
   515
}
slouken@0
   516
slouken@0
   517
slouken@1895
   518
int
slouken@1895
   519
SDL_UpperBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1895
   520
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   521
{
slouken@1895
   522
    SDL_Rect fulldst;
slouken@1895
   523
    int srcx, srcy, w, h;
slouken@0
   524
slouken@1895
   525
    /* Make sure the surfaces aren't locked */
slouken@1895
   526
    if (!src || !dst) {
slouken@1895
   527
        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1895
   528
        return (-1);
slouken@1895
   529
    }
slouken@1895
   530
    if (src->locked || dst->locked) {
slouken@1895
   531
        SDL_SetError("Surfaces must not be locked during blit");
slouken@1895
   532
        return (-1);
slouken@1895
   533
    }
slouken@0
   534
slouken@1895
   535
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1895
   536
    if (dstrect == NULL) {
slouken@1895
   537
        fulldst.x = fulldst.y = 0;
slouken@1895
   538
        dstrect = &fulldst;
slouken@1895
   539
    }
slouken@0
   540
slouken@1895
   541
    /* clip the source rectangle to the source surface */
slouken@1895
   542
    if (srcrect) {
slouken@1895
   543
        int maxw, maxh;
slouken@0
   544
slouken@1895
   545
        srcx = srcrect->x;
slouken@1895
   546
        w = srcrect->w;
slouken@1895
   547
        if (srcx < 0) {
slouken@1895
   548
            w += srcx;
slouken@1895
   549
            dstrect->x -= srcx;
slouken@1895
   550
            srcx = 0;
slouken@1895
   551
        }
slouken@1895
   552
        maxw = src->w - srcx;
slouken@1895
   553
        if (maxw < w)
slouken@1895
   554
            w = maxw;
slouken@0
   555
slouken@1895
   556
        srcy = srcrect->y;
slouken@1895
   557
        h = srcrect->h;
slouken@1895
   558
        if (srcy < 0) {
slouken@1895
   559
            h += srcy;
slouken@1895
   560
            dstrect->y -= srcy;
slouken@1895
   561
            srcy = 0;
slouken@1895
   562
        }
slouken@1895
   563
        maxh = src->h - srcy;
slouken@1895
   564
        if (maxh < h)
slouken@1895
   565
            h = maxh;
slouken@0
   566
slouken@1895
   567
    } else {
slouken@1895
   568
        srcx = srcy = 0;
slouken@1895
   569
        w = src->w;
slouken@1895
   570
        h = src->h;
slouken@1895
   571
    }
slouken@0
   572
slouken@1895
   573
    /* clip the destination rectangle against the clip rectangle */
slouken@1895
   574
    {
slouken@1895
   575
        SDL_Rect *clip = &dst->clip_rect;
slouken@1895
   576
        int dx, dy;
slouken@0
   577
slouken@1895
   578
        dx = clip->x - dstrect->x;
slouken@1895
   579
        if (dx > 0) {
slouken@1895
   580
            w -= dx;
slouken@1895
   581
            dstrect->x += dx;
slouken@1895
   582
            srcx += dx;
slouken@1895
   583
        }
slouken@1895
   584
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1895
   585
        if (dx > 0)
slouken@1895
   586
            w -= dx;
slouken@1895
   587
slouken@1895
   588
        dy = clip->y - dstrect->y;
slouken@1895
   589
        if (dy > 0) {
slouken@1895
   590
            h -= dy;
slouken@1895
   591
            dstrect->y += dy;
slouken@1895
   592
            srcy += dy;
slouken@1895
   593
        }
slouken@1895
   594
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1895
   595
        if (dy > 0)
slouken@1895
   596
            h -= dy;
slouken@1895
   597
    }
slouken@1895
   598
slouken@1895
   599
    if (w > 0 && h > 0) {
slouken@1895
   600
        SDL_Rect sr;
slouken@1895
   601
        sr.x = srcx;
slouken@1895
   602
        sr.y = srcy;
slouken@1895
   603
        sr.w = dstrect->w = w;
slouken@1895
   604
        sr.h = dstrect->h = h;
slouken@1895
   605
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1895
   606
    }
slouken@1895
   607
    dstrect->w = dstrect->h = 0;
slouken@1895
   608
    return 0;
slouken@0
   609
}
slouken@0
   610
slouken@0
   611
/*
slouken@0
   612
 * Lock a surface to directly access the pixels
slouken@0
   613
 */
slouken@1895
   614
int
slouken@1895
   615
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   616
{
slouken@1895
   617
    if (!surface->locked) {
slouken@1895
   618
        /* Perform the lock */
slouken@1895
   619
        if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   620
            SDL_UnRLESurface(surface, 1);
slouken@1895
   621
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1895
   622
        }
slouken@1895
   623
    }
slouken@0
   624
slouken@1895
   625
    /* Increment the surface lock count, for recursive locks */
slouken@1895
   626
    ++surface->locked;
slouken@0
   627
slouken@1895
   628
    /* Ready to go.. */
slouken@1895
   629
    return (0);
slouken@0
   630
}
slouken@1895
   631
slouken@0
   632
/*
slouken@0
   633
 * Unlock a previously locked surface
slouken@0
   634
 */
slouken@1895
   635
void
slouken@1895
   636
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   637
{
slouken@1895
   638
    /* Only perform an unlock if we are locked */
slouken@1895
   639
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1895
   640
        return;
slouken@1895
   641
    }
slouken@0
   642
slouken@1895
   643
    /* Update RLE encoded surface with new data */
slouken@1895
   644
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   645
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
slouken@1895
   646
        SDL_RLESurface(surface);
slouken@1895
   647
    }
slouken@0
   648
}
slouken@0
   649
slouken@0
   650
/* 
slouken@0
   651
 * Convert a surface into the specified pixel format.
slouken@0
   652
 */
slouken@1895
   653
SDL_Surface *
slouken@1895
   654
SDL_ConvertSurface(SDL_Surface * surface,
slouken@1895
   655
                   SDL_PixelFormat * format, Uint32 flags)
slouken@0
   656
{
slouken@1895
   657
    SDL_Surface *convert;
slouken@2266
   658
    Uint32 copy_flags;
slouken@1895
   659
    SDL_Rect bounds;
slouken@0
   660
slouken@1895
   661
    /* Check for empty destination palette! (results in empty image) */
slouken@1895
   662
    if (format->palette != NULL) {
slouken@1895
   663
        int i;
slouken@1895
   664
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1895
   665
            if ((format->palette->colors[i].r != 0xFF) ||
slouken@1895
   666
                (format->palette->colors[i].g != 0xFF) ||
slouken@1895
   667
                (format->palette->colors[i].b != 0xFF))
slouken@1895
   668
                break;
slouken@1895
   669
        }
slouken@1895
   670
        if (i == format->palette->ncolors) {
slouken@1895
   671
            SDL_SetError("Empty destination palette");
slouken@1895
   672
            return (NULL);
slouken@1895
   673
        }
slouken@1895
   674
    }
slouken@0
   675
slouken@1895
   676
    /* Create a new surface with the desired format */
slouken@2266
   677
    convert = SDL_CreateRGBSurface(0, surface->w, surface->h,
slouken@1895
   678
                                   format->BitsPerPixel, format->Rmask,
slouken@1895
   679
                                   format->Gmask, format->Bmask,
slouken@1895
   680
                                   format->Amask);
slouken@1895
   681
    if (convert == NULL) {
slouken@1895
   682
        return (NULL);
slouken@1895
   683
    }
slouken@264
   684
slouken@1895
   685
    /* Copy the palette if any */
slouken@1895
   686
    if (format->palette && convert->format->palette) {
slouken@1895
   687
        SDL_memcpy(convert->format->palette->colors,
slouken@1895
   688
                   format->palette->colors,
slouken@1895
   689
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1895
   690
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1895
   691
    }
slouken@0
   692
slouken@2266
   693
    /* Save the original copy flags */
slouken@2266
   694
    copy_flags = surface->map->info.flags;
slouken@2266
   695
    surface->map->info.flags = 0;
slouken@0
   696
slouken@1895
   697
    /* Copy over the image data */
slouken@1895
   698
    bounds.x = 0;
slouken@1895
   699
    bounds.y = 0;
slouken@1895
   700
    bounds.w = surface->w;
slouken@1895
   701
    bounds.h = surface->h;
slouken@1895
   702
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   703
slouken@1895
   704
    /* Clean up the original surface, and update converted surface */
slouken@2266
   705
    SDL_SetClipRect(convert, &surface->clip_rect);
slouken@2266
   706
    if (copy_flags & SDL_COPY_COLORKEY) {
slouken@2266
   707
        Uint8 keyR, keyG, keyB, keyA;
slouken@2266
   708
slouken@2266
   709
        SDL_GetRGBA(colorkey, surface->format, &keyR, &keyG, &keyB, &keyA);
slouken@2266
   710
        SDL_SetColorKey(convert, 1,
slouken@2266
   711
                        SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
slouken@1895
   712
    }
slouken@2266
   713
    convert->map->info.r = surface->map->info.r;
slouken@2266
   714
    convert->map->info.g = surface->map->info.g;
slouken@2266
   715
    convert->map->info.b = surface->map->info.b;
slouken@2266
   716
    convert->map->info.a = surface->map->info.a;
slouken@2266
   717
    convert->map->info.flags = copy_flags;
slouken@2266
   718
    surface->map->info.flags = copy_flags;
slouken@0
   719
slouken@2266
   720
    /* Enable alpha blending by default if the new surface has an
slouken@2266
   721
     * alpha channel or alpha modulation */
slouken@2266
   722
    if (format->Amask || (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
slouken@2266
   723
        SDL_SetSurfaceBlendMode(convert, SDL_TEXTUREBLENDMODE_BLEND);
slouken@1895
   724
    }
slouken@0
   725
slouken@1895
   726
    /* We're ready to go! */
slouken@1895
   727
    return (convert);
slouken@0
   728
}
slouken@0
   729
slouken@0
   730
/*
slouken@0
   731
 * Free a surface created by the above function.
slouken@0
   732
 */
slouken@1895
   733
void
slouken@1895
   734
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   735
{
slouken@1895
   736
    if (surface == NULL) {
slouken@1895
   737
        return;
slouken@1895
   738
    }
slouken@1895
   739
    if (--surface->refcount > 0) {
slouken@1895
   740
        return;
slouken@1895
   741
    }
slouken@1895
   742
    while (surface->locked > 0) {
slouken@1895
   743
        SDL_UnlockSurface(surface);
slouken@1895
   744
    }
slouken@1895
   745
    if (surface->flags & SDL_RLEACCEL) {
slouken@1895
   746
        SDL_UnRLESurface(surface, 0);
slouken@1895
   747
    }
slouken@1895
   748
    if (surface->format) {
slouken@1895
   749
        SDL_SetSurfacePalette(surface, NULL);
slouken@1895
   750
        SDL_FreeFormat(surface->format);
slouken@1895
   751
        surface->format = NULL;
slouken@1895
   752
    }
slouken@1895
   753
    if (surface->map != NULL) {
slouken@1895
   754
        SDL_FreeBlitMap(surface->map);
slouken@1895
   755
        surface->map = NULL;
slouken@1895
   756
    }
slouken@1895
   757
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1895
   758
        SDL_free(surface->pixels);
slouken@1895
   759
    }
slouken@1895
   760
    SDL_free(surface);
slouken@0
   761
#ifdef CHECK_LEAKS
slouken@1895
   762
    --surfaces_allocated;
slouken@0
   763
#endif
slouken@0
   764
}
slouken@1895
   765
slouken@1895
   766
/* vi: set ts=4 sw=4 expandtab: */