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