src/video/SDL_blendline.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 12 Dec 2010 15:19:05 -0800
changeset 4929 aa8888658021
parent 3697 f7b03b6838cb
child 5140 e743b9c3f6d6
permissions -rw-r--r--
Use the enumerated type for blend and scale mode instead of int
Renamed SDL_TextureScaleMode to SDL_ScaleMode
slouken@2888
     1
/*
slouken@2888
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@2888
     4
slouken@2888
     5
    This library is free software; you can redistribute it and/or
slouken@2888
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2888
     7
    License as published by the Free Software Foundation; either
slouken@2888
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2888
     9
slouken@2888
    10
    This library is distributed in the hope that it will be useful,
slouken@2888
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2888
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2888
    13
    Lesser General Public License for more details.
slouken@2888
    14
slouken@2888
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2888
    16
    License along with this library; if not, write to the Free Software
slouken@2888
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2888
    18
slouken@2888
    19
    Sam Lantinga
slouken@2888
    20
    slouken@libsdl.org
slouken@2888
    21
*/
slouken@2888
    22
#include "SDL_config.h"
slouken@2888
    23
slouken@2898
    24
#include "SDL_draw.h"
slouken@2898
    25
slouken@3596
    26
slouken@3596
    27
static void
slouken@3596
    28
SDL_BlendLine_RGB2(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
    29
                   SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
    30
                   SDL_bool draw_end)
slouken@2898
    31
{
slouken@3596
    32
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
    33
    unsigned r, g, b, a, inva;
slouken@3596
    34
slouken@3596
    35
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
    36
        r = DRAW_MUL(_r, _a);
slouken@3596
    37
        g = DRAW_MUL(_g, _a);
slouken@3596
    38
        b = DRAW_MUL(_b, _a);
slouken@3596
    39
        a = _a;
slouken@3596
    40
    } else {
slouken@3596
    41
        r = _r;
slouken@3596
    42
        g = _g;
slouken@3596
    43
        b = _b;
slouken@3596
    44
        a = _a;
slouken@3596
    45
    }
slouken@3596
    46
    inva = (a ^ 0xff);
slouken@2888
    47
slouken@3596
    48
    if (y1 == y2) {
slouken@3596
    49
        switch (blendMode) {
slouken@3596
    50
        case SDL_BLENDMODE_BLEND:
slouken@3596
    51
            HLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
    52
            break;
slouken@3596
    53
        case SDL_BLENDMODE_ADD:
slouken@3596
    54
            HLINE(Uint16, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
    55
            break;
slouken@3596
    56
        case SDL_BLENDMODE_MOD:
slouken@3596
    57
            HLINE(Uint16, DRAW_SETPIXEL_MOD_RGB, draw_end);
slouken@3596
    58
            break;
slouken@3596
    59
        default:
slouken@3596
    60
            HLINE(Uint16, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
    61
            break;
slouken@3596
    62
        }
slouken@3596
    63
    } else if (x1 == x2) {
slouken@3596
    64
        switch (blendMode) {
slouken@3596
    65
        case SDL_BLENDMODE_BLEND:
slouken@3596
    66
            VLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
    67
            break;
slouken@3596
    68
        case SDL_BLENDMODE_ADD:
slouken@3596
    69
            VLINE(Uint16, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
    70
            break;
slouken@3596
    71
        case SDL_BLENDMODE_MOD:
slouken@3596
    72
            VLINE(Uint16, DRAW_SETPIXEL_MOD_RGB, draw_end);
slouken@3596
    73
            break;
slouken@3596
    74
        default:
slouken@3596
    75
            VLINE(Uint16, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
    76
            break;
slouken@3596
    77
        }
slouken@3596
    78
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    79
        switch (blendMode) {
slouken@3596
    80
        case SDL_BLENDMODE_BLEND:
slouken@3596
    81
            DLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
    82
            break;
slouken@3596
    83
        case SDL_BLENDMODE_ADD:
slouken@3596
    84
            DLINE(Uint16, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
    85
            break;
slouken@3596
    86
        case SDL_BLENDMODE_MOD:
slouken@3596
    87
            DLINE(Uint16, DRAW_SETPIXEL_MOD_RGB, draw_end);
slouken@3596
    88
            break;
slouken@3596
    89
        default:
slouken@3596
    90
            DLINE(Uint16, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
    91
            break;
slouken@3596
    92
        }
slouken@3596
    93
    } else {
slouken@3596
    94
        switch (blendMode) {
slouken@3596
    95
        case SDL_BLENDMODE_BLEND:
slouken@3596
    96
            AALINE(x1, y1, x2, y2,
slouken@3596
    97
                   DRAW_SETPIXELXY2_BLEND_RGB, DRAW_SETPIXELXY2_BLEND_RGB,
slouken@3596
    98
                   draw_end);
slouken@3596
    99
            break;
slouken@3596
   100
        case SDL_BLENDMODE_ADD:
slouken@3596
   101
            AALINE(x1, y1, x2, y2,
slouken@3596
   102
                   DRAW_SETPIXELXY2_ADD_RGB, DRAW_SETPIXELXY2_ADD_RGB,
slouken@3596
   103
                   draw_end);
slouken@3596
   104
            break;
slouken@3596
   105
        case SDL_BLENDMODE_MOD:
slouken@3596
   106
            AALINE(x1, y1, x2, y2,
slouken@3596
   107
                   DRAW_SETPIXELXY2_MOD_RGB, DRAW_SETPIXELXY2_MOD_RGB,
slouken@3596
   108
                   draw_end);
slouken@3596
   109
            break;
slouken@3596
   110
        default:
slouken@3596
   111
            AALINE(x1, y1, x2, y2,
slouken@3596
   112
                   DRAW_SETPIXELXY2_RGB, DRAW_SETPIXELXY2_BLEND_RGB,
slouken@3596
   113
                   draw_end);
slouken@3596
   114
            break;
slouken@3596
   115
        }
slouken@2898
   116
    }
slouken@2898
   117
}
slouken@2896
   118
slouken@3596
   119
static void
slouken@3596
   120
SDL_BlendLine_RGB555(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   121
                     SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3594
   122
                     SDL_bool draw_end)
slouken@2898
   123
{
slouken@3596
   124
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   125
    unsigned r, g, b, a, inva;
slouken@2898
   126
slouken@3596
   127
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   128
        r = DRAW_MUL(_r, _a);
slouken@3596
   129
        g = DRAW_MUL(_g, _a);
slouken@3596
   130
        b = DRAW_MUL(_b, _a);
slouken@3596
   131
        a = _a;
slouken@3596
   132
    } else {
slouken@3596
   133
        r = _r;
slouken@3596
   134
        g = _g;
slouken@3596
   135
        b = _b;
slouken@3596
   136
        a = _a;
slouken@2898
   137
    }
slouken@3596
   138
    inva = (a ^ 0xff);
slouken@2896
   139
slouken@3596
   140
    if (y1 == y2) {
slouken@3596
   141
        switch (blendMode) {
slouken@3596
   142
        case SDL_BLENDMODE_BLEND:
slouken@3596
   143
            HLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB555, draw_end);
slouken@3596
   144
            break;
slouken@3596
   145
        case SDL_BLENDMODE_ADD:
slouken@3596
   146
            HLINE(Uint16, DRAW_SETPIXEL_ADD_RGB555, draw_end);
slouken@3596
   147
            break;
slouken@3596
   148
        case SDL_BLENDMODE_MOD:
slouken@3596
   149
            HLINE(Uint16, DRAW_SETPIXEL_MOD_RGB555, draw_end);
slouken@3596
   150
            break;
slouken@3596
   151
        default:
slouken@3596
   152
            HLINE(Uint16, DRAW_SETPIXEL_RGB555, draw_end);
slouken@3596
   153
            break;
slouken@3596
   154
        }
slouken@3596
   155
    } else if (x1 == x2) {
slouken@3596
   156
        switch (blendMode) {
slouken@3596
   157
        case SDL_BLENDMODE_BLEND:
slouken@3596
   158
            VLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB555, draw_end);
slouken@3596
   159
            break;
slouken@3596
   160
        case SDL_BLENDMODE_ADD:
slouken@3596
   161
            VLINE(Uint16, DRAW_SETPIXEL_ADD_RGB555, draw_end);
slouken@3596
   162
            break;
slouken@3596
   163
        case SDL_BLENDMODE_MOD:
slouken@3596
   164
            VLINE(Uint16, DRAW_SETPIXEL_MOD_RGB555, draw_end);
slouken@3596
   165
            break;
slouken@3596
   166
        default:
slouken@3596
   167
            VLINE(Uint16, DRAW_SETPIXEL_RGB555, draw_end);
slouken@3596
   168
            break;
slouken@3596
   169
        }
slouken@3596
   170
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   171
        switch (blendMode) {
slouken@3596
   172
        case SDL_BLENDMODE_BLEND:
slouken@3596
   173
            DLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB555, draw_end);
slouken@3596
   174
            break;
slouken@3596
   175
        case SDL_BLENDMODE_ADD:
slouken@3596
   176
            DLINE(Uint16, DRAW_SETPIXEL_ADD_RGB555, draw_end);
slouken@3596
   177
            break;
slouken@3596
   178
        case SDL_BLENDMODE_MOD:
slouken@3596
   179
            DLINE(Uint16, DRAW_SETPIXEL_MOD_RGB555, draw_end);
slouken@3596
   180
            break;
slouken@3596
   181
        default:
slouken@3596
   182
            DLINE(Uint16, DRAW_SETPIXEL_RGB555, draw_end);
slouken@3596
   183
            break;
slouken@3596
   184
        }
slouken@3596
   185
    } else {
slouken@3596
   186
        switch (blendMode) {
slouken@3596
   187
        case SDL_BLENDMODE_BLEND:
slouken@3596
   188
            AALINE(x1, y1, x2, y2,
slouken@3596
   189
                   DRAW_SETPIXELXY_BLEND_RGB555, DRAW_SETPIXELXY_BLEND_RGB555,
slouken@3596
   190
                   draw_end);
slouken@3596
   191
            break;
slouken@3596
   192
        case SDL_BLENDMODE_ADD:
slouken@3596
   193
            AALINE(x1, y1, x2, y2,
slouken@3596
   194
                   DRAW_SETPIXELXY_ADD_RGB555, DRAW_SETPIXELXY_ADD_RGB555,
slouken@3596
   195
                   draw_end);
slouken@3596
   196
            break;
slouken@3596
   197
        case SDL_BLENDMODE_MOD:
slouken@3596
   198
            AALINE(x1, y1, x2, y2,
slouken@3596
   199
                   DRAW_SETPIXELXY_MOD_RGB555, DRAW_SETPIXELXY_MOD_RGB555,
slouken@3596
   200
                   draw_end);
slouken@3596
   201
            break;
slouken@3596
   202
        default:
slouken@3596
   203
            AALINE(x1, y1, x2, y2,
slouken@3596
   204
                   DRAW_SETPIXELXY_RGB555, DRAW_SETPIXELXY_BLEND_RGB555,
slouken@3596
   205
                   draw_end);
slouken@3596
   206
            break;
slouken@3596
   207
        }
slouken@2899
   208
    }
slouken@2899
   209
}
slouken@2899
   210
slouken@3596
   211
static void
slouken@3596
   212
SDL_BlendLine_RGB565(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   213
                     SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   214
                     SDL_bool draw_end)
slouken@2898
   215
{
slouken@3596
   216
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   217
    unsigned r, g, b, a, inva;
slouken@2896
   218
slouken@3596
   219
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   220
        r = DRAW_MUL(_r, _a);
slouken@3596
   221
        g = DRAW_MUL(_g, _a);
slouken@3596
   222
        b = DRAW_MUL(_b, _a);
slouken@3596
   223
        a = _a;
slouken@3596
   224
    } else {
slouken@3596
   225
        r = _r;
slouken@3596
   226
        g = _g;
slouken@3596
   227
        b = _b;
slouken@3596
   228
        a = _a;
slouken@3596
   229
    }
slouken@3596
   230
    inva = (a ^ 0xff);
slouken@3596
   231
slouken@3596
   232
    if (y1 == y2) {
slouken@2898
   233
        switch (blendMode) {
slouken@2898
   234
        case SDL_BLENDMODE_BLEND:
slouken@3596
   235
            HLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB565, draw_end);
slouken@2898
   236
            break;
slouken@2898
   237
        case SDL_BLENDMODE_ADD:
slouken@3596
   238
            HLINE(Uint16, DRAW_SETPIXEL_ADD_RGB565, draw_end);
slouken@2898
   239
            break;
slouken@2898
   240
        case SDL_BLENDMODE_MOD:
slouken@3596
   241
            HLINE(Uint16, DRAW_SETPIXEL_MOD_RGB565, draw_end);
slouken@2898
   242
            break;
slouken@2898
   243
        default:
slouken@3596
   244
            HLINE(Uint16, DRAW_SETPIXEL_RGB565, draw_end);
slouken@2898
   245
            break;
slouken@2898
   246
        }
slouken@3596
   247
    } else if (x1 == x2) {
slouken@2898
   248
        switch (blendMode) {
slouken@2898
   249
        case SDL_BLENDMODE_BLEND:
slouken@3596
   250
            VLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB565, draw_end);
slouken@3596
   251
            break;
slouken@3596
   252
        case SDL_BLENDMODE_ADD:
slouken@3596
   253
            VLINE(Uint16, DRAW_SETPIXEL_ADD_RGB565, draw_end);
slouken@3596
   254
            break;
slouken@3596
   255
        case SDL_BLENDMODE_MOD:
slouken@3596
   256
            VLINE(Uint16, DRAW_SETPIXEL_MOD_RGB565, draw_end);
slouken@3596
   257
            break;
slouken@3596
   258
        default:
slouken@3596
   259
            VLINE(Uint16, DRAW_SETPIXEL_RGB565, draw_end);
slouken@3596
   260
            break;
slouken@3596
   261
        }
slouken@3596
   262
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   263
        switch (blendMode) {
slouken@3596
   264
        case SDL_BLENDMODE_BLEND:
slouken@3596
   265
            DLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB565, draw_end);
slouken@2898
   266
            break;
slouken@2898
   267
        case SDL_BLENDMODE_ADD:
slouken@3596
   268
            DLINE(Uint16, DRAW_SETPIXEL_ADD_RGB565, draw_end);
slouken@2898
   269
            break;
slouken@2898
   270
        case SDL_BLENDMODE_MOD:
slouken@3596
   271
            DLINE(Uint16, DRAW_SETPIXEL_MOD_RGB565, draw_end);
slouken@2898
   272
            break;
slouken@2898
   273
        default:
slouken@3596
   274
            DLINE(Uint16, DRAW_SETPIXEL_RGB565, draw_end);
slouken@2898
   275
            break;
slouken@2898
   276
        }
slouken@3596
   277
    } else {
slouken@3596
   278
        switch (blendMode) {
slouken@3596
   279
        case SDL_BLENDMODE_BLEND:
slouken@3596
   280
            AALINE(x1, y1, x2, y2,
slouken@3596
   281
                   DRAW_SETPIXELXY_BLEND_RGB565, DRAW_SETPIXELXY_BLEND_RGB565,
slouken@3596
   282
                   draw_end);
slouken@3596
   283
            break;
slouken@3596
   284
        case SDL_BLENDMODE_ADD:
slouken@3596
   285
            AALINE(x1, y1, x2, y2,
slouken@3596
   286
                   DRAW_SETPIXELXY_ADD_RGB565, DRAW_SETPIXELXY_ADD_RGB565,
slouken@3596
   287
                   draw_end);
slouken@3596
   288
            break;
slouken@3596
   289
        case SDL_BLENDMODE_MOD:
slouken@3596
   290
            AALINE(x1, y1, x2, y2,
slouken@3596
   291
                   DRAW_SETPIXELXY_MOD_RGB565, DRAW_SETPIXELXY_MOD_RGB565,
slouken@3596
   292
                   draw_end);
slouken@3596
   293
            break;
slouken@3596
   294
        default:
slouken@3596
   295
            AALINE(x1, y1, x2, y2,
slouken@3596
   296
                   DRAW_SETPIXELXY_RGB565, DRAW_SETPIXELXY_BLEND_RGB565,
slouken@3596
   297
                   draw_end);
slouken@3596
   298
            break;
slouken@3596
   299
        }
slouken@2898
   300
    }
slouken@2898
   301
}
slouken@2896
   302
slouken@3596
   303
static void
slouken@3596
   304
SDL_BlendLine_RGB4(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   305
                   SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3594
   306
                   SDL_bool draw_end)
slouken@2898
   307
{
slouken@3596
   308
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   309
    unsigned r, g, b, a, inva;
slouken@3596
   310
slouken@3596
   311
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   312
        r = DRAW_MUL(_r, _a);
slouken@3596
   313
        g = DRAW_MUL(_g, _a);
slouken@3596
   314
        b = DRAW_MUL(_b, _a);
slouken@3596
   315
        a = _a;
slouken@3596
   316
    } else {
slouken@3596
   317
        r = _r;
slouken@3596
   318
        g = _g;
slouken@3596
   319
        b = _b;
slouken@3596
   320
        a = _a;
slouken@3596
   321
    }
slouken@3596
   322
    inva = (a ^ 0xff);
slouken@2896
   323
slouken@3596
   324
    if (y1 == y2) {
slouken@3596
   325
        switch (blendMode) {
slouken@3596
   326
        case SDL_BLENDMODE_BLEND:
slouken@3596
   327
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
   328
            break;
slouken@3596
   329
        case SDL_BLENDMODE_ADD:
slouken@3596
   330
            HLINE(Uint32, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
   331
            break;
slouken@3596
   332
        case SDL_BLENDMODE_MOD:
slouken@3596
   333
            HLINE(Uint32, DRAW_SETPIXEL_MOD_RGB, draw_end);
slouken@3596
   334
            break;
slouken@3596
   335
        default:
slouken@3596
   336
            HLINE(Uint32, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
   337
            break;
slouken@3596
   338
        }
slouken@3596
   339
    } else if (x1 == x2) {
slouken@3596
   340
        switch (blendMode) {
slouken@3596
   341
        case SDL_BLENDMODE_BLEND:
slouken@3596
   342
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
   343
            break;
slouken@3596
   344
        case SDL_BLENDMODE_ADD:
slouken@3596
   345
            VLINE(Uint32, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
   346
            break;
slouken@3596
   347
        case SDL_BLENDMODE_MOD:
slouken@3596
   348
            VLINE(Uint32, DRAW_SETPIXEL_MOD_RGB, draw_end);
slouken@3596
   349
            break;
slouken@3596
   350
        default:
slouken@3596
   351
            VLINE(Uint32, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
   352
            break;
slouken@3596
   353
        }
slouken@3596
   354
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   355
        switch (blendMode) {
slouken@3596
   356
        case SDL_BLENDMODE_BLEND:
slouken@3596
   357
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
   358
            break;
slouken@3596
   359
        case SDL_BLENDMODE_ADD:
slouken@3596
   360
            DLINE(Uint32, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
   361
            break;
slouken@3596
   362
        case SDL_BLENDMODE_MOD:
slouken@3596
   363
            DLINE(Uint32, DRAW_SETPIXEL_MOD_RGB, draw_end);
slouken@3596
   364
            break;
slouken@3596
   365
        default:
slouken@3596
   366
            DLINE(Uint32, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
   367
            break;
slouken@3596
   368
        }
slouken@3596
   369
    } else {
slouken@2898
   370
        switch (blendMode) {
slouken@2898
   371
        case SDL_BLENDMODE_BLEND:
slouken@3596
   372
            AALINE(x1, y1, x2, y2,
slouken@3596
   373
                   DRAW_SETPIXELXY4_BLEND_RGB, DRAW_SETPIXELXY4_BLEND_RGB,
slouken@3596
   374
                   draw_end);
slouken@3596
   375
            break;
slouken@3596
   376
        case SDL_BLENDMODE_ADD:
slouken@3596
   377
            AALINE(x1, y1, x2, y2,
slouken@3596
   378
                   DRAW_SETPIXELXY4_ADD_RGB, DRAW_SETPIXELXY4_ADD_RGB,
slouken@3596
   379
                   draw_end);
slouken@3596
   380
            break;
slouken@3596
   381
        case SDL_BLENDMODE_MOD:
slouken@3596
   382
            AALINE(x1, y1, x2, y2,
slouken@3596
   383
                   DRAW_SETPIXELXY4_MOD_RGB, DRAW_SETPIXELXY4_MOD_RGB,
slouken@3596
   384
                   draw_end);
slouken@3596
   385
            break;
slouken@3596
   386
        default:
slouken@3596
   387
            AALINE(x1, y1, x2, y2,
slouken@3596
   388
                   DRAW_SETPIXELXY4_RGB, DRAW_SETPIXELXY4_BLEND_RGB,
slouken@3596
   389
                   draw_end);
slouken@3596
   390
            break;
slouken@3596
   391
        }
slouken@3596
   392
    }
slouken@3596
   393
}
slouken@3596
   394
slouken@3596
   395
static void
slouken@3596
   396
SDL_BlendLine_RGBA4(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   397
                    SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   398
                    SDL_bool draw_end)
slouken@3596
   399
{
slouken@3596
   400
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   401
    unsigned r, g, b, a, inva;
slouken@3596
   402
slouken@3596
   403
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   404
        r = DRAW_MUL(_r, _a);
slouken@3596
   405
        g = DRAW_MUL(_g, _a);
slouken@3596
   406
        b = DRAW_MUL(_b, _a);
slouken@3596
   407
        a = _a;
slouken@3596
   408
    } else {
slouken@3596
   409
        r = _r;
slouken@3596
   410
        g = _g;
slouken@3596
   411
        b = _b;
slouken@3596
   412
        a = _a;
slouken@3596
   413
    }
slouken@3596
   414
    inva = (a ^ 0xff);
slouken@3596
   415
slouken@3596
   416
    if (y1 == y2) {
slouken@3596
   417
        switch (blendMode) {
slouken@3596
   418
        case SDL_BLENDMODE_BLEND:
slouken@3596
   419
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_RGBA, draw_end);
slouken@3596
   420
            break;
slouken@3596
   421
        case SDL_BLENDMODE_ADD:
slouken@3596
   422
            HLINE(Uint32, DRAW_SETPIXEL_ADD_RGBA, draw_end);
slouken@3596
   423
            break;
slouken@3596
   424
        case SDL_BLENDMODE_MOD:
slouken@3596
   425
            HLINE(Uint32, DRAW_SETPIXEL_MOD_RGBA, draw_end);
slouken@3596
   426
            break;
slouken@3596
   427
        default:
slouken@3596
   428
            HLINE(Uint32, DRAW_SETPIXEL_RGBA, draw_end);
slouken@3596
   429
            break;
slouken@3596
   430
        }
slouken@3596
   431
    } else if (x1 == x2) {
slouken@3596
   432
        switch (blendMode) {
slouken@3596
   433
        case SDL_BLENDMODE_BLEND:
slouken@3596
   434
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_RGBA, draw_end);
slouken@3596
   435
            break;
slouken@3596
   436
        case SDL_BLENDMODE_ADD:
slouken@3596
   437
            VLINE(Uint32, DRAW_SETPIXEL_ADD_RGBA, draw_end);
slouken@3596
   438
            break;
slouken@3596
   439
        case SDL_BLENDMODE_MOD:
slouken@3596
   440
            VLINE(Uint32, DRAW_SETPIXEL_MOD_RGBA, draw_end);
slouken@3596
   441
            break;
slouken@3596
   442
        default:
slouken@3596
   443
            VLINE(Uint32, DRAW_SETPIXEL_RGBA, draw_end);
slouken@3596
   444
            break;
slouken@3596
   445
        }
slouken@3596
   446
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   447
        switch (blendMode) {
slouken@3596
   448
        case SDL_BLENDMODE_BLEND:
slouken@3596
   449
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_RGBA, draw_end);
slouken@3596
   450
            break;
slouken@3596
   451
        case SDL_BLENDMODE_ADD:
slouken@3596
   452
            DLINE(Uint32, DRAW_SETPIXEL_ADD_RGBA, draw_end);
slouken@3596
   453
            break;
slouken@3596
   454
        case SDL_BLENDMODE_MOD:
slouken@3596
   455
            DLINE(Uint32, DRAW_SETPIXEL_MOD_RGBA, draw_end);
slouken@3596
   456
            break;
slouken@3596
   457
        default:
slouken@3596
   458
            DLINE(Uint32, DRAW_SETPIXEL_RGBA, draw_end);
slouken@3596
   459
            break;
slouken@3596
   460
        }
slouken@3596
   461
    } else {
slouken@3596
   462
        switch (blendMode) {
slouken@3596
   463
        case SDL_BLENDMODE_BLEND:
slouken@3596
   464
            AALINE(x1, y1, x2, y2,
slouken@3596
   465
                   DRAW_SETPIXELXY4_BLEND_RGBA, DRAW_SETPIXELXY4_BLEND_RGBA,
slouken@3596
   466
                   draw_end);
slouken@2898
   467
            break;
slouken@2898
   468
        case SDL_BLENDMODE_ADD:
slouken@3596
   469
            AALINE(x1, y1, x2, y2,
slouken@3596
   470
                   DRAW_SETPIXELXY4_ADD_RGBA, DRAW_SETPIXELXY4_ADD_RGBA,
slouken@3596
   471
                   draw_end);
slouken@3596
   472
            break;
slouken@3596
   473
        case SDL_BLENDMODE_MOD:
slouken@3596
   474
            AALINE(x1, y1, x2, y2,
slouken@3596
   475
                   DRAW_SETPIXELXY4_MOD_RGBA, DRAW_SETPIXELXY4_MOD_RGBA,
slouken@3596
   476
                   draw_end);
slouken@3596
   477
            break;
slouken@3596
   478
        default:
slouken@3596
   479
            AALINE(x1, y1, x2, y2,
slouken@3596
   480
                   DRAW_SETPIXELXY4_RGBA, DRAW_SETPIXELXY4_BLEND_RGBA,
slouken@3596
   481
                   draw_end);
slouken@3596
   482
            break;
slouken@3596
   483
        }
slouken@3596
   484
    }
slouken@3596
   485
}
slouken@3596
   486
slouken@3596
   487
static void
slouken@3596
   488
SDL_BlendLine_RGB888(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   489
                     SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   490
                     SDL_bool draw_end)
slouken@3596
   491
{
slouken@3596
   492
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   493
    unsigned r, g, b, a, inva;
slouken@3596
   494
slouken@3596
   495
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   496
        r = DRAW_MUL(_r, _a);
slouken@3596
   497
        g = DRAW_MUL(_g, _a);
slouken@3596
   498
        b = DRAW_MUL(_b, _a);
slouken@3596
   499
        a = _a;
slouken@3596
   500
    } else {
slouken@3596
   501
        r = _r;
slouken@3596
   502
        g = _g;
slouken@3596
   503
        b = _b;
slouken@3596
   504
        a = _a;
slouken@3596
   505
    }
slouken@3596
   506
    inva = (a ^ 0xff);
slouken@3596
   507
slouken@3596
   508
    if (y1 == y2) {
slouken@3596
   509
        switch (blendMode) {
slouken@3596
   510
        case SDL_BLENDMODE_BLEND:
slouken@3596
   511
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB888, draw_end);
slouken@3596
   512
            break;
slouken@3596
   513
        case SDL_BLENDMODE_ADD:
slouken@3596
   514
            HLINE(Uint32, DRAW_SETPIXEL_ADD_RGB888, draw_end);
slouken@2898
   515
            break;
slouken@2898
   516
        case SDL_BLENDMODE_MOD:
slouken@3596
   517
            HLINE(Uint32, DRAW_SETPIXEL_MOD_RGB888, draw_end);
slouken@3596
   518
            break;
slouken@3596
   519
        default:
slouken@3596
   520
            HLINE(Uint32, DRAW_SETPIXEL_RGB888, draw_end);
slouken@3596
   521
            break;
slouken@3596
   522
        }
slouken@3596
   523
    } else if (x1 == x2) {
slouken@3596
   524
        switch (blendMode) {
slouken@3596
   525
        case SDL_BLENDMODE_BLEND:
slouken@3596
   526
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB888, draw_end);
slouken@3596
   527
            break;
slouken@3596
   528
        case SDL_BLENDMODE_ADD:
slouken@3596
   529
            VLINE(Uint32, DRAW_SETPIXEL_ADD_RGB888, draw_end);
slouken@3596
   530
            break;
slouken@3596
   531
        case SDL_BLENDMODE_MOD:
slouken@3596
   532
            VLINE(Uint32, DRAW_SETPIXEL_MOD_RGB888, draw_end);
slouken@3596
   533
            break;
slouken@3596
   534
        default:
slouken@3596
   535
            VLINE(Uint32, DRAW_SETPIXEL_RGB888, draw_end);
slouken@3596
   536
            break;
slouken@3596
   537
        }
slouken@3596
   538
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   539
        switch (blendMode) {
slouken@3596
   540
        case SDL_BLENDMODE_BLEND:
slouken@3596
   541
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB888, draw_end);
slouken@3596
   542
            break;
slouken@3596
   543
        case SDL_BLENDMODE_ADD:
slouken@3596
   544
            DLINE(Uint32, DRAW_SETPIXEL_ADD_RGB888, draw_end);
slouken@3596
   545
            break;
slouken@3596
   546
        case SDL_BLENDMODE_MOD:
slouken@3596
   547
            DLINE(Uint32, DRAW_SETPIXEL_MOD_RGB888, draw_end);
slouken@3596
   548
            break;
slouken@3596
   549
        default:
slouken@3596
   550
            DLINE(Uint32, DRAW_SETPIXEL_RGB888, draw_end);
slouken@3596
   551
            break;
slouken@3596
   552
        }
slouken@3596
   553
    } else {
slouken@3596
   554
        switch (blendMode) {
slouken@3596
   555
        case SDL_BLENDMODE_BLEND:
slouken@3596
   556
            AALINE(x1, y1, x2, y2,
slouken@3596
   557
                   DRAW_SETPIXELXY_BLEND_RGB888, DRAW_SETPIXELXY_BLEND_RGB888,
slouken@3596
   558
                   draw_end);
slouken@3596
   559
            break;
slouken@3596
   560
        case SDL_BLENDMODE_ADD:
slouken@3596
   561
            AALINE(x1, y1, x2, y2,
slouken@3596
   562
                   DRAW_SETPIXELXY_ADD_RGB888, DRAW_SETPIXELXY_ADD_RGB888,
slouken@3596
   563
                   draw_end);
slouken@3596
   564
            break;
slouken@3596
   565
        case SDL_BLENDMODE_MOD:
slouken@3596
   566
            AALINE(x1, y1, x2, y2,
slouken@3596
   567
                   DRAW_SETPIXELXY_MOD_RGB888, DRAW_SETPIXELXY_MOD_RGB888,
slouken@3596
   568
                   draw_end);
slouken@2898
   569
            break;
slouken@2898
   570
        default:
slouken@3596
   571
            AALINE(x1, y1, x2, y2,
slouken@3596
   572
                   DRAW_SETPIXELXY_RGB888, DRAW_SETPIXELXY_BLEND_RGB888,
slouken@3596
   573
                   draw_end);
slouken@3596
   574
            break;
slouken@3596
   575
        }
slouken@3596
   576
    }
slouken@3596
   577
}
slouken@3596
   578
slouken@3596
   579
static void
slouken@3596
   580
SDL_BlendLine_ARGB8888(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   581
                       SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   582
                       SDL_bool draw_end)
slouken@3596
   583
{
slouken@3596
   584
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   585
    unsigned r, g, b, a, inva;
slouken@3596
   586
slouken@3596
   587
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   588
        r = DRAW_MUL(_r, _a);
slouken@3596
   589
        g = DRAW_MUL(_g, _a);
slouken@3596
   590
        b = DRAW_MUL(_b, _a);
slouken@3596
   591
        a = _a;
slouken@3596
   592
    } else {
slouken@3596
   593
        r = _r;
slouken@3596
   594
        g = _g;
slouken@3596
   595
        b = _b;
slouken@3596
   596
        a = _a;
slouken@3596
   597
    }
slouken@3596
   598
    inva = (a ^ 0xff);
slouken@3596
   599
slouken@3596
   600
    if (y1 == y2) {
slouken@3596
   601
        switch (blendMode) {
slouken@3596
   602
        case SDL_BLENDMODE_BLEND:
slouken@3596
   603
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888, draw_end);
slouken@3596
   604
            break;
slouken@3596
   605
        case SDL_BLENDMODE_ADD:
slouken@3596
   606
            HLINE(Uint32, DRAW_SETPIXEL_ADD_ARGB8888, draw_end);
slouken@3596
   607
            break;
slouken@3596
   608
        case SDL_BLENDMODE_MOD:
slouken@3596
   609
            HLINE(Uint32, DRAW_SETPIXEL_MOD_ARGB8888, draw_end);
slouken@3596
   610
            break;
slouken@3596
   611
        default:
slouken@3596
   612
            HLINE(Uint32, DRAW_SETPIXEL_ARGB8888, draw_end);
slouken@3596
   613
            break;
slouken@3596
   614
        }
slouken@3596
   615
    } else if (x1 == x2) {
slouken@3596
   616
        switch (blendMode) {
slouken@3596
   617
        case SDL_BLENDMODE_BLEND:
slouken@3596
   618
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888, draw_end);
slouken@3596
   619
            break;
slouken@3596
   620
        case SDL_BLENDMODE_ADD:
slouken@3596
   621
            VLINE(Uint32, DRAW_SETPIXEL_ADD_ARGB8888, draw_end);
slouken@3596
   622
            break;
slouken@3596
   623
        case SDL_BLENDMODE_MOD:
slouken@3596
   624
            VLINE(Uint32, DRAW_SETPIXEL_MOD_ARGB8888, draw_end);
slouken@3596
   625
            break;
slouken@3596
   626
        default:
slouken@3596
   627
            VLINE(Uint32, DRAW_SETPIXEL_ARGB8888, draw_end);
slouken@2898
   628
            break;
slouken@2898
   629
        }
slouken@3596
   630
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   631
        switch (blendMode) {
slouken@3596
   632
        case SDL_BLENDMODE_BLEND:
slouken@3596
   633
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888, draw_end);
slouken@3596
   634
            break;
slouken@3596
   635
        case SDL_BLENDMODE_ADD:
slouken@3596
   636
            DLINE(Uint32, DRAW_SETPIXEL_ADD_ARGB8888, draw_end);
slouken@3596
   637
            break;
slouken@3596
   638
        case SDL_BLENDMODE_MOD:
slouken@3596
   639
            DLINE(Uint32, DRAW_SETPIXEL_MOD_ARGB8888, draw_end);
slouken@3596
   640
            break;
slouken@3596
   641
        default:
slouken@3596
   642
            DLINE(Uint32, DRAW_SETPIXEL_ARGB8888, draw_end);
slouken@3596
   643
            break;
slouken@3596
   644
        }
slouken@3596
   645
    } else {
slouken@3596
   646
        switch (blendMode) {
slouken@3596
   647
        case SDL_BLENDMODE_BLEND:
slouken@3596
   648
            AALINE(x1, y1, x2, y2,
slouken@3596
   649
                   DRAW_SETPIXELXY_BLEND_ARGB8888, DRAW_SETPIXELXY_BLEND_ARGB8888,
slouken@3596
   650
                   draw_end);
slouken@3596
   651
            break;
slouken@3596
   652
        case SDL_BLENDMODE_ADD:
slouken@3596
   653
            AALINE(x1, y1, x2, y2,
slouken@3596
   654
                   DRAW_SETPIXELXY_ADD_ARGB8888, DRAW_SETPIXELXY_ADD_ARGB8888,
slouken@3596
   655
                   draw_end);
slouken@3596
   656
            break;
slouken@3596
   657
        case SDL_BLENDMODE_MOD:
slouken@3596
   658
            AALINE(x1, y1, x2, y2,
slouken@3596
   659
                   DRAW_SETPIXELXY_MOD_ARGB8888, DRAW_SETPIXELXY_MOD_ARGB8888,
slouken@3596
   660
                   draw_end);
slouken@3596
   661
            break;
slouken@3596
   662
        default:
slouken@3596
   663
            AALINE(x1, y1, x2, y2,
slouken@3596
   664
                   DRAW_SETPIXELXY_ARGB8888, DRAW_SETPIXELXY_BLEND_ARGB8888,
slouken@3596
   665
                   draw_end);
slouken@3596
   666
            break;
slouken@3596
   667
        }
slouken@2898
   668
    }
slouken@2898
   669
}
slouken@2888
   670
slouken@3596
   671
typedef void (*BlendLineFunc) (SDL_Surface * dst,
slouken@3596
   672
                               int x1, int y1, int x2, int y2,
slouken@4929
   673
                               SDL_BlendMode blendMode,
slouken@3596
   674
                               Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@3596
   675
                               SDL_bool draw_end);
slouken@3596
   676
slouken@3596
   677
static BlendLineFunc
slouken@3596
   678
SDL_CalculateBlendLineFunc(const SDL_PixelFormat * fmt)
slouken@3596
   679
{
slouken@3596
   680
    switch (fmt->BytesPerPixel) {
slouken@3596
   681
    case 2:
slouken@3596
   682
        if (fmt->Rmask == 0x7C00) {
slouken@3596
   683
            return SDL_BlendLine_RGB555;
slouken@3596
   684
        } else if (fmt->Rmask == 0xF800) {
slouken@3596
   685
            return SDL_BlendLine_RGB565;
slouken@3596
   686
        } else {
slouken@3596
   687
            return SDL_BlendLine_RGB2;
slouken@3596
   688
        }
slouken@3596
   689
        break;
slouken@3596
   690
    case 4:
slouken@3596
   691
        if (fmt->Rmask == 0x00FF0000) {
slouken@3596
   692
            if (fmt->Amask) {
slouken@3596
   693
                return SDL_BlendLine_ARGB8888;
slouken@3596
   694
            } else {
slouken@3596
   695
                return SDL_BlendLine_RGB888;
slouken@3596
   696
            }
slouken@3596
   697
        } else {
slouken@3596
   698
            if (fmt->Amask) {
slouken@3596
   699
                return SDL_BlendLine_RGBA4;
slouken@3596
   700
            } else {
slouken@3596
   701
                return SDL_BlendLine_RGB4;
slouken@3596
   702
            }
slouken@3596
   703
        }
slouken@3596
   704
    }
slouken@3596
   705
    return NULL;
slouken@3596
   706
}
slouken@3596
   707
slouken@2888
   708
int
slouken@2888
   709
SDL_BlendLine(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   710
              SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2888
   711
{
slouken@3596
   712
    BlendLineFunc func;
slouken@3596
   713
slouken@3596
   714
    if (!dst) {
slouken@3596
   715
        SDL_SetError("SDL_BlendLine(): Passed NULL destination surface");
slouken@3596
   716
        return -1;
slouken@3596
   717
    }
slouken@3596
   718
slouken@3596
   719
    func = SDL_CalculateBlendLineFunc(dst->format);
slouken@3596
   720
    if (!func) {
slouken@2888
   721
        SDL_SetError("SDL_BlendLine(): Unsupported surface format");
slouken@3596
   722
        return -1;
slouken@2888
   723
    }
slouken@2888
   724
slouken@2888
   725
    /* Perform clipping */
slouken@3596
   726
    /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@2910
   727
    if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3596
   728
        return 0;
slouken@2896
   729
    }
slouken@2898
   730
slouken@3596
   731
    func(dst, x1, y1, x2, y2, blendMode, r, g, b, a, SDL_TRUE);
slouken@3596
   732
    return 0;
slouken@2888
   733
}
slouken@2888
   734
slouken@3536
   735
int
slouken@3536
   736
SDL_BlendLines(SDL_Surface * dst, const SDL_Point * points, int count,
slouken@4929
   737
               SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@3536
   738
{
slouken@3536
   739
    int i;
slouken@3536
   740
    int x1, y1;
slouken@3536
   741
    int x2, y2;
slouken@3596
   742
    SDL_bool draw_end;
slouken@3596
   743
    BlendLineFunc func;
slouken@3536
   744
slouken@3536
   745
    if (!dst) {
slouken@3596
   746
        SDL_SetError("SDL_BlendLines(): Passed NULL destination surface");
slouken@3536
   747
        return -1;
slouken@3536
   748
    }
slouken@3536
   749
slouken@3596
   750
    func = SDL_CalculateBlendLineFunc(dst->format);
slouken@3536
   751
    if (!func) {
slouken@3596
   752
        SDL_SetError("SDL_BlendLines(): Unsupported surface format");
slouken@3596
   753
        return -1;
slouken@3536
   754
    }
slouken@3536
   755
slouken@3536
   756
    for (i = 1; i < count; ++i) {
slouken@3536
   757
        x1 = points[i-1].x;
slouken@3536
   758
        y1 = points[i-1].y;
slouken@3536
   759
        x2 = points[i].x;
slouken@3536
   760
        y2 = points[i].y;
slouken@3536
   761
slouken@3536
   762
        /* Perform clipping */
slouken@3536
   763
        /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@3536
   764
        if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3536
   765
            continue;
slouken@3536
   766
        }
slouken@3536
   767
slouken@3596
   768
        /* Draw the end if it was clipped */
slouken@3596
   769
        draw_end = (x2 != points[i].x || y2 != points[i].y);
slouken@3596
   770
slouken@3596
   771
        func(dst, x1, y1, x2, y2, blendMode, r, g, b, a, draw_end);
slouken@3594
   772
    }
slouken@3594
   773
    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@3596
   774
        SDL_BlendPoint(dst, points[count-1].x, points[count-1].y,
slouken@3596
   775
                       blendMode, r, g, b, a);
slouken@3536
   776
    }
slouken@3596
   777
    return 0;
slouken@3536
   778
}
slouken@3536
   779
slouken@2888
   780
/* vi: set ts=4 sw=4 expandtab: */