src/video/SDL_blendpoint.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Aug 2009 18:39:57 +0000
changeset 3227 458e53d8662c
parent 2901 133601e3b255
child 3536 0267b8b1595c
permissions -rw-r--r--
Clarified API documentation
slouken@2901
     1
/*
slouken@2901
     2
    SDL - Simple DirectMedia Layer
slouken@2901
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@2901
     4
slouken@2901
     5
    This library is free software; you can redistribute it and/or
slouken@2901
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2901
     7
    License as published by the Free Software Foundation; either
slouken@2901
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2901
     9
slouken@2901
    10
    This library is distributed in the hope that it will be useful,
slouken@2901
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2901
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2901
    13
    Lesser General Public License for more details.
slouken@2901
    14
slouken@2901
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2901
    16
    License along with this library; if not, write to the Free Software
slouken@2901
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2901
    18
slouken@2901
    19
    Sam Lantinga
slouken@2901
    20
    slouken@libsdl.org
slouken@2901
    21
*/
slouken@2901
    22
#include "SDL_config.h"
slouken@2901
    23
slouken@2901
    24
#include "SDL_draw.h"
slouken@2901
    25
slouken@2901
    26
static int
slouken@2901
    27
SDL_BlendPoint_RGB555(SDL_Surface * dst, int x, int y, int blendMode, Uint8 r,
slouken@2901
    28
                      Uint8 g, Uint8 b, Uint8 a)
slouken@2901
    29
{
slouken@2901
    30
    unsigned inva = 0xff - a;
slouken@2901
    31
slouken@2901
    32
    switch (blendMode) {
slouken@2901
    33
    case SDL_BLENDMODE_BLEND:
slouken@2901
    34
        DRAW_SETPIXELXY_BLEND_RGB555(x, y);
slouken@2901
    35
        break;
slouken@2901
    36
    case SDL_BLENDMODE_ADD:
slouken@2901
    37
        DRAW_SETPIXELXY_ADD_RGB555(x, y);
slouken@2901
    38
        break;
slouken@2901
    39
    case SDL_BLENDMODE_MOD:
slouken@2901
    40
        DRAW_SETPIXELXY_MOD_RGB555(x, y);
slouken@2901
    41
        break;
slouken@2901
    42
    default:
slouken@2901
    43
        DRAW_SETPIXELXY_RGB555(x, y);
slouken@2901
    44
        break;
slouken@2901
    45
    }
slouken@2901
    46
    return 0;
slouken@2901
    47
}
slouken@2901
    48
slouken@2901
    49
static int
slouken@2901
    50
SDL_BlendPoint_RGB565(SDL_Surface * dst, int x, int y, int blendMode, Uint8 r,
slouken@2901
    51
                      Uint8 g, Uint8 b, Uint8 a)
slouken@2901
    52
{
slouken@2901
    53
    unsigned inva = 0xff - a;
slouken@2901
    54
slouken@2901
    55
    switch (blendMode) {
slouken@2901
    56
    case SDL_BLENDMODE_BLEND:
slouken@2901
    57
        DRAW_SETPIXELXY_BLEND_RGB565(x, y);
slouken@2901
    58
        break;
slouken@2901
    59
    case SDL_BLENDMODE_ADD:
slouken@2901
    60
        DRAW_SETPIXELXY_ADD_RGB565(x, y);
slouken@2901
    61
        break;
slouken@2901
    62
    case SDL_BLENDMODE_MOD:
slouken@2901
    63
        DRAW_SETPIXELXY_MOD_RGB565(x, y);
slouken@2901
    64
        break;
slouken@2901
    65
    default:
slouken@2901
    66
        DRAW_SETPIXELXY_RGB565(x, y);
slouken@2901
    67
        break;
slouken@2901
    68
    }
slouken@2901
    69
    return 0;
slouken@2901
    70
}
slouken@2901
    71
slouken@2901
    72
static int
slouken@2901
    73
SDL_BlendPoint_RGB888(SDL_Surface * dst, int x, int y, int blendMode, Uint8 r,
slouken@2901
    74
                      Uint8 g, Uint8 b, Uint8 a)
slouken@2901
    75
{
slouken@2901
    76
    unsigned inva = 0xff - a;
slouken@2901
    77
slouken@2901
    78
    switch (blendMode) {
slouken@2901
    79
    case SDL_BLENDMODE_BLEND:
slouken@2901
    80
        DRAW_SETPIXELXY_BLEND_RGB888(x, y);
slouken@2901
    81
        break;
slouken@2901
    82
    case SDL_BLENDMODE_ADD:
slouken@2901
    83
        DRAW_SETPIXELXY_ADD_RGB888(x, y);
slouken@2901
    84
        break;
slouken@2901
    85
    case SDL_BLENDMODE_MOD:
slouken@2901
    86
        DRAW_SETPIXELXY_MOD_RGB888(x, y);
slouken@2901
    87
        break;
slouken@2901
    88
    default:
slouken@2901
    89
        DRAW_SETPIXELXY_RGB888(x, y);
slouken@2901
    90
        break;
slouken@2901
    91
    }
slouken@2901
    92
    return 0;
slouken@2901
    93
}
slouken@2901
    94
slouken@2901
    95
static int
slouken@2901
    96
SDL_BlendPoint_ARGB8888(SDL_Surface * dst, int x, int y, int blendMode,
slouken@2901
    97
                        Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2901
    98
{
slouken@2901
    99
    unsigned inva = 0xff - a;
slouken@2901
   100
slouken@2901
   101
    switch (blendMode) {
slouken@2901
   102
    case SDL_BLENDMODE_BLEND:
slouken@2901
   103
        DRAW_SETPIXELXY_BLEND_ARGB8888(x, y);
slouken@2901
   104
        break;
slouken@2901
   105
    case SDL_BLENDMODE_ADD:
slouken@2901
   106
        DRAW_SETPIXELXY_ADD_ARGB8888(x, y);
slouken@2901
   107
        break;
slouken@2901
   108
    case SDL_BLENDMODE_MOD:
slouken@2901
   109
        DRAW_SETPIXELXY_MOD_ARGB8888(x, y);
slouken@2901
   110
        break;
slouken@2901
   111
    default:
slouken@2901
   112
        DRAW_SETPIXELXY_ARGB8888(x, y);
slouken@2901
   113
        break;
slouken@2901
   114
    }
slouken@2901
   115
    return 0;
slouken@2901
   116
}
slouken@2901
   117
slouken@2901
   118
static int
slouken@2901
   119
SDL_BlendPoint_RGB(SDL_Surface * dst, int x, int y, int blendMode, Uint8 r,
slouken@2901
   120
                   Uint8 g, Uint8 b, Uint8 a)
slouken@2901
   121
{
slouken@2901
   122
    SDL_PixelFormat *fmt = dst->format;
slouken@2901
   123
    unsigned inva = 0xff - a;
slouken@2901
   124
slouken@2901
   125
    switch (fmt->BytesPerPixel) {
slouken@2901
   126
    case 2:
slouken@2901
   127
        switch (blendMode) {
slouken@2901
   128
        case SDL_BLENDMODE_BLEND:
slouken@2901
   129
            DRAW_SETPIXELXY2_BLEND_RGB(x, y);
slouken@2901
   130
            break;
slouken@2901
   131
        case SDL_BLENDMODE_ADD:
slouken@2901
   132
            DRAW_SETPIXELXY2_ADD_RGB(x, y);
slouken@2901
   133
            break;
slouken@2901
   134
        case SDL_BLENDMODE_MOD:
slouken@2901
   135
            DRAW_SETPIXELXY2_MOD_RGB(x, y);
slouken@2901
   136
            break;
slouken@2901
   137
        default:
slouken@2901
   138
            DRAW_SETPIXELXY2_RGB(x, y);
slouken@2901
   139
            break;
slouken@2901
   140
        }
slouken@2901
   141
        return 0;
slouken@2901
   142
    case 4:
slouken@2901
   143
        switch (blendMode) {
slouken@2901
   144
        case SDL_BLENDMODE_BLEND:
slouken@2901
   145
            DRAW_SETPIXELXY4_BLEND_RGB(x, y);
slouken@2901
   146
            break;
slouken@2901
   147
        case SDL_BLENDMODE_ADD:
slouken@2901
   148
            DRAW_SETPIXELXY4_ADD_RGB(x, y);
slouken@2901
   149
            break;
slouken@2901
   150
        case SDL_BLENDMODE_MOD:
slouken@2901
   151
            DRAW_SETPIXELXY4_MOD_RGB(x, y);
slouken@2901
   152
            break;
slouken@2901
   153
        default:
slouken@2901
   154
            DRAW_SETPIXELXY4_RGB(x, y);
slouken@2901
   155
            break;
slouken@2901
   156
        }
slouken@2901
   157
        return 0;
slouken@2901
   158
    default:
slouken@2901
   159
        SDL_Unsupported();
slouken@2901
   160
        return -1;
slouken@2901
   161
    }
slouken@2901
   162
}
slouken@2901
   163
slouken@2901
   164
static int
slouken@2901
   165
SDL_BlendPoint_RGBA(SDL_Surface * dst, int x, int y, int blendMode, Uint8 r,
slouken@2901
   166
                    Uint8 g, Uint8 b, Uint8 a)
slouken@2901
   167
{
slouken@2901
   168
    SDL_PixelFormat *fmt = dst->format;
slouken@2901
   169
    unsigned inva = 0xff - a;
slouken@2901
   170
slouken@2901
   171
    switch (fmt->BytesPerPixel) {
slouken@2901
   172
    case 4:
slouken@2901
   173
        switch (blendMode) {
slouken@2901
   174
        case SDL_BLENDMODE_BLEND:
slouken@2901
   175
            DRAW_SETPIXELXY4_BLEND_RGBA(x, y);
slouken@2901
   176
            break;
slouken@2901
   177
        case SDL_BLENDMODE_ADD:
slouken@2901
   178
            DRAW_SETPIXELXY4_ADD_RGBA(x, y);
slouken@2901
   179
            break;
slouken@2901
   180
        case SDL_BLENDMODE_MOD:
slouken@2901
   181
            DRAW_SETPIXELXY4_MOD_RGBA(x, y);
slouken@2901
   182
            break;
slouken@2901
   183
        default:
slouken@2901
   184
            DRAW_SETPIXELXY4_RGBA(x, y);
slouken@2901
   185
            break;
slouken@2901
   186
        }
slouken@2901
   187
        return 0;
slouken@2901
   188
    default:
slouken@2901
   189
        SDL_Unsupported();
slouken@2901
   190
        return -1;
slouken@2901
   191
    }
slouken@2901
   192
}
slouken@2901
   193
slouken@2901
   194
int
slouken@2901
   195
SDL_BlendPoint(SDL_Surface * dst, int x, int y, int blendMode, Uint8 r,
slouken@2901
   196
               Uint8 g, Uint8 b, Uint8 a)
slouken@2901
   197
{
slouken@2901
   198
    SDL_PixelFormat *fmt = dst->format;
slouken@2901
   199
slouken@2901
   200
    /* This function doesn't work on surfaces < 8 bpp */
slouken@2901
   201
    if (dst->format->BitsPerPixel < 8) {
slouken@2901
   202
        SDL_SetError("SDL_BlendPoint(): Unsupported surface format");
slouken@2901
   203
        return (-1);
slouken@2901
   204
    }
slouken@2901
   205
slouken@2901
   206
    /* Perform clipping */
slouken@2901
   207
    if (x < dst->clip_rect.x || y < dst->clip_rect.y ||
slouken@2901
   208
        x >= (dst->clip_rect.x + dst->clip_rect.w) ||
slouken@2901
   209
        y >= (dst->clip_rect.y + dst->clip_rect.h)) {
slouken@2901
   210
        return 0;
slouken@2901
   211
    }
slouken@2901
   212
slouken@2901
   213
    if ((blendMode == SDL_BLENDMODE_BLEND)
slouken@2901
   214
        || (blendMode == SDL_BLENDMODE_ADD)) {
slouken@2901
   215
        r = DRAW_MUL(r, a);
slouken@2901
   216
        g = DRAW_MUL(g, a);
slouken@2901
   217
        b = DRAW_MUL(b, a);
slouken@2901
   218
    }
slouken@2901
   219
slouken@2901
   220
    switch (fmt->BitsPerPixel) {
slouken@2901
   221
    case 15:
slouken@2901
   222
        switch (fmt->Rmask) {
slouken@2901
   223
        case 0x7C00:
slouken@2901
   224
            return SDL_BlendPoint_RGB555(dst, x, y, blendMode, r, g, b, a);
slouken@2901
   225
        }
slouken@2901
   226
        break;
slouken@2901
   227
    case 16:
slouken@2901
   228
        switch (fmt->Rmask) {
slouken@2901
   229
        case 0xF800:
slouken@2901
   230
            return SDL_BlendPoint_RGB565(dst, x, y, blendMode, r, g, b, a);
slouken@2901
   231
        }
slouken@2901
   232
        break;
slouken@2901
   233
    case 32:
slouken@2901
   234
        switch (fmt->Rmask) {
slouken@2901
   235
        case 0x00FF0000:
slouken@2901
   236
            if (!fmt->Amask) {
slouken@2901
   237
                return SDL_BlendPoint_RGB888(dst, x, y, blendMode, r, g, b,
slouken@2901
   238
                                             a);
slouken@2901
   239
            } else {
slouken@2901
   240
                return SDL_BlendPoint_ARGB8888(dst, x, y, blendMode, r, g, b,
slouken@2901
   241
                                               a);
slouken@2901
   242
            }
slouken@2901
   243
            break;
slouken@2901
   244
        }
slouken@2901
   245
    default:
slouken@2901
   246
        break;
slouken@2901
   247
    }
slouken@2901
   248
slouken@2901
   249
    if (!fmt->Amask) {
slouken@2901
   250
        return SDL_BlendPoint_RGB(dst, x, y, blendMode, r, g, b, a);
slouken@2901
   251
    } else {
slouken@2901
   252
        return SDL_BlendPoint_RGBA(dst, x, y, blendMode, r, g, b, a);
slouken@2901
   253
    }
slouken@2901
   254
}
slouken@2901
   255
slouken@2901
   256
/* vi: set ts=4 sw=4 expandtab: */