src/video/SDL_surface.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 09 Jun 2006 07:06:12 +0000
branchSDL-1.3
changeset 1672 8e754b82cecc
parent 1670 eef792d31de8
child 1678 90bf530ced8e
permissions -rw-r--r--
Updated SDL_Surface code for software-only access, fixed some build errors
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@1672
    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@1662
    37
SDL_Surface *
slouken@1668
    38
SDL_CreateRGBSurface(Uint32 flags,
slouken@1668
    39
                     int width, int height, int depth,
slouken@1668
    40
                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
slouken@0
    41
{
slouken@1668
    42
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1662
    43
    SDL_Surface *screen;
slouken@1662
    44
    SDL_Surface *surface;
slouken@0
    45
slouken@1670
    46
    /* FIXME!! */
slouken@1662
    47
    /* Make sure the size requested doesn't overflow our datatypes */
slouken@1662
    48
    /* Next time I write a library like SDL, I'll use int for size. :) */
slouken@1662
    49
    if (width >= 16384 || height >= 65536) {
slouken@1668
    50
        SDL_SetError("Width or height is too large");
slouken@1662
    51
        return (NULL);
slouken@1662
    52
    }
slouken@940
    53
slouken@1662
    54
    /* Allocate the surface */
slouken@1668
    55
    surface = (SDL_Surface *) SDL_malloc(sizeof(*surface));
slouken@1662
    56
    if (surface == NULL) {
slouken@1668
    57
        SDL_OutOfMemory();
slouken@1662
    58
        return (NULL);
slouken@1662
    59
    }
slouken@1670
    60
    surface->flags = 0;
slouken@1668
    61
    surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
slouken@1662
    62
    if (surface->format == NULL) {
slouken@1668
    63
        SDL_free(surface);
slouken@1662
    64
        return (NULL);
slouken@1662
    65
    }
slouken@1662
    66
    if (Amask) {
slouken@1662
    67
        surface->flags |= SDL_SRCALPHA;
slouken@1662
    68
    }
slouken@1662
    69
    surface->w = width;
slouken@1662
    70
    surface->h = height;
slouken@1668
    71
    surface->pitch = SDL_CalculatePitch(surface);
slouken@1662
    72
    surface->pixels = NULL;
slouken@1662
    73
    surface->locked = 0;
slouken@1662
    74
    surface->map = NULL;
slouken@1668
    75
    SDL_SetClipRect(surface, NULL);
slouken@1668
    76
    SDL_FormatChanged(surface);
slouken@0
    77
slouken@1662
    78
    /* Get the pixels */
slouken@1670
    79
    if (surface->w && surface->h) {
slouken@1670
    80
        surface->pixels = SDL_malloc(surface->h * surface->pitch);
slouken@1670
    81
        if (surface->pixels == NULL) {
slouken@1670
    82
            SDL_FreeSurface(surface);
slouken@1670
    83
            SDL_OutOfMemory();
slouken@1670
    84
            return NULL;
slouken@1662
    85
        }
slouken@1670
    86
        /* This is important for bitmaps */
slouken@1670
    87
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
slouken@1662
    88
    }
slouken@0
    89
slouken@1662
    90
    /* Allocate an empty mapping */
slouken@1668
    91
    surface->map = SDL_AllocBlitMap();
slouken@1662
    92
    if (surface->map == NULL) {
slouken@1668
    93
        SDL_FreeSurface(surface);
slouken@1662
    94
        return (NULL);
slouken@1662
    95
    }
slouken@0
    96
slouken@1662
    97
    /* The surface is ready to go */
slouken@1662
    98
    surface->refcount = 1;
slouken@0
    99
#ifdef CHECK_LEAKS
slouken@1662
   100
    ++surfaces_allocated;
slouken@0
   101
#endif
slouken@1662
   102
    return (surface);
slouken@0
   103
}
slouken@1662
   104
slouken@0
   105
/*
slouken@0
   106
 * Create an RGB surface from an existing memory buffer
slouken@0
   107
 */
slouken@1662
   108
SDL_Surface *
slouken@1668
   109
SDL_CreateRGBSurfaceFrom(void *pixels,
slouken@1668
   110
                         int width, int height, int depth, int pitch,
slouken@1668
   111
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
slouken@1668
   112
                         Uint32 Amask)
slouken@0
   113
{
slouken@1662
   114
    SDL_Surface *surface;
slouken@0
   115
slouken@1672
   116
    surface =
slouken@1672
   117
        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
slouken@1662
   118
    if (surface != NULL) {
slouken@1662
   119
        surface->flags |= SDL_PREALLOC;
slouken@1662
   120
        surface->pixels = pixels;
slouken@1662
   121
        surface->w = width;
slouken@1662
   122
        surface->h = height;
slouken@1662
   123
        surface->pitch = pitch;
slouken@1668
   124
        SDL_SetClipRect(surface, NULL);
slouken@1662
   125
    }
slouken@1670
   126
    return surface;
slouken@1670
   127
}
slouken@1670
   128
slouken@1670
   129
SDL_Surface *
slouken@1670
   130
SDL_CreateRGBSurfaceFromTexture(SDL_TextureID textureID)
slouken@1670
   131
{
slouken@1670
   132
    SDL_Surface *surface;
slouken@1670
   133
    Uint32 format;
slouken@1670
   134
    int w, h;
slouken@1670
   135
    int bpp;
slouken@1670
   136
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1670
   137
slouken@1670
   138
    if (SDL_QueryTexture(textureID, &format, NULL, &w, &h) < 0) {
slouken@1670
   139
        return NULL;
slouken@1670
   140
    }
slouken@1670
   141
slouken@1670
   142
    if (!SDL_PixelFormatEnumToMasks
slouken@1670
   143
        (format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
slouken@1670
   144
        SDL_SetError("Unknown texture format");
slouken@1670
   145
        return NULL;
slouken@1670
   146
    }
slouken@1670
   147
slouken@1672
   148
    surface = SDL_CreateRGBSurface(0, 0, 0, bpp, Rmask, Gmask, Bmask, Amask);
slouken@1670
   149
    if (surface != NULL) {
slouken@1670
   150
        surface->flags |= (SDL_HWSURFACE | SDL_PREALLOC);
slouken@1672
   151
        surface->w = w;
slouken@1672
   152
        surface->h = h;
slouken@1672
   153
        surface->lock_data = (void *) textureID;
slouken@1670
   154
        SDL_SetClipRect(surface, NULL);
slouken@1670
   155
    }
slouken@1670
   156
    return surface;
slouken@1670
   157
}
slouken@1670
   158
slouken@1670
   159
/*
slouken@1670
   160
 * Set the palette in a blittable surface
slouken@1670
   161
 */
slouken@1670
   162
int
slouken@1670
   163
SDL_SetColors(SDL_Surface * surface, SDL_Color * colors, int firstcolor,
slouken@1670
   164
              int ncolors)
slouken@1670
   165
{
slouken@1670
   166
    SDL_Palette *pal;
slouken@1670
   167
    int gotall;
slouken@1670
   168
    int palsize;
slouken@1670
   169
slouken@1670
   170
    /* Verify the parameters */
slouken@1670
   171
    pal = surface->format->palette;
slouken@1670
   172
    if (!pal) {
slouken@1670
   173
        return 0;               /* not a palettized surface */
slouken@1670
   174
    }
slouken@1670
   175
    gotall = 1;
slouken@1670
   176
    palsize = 1 << surface->format->BitsPerPixel;
slouken@1670
   177
    if (ncolors > (palsize - firstcolor)) {
slouken@1670
   178
        ncolors = (palsize - firstcolor);
slouken@1670
   179
        gotall = 0;
slouken@1670
   180
    }
slouken@1670
   181
slouken@1670
   182
    if (colors != (pal->colors + firstcolor)) {
slouken@1670
   183
        SDL_memcpy(pal->colors + firstcolor, colors,
slouken@1670
   184
                   ncolors * sizeof(*colors));
slouken@1670
   185
    }
slouken@1670
   186
    SDL_FormatChanged(surface);
slouken@1670
   187
slouken@1670
   188
    if (surface->flags & (SDL_SHADOW_SURFACE | SDL_SCREEN_SURFACE)) {
slouken@1670
   189
        gotall &= SDL_SetScreenColors(surface, colors, firstcolor, ncolors);
slouken@1670
   190
    }
slouken@1670
   191
    return gotall;
slouken@0
   192
}
slouken@1662
   193
slouken@0
   194
/*
slouken@0
   195
 * Set the color key in a blittable surface
slouken@0
   196
 */
slouken@1662
   197
int
slouken@1668
   198
SDL_SetColorKey(SDL_Surface * surface, Uint32 flag, Uint32 key)
slouken@0
   199
{
slouken@1662
   200
    /* Sanity check the flag as it gets passed in */
slouken@1662
   201
    if (flag & SDL_SRCCOLORKEY) {
slouken@1662
   202
        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
slouken@1662
   203
            flag = (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1662
   204
        } else {
slouken@1662
   205
            flag = SDL_SRCCOLORKEY;
slouken@1662
   206
        }
slouken@1662
   207
    } else {
slouken@1662
   208
        flag = 0;
slouken@1662
   209
    }
slouken@0
   210
slouken@1662
   211
    /* Optimize away operations that don't change anything */
slouken@1662
   212
    if ((flag == (surface->flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK))) &&
slouken@1662
   213
        (key == surface->format->colorkey)) {
slouken@1662
   214
        return (0);
slouken@1662
   215
    }
slouken@0
   216
slouken@1662
   217
    /* UnRLE surfaces before we change the colorkey */
slouken@1662
   218
    if (surface->flags & SDL_RLEACCEL) {
slouken@1668
   219
        SDL_UnRLESurface(surface, 1);
slouken@1662
   220
    }
slouken@0
   221
slouken@1662
   222
    if (flag) {
slouken@1662
   223
        surface->flags |= SDL_SRCCOLORKEY;
slouken@1662
   224
        surface->format->colorkey = key;
slouken@1662
   225
        if (flag & SDL_RLEACCELOK) {
slouken@1662
   226
            surface->flags |= SDL_RLEACCELOK;
slouken@1662
   227
        } else {
slouken@1662
   228
            surface->flags &= ~SDL_RLEACCELOK;
slouken@1662
   229
        }
slouken@1662
   230
    } else {
slouken@1662
   231
        surface->flags &= ~(SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1662
   232
        surface->format->colorkey = 0;
slouken@1662
   233
    }
slouken@1668
   234
    SDL_InvalidateMap(surface->map);
slouken@1662
   235
    return (0);
slouken@1662
   236
}
slouken@0
   237
slouken@1662
   238
/* This function sets the alpha channel of a surface */
slouken@1662
   239
int
slouken@1668
   240
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
slouken@1662
   241
{
slouken@1662
   242
    Uint32 oldflags = surface->flags;
slouken@1662
   243
    Uint32 oldalpha = surface->format->alpha;
slouken@1662
   244
slouken@1662
   245
    /* Sanity check the flag as it gets passed in */
slouken@1662
   246
    if (flag & SDL_SRCALPHA) {
slouken@1662
   247
        if (flag & (SDL_RLEACCEL | SDL_RLEACCELOK)) {
slouken@1662
   248
            flag = (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1662
   249
        } else {
slouken@1662
   250
            flag = SDL_SRCALPHA;
slouken@1662
   251
        }
slouken@1662
   252
    } else {
slouken@1662
   253
        flag = 0;
slouken@1662
   254
    }
slouken@1662
   255
slouken@1662
   256
    /* Optimize away operations that don't change anything */
slouken@1662
   257
    if ((flag == (surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK))) &&
slouken@1662
   258
        (!flag || value == oldalpha)) {
slouken@1662
   259
        return (0);
slouken@1662
   260
    }
slouken@1662
   261
slouken@1662
   262
    if (!(flag & SDL_RLEACCELOK) && (surface->flags & SDL_RLEACCEL))
slouken@1668
   263
        SDL_UnRLESurface(surface, 1);
slouken@1662
   264
slouken@1662
   265
    if (flag) {
slouken@1662
   266
        surface->flags |= SDL_SRCALPHA;
slouken@1662
   267
        surface->format->alpha = value;
slouken@1662
   268
        if (flag & SDL_RLEACCELOK) {
slouken@1662
   269
            surface->flags |= SDL_RLEACCELOK;
slouken@1662
   270
        } else {
slouken@1662
   271
            surface->flags &= ~SDL_RLEACCELOK;
slouken@1662
   272
        }
slouken@1662
   273
    } else {
slouken@1662
   274
        surface->flags &= ~SDL_SRCALPHA;
slouken@1662
   275
        surface->format->alpha = SDL_ALPHA_OPAQUE;
slouken@1662
   276
    }
slouken@1662
   277
    /*
slouken@1662
   278
     * The representation for software surfaces is independent of
slouken@1662
   279
     * per-surface alpha, so no need to invalidate the blit mapping
slouken@1662
   280
     * if just the alpha value was changed. (If either is 255, we still
slouken@1662
   281
     * need to invalidate.)
slouken@1662
   282
     */
slouken@1672
   283
    if (oldflags != surface->flags
slouken@1672
   284
        || (((oldalpha + 1) ^ (value + 1)) & 0x100)) {
slouken@1668
   285
        SDL_InvalidateMap(surface->map);
slouken@1672
   286
    }
slouken@1662
   287
    return (0);
slouken@0
   288
}
slouken@1662
   289
slouken@1662
   290
int
slouken@1668
   291
SDL_SetAlphaChannel(SDL_Surface * surface, Uint8 value)
slouken@0
   292
{
slouken@1662
   293
    int row, col;
slouken@1662
   294
    int offset;
slouken@1662
   295
    Uint8 *buf;
slouken@0
   296
slouken@1662
   297
    if ((surface->format->Amask != 0xFF000000) &&
slouken@1662
   298
        (surface->format->Amask != 0x000000FF)) {
slouken@1668
   299
        SDL_SetError("Unsupported surface alpha mask format");
slouken@1662
   300
        return -1;
slouken@1662
   301
    }
slouken@431
   302
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@1662
   303
    if (surface->format->Amask == 0xFF000000) {
slouken@1662
   304
        offset = 3;
slouken@1662
   305
    } else {
slouken@1662
   306
        offset = 0;
slouken@1662
   307
    }
slouken@431
   308
#else
slouken@1662
   309
    if (surface->format->Amask == 0xFF000000) {
slouken@1662
   310
        offset = 0;
slouken@1662
   311
    } else {
slouken@1662
   312
        offset = 3;
slouken@1662
   313
    }
slouken@431
   314
#endif /* Byte ordering */
slouken@431
   315
slouken@1662
   316
    /* Quickly set the alpha channel of an RGBA or ARGB surface */
slouken@1668
   317
    if (SDL_MUSTLOCK(surface)) {
slouken@1668
   318
        if (SDL_LockSurface(surface) < 0) {
slouken@1662
   319
            return -1;
slouken@1662
   320
        }
slouken@1662
   321
    }
slouken@1662
   322
    row = surface->h;
slouken@1662
   323
    while (row--) {
slouken@1662
   324
        col = surface->w;
slouken@1662
   325
        buf = (Uint8 *) surface->pixels + row * surface->pitch + offset;
slouken@1662
   326
        while (col--) {
slouken@1662
   327
            *buf = value;
slouken@1662
   328
            buf += 4;
slouken@1662
   329
        }
slouken@1662
   330
    }
slouken@1668
   331
    if (SDL_MUSTLOCK(surface)) {
slouken@1668
   332
        SDL_UnlockSurface(surface);
slouken@1662
   333
    }
slouken@1662
   334
    return 0;
slouken@431
   335
}
slouken@0
   336
slouken@0
   337
/*
slouken@0
   338
 * A function to calculate the intersection of two rectangles:
slouken@0
   339
 * return true if the rectangles intersect, false otherwise
slouken@0
   340
 */
slouken@1662
   341
static __inline__ SDL_bool
slouken@1668
   342
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B,
slouken@1668
   343
                  SDL_Rect * intersection)
slouken@0
   344
{
slouken@1662
   345
    int Amin, Amax, Bmin, Bmax;
slouken@0
   346
slouken@1662
   347
    /* Horizontal intersection */
slouken@1662
   348
    Amin = A->x;
slouken@1662
   349
    Amax = Amin + A->w;
slouken@1662
   350
    Bmin = B->x;
slouken@1662
   351
    Bmax = Bmin + B->w;
slouken@1662
   352
    if (Bmin > Amin)
slouken@1662
   353
        Amin = Bmin;
slouken@1662
   354
    intersection->x = Amin;
slouken@1662
   355
    if (Bmax < Amax)
slouken@1662
   356
        Amax = Bmax;
slouken@1662
   357
    intersection->w = Amax - Amin > 0 ? Amax - Amin : 0;
slouken@0
   358
slouken@1662
   359
    /* Vertical intersection */
slouken@1662
   360
    Amin = A->y;
slouken@1662
   361
    Amax = Amin + A->h;
slouken@1662
   362
    Bmin = B->y;
slouken@1662
   363
    Bmax = Bmin + B->h;
slouken@1662
   364
    if (Bmin > Amin)
slouken@1662
   365
        Amin = Bmin;
slouken@1662
   366
    intersection->y = Amin;
slouken@1662
   367
    if (Bmax < Amax)
slouken@1662
   368
        Amax = Bmax;
slouken@1662
   369
    intersection->h = Amax - Amin > 0 ? Amax - Amin : 0;
slouken@0
   370
slouken@1662
   371
    return (intersection->w && intersection->h);
slouken@0
   372
}
slouken@1662
   373
slouken@0
   374
/*
slouken@0
   375
 * Set the clipping rectangle for a blittable surface
slouken@0
   376
 */
slouken@1662
   377
SDL_bool
slouken@1668
   378
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
slouken@0
   379
{
slouken@1662
   380
    SDL_Rect full_rect;
slouken@0
   381
slouken@1662
   382
    /* Don't do anything if there's no surface to act on */
slouken@1662
   383
    if (!surface) {
slouken@1662
   384
        return SDL_FALSE;
slouken@1662
   385
    }
slouken@0
   386
slouken@1662
   387
    /* Set up the full surface rectangle */
slouken@1662
   388
    full_rect.x = 0;
slouken@1662
   389
    full_rect.y = 0;
slouken@1662
   390
    full_rect.w = surface->w;
slouken@1662
   391
    full_rect.h = surface->h;
slouken@0
   392
slouken@1662
   393
    /* Set the clipping rectangle */
slouken@1662
   394
    if (!rect) {
slouken@1662
   395
        surface->clip_rect = full_rect;
slouken@1662
   396
        return 1;
slouken@1662
   397
    }
slouken@1668
   398
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
slouken@0
   399
}
slouken@1662
   400
slouken@1662
   401
void
slouken@1668
   402
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
slouken@0
   403
{
slouken@1662
   404
    if (surface && rect) {
slouken@1662
   405
        *rect = surface->clip_rect;
slouken@1662
   406
    }
slouken@0
   407
}
slouken@1662
   408
slouken@0
   409
/* 
slouken@0
   410
 * Set up a blit between two surfaces -- split into three parts:
slouken@0
   411
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle 
slouken@0
   412
 * verification.  The lower part is a pointer to a low level
slouken@0
   413
 * accelerated blitting function.
slouken@0
   414
 *
slouken@0
   415
 * These parts are separated out and each used internally by this 
slouken@0
   416
 * library in the optimimum places.  They are exported so that if
slouken@0
   417
 * you know exactly what you are doing, you can optimize your code
slouken@0
   418
 * by calling the one(s) you need.
slouken@0
   419
 */
slouken@1662
   420
int
slouken@1668
   421
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1668
   422
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   423
{
slouken@1662
   424
    /* Check to make sure the blit mapping is valid */
slouken@1662
   425
    if ((src->map->dst != dst) ||
slouken@1662
   426
        (src->map->dst->format_version != src->map->format_version)) {
slouken@1668
   427
        if (SDL_MapSurface(src, dst) < 0) {
slouken@1662
   428
            return (-1);
slouken@1662
   429
        }
slouken@1662
   430
    }
slouken@1672
   431
    return (src->map->sw_blit(src, srcrect, dst, dstrect));
slouken@0
   432
}
slouken@0
   433
slouken@0
   434
slouken@1662
   435
int
slouken@1668
   436
SDL_UpperBlit(SDL_Surface * src, SDL_Rect * srcrect,
slouken@1668
   437
              SDL_Surface * dst, SDL_Rect * dstrect)
slouken@0
   438
{
slouken@1662
   439
    SDL_Rect fulldst;
slouken@1662
   440
    int srcx, srcy, w, h;
slouken@0
   441
slouken@1662
   442
    /* Make sure the surfaces aren't locked */
slouken@1662
   443
    if (!src || !dst) {
slouken@1668
   444
        SDL_SetError("SDL_UpperBlit: passed a NULL surface");
slouken@1662
   445
        return (-1);
slouken@1662
   446
    }
slouken@1662
   447
    if (src->locked || dst->locked) {
slouken@1668
   448
        SDL_SetError("Surfaces must not be locked during blit");
slouken@1662
   449
        return (-1);
slouken@1662
   450
    }
slouken@0
   451
slouken@1662
   452
    /* If the destination rectangle is NULL, use the entire dest surface */
slouken@1662
   453
    if (dstrect == NULL) {
slouken@1662
   454
        fulldst.x = fulldst.y = 0;
slouken@1662
   455
        dstrect = &fulldst;
slouken@1662
   456
    }
slouken@0
   457
slouken@1662
   458
    /* clip the source rectangle to the source surface */
slouken@1662
   459
    if (srcrect) {
slouken@1662
   460
        int maxw, maxh;
slouken@0
   461
slouken@1662
   462
        srcx = srcrect->x;
slouken@1662
   463
        w = srcrect->w;
slouken@1662
   464
        if (srcx < 0) {
slouken@1662
   465
            w += srcx;
slouken@1662
   466
            dstrect->x -= srcx;
slouken@1662
   467
            srcx = 0;
slouken@1662
   468
        }
slouken@1662
   469
        maxw = src->w - srcx;
slouken@1662
   470
        if (maxw < w)
slouken@1662
   471
            w = maxw;
slouken@0
   472
slouken@1662
   473
        srcy = srcrect->y;
slouken@1662
   474
        h = srcrect->h;
slouken@1662
   475
        if (srcy < 0) {
slouken@1662
   476
            h += srcy;
slouken@1662
   477
            dstrect->y -= srcy;
slouken@1662
   478
            srcy = 0;
slouken@1662
   479
        }
slouken@1662
   480
        maxh = src->h - srcy;
slouken@1662
   481
        if (maxh < h)
slouken@1662
   482
            h = maxh;
slouken@0
   483
slouken@1662
   484
    } else {
slouken@1662
   485
        srcx = srcy = 0;
slouken@1662
   486
        w = src->w;
slouken@1662
   487
        h = src->h;
slouken@1662
   488
    }
slouken@0
   489
slouken@1662
   490
    /* clip the destination rectangle against the clip rectangle */
slouken@1662
   491
    {
slouken@1662
   492
        SDL_Rect *clip = &dst->clip_rect;
slouken@1662
   493
        int dx, dy;
slouken@0
   494
slouken@1662
   495
        dx = clip->x - dstrect->x;
slouken@1662
   496
        if (dx > 0) {
slouken@1662
   497
            w -= dx;
slouken@1662
   498
            dstrect->x += dx;
slouken@1662
   499
            srcx += dx;
slouken@1662
   500
        }
slouken@1662
   501
        dx = dstrect->x + w - clip->x - clip->w;
slouken@1662
   502
        if (dx > 0)
slouken@1662
   503
            w -= dx;
slouken@1662
   504
slouken@1662
   505
        dy = clip->y - dstrect->y;
slouken@1662
   506
        if (dy > 0) {
slouken@1662
   507
            h -= dy;
slouken@1662
   508
            dstrect->y += dy;
slouken@1662
   509
            srcy += dy;
slouken@1662
   510
        }
slouken@1662
   511
        dy = dstrect->y + h - clip->y - clip->h;
slouken@1662
   512
        if (dy > 0)
slouken@1662
   513
            h -= dy;
slouken@1662
   514
    }
slouken@1662
   515
slouken@1662
   516
    if (w > 0 && h > 0) {
slouken@1662
   517
        SDL_Rect sr;
slouken@1662
   518
        sr.x = srcx;
slouken@1662
   519
        sr.y = srcy;
slouken@1662
   520
        sr.w = dstrect->w = w;
slouken@1662
   521
        sr.h = dstrect->h = h;
slouken@1668
   522
        return SDL_LowerBlit(src, &sr, dst, dstrect);
slouken@1662
   523
    }
slouken@1662
   524
    dstrect->w = dstrect->h = 0;
slouken@1662
   525
    return 0;
slouken@0
   526
}
slouken@0
   527
slouken@1662
   528
static int
slouken@1668
   529
SDL_FillRect1(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@532
   530
{
slouken@1662
   531
    /* FIXME: We have to worry about packing order.. *sigh* */
slouken@1668
   532
    SDL_SetError("1-bpp rect fill not yet implemented");
slouken@1662
   533
    return -1;
slouken@532
   534
}
slouken@532
   535
slouken@1662
   536
static int
slouken@1668
   537
SDL_FillRect4(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@532
   538
{
slouken@1662
   539
    /* FIXME: We have to worry about packing order.. *sigh* */
slouken@1668
   540
    SDL_SetError("4-bpp rect fill not yet implemented");
slouken@1662
   541
    return -1;
slouken@532
   542
}
slouken@532
   543
slouken@0
   544
/* 
slouken@0
   545
 * This function performs a fast fill of the given rectangle with 'color'
slouken@0
   546
 */
slouken@1662
   547
int
slouken@1668
   548
SDL_FillRect(SDL_Surface * dst, SDL_Rect * dstrect, Uint32 color)
slouken@0
   549
{
slouken@1668
   550
    SDL_VideoDevice *_this = SDL_GetVideoDevice();
slouken@1662
   551
    int x, y;
slouken@1662
   552
    Uint8 *row;
slouken@0
   553
slouken@1662
   554
    /* This function doesn't work on surfaces < 8 bpp */
slouken@1662
   555
    if (dst->format->BitsPerPixel < 8) {
slouken@1662
   556
        switch (dst->format->BitsPerPixel) {
slouken@1662
   557
        case 1:
slouken@1668
   558
            return SDL_FillRect1(dst, dstrect, color);
slouken@1662
   559
            break;
slouken@1662
   560
        case 4:
slouken@1668
   561
            return SDL_FillRect4(dst, dstrect, color);
slouken@1662
   562
            break;
slouken@1662
   563
        default:
slouken@1668
   564
            SDL_SetError("Fill rect on unsupported surface format");
slouken@1662
   565
            return (-1);
slouken@1662
   566
            break;
slouken@1662
   567
        }
slouken@1662
   568
    }
slouken@532
   569
slouken@1662
   570
    /* If 'dstrect' == NULL, then fill the whole surface */
slouken@1662
   571
    if (dstrect) {
slouken@1662
   572
        /* Perform clipping */
slouken@1668
   573
        if (!SDL_IntersectRect(dstrect, &dst->clip_rect, dstrect)) {
slouken@1662
   574
            return (0);
slouken@1662
   575
        }
slouken@1662
   576
    } else {
slouken@1662
   577
        dstrect = &dst->clip_rect;
slouken@1662
   578
    }
slouken@0
   579
slouken@1662
   580
    /* Perform software fill */
slouken@1668
   581
    if (SDL_LockSurface(dst) != 0) {
slouken@1662
   582
        return (-1);
slouken@1662
   583
    }
slouken@1662
   584
    row = (Uint8 *) dst->pixels + dstrect->y * dst->pitch +
slouken@1662
   585
        dstrect->x * dst->format->BytesPerPixel;
slouken@1662
   586
    if (dst->format->palette || (color == 0)) {
slouken@1662
   587
        x = dstrect->w * dst->format->BytesPerPixel;
slouken@1662
   588
        if (!color && !((uintptr_t) row & 3) && !(x & 3)
slouken@1662
   589
            && !(dst->pitch & 3)) {
slouken@1662
   590
            int n = x >> 2;
slouken@1662
   591
            for (y = dstrect->h; y; --y) {
slouken@1668
   592
                SDL_memset4(row, 0, n);
slouken@1662
   593
                row += dst->pitch;
slouken@1662
   594
            }
slouken@1662
   595
        } else {
slouken@0
   596
#ifdef __powerpc__
slouken@1662
   597
            /*
slouken@1662
   598
             * SDL_memset() on PPC (both glibc and codewarrior) uses
slouken@1662
   599
             * the dcbz (Data Cache Block Zero) instruction, which
slouken@1662
   600
             * causes an alignment exception if the destination is
slouken@1662
   601
             * uncachable, so only use it on software surfaces
slouken@1662
   602
             */
slouken@1672
   603
            if (dst->flags & SDL_HWSURFACE) {
slouken@1662
   604
                if (dstrect->w >= 8) {
slouken@1662
   605
                    /*
slouken@1662
   606
                     * 64-bit stores are probably most
slouken@1662
   607
                     * efficient to uncached video memory
slouken@1662
   608
                     */
slouken@1662
   609
                    double fill;
slouken@1668
   610
                    SDL_memset(&fill, color, (sizeof fill));
slouken@1662
   611
                    for (y = dstrect->h; y; y--) {
slouken@1662
   612
                        Uint8 *d = row;
slouken@1662
   613
                        unsigned n = x;
slouken@1662
   614
                        unsigned nn;
slouken@1662
   615
                        Uint8 c = color;
slouken@1662
   616
                        double f = fill;
slouken@1668
   617
                        while ((unsigned long) d & (sizeof(double) - 1)) {
slouken@1662
   618
                            *d++ = c;
slouken@1662
   619
                            n--;
slouken@1662
   620
                        }
slouken@1668
   621
                        nn = n / (sizeof(double) * 4);
slouken@1662
   622
                        while (nn) {
slouken@1662
   623
                            ((double *) d)[0] = f;
slouken@1662
   624
                            ((double *) d)[1] = f;
slouken@1662
   625
                            ((double *) d)[2] = f;
slouken@1662
   626
                            ((double *) d)[3] = f;
slouken@1668
   627
                            d += 4 * sizeof(double);
slouken@1662
   628
                            nn--;
slouken@1662
   629
                        }
slouken@1668
   630
                        n &= ~(sizeof(double) * 4 - 1);
slouken@1668
   631
                        nn = n / sizeof(double);
slouken@1662
   632
                        while (nn) {
slouken@1662
   633
                            *(double *) d = f;
slouken@1668
   634
                            d += sizeof(double);
slouken@1662
   635
                            nn--;
slouken@1662
   636
                        }
slouken@1668
   637
                        n &= ~(sizeof(double) - 1);
slouken@1662
   638
                        while (n) {
slouken@1662
   639
                            *d++ = c;
slouken@1662
   640
                            n--;
slouken@1662
   641
                        }
slouken@1662
   642
                        row += dst->pitch;
slouken@1662
   643
                    }
slouken@1662
   644
                } else {
slouken@1662
   645
                    /* narrow boxes */
slouken@1662
   646
                    for (y = dstrect->h; y; y--) {
slouken@1662
   647
                        Uint8 *d = row;
slouken@1662
   648
                        Uint8 c = color;
slouken@1662
   649
                        int n = x;
slouken@1662
   650
                        while (n) {
slouken@1662
   651
                            *d++ = c;
slouken@1662
   652
                            n--;
slouken@1662
   653
                        }
slouken@1662
   654
                        row += dst->pitch;
slouken@1662
   655
                    }
slouken@1662
   656
                }
slouken@1662
   657
            } else
slouken@0
   658
#endif /* __powerpc__ */
slouken@1662
   659
            {
slouken@1662
   660
                for (y = dstrect->h; y; y--) {
slouken@1668
   661
                    SDL_memset(row, color, x);
slouken@1662
   662
                    row += dst->pitch;
slouken@1662
   663
                }
slouken@1662
   664
            }
slouken@1662
   665
        }
slouken@1662
   666
    } else {
slouken@1662
   667
        switch (dst->format->BytesPerPixel) {
slouken@1662
   668
        case 2:
slouken@1662
   669
            for (y = dstrect->h; y; --y) {
slouken@1662
   670
                Uint16 *pixels = (Uint16 *) row;
slouken@1662
   671
                Uint16 c = (Uint16) color;
slouken@1662
   672
                Uint32 cc = (Uint32) c << 16 | c;
slouken@1662
   673
                int n = dstrect->w;
slouken@1662
   674
                if ((uintptr_t) pixels & 3) {
slouken@1662
   675
                    *pixels++ = c;
slouken@1662
   676
                    n--;
slouken@1662
   677
                }
slouken@1662
   678
                if (n >> 1)
slouken@1668
   679
                    SDL_memset4(pixels, cc, n >> 1);
slouken@1662
   680
                if (n & 1)
slouken@1662
   681
                    pixels[n - 1] = c;
slouken@1662
   682
                row += dst->pitch;
slouken@1662
   683
            }
slouken@1662
   684
            break;
slouken@0
   685
slouken@1662
   686
        case 3:
slouken@1662
   687
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1662
   688
            color <<= 8;
slouken@1662
   689
#endif
slouken@1662
   690
            for (y = dstrect->h; y; --y) {
slouken@1662
   691
                Uint8 *pixels = row;
slouken@1662
   692
                for (x = dstrect->w; x; --x) {
slouken@1668
   693
                    SDL_memcpy(pixels, &color, 3);
slouken@1662
   694
                    pixels += 3;
slouken@1662
   695
                }
slouken@1662
   696
                row += dst->pitch;
slouken@1662
   697
            }
slouken@1662
   698
            break;
slouken@0
   699
slouken@1662
   700
        case 4:
slouken@1662
   701
            for (y = dstrect->h; y; --y) {
slouken@1668
   702
                SDL_memset4(row, color, dstrect->w);
slouken@1662
   703
                row += dst->pitch;
slouken@1662
   704
            }
slouken@1662
   705
            break;
slouken@1662
   706
        }
slouken@1662
   707
    }
slouken@1668
   708
    SDL_UnlockSurface(dst);
slouken@0
   709
slouken@1662
   710
    /* We're done! */
slouken@1662
   711
    return (0);
slouken@0
   712
}
slouken@0
   713
slouken@0
   714
/*
slouken@0
   715
 * Lock a surface to directly access the pixels
slouken@0
   716
 */
slouken@1662
   717
int
slouken@1668
   718
SDL_LockSurface(SDL_Surface * surface)
slouken@0
   719
{
slouken@1662
   720
    if (!surface->locked) {
slouken@1662
   721
        /* Perform the lock */
slouken@1662
   722
        if (surface->flags & SDL_HWSURFACE) {
slouken@1672
   723
            if (SDL_LockTexture
slouken@1672
   724
                ((SDL_TextureID) surface->lock_data, NULL, 1,
slouken@1672
   725
                 &surface->pixels, &surface->pitch) < 0) {
slouken@1662
   726
                return (-1);
slouken@1662
   727
            }
slouken@1662
   728
        }
slouken@1662
   729
        if (surface->flags & SDL_RLEACCEL) {
slouken@1668
   730
            SDL_UnRLESurface(surface, 1);
slouken@1662
   731
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
slouken@1662
   732
        }
slouken@1662
   733
    }
slouken@0
   734
slouken@1662
   735
    /* Increment the surface lock count, for recursive locks */
slouken@1662
   736
    ++surface->locked;
slouken@0
   737
slouken@1662
   738
    /* Ready to go.. */
slouken@1662
   739
    return (0);
slouken@0
   740
}
slouken@1662
   741
slouken@0
   742
/*
slouken@0
   743
 * Unlock a previously locked surface
slouken@0
   744
 */
slouken@1662
   745
void
slouken@1668
   746
SDL_UnlockSurface(SDL_Surface * surface)
slouken@0
   747
{
slouken@1662
   748
    /* Only perform an unlock if we are locked */
slouken@1662
   749
    if (!surface->locked || (--surface->locked > 0)) {
slouken@1662
   750
        return;
slouken@1662
   751
    }
slouken@0
   752
slouken@1662
   753
    /* Unlock hardware or accelerated surfaces */
slouken@1662
   754
    if (surface->flags & SDL_HWSURFACE) {
slouken@1672
   755
        SDL_UnlockTexture((SDL_TextureID) surface->lock_data);
slouken@1662
   756
    } else {
slouken@1662
   757
        /* Update RLE encoded surface with new data */
slouken@1662
   758
        if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1662
   759
            surface->flags &= ~SDL_RLEACCEL;    /* stop lying */
slouken@1668
   760
            SDL_RLESurface(surface);
slouken@1662
   761
        }
slouken@1662
   762
    }
slouken@0
   763
}
slouken@0
   764
slouken@0
   765
/* 
slouken@0
   766
 * Convert a surface into the specified pixel format.
slouken@0
   767
 */
slouken@1662
   768
SDL_Surface *
slouken@1668
   769
SDL_ConvertSurface(SDL_Surface * surface,
slouken@1668
   770
                   SDL_PixelFormat * format, Uint32 flags)
slouken@0
   771
{
slouken@1662
   772
    SDL_Surface *convert;
slouken@1662
   773
    Uint32 colorkey = 0;
slouken@1662
   774
    Uint8 alpha = 0;
slouken@1662
   775
    Uint32 surface_flags;
slouken@1662
   776
    SDL_Rect bounds;
slouken@0
   777
slouken@1662
   778
    /* Check for empty destination palette! (results in empty image) */
slouken@1662
   779
    if (format->palette != NULL) {
slouken@1662
   780
        int i;
slouken@1662
   781
        for (i = 0; i < format->palette->ncolors; ++i) {
slouken@1662
   782
            if ((format->palette->colors[i].r != 0) ||
slouken@1662
   783
                (format->palette->colors[i].g != 0) ||
slouken@1662
   784
                (format->palette->colors[i].b != 0))
slouken@1662
   785
                break;
slouken@1662
   786
        }
slouken@1662
   787
        if (i == format->palette->ncolors) {
slouken@1668
   788
            SDL_SetError("Empty destination palette");
slouken@1662
   789
            return (NULL);
slouken@1662
   790
        }
slouken@1662
   791
    }
slouken@0
   792
slouken@1662
   793
    /* Create a new surface with the desired format */
slouken@1668
   794
    convert = SDL_CreateRGBSurface(flags,
slouken@1668
   795
                                   surface->w, surface->h,
slouken@1668
   796
                                   format->BitsPerPixel, format->Rmask,
slouken@1668
   797
                                   format->Gmask, format->Bmask,
slouken@1668
   798
                                   format->Amask);
slouken@1662
   799
    if (convert == NULL) {
slouken@1662
   800
        return (NULL);
slouken@1662
   801
    }
slouken@0
   802
slouken@1662
   803
    /* Copy the palette if any */
slouken@1662
   804
    if (format->palette && convert->format->palette) {
slouken@1668
   805
        SDL_memcpy(convert->format->palette->colors,
slouken@1668
   806
                   format->palette->colors,
slouken@1668
   807
                   format->palette->ncolors * sizeof(SDL_Color));
slouken@1662
   808
        convert->format->palette->ncolors = format->palette->ncolors;
slouken@1662
   809
    }
slouken@0
   810
slouken@1662
   811
    /* Save the original surface color key and alpha */
slouken@1662
   812
    surface_flags = surface->flags;
slouken@1662
   813
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1662
   814
        /* Convert colourkeyed surfaces to RGBA if requested */
slouken@1662
   815
        if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
slouken@1662
   816
            surface_flags &= ~SDL_SRCCOLORKEY;
slouken@1662
   817
        } else {
slouken@1662
   818
            colorkey = surface->format->colorkey;
slouken@1668
   819
            SDL_SetColorKey(surface, 0, 0);
slouken@1662
   820
        }
slouken@1662
   821
    }
slouken@1662
   822
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1662
   823
        /* Copy over the alpha channel to RGBA if requested */
slouken@1662
   824
        if (format->Amask) {
slouken@1662
   825
            surface->flags &= ~SDL_SRCALPHA;
slouken@1662
   826
        } else {
slouken@1662
   827
            alpha = surface->format->alpha;
slouken@1668
   828
            SDL_SetAlpha(surface, 0, 0);
slouken@1662
   829
        }
slouken@1662
   830
    }
slouken@0
   831
slouken@1662
   832
    /* Copy over the image data */
slouken@1662
   833
    bounds.x = 0;
slouken@1662
   834
    bounds.y = 0;
slouken@1662
   835
    bounds.w = surface->w;
slouken@1662
   836
    bounds.h = surface->h;
slouken@1668
   837
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
slouken@0
   838
slouken@1662
   839
    /* Clean up the original surface, and update converted surface */
slouken@1662
   840
    if (convert != NULL) {
slouken@1668
   841
        SDL_SetClipRect(convert, &surface->clip_rect);
slouken@1662
   842
    }
slouken@1662
   843
    if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1662
   844
        Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
slouken@1662
   845
        if (convert != NULL) {
slouken@1662
   846
            Uint8 keyR, keyG, keyB;
slouken@0
   847
slouken@1668
   848
            SDL_GetRGB(colorkey, surface->format, &keyR, &keyG, &keyB);
slouken@1668
   849
            SDL_SetColorKey(convert, cflags | (flags & SDL_RLEACCELOK),
slouken@1668
   850
                            SDL_MapRGB(convert->format, keyR, keyG, keyB));
slouken@1662
   851
        }
slouken@1668
   852
        SDL_SetColorKey(surface, cflags, colorkey);
slouken@1662
   853
    }
slouken@1662
   854
    if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
slouken@1662
   855
        Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
slouken@1662
   856
        if (convert != NULL) {
slouken@1668
   857
            SDL_SetAlpha(convert, aflags | (flags & SDL_RLEACCELOK), alpha);
slouken@1662
   858
        }
slouken@1662
   859
        if (format->Amask) {
slouken@1662
   860
            surface->flags |= SDL_SRCALPHA;
slouken@1662
   861
        } else {
slouken@1668
   862
            SDL_SetAlpha(surface, aflags, alpha);
slouken@1662
   863
        }
slouken@1662
   864
    }
slouken@0
   865
slouken@1662
   866
    /* We're ready to go! */
slouken@1662
   867
    return (convert);
slouken@0
   868
}
slouken@0
   869
slouken@0
   870
/*
slouken@0
   871
 * Free a surface created by the above function.
slouken@0
   872
 */
slouken@1662
   873
void
slouken@1668
   874
SDL_FreeSurface(SDL_Surface * surface)
slouken@0
   875
{
slouken@1666
   876
    if (surface == NULL) {
slouken@1662
   877
        return;
slouken@1662
   878
    }
slouken@1662
   879
    if (--surface->refcount > 0) {
slouken@1662
   880
        return;
slouken@1662
   881
    }
slouken@1662
   882
    while (surface->locked > 0) {
slouken@1668
   883
        SDL_UnlockSurface(surface);
slouken@1662
   884
    }
slouken@1672
   885
    if (surface->flags & SDL_RLEACCEL) {
slouken@1668
   886
        SDL_UnRLESurface(surface, 0);
slouken@1662
   887
    }
slouken@1662
   888
    if (surface->format) {
slouken@1668
   889
        SDL_FreeFormat(surface->format);
slouken@1662
   890
        surface->format = NULL;
slouken@1662
   891
    }
slouken@1662
   892
    if (surface->map != NULL) {
slouken@1668
   893
        SDL_FreeBlitMap(surface->map);
slouken@1662
   894
        surface->map = NULL;
slouken@1662
   895
    }
slouken@1672
   896
    /* Should we destroy the texture too?
slouken@1672
   897
       if (surface->flags & SDL_HWSURFACE) {
slouken@1672
   898
       SDL_DestroyTexture((SDL_TextureID)surface->lock_data);
slouken@1672
   899
       }
slouken@1672
   900
     */
slouken@1662
   901
    if (surface->pixels && ((surface->flags & SDL_PREALLOC) != SDL_PREALLOC)) {
slouken@1668
   902
        SDL_free(surface->pixels);
slouken@1662
   903
    }
slouken@1668
   904
    SDL_free(surface);
slouken@0
   905
#ifdef CHECK_LEAKS
slouken@1662
   906
    --surfaces_allocated;
slouken@0
   907
#endif
slouken@0
   908
}
slouken@1662
   909
slouken@1662
   910
/* vi: set ts=4 sw=4 expandtab: */