src/video/SDL_surface.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 06 Dec 2014 00:17:52 +0100
changeset 9267 6fe4583d7e1f
parent 9080 2e4e71ec140f
permissions -rw-r--r--
Fixed warnings about possible loss of data in conversion.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
     2
  Simple DirectMedia Layer
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5515
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    21
#include "../SDL_internal.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
#include "SDL_video.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
#include "SDL_sysvideo.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#include "SDL_blit.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
#include "SDL_RLEaccel_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include "SDL_pixels_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
/* Public routines */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
 * Create an empty RGB surface of the appropriate depth
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    33
SDL_Surface *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    34
SDL_CreateRGBSurface(Uint32 flags,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    35
                     int width, int height, int depth,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    36
                     Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    38
    SDL_Surface *surface;
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    39
    Uint32 format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
2807
365fe1a2aad5 The SDL_RLEACCEL flag is respected in SDL_ConvertSurface(), per the docs.
Sam Lantinga <slouken@libsdl.org>
parents: 2787
diff changeset
    41
    /* The flags are no longer used, make the compiler happy */
4461
39c22a953456 Make the compiler even happier
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
    42
    (void)flags;
2807
365fe1a2aad5 The SDL_RLEACCEL flag is respected in SDL_ConvertSurface(), per the docs.
Sam Lantinga <slouken@libsdl.org>
parents: 2787
diff changeset
    43
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    44
    /* Get the pixel format */
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    45
    format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    46
    if (format == SDL_PIXELFORMAT_UNKNOWN) {
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    47
        SDL_SetError("Unknown pixel format");
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    48
        return NULL;
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    49
    }
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    50
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    51
    /* Allocate the surface */
1920
8a162bfdc838 Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
    52
    surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    53
    if (surface == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    54
        SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    55
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    56
    }
940
bb1588ebe47b Date: Sat, 10 Jul 2004 21:02:33 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 915
diff changeset
    57
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    58
    surface->format = SDL_AllocFormat(format);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    59
    if (!surface->format) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    60
        SDL_FreeSurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    61
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    62
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    63
    surface->w = width;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    64
    surface->h = height;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    65
    surface->pitch = SDL_CalculatePitch(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    66
    SDL_SetClipRect(surface, NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    67
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    68
    if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    69
        SDL_Palette *palette =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    70
            SDL_AllocPalette((1 << surface->format->BitsPerPixel));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    71
        if (!palette) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    72
            SDL_FreeSurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    73
            return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    74
        }
5438
b705640cb34a Fixed bitmap order interpretation; SDL defaults to MSB ordering so a bitstream corresponds to a pixel stream.
Sam Lantinga <slouken@libsdl.org>
parents: 5375
diff changeset
    75
        if (palette->ncolors == 2) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    76
            /* Create a black and white bitmap palette */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    77
            palette->colors[0].r = 0xFF;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    78
            palette->colors[0].g = 0xFF;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    79
            palette->colors[0].b = 0xFF;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    80
            palette->colors[1].r = 0x00;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    81
            palette->colors[1].g = 0x00;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    82
            palette->colors[1].b = 0x00;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    83
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    84
        SDL_SetSurfacePalette(surface, palette);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    85
        SDL_FreePalette(palette);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    86
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    87
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    88
    /* Get the pixels */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    89
    if (surface->w && surface->h) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    90
        surface->pixels = SDL_malloc(surface->h * surface->pitch);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    91
        if (!surface->pixels) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    92
            SDL_FreeSurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    93
            SDL_OutOfMemory();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    94
            return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    95
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    96
        /* This is important for bitmaps */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    97
        SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
    98
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    99
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   100
    /* Allocate an empty mapping */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   101
    surface->map = SDL_AllocBlitMap();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   102
    if (!surface->map) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   103
        SDL_FreeSurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   104
        return NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   105
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   106
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   107
    /* By default surface with an alpha mask are set up for blending */
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   108
    if (Amask) {
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   109
        SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   110
    }
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   111
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   112
    /* The surface is ready to go */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   113
    surface->refcount = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   114
    return surface;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   115
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   116
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   117
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   118
 * Create an RGB surface from an existing memory buffer
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   119
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   120
SDL_Surface *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   121
SDL_CreateRGBSurfaceFrom(void *pixels,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   122
                         int width, int height, int depth, int pitch,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   123
                         Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   124
                         Uint32 Amask)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   125
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   126
    SDL_Surface *surface;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   127
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   128
    surface =
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   129
        SDL_CreateRGBSurface(0, 0, 0, depth, Rmask, Gmask, Bmask, Amask);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   130
    if (surface != NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   131
        surface->flags |= SDL_PREALLOC;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   132
        surface->pixels = pixels;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   133
        surface->w = width;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   134
        surface->h = height;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   135
        surface->pitch = pitch;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   136
        SDL_SetClipRect(surface, NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   137
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   138
    return surface;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   139
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   140
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   141
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   142
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   143
{
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   144
    if (!surface) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
   145
        return SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   146
    }
8990
7e3f54f2b16f Fixed bug 2628 - invalidate surface->map in SDL_SurfacePalette()
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   147
    if (SDL_SetPixelFormatPalette(surface->format, palette) < 0) {
7e3f54f2b16f Fixed bug 2628 - invalidate surface->map in SDL_SurfacePalette()
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   148
        return -1;
7e3f54f2b16f Fixed bug 2628 - invalidate surface->map in SDL_SurfacePalette()
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   149
    }
7e3f54f2b16f Fixed bug 2628 - invalidate surface->map in SDL_SurfacePalette()
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   150
    SDL_InvalidateMap(surface->map);
7e3f54f2b16f Fixed bug 2628 - invalidate surface->map in SDL_SurfacePalette()
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   151
7e3f54f2b16f Fixed bug 2628 - invalidate surface->map in SDL_SurfacePalette()
Sam Lantinga <slouken@libsdl.org>
parents: 8149
diff changeset
   152
    return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   153
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   154
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   155
int
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   156
SDL_SetSurfaceRLE(SDL_Surface * surface, int flag)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   157
{
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   158
    int flags;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   159
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   160
    if (!surface) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   161
        return -1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   162
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   163
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   164
    flags = surface->map->info.flags;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   165
    if (flag) {
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   166
        surface->map->info.flags |= SDL_COPY_RLE_DESIRED;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   167
    } else {
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   168
        surface->map->info.flags &= ~SDL_COPY_RLE_DESIRED;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   169
    }
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   170
    if (surface->map->info.flags != flags) {
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   171
        SDL_InvalidateMap(surface->map);
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   172
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   173
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   174
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   175
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   176
int
3560
5543db4239e6 The SDL 1.3 tests have been cleaned up not to include any 1.2 compatibility code.
Sam Lantinga <slouken@libsdl.org>
parents: 3436
diff changeset
   177
SDL_SetColorKey(SDL_Surface * surface, int flag, Uint32 key)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   178
{
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   179
    int flags;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   180
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   181
    if (!surface) {
7363
75b8a207cb65 Fixed bug 1923 - Crash with SDL_SetColorKey
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   182
        return SDL_InvalidParamError("surface");
75b8a207cb65 Fixed bug 1923 - Crash with SDL_SetColorKey
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   183
    }
75b8a207cb65 Fixed bug 1923 - Crash with SDL_SetColorKey
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   184
7482
249d8ecbbb7d Fixed some compiler warnings that Visual Studio reported.
Ryan C. Gordon <icculus@icculus.org>
parents: 7363
diff changeset
   185
    if (surface->format->palette && key >= ((Uint32) surface->format->palette->ncolors)) {
7363
75b8a207cb65 Fixed bug 1923 - Crash with SDL_SetColorKey
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   186
        return SDL_InvalidParamError("key");
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   187
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   188
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   189
    if (flag & SDL_RLEACCEL) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   190
        SDL_SetSurfaceRLE(surface, 1);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   191
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   192
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   193
    flags = surface->map->info.flags;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   194
    if (flag) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   195
        surface->map->info.flags |= SDL_COPY_COLORKEY;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   196
        surface->map->info.colorkey = key;
7023
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   197
        if (surface->format->palette) {
7024
72cb3e205571 We're using the alpha component of the palette entries, let's name it appropriately.
Sam Lantinga <slouken@libsdl.org>
parents: 7023
diff changeset
   198
            surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_TRANSPARENT;
7025
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   199
            ++surface->format->palette->version;
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   200
            if (!surface->format->palette->version) {
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   201
                surface->format->palette->version = 1;
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   202
            }
7023
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   203
        }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   204
    } else {
7023
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   205
        if (surface->format->palette) {
7024
72cb3e205571 We're using the alpha component of the palette entries, let's name it appropriately.
Sam Lantinga <slouken@libsdl.org>
parents: 7023
diff changeset
   206
            surface->format->palette->colors[surface->map->info.colorkey].a = SDL_ALPHA_OPAQUE;
7025
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   207
            ++surface->format->palette->version;
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   208
            if (!surface->format->palette->version) {
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   209
                surface->format->palette->version = 1;
4eb8ac58642c Make sure we bump the palette version when we modify it.
Sam Lantinga <slouken@libsdl.org>
parents: 7024
diff changeset
   210
            }
7023
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   211
        }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   212
        surface->map->info.flags &= ~SDL_COPY_COLORKEY;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   213
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   214
    if (surface->map->info.flags != flags) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   215
        SDL_InvalidateMap(surface->map);
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   216
    }
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   217
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   218
    return 0;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   219
}
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   220
3103
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   221
int
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   222
SDL_GetColorKey(SDL_Surface * surface, Uint32 * key)
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   223
{
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   224
    if (!surface) {
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   225
        return -1;
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   226
    }
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   227
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   228
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY)) {
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   229
        return -1;
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   230
    }
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   231
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   232
    if (key) {
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   233
        *key = surface->map->info.colorkey;
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   234
    }
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   235
    return 0;
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   236
}
7be21a78777e Added SDL_GetColorKey()
Sam Lantinga <slouken@libsdl.org>
parents: 3013
diff changeset
   237
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   238
/* This is a fairly slow function to switch from colorkey to alpha */
2787
93764fe8601a It turns out both the software and the OpenGL renderer had the same problem.
Sam Lantinga <slouken@libsdl.org>
parents: 2786
diff changeset
   239
static void
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   240
SDL_ConvertColorkeyToAlpha(SDL_Surface * surface)
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   241
{
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   242
    int x, y;
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   243
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   244
    if (!surface) {
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   245
        return;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   246
    }
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   247
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   248
    if (!(surface->map->info.flags & SDL_COPY_COLORKEY) ||
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   249
        !surface->format->Amask) {
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   250
        return;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   251
    }
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   252
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   253
    SDL_LockSurface(surface);
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   254
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   255
    switch (surface->format->BytesPerPixel) {
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   256
    case 2:
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   257
        {
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   258
            Uint16 *row, *spot;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   259
            Uint16 ckey = (Uint16) surface->map->info.colorkey;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   260
            Uint16 mask = (Uint16) (~surface->format->Amask);
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   261
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   262
            /* Ignore alpha in colorkey comparison */
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   263
            ckey &= mask;
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   264
            row = (Uint16 *) surface->pixels;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   265
            for (y = surface->h; y--;) {
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   266
                spot = row;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   267
                for (x = surface->w; x--;) {
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   268
                    if ((*spot & mask) == ckey) {
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   269
                        *spot &= mask;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   270
                    }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   271
                    ++spot;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   272
                }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   273
                row += surface->pitch / 2;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   274
            }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   275
        }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   276
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   277
    case 3:
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   278
        /* FIXME */
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   279
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   280
    case 4:
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   281
        {
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   282
            Uint32 *row, *spot;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   283
            Uint32 ckey = surface->map->info.colorkey;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   284
            Uint32 mask = ~surface->format->Amask;
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   285
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   286
            /* Ignore alpha in colorkey comparison */
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   287
            ckey &= mask;
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   288
            row = (Uint32 *) surface->pixels;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   289
            for (y = surface->h; y--;) {
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   290
                spot = row;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   291
                for (x = surface->w; x--;) {
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   292
                    if ((*spot & mask) == ckey) {
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   293
                        *spot &= mask;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   294
                    }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   295
                    ++spot;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   296
                }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   297
                row += surface->pitch / 4;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   298
            }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   299
        }
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   300
        break;
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   301
    }
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   302
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   303
    SDL_UnlockSurface(surface);
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   304
2786
Sam Lantinga <slouken@libsdl.org>
parents: 2785
diff changeset
   305
    SDL_SetColorKey(surface, 0, 0);
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   306
    SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
2785
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   307
}
fa1095d42a5b Fixed bug with converting colorkey surface to texture
Sam Lantinga <slouken@libsdl.org>
parents: 2735
diff changeset
   308
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   309
int
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   310
SDL_SetSurfaceColorMod(SDL_Surface * surface, Uint8 r, Uint8 g, Uint8 b)
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   311
{
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   312
    int flags;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   313
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   314
    if (!surface) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   315
        return -1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   316
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   317
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   318
    surface->map->info.r = r;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   319
    surface->map->info.g = g;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   320
    surface->map->info.b = b;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   321
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   322
    flags = surface->map->info.flags;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   323
    if (r != 0xFF || g != 0xFF || b != 0xFF) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   324
        surface->map->info.flags |= SDL_COPY_MODULATE_COLOR;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   325
    } else {
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   326
        surface->map->info.flags &= ~SDL_COPY_MODULATE_COLOR;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   327
    }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   328
    if (surface->map->info.flags != flags) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   329
        SDL_InvalidateMap(surface->map);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   330
    }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   331
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   332
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   333
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   334
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   335
int
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   336
SDL_GetSurfaceColorMod(SDL_Surface * surface, Uint8 * r, Uint8 * g, Uint8 * b)
431
41cadcba32e8 Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents: 422
diff changeset
   337
{
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   338
    if (!surface) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   339
        return -1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   340
    }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   341
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   342
    if (r) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   343
        *r = surface->map->info.r;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   344
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   345
    if (g) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   346
        *g = surface->map->info.g;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   347
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   348
    if (b) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   349
        *b = surface->map->info.b;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   350
    }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   351
    return 0;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   352
}
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   353
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   354
int
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   355
SDL_SetSurfaceAlphaMod(SDL_Surface * surface, Uint8 alpha)
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   356
{
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   357
    int flags;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   358
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   359
    if (!surface) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   360
        return -1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   361
    }
431
41cadcba32e8 Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents: 422
diff changeset
   362
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   363
    surface->map->info.a = alpha;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   364
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   365
    flags = surface->map->info.flags;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   366
    if (alpha != 0xFF) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   367
        surface->map->info.flags |= SDL_COPY_MODULATE_ALPHA;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   368
    } else {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   369
        surface->map->info.flags &= ~SDL_COPY_MODULATE_ALPHA;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   370
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   371
    if (surface->map->info.flags != flags) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   372
        SDL_InvalidateMap(surface->map);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   373
    }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   374
    return 0;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   375
}
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   376
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   377
int
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   378
SDL_GetSurfaceAlphaMod(SDL_Surface * surface, Uint8 * alpha)
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   379
{
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   380
    if (!surface) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   381
        return -1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   382
    }
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   383
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   384
    if (alpha) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   385
        *alpha = surface->map->info.a;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   386
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   387
    return 0;
431
41cadcba32e8 Fixed SDL_DisplayFormatAlpha() on RGB surfaces with alpha
Sam Lantinga <slouken@libsdl.org>
parents: 422
diff changeset
   388
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   389
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   390
int
4929
aa8888658021 Use the enumerated type for blend and scale mode instead of int
Sam Lantinga <slouken@libsdl.org>
parents: 4491
diff changeset
   391
SDL_SetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode blendMode)
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   392
{
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   393
    int flags, status;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   394
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   395
    if (!surface) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   396
        return -1;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   397
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   398
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   399
    status = 0;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   400
    flags = surface->map->info.flags;
5184
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   401
    surface->map->info.flags &=
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   402
        ~(SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD);
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   403
    switch (blendMode) {
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   404
    case SDL_BLENDMODE_NONE:
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   405
        break;
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   406
    case SDL_BLENDMODE_BLEND:
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   407
        surface->map->info.flags |= SDL_COPY_BLEND;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   408
        break;
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   409
    case SDL_BLENDMODE_ADD:
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   410
        surface->map->info.flags |= SDL_COPY_ADD;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   411
        break;
5184
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   412
    case SDL_BLENDMODE_MOD:
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   413
        surface->map->info.flags |= SDL_COPY_MOD;
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   414
        break;
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   415
    default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
   416
        status = SDL_Unsupported();
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   417
        break;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   418
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   419
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   420
    if (surface->map->info.flags != flags) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   421
        SDL_InvalidateMap(surface->map);
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   422
    }
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   423
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   424
    return status;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   425
}
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   426
2267
c785543d1843 Okay, still some bugs, but everything builds again...
Sam Lantinga <slouken@libsdl.org>
parents: 2266
diff changeset
   427
int
4929
aa8888658021 Use the enumerated type for blend and scale mode instead of int
Sam Lantinga <slouken@libsdl.org>
parents: 4491
diff changeset
   428
SDL_GetSurfaceBlendMode(SDL_Surface * surface, SDL_BlendMode *blendMode)
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   429
{
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   430
    if (!surface) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   431
        return -1;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   432
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   433
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   434
    if (!blendMode) {
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   435
        return 0;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   436
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   437
5184
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   438
    switch (surface->map->
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   439
            info.flags & (SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD)) {
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   440
    case SDL_COPY_BLEND:
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   441
        *blendMode = SDL_BLENDMODE_BLEND;
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   442
        break;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   443
    case SDL_COPY_ADD:
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   444
        *blendMode = SDL_BLENDMODE_ADD;
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   445
        break;
5184
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   446
    case SDL_COPY_MOD:
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   447
        *blendMode = SDL_BLENDMODE_MOD;
d976b67150c5 Restored SDL_BLENDMODE_MOD for MAME
Sam Lantinga <slouken@libsdl.org>
parents: 5140
diff changeset
   448
        break;
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   449
    default:
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   450
        *blendMode = SDL_BLENDMODE_NONE;
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   451
        break;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   452
    }
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   453
    return 0;
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   454
}
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   455
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   456
SDL_bool
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   457
SDL_SetClipRect(SDL_Surface * surface, const SDL_Rect * rect)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   458
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   459
    SDL_Rect full_rect;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   460
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   461
    /* Don't do anything if there's no surface to act on */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   462
    if (!surface) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   463
        return SDL_FALSE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   464
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   465
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   466
    /* Set up the full surface rectangle */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   467
    full_rect.x = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   468
    full_rect.y = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   469
    full_rect.w = surface->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   470
    full_rect.h = surface->h;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   471
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   472
    /* Set the clipping rectangle */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   473
    if (!rect) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   474
        surface->clip_rect = full_rect;
4966
a130bc2f0a18 Fixed return value
Sam Lantinga <slouken@libsdl.org>
parents: 4949
diff changeset
   475
        return SDL_TRUE;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   476
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   477
    return SDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   478
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   479
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   480
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   481
SDL_GetClipRect(SDL_Surface * surface, SDL_Rect * rect)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   482
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   483
    if (surface && rect) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   484
        *rect = surface->clip_rect;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   485
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   486
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   487
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   488
/*
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   489
 * Set up a blit between two surfaces -- split into three parts:
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   490
 * The upper part, SDL_UpperBlit(), performs clipping and rectangle
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   491
 * verification.  The lower part is a pointer to a low level
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   492
 * accelerated blitting function.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   493
 *
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   494
 * These parts are separated out and each used internally by this
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   495
 * library in the optimimum places.  They are exported so that if
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   496
 * you know exactly what you are doing, you can optimize your code
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   497
 * by calling the one(s) you need.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   498
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   499
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   500
SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   501
              SDL_Surface * dst, SDL_Rect * dstrect)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   502
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   503
    /* Check to make sure the blit mapping is valid */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   504
    if ((src->map->dst != dst) ||
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   505
        (dst->format->palette &&
6166
0bc1f6da9c74 Fixed bug 1224 - Blit map not updated if source palette changed
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   506
         src->map->dst_palette_version != dst->format->palette->version) ||
0bc1f6da9c74 Fixed bug 1224 - Blit map not updated if source palette changed
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   507
        (src->format->palette &&
0bc1f6da9c74 Fixed bug 1224 - Blit map not updated if source palette changed
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   508
         src->map->src_palette_version != src->format->palette->version)) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   509
        if (SDL_MapSurface(src, dst) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   510
            return (-1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   511
        }
2328
91e601d9df8b re: bug#563. checking in some commented out trace code and a fix so that the in testalpha.c the background only flashes when alpha == 255. The problem that is being
Bob Pendleton <bob@pendleton.com>
parents: 2267
diff changeset
   512
        /* just here for debugging */
2329
c1a390f6a819 ran make indent
Bob Pendleton <bob@pendleton.com>
parents: 2328
diff changeset
   513
/*         printf */
c1a390f6a819 ran make indent
Bob Pendleton <bob@pendleton.com>
parents: 2328
diff changeset
   514
/*             ("src = 0x%08X src->flags = %08X src->map->info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->map->info.flags = %08X\nsrc->map->blit = 0x%08x\n", */
c1a390f6a819 ran make indent
Bob Pendleton <bob@pendleton.com>
parents: 2328
diff changeset
   515
/*              src, dst->flags, src->map->info.flags, dst, dst->flags, */
c1a390f6a819 ran make indent
Bob Pendleton <bob@pendleton.com>
parents: 2328
diff changeset
   516
/*              dst->map->info.flags, src->map->blit); */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   517
    }
2257
340942cfda48 Moved the colorkey and per-surface alpha into the blit info,
Sam Lantinga <slouken@libsdl.org>
parents: 2253
diff changeset
   518
    return (src->map->blit(src, srcrect, dst, dstrect));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   519
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   520
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   521
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   522
int
4949
68da5a234bba The source rectangle isn't modified in SDL_UpperBlit
Sam Lantinga <slouken@libsdl.org>
parents: 4929
diff changeset
   523
SDL_UpperBlit(SDL_Surface * src, const SDL_Rect * srcrect,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   524
              SDL_Surface * dst, SDL_Rect * dstrect)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   525
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   526
    SDL_Rect fulldst;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   527
    int srcx, srcy, w, h;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   528
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   529
    /* Make sure the surfaces aren't locked */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   530
    if (!src || !dst) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
   531
        return SDL_SetError("SDL_UpperBlit: passed a NULL surface");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   532
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   533
    if (src->locked || dst->locked) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
   534
        return SDL_SetError("Surfaces must not be locked during blit");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   535
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   536
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   537
    /* If the destination rectangle is NULL, use the entire dest surface */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   538
    if (dstrect == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   539
        fulldst.x = fulldst.y = 0;
7511
4ce3cc948a00 A little cleanup on the cleanup, just for consistency.
Sam Lantinga <slouken@libsdl.org>
parents: 7508
diff changeset
   540
        fulldst.w = dst->w;
4ce3cc948a00 A little cleanup on the cleanup, just for consistency.
Sam Lantinga <slouken@libsdl.org>
parents: 7508
diff changeset
   541
        fulldst.h = dst->h;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   542
        dstrect = &fulldst;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   543
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   544
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   545
    /* clip the source rectangle to the source surface */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   546
    if (srcrect) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   547
        int maxw, maxh;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   548
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   549
        srcx = srcrect->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   550
        w = srcrect->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   551
        if (srcx < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   552
            w += srcx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   553
            dstrect->x -= srcx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   554
            srcx = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   555
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   556
        maxw = src->w - srcx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   557
        if (maxw < w)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   558
            w = maxw;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   559
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   560
        srcy = srcrect->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   561
        h = srcrect->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   562
        if (srcy < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   563
            h += srcy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   564
            dstrect->y -= srcy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   565
            srcy = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   566
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   567
        maxh = src->h - srcy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   568
        if (maxh < h)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   569
            h = maxh;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   570
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   571
    } else {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   572
        srcx = srcy = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   573
        w = src->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   574
        h = src->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   575
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   576
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   577
    /* clip the destination rectangle against the clip rectangle */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   578
    {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   579
        SDL_Rect *clip = &dst->clip_rect;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   580
        int dx, dy;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   581
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   582
        dx = clip->x - dstrect->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   583
        if (dx > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   584
            w -= dx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   585
            dstrect->x += dx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   586
            srcx += dx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   587
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   588
        dx = dstrect->x + w - clip->x - clip->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   589
        if (dx > 0)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   590
            w -= dx;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   591
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   592
        dy = clip->y - dstrect->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   593
        if (dy > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   594
            h -= dy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   595
            dstrect->y += dy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   596
            srcy += dy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   597
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   598
        dy = dstrect->y + h - clip->y - clip->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   599
        if (dy > 0)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   600
            h -= dy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   601
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   602
7834
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   603
    /* Switch back to a fast blit if we were previously stretching */
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   604
    if (src->map->info.flags & SDL_COPY_NEAREST) {
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   605
        src->map->info.flags &= ~SDL_COPY_NEAREST;
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   606
        SDL_InvalidateMap(src->map);
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   607
    }
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   608
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   609
    if (w > 0 && h > 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   610
        SDL_Rect sr;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   611
        sr.x = srcx;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   612
        sr.y = srcy;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   613
        sr.w = dstrect->w = w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   614
        sr.h = dstrect->h = h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   615
        return SDL_LowerBlit(src, &sr, dst, dstrect);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   616
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   617
    dstrect->w = dstrect->h = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   618
    return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   619
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   620
5296
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   621
int
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   622
SDL_UpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   623
              SDL_Surface * dst, SDL_Rect * dstrect)
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   624
{
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   625
    double src_x0, src_y0, src_x1, src_y1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   626
    double dst_x0, dst_y0, dst_x1, dst_y1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   627
    SDL_Rect final_src, final_dst;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   628
    double scaling_w, scaling_h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   629
    int src_w, src_h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   630
    int dst_w, dst_h;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   631
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   632
    /* Make sure the surfaces aren't locked */
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   633
    if (!src || !dst) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
   634
        return SDL_SetError("SDL_UpperBlitScaled: passed a NULL surface");
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   635
    }
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   636
    if (src->locked || dst->locked) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
   637
        return SDL_SetError("Surfaces must not be locked during blit");
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   638
    }
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   639
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   640
    if (NULL == srcrect) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   641
        src_w = src->w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   642
        src_h = src->h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   643
    } else {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   644
        src_w = srcrect->w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   645
        src_h = srcrect->h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   646
    }
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   647
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   648
    if (NULL == dstrect) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   649
        dst_w = dst->w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   650
        dst_h = dst->h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   651
    } else {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   652
        dst_w = dstrect->w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   653
        dst_h = dstrect->h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   654
    }
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   655
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   656
    if (dst_w == src_w && dst_h == src_h) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   657
        /* No scaling, defer to regular blit */
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   658
        return SDL_BlitSurface(src, srcrect, dst, dstrect);
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   659
    }
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   660
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   661
    scaling_w = (double)dst_w / src_w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   662
    scaling_h = (double)dst_h / src_h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   663
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   664
    if (NULL == dstrect) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   665
        dst_x0 = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   666
        dst_y0 = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   667
        dst_x1 = dst_w - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   668
        dst_y1 = dst_h - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   669
    } else {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   670
        dst_x0 = dstrect->x;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   671
        dst_y0 = dstrect->y;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   672
        dst_x1 = dst_x0 + dst_w - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   673
        dst_y1 = dst_y0 + dst_h - 1;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   674
    }
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   675
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   676
    if (NULL == srcrect) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   677
        src_x0 = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   678
        src_y0 = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   679
        src_x1 = src_w - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   680
        src_y1 = src_h - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   681
    } else {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   682
        src_x0 = srcrect->x;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   683
        src_y0 = srcrect->y;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   684
        src_x1 = src_x0 + src_w - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   685
        src_y1 = src_y0 + src_h - 1;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   686
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   687
        /* Clip source rectangle to the source surface */
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   688
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   689
        if (src_x0 < 0) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   690
            dst_x0 -= src_x0 * scaling_w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   691
            src_x0 = 0;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   692
        }
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   693
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   694
        if (src_x1 >= src->w) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   695
            dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   696
            src_x1 = src->w - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   697
        }
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   698
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   699
        if (src_y0 < 0) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   700
            dst_y0 -= src_y0 * scaling_h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   701
            src_y0 = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   702
        }
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   703
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   704
        if (src_y1 >= src->h) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   705
            dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   706
            src_y1 = src->h - 1;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   707
        }
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   708
    }
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   709
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   710
    /* Clip destination rectangle to the clip rectangle */
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   711
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   712
    /* Translate to clip space for easier calculations */
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   713
    dst_x0 -= dst->clip_rect.x;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   714
    dst_x1 -= dst->clip_rect.x;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   715
    dst_y0 -= dst->clip_rect.y;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   716
    dst_y1 -= dst->clip_rect.y;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   717
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   718
    if (dst_x0 < 0) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   719
        src_x0 -= dst_x0 / scaling_w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   720
        dst_x0 = 0;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   721
    }
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   722
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   723
    if (dst_x1 >= dst->clip_rect.w) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   724
        src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   725
        dst_x1 = dst->clip_rect.w - 1;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   726
    }
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   727
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   728
    if (dst_y0 < 0) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   729
        src_y0 -= dst_y0 / scaling_h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   730
        dst_y0 = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   731
    }
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   732
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   733
    if (dst_y1 >= dst->clip_rect.h) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   734
        src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   735
        dst_y1 = dst->clip_rect.h - 1;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   736
    }
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   737
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   738
    /* Translate back to surface coordinates */
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   739
    dst_x0 += dst->clip_rect.x;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   740
    dst_x1 += dst->clip_rect.x;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   741
    dst_y0 += dst->clip_rect.y;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   742
    dst_y1 += dst->clip_rect.y;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   743
9267
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   744
    final_src.x = (int)SDL_floor(src_x0 + 0.5);
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   745
    final_src.y = (int)SDL_floor(src_y0 + 0.5);
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   746
    final_src.w = (int)SDL_floor(src_x1 - src_x0 + 1.5);
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   747
    final_src.h = (int)SDL_floor(src_y1 - src_y0 + 1.5);
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   748
9267
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   749
    final_dst.x = (int)SDL_floor(dst_x0 + 0.5);
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   750
    final_dst.y = (int)SDL_floor(dst_y0 + 0.5);
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   751
    final_dst.w = (int)SDL_floor(dst_x1 - dst_x0 + 1.5);
6fe4583d7e1f Fixed warnings about possible loss of data in conversion.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9080
diff changeset
   752
    final_dst.h = (int)SDL_floor(dst_y1 - dst_y0 + 1.5);
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   753
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   754
    if (final_dst.w < 0)
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   755
        final_dst.w = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   756
    if (final_dst.h < 0)
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   757
        final_dst.h = 0;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   758
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   759
    if (dstrect)
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   760
        *dstrect = final_dst;
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   761
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   762
    if (final_dst.w == 0 || final_dst.h == 0 ||
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   763
        final_src.w <= 0 || final_src.h <= 0) {
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   764
        /* No-op. */
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   765
        return 0;
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   766
    }
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   767
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   768
    return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
5499
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   769
}
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   770
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   771
/**
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   772
 *  This is a semi-private blit function and it performs low-level surface
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   773
 *  scaled blitting only.
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   774
 */
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   775
int
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   776
SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
2b8e6d1e3817 Split SDL_BlitScaled into SDL_UpperBlitScaled and SDL_LowerBlitScaled.
Ken Rogoway <ken@rogoway.com>
parents: 5438
diff changeset
   777
                SDL_Surface * dst, SDL_Rect * dstrect)
5296
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   778
{
6526
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   779
    static const Uint32 complex_copy_flags = (
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   780
        SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   781
        SDL_COPY_BLEND | SDL_COPY_ADD | SDL_COPY_MOD |
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   782
        SDL_COPY_COLORKEY
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   783
    );
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   784
7834
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   785
    if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   786
        src->map->info.flags |= SDL_COPY_NEAREST;
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   787
        SDL_InvalidateMap(src->map);
e22726c82922 Fixed bug 2123 - SDL_BlitScaled crashes in src/video/SDL_blit_N.c:2145
Sam Lantinga <slouken@libsdl.org>
parents: 7725
diff changeset
   788
    }
5296
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   789
6526
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   790
    if ( !(src->map->info.flags & complex_copy_flags) &&
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   791
         src->format->format == dst->format->format &&
6526
e9c3a540c52f Fixed scaled blitting for complex blit operations (e.g. color modulation and alpha blending)
Sam Lantinga <slouken@libsdl.org>
parents: 6407
diff changeset
   792
         !SDL_ISPIXELFORMAT_INDEXED(src->format->format) ) {
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   793
        return SDL_SoftStretch( src, srcrect, dst, dstrect );
5296
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   794
    } else {
9076
5e713281410c Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly
Sam Lantinga <slouken@libsdl.org>
parents: 9046
diff changeset
   795
        return SDL_LowerBlit( src, srcrect, dst, dstrect );
5296
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   796
    }
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   797
}
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   798
48067bfc300c Software scaling support. Not very fast, but it seems to work.
Ken Rogoway <ken@rogoway.com>
parents: 5288
diff changeset
   799
/*
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   800
 * Lock a surface to directly access the pixels
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   801
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   802
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   803
SDL_LockSurface(SDL_Surface * surface)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   804
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   805
    if (!surface->locked) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   806
        /* Perform the lock */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   807
        if (surface->flags & SDL_RLEACCEL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   808
            SDL_UnRLESurface(surface, 1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   809
            surface->flags |= SDL_RLEACCEL;     /* save accel'd state */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   810
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   811
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   812
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   813
    /* Increment the surface lock count, for recursive locks */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   814
    ++surface->locked;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   815
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   816
    /* Ready to go.. */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   817
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   818
}
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   819
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   820
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   821
 * Unlock a previously locked surface
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   822
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   823
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   824
SDL_UnlockSurface(SDL_Surface * surface)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   825
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   826
    /* Only perform an unlock if we are locked */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   827
    if (!surface->locked || (--surface->locked > 0)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   828
        return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   829
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   830
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   831
    /* Update RLE encoded surface with new data */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   832
    if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   833
        surface->flags &= ~SDL_RLEACCEL;        /* stop lying */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   834
        SDL_RLESurface(surface);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   835
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   836
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   837
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   838
/*
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   839
 * Convert a surface into the specified pixel format.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   840
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   841
SDL_Surface *
7725
c61ab95113fe The SDL_PixelFormat* passed to SDL_ConvertSurface() should be const.
Ryan C. Gordon <icculus@icculus.org>
parents: 7719
diff changeset
   842
SDL_ConvertSurface(SDL_Surface * surface, const SDL_PixelFormat * format,
2807
365fe1a2aad5 The SDL_RLEACCEL flag is respected in SDL_ConvertSurface(), per the docs.
Sam Lantinga <slouken@libsdl.org>
parents: 2787
diff changeset
   843
                   Uint32 flags)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   844
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   845
    SDL_Surface *convert;
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   846
    Uint32 copy_flags;
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   847
    SDL_Color copy_color;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   848
    SDL_Rect bounds;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   849
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   850
    /* Check for empty destination palette! (results in empty image) */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   851
    if (format->palette != NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   852
        int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   853
        for (i = 0; i < format->palette->ncolors; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   854
            if ((format->palette->colors[i].r != 0xFF) ||
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   855
                (format->palette->colors[i].g != 0xFF) ||
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   856
                (format->palette->colors[i].b != 0xFF))
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   857
                break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   858
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   859
        if (i == format->palette->ncolors) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   860
            SDL_SetError("Empty destination palette");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   861
            return (NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   862
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   863
    }
264
c9cd3b564e4b *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   864
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   865
    /* Create a new surface with the desired format */
2807
365fe1a2aad5 The SDL_RLEACCEL flag is respected in SDL_ConvertSurface(), per the docs.
Sam Lantinga <slouken@libsdl.org>
parents: 2787
diff changeset
   866
    convert = SDL_CreateRGBSurface(flags, surface->w, surface->h,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   867
                                   format->BitsPerPixel, format->Rmask,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   868
                                   format->Gmask, format->Bmask,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   869
                                   format->Amask);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   870
    if (convert == NULL) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   871
        return (NULL);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   872
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   873
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   874
    /* Copy the palette if any */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   875
    if (format->palette && convert->format->palette) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   876
        SDL_memcpy(convert->format->palette->colors,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   877
                   format->palette->colors,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   878
                   format->palette->ncolors * sizeof(SDL_Color));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   879
        convert->format->palette->ncolors = format->palette->ncolors;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   880
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   881
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   882
    /* Save the original copy flags */
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   883
    copy_flags = surface->map->info.flags;
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   884
    copy_color.r = surface->map->info.r;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   885
    copy_color.g = surface->map->info.g;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   886
    copy_color.b = surface->map->info.b;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   887
    copy_color.a = surface->map->info.a;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   888
    surface->map->info.r = 0xFF;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   889
    surface->map->info.g = 0xFF;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   890
    surface->map->info.b = 0xFF;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   891
    surface->map->info.a = 0xFF;
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   892
    surface->map->info.flags = 0;
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   893
    SDL_InvalidateMap(surface->map);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   894
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   895
    /* Copy over the image data */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   896
    bounds.x = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   897
    bounds.y = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   898
    bounds.w = surface->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   899
    bounds.h = surface->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   900
    SDL_LowerBlit(surface, &bounds, convert, &bounds);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   901
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   902
    /* Clean up the original surface, and update converted surface */
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   903
    convert->map->info.r = copy_color.r;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   904
    convert->map->info.g = copy_color.g;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   905
    convert->map->info.b = copy_color.b;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   906
    convert->map->info.a = copy_color.a;
2824
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   907
    convert->map->info.flags =
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   908
        (copy_flags &
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   909
         ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   910
           | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   911
           SDL_COPY_RLE_ALPHAKEY));
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   912
    surface->map->info.r = copy_color.r;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   913
    surface->map->info.g = copy_color.g;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   914
    surface->map->info.b = copy_color.b;
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   915
    surface->map->info.a = copy_color.a;
2824
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   916
    surface->map->info.flags = copy_flags;
7601
fdf89883bbaa Fixed bug 2009 - Creating texture from 32-bit surface with colorkey and per-surface alpha ignores the colorkey
Sam Lantinga <slouken@libsdl.org>
parents: 7511
diff changeset
   917
    SDL_InvalidateMap(surface->map);
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   918
    if (copy_flags & SDL_COPY_COLORKEY) {
7023
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   919
        SDL_bool set_colorkey_by_color = SDL_FALSE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   920
7023
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   921
        if (surface->format->palette) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   922
            if (format->palette &&
7023
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   923
                surface->format->palette->ncolors <= format->palette->ncolors &&
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   924
                (SDL_memcmp(surface->format->palette->colors, format->palette->colors,
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   925
                  surface->format->palette->ncolors * sizeof(SDL_Color)) == 0)) {
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   926
                /* The palette is identical, just set the same colorkey */
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   927
                SDL_SetColorKey(convert, 1, surface->map->info.colorkey);
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   928
            } else if (format->Amask) {
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   929
                /* The alpha was set in the destination from the palette */
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   930
            } else {
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   931
                set_colorkey_by_color = SDL_TRUE;
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   932
            }
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   933
        } else {
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   934
            set_colorkey_by_color = SDL_TRUE;
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   935
        }
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   936
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   937
        if (set_colorkey_by_color) {
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   938
            /* Set the colorkey by color, which needs to be unique */
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   939
            Uint8 keyR, keyG, keyB, keyA;
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   940
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   941
            SDL_GetRGBA(surface->map->info.colorkey, surface->format, &keyR,
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   942
                        &keyG, &keyB, &keyA);
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   943
            SDL_SetColorKey(convert, 1,
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   944
                            SDL_MapRGBA(convert->format, keyR, keyG, keyB, keyA));
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   945
            /* This is needed when converting for 3D texture upload */
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   946
            SDL_ConvertColorkeyToAlpha(convert);
1d49dc7b5ce9 The palette unused value is treated as alpha and updated when setting the colorkey.
Sam Lantinga <slouken@libsdl.org>
parents: 6920
diff changeset
   947
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   948
    }
2824
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   949
    SDL_SetClipRect(convert, &surface->clip_rect);
2266
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   950
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   951
    /* Enable alpha blending by default if the new surface has an
e61ad15a205f More work in progress integrating SDL_Surface and the new SDL_Texture API
Sam Lantinga <slouken@libsdl.org>
parents: 2260
diff changeset
   952
     * alpha channel or alpha modulation */
2824
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   953
    if ((surface->format->Amask && format->Amask) ||
4491
2cd7bb613a83 Turn on blending if we're converting from a surface with colorkey enabled
Sam Lantinga <slouken@libsdl.org>
parents: 4461
diff changeset
   954
        (copy_flags & (SDL_COPY_COLORKEY|SDL_COPY_MODULATE_ALPHA))) {
2884
9dde605c7540 Date: Fri, 19 Dec 2008 20:17:35 +0100
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
   955
        SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   956
    }
2824
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   957
    if ((copy_flags & SDL_COPY_RLE_DESIRED) || (flags & SDL_RLEACCEL)) {
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   958
        SDL_SetSurfaceRLE(convert, SDL_RLEACCEL);
4dba7aa7ea77 Added slow but complete blit fallback
Sam Lantinga <slouken@libsdl.org>
parents: 2807
diff changeset
   959
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   960
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   961
    /* We're ready to go! */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1456
diff changeset
   962
    return (convert);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   963
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   964
5375
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   965
SDL_Surface *
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   966
SDL_ConvertSurfaceFormat(SDL_Surface * surface, Uint32 pixel_format,
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   967
                         Uint32 flags)
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   968
{
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   969
    SDL_PixelFormat *fmt;
5515
0fcf231a6e74 NDS update
Sam Lantinga <slouken@libsdl.org>
parents: 5499
diff changeset
   970
    SDL_Surface *convert = NULL;
5375
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   971
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   972
    fmt = SDL_AllocFormat(pixel_format);
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   973
    if (fmt) {
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   974
        convert = SDL_ConvertSurface(surface, fmt, flags);
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   975
        SDL_FreeFormat(fmt);
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   976
    }
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   977
    return convert;
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   978
}
16877f74123c Implemented Cocoa_SetWindowIcon(), added SDL_ConvertSurfaceFormat()
Sam Lantinga <slouken@libsdl.org>
parents: 5296
diff changeset
   979
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   980
/*
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   981
 * Create a surface on the stack for quick blit operations
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   982
 */
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7834
diff changeset
   983
static SDL_INLINE SDL_bool
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   984
SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7037
diff changeset
   985
                         void * pixels, int pitch, SDL_Surface * surface,
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   986
                         SDL_PixelFormat * format, SDL_BlitMap * blitmap)
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   987
{
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   988
    if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   989
        SDL_SetError("Indexed pixel formats not supported");
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   990
        return SDL_FALSE;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   991
    }
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   992
    if (SDL_InitFormat(format, pixel_format) < 0) {
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   993
        return SDL_FALSE;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   994
    }
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   995
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   996
    SDL_zerop(surface);
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   997
    surface->flags = SDL_PREALLOC;
5288
d4381f3b0d1e A few fixes:
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   998
    surface->format = format;
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
   999
    surface->pixels = pixels;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1000
    surface->w = width;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1001
    surface->h = height;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1002
    surface->pitch = pitch;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1003
    /* We don't actually need to set up the clip rect for our purposes */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
  1004
    /* SDL_SetClipRect(surface, NULL); */
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1005
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1006
    /* Allocate an empty mapping */
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1007
    SDL_zerop(blitmap);
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1008
    blitmap->info.r = 0xFF;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1009
    blitmap->info.g = 0xFF;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1010
    blitmap->info.b = 0xFF;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1011
    blitmap->info.a = 0xFF;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1012
    surface->map = blitmap;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1013
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1014
    /* The surface is ready to go */
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1015
    surface->refcount = 1;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1016
    return SDL_TRUE;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1017
}
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1018
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1019
/*
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1020
 * Copy a block of pixels of one format to another format
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1021
 */
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1022
int SDL_ConvertPixels(int width, int height,
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1023
                      Uint32 src_format, const void * src, int src_pitch,
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1024
                      Uint32 dst_format, void * dst, int dst_pitch)
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1025
{
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1026
    SDL_Surface src_surface, dst_surface;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1027
    SDL_PixelFormat src_fmt, dst_fmt;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1028
    SDL_BlitMap src_blitmap, dst_blitmap;
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1029
    SDL_Rect rect;
6407
93f481657c6d Fixed compiler warning on some versions of GCC.
Ryan C. Gordon <icculus@icculus.org>
parents: 6256
diff changeset
  1030
    void *nonconst_src = (void *) src;
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1031
9046
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1032
    /* Check to make sure we are blitting somewhere, so we don't crash */
6920
a966229b4107 Don't crash if we are asked to convert a surface to a NULl buffer.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
  1033
    if (!dst) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
  1034
        return SDL_InvalidParamError("dst");
6920
a966229b4107 Don't crash if we are asked to convert a surface to a NULl buffer.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
  1035
    }
a966229b4107 Don't crash if we are asked to convert a surface to a NULl buffer.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
  1036
    if (!dst_pitch) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
  1037
        return SDL_InvalidParamError("dst_pitch");
6920
a966229b4107 Don't crash if we are asked to convert a surface to a NULl buffer.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
  1038
    }
a966229b4107 Don't crash if we are asked to convert a surface to a NULl buffer.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
  1039
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1040
    /* Fast path for same format copy */
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1041
    if (src_format == dst_format) {
9046
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1042
        int bpp, i;
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1043
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1044
        if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1045
            switch (src_format) {
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1046
            case SDL_PIXELFORMAT_YUY2:
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1047
            case SDL_PIXELFORMAT_UYVY:
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1048
            case SDL_PIXELFORMAT_YVYU:
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1049
                bpp = 2;
6663
2cbffb5aa19d Fixed bug 1635 - SDL_ConvertPixels - missing break
Sam Lantinga <slouken@libsdl.org>
parents: 6527
diff changeset
  1050
                break;
9046
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1051
            case SDL_PIXELFORMAT_YV12:
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1052
            case SDL_PIXELFORMAT_IYUV:
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1053
            case SDL_PIXELFORMAT_NV12:
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1054
            case SDL_PIXELFORMAT_NV21:
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1055
                bpp = 1;
c3ec7c3e6c24 Added NV12 and NV21 texture support for OpenGL and OpenGL ES 2.0 renderers
Sam Lantinga <slouken@libsdl.org>
parents: 8990
diff changeset
  1056
                break;
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1057
            default:
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7025
diff changeset
  1058
                return SDL_SetError("Unknown FOURCC pixel format");
3434
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: 3103
diff changeset
  1059
            }
147d6ef5be03 Added a utility function to convert blocks of pixels
Sam Lantinga <slouken@libsdl.org>
parents: