src/render/software/SDL_blendline.c
author Sam Lantinga
Thu, 03 Feb 2011 02:45:29 -0800
changeset 5163 d72793305335
parent 5140 src/video/SDL_blendline.c@e743b9c3f6d6
child 5166 4d39eeaad00b
permissions -rw-r--r--
Making the API simpler, moved the surface drawing functions to the software renderer.
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@5163
    25
#include "SDL_blendline.h"
slouken@2898
    26
slouken@3596
    27
slouken@3596
    28
static void
slouken@3596
    29
SDL_BlendLine_RGB2(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
    30
                   SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
    31
                   SDL_bool draw_end)
slouken@2898
    32
{
slouken@3596
    33
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
    34
    unsigned r, g, b, a, inva;
slouken@3596
    35
slouken@3596
    36
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
    37
        r = DRAW_MUL(_r, _a);
slouken@3596
    38
        g = DRAW_MUL(_g, _a);
slouken@3596
    39
        b = DRAW_MUL(_b, _a);
slouken@3596
    40
        a = _a;
slouken@3596
    41
    } else {
slouken@3596
    42
        r = _r;
slouken@3596
    43
        g = _g;
slouken@3596
    44
        b = _b;
slouken@3596
    45
        a = _a;
slouken@3596
    46
    }
slouken@3596
    47
    inva = (a ^ 0xff);
slouken@2888
    48
slouken@3596
    49
    if (y1 == y2) {
slouken@3596
    50
        switch (blendMode) {
slouken@3596
    51
        case SDL_BLENDMODE_BLEND:
slouken@3596
    52
            HLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
    53
            break;
slouken@3596
    54
        case SDL_BLENDMODE_ADD:
slouken@3596
    55
            HLINE(Uint16, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
    56
            break;
slouken@3596
    57
        default:
slouken@3596
    58
            HLINE(Uint16, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
    59
            break;
slouken@3596
    60
        }
slouken@3596
    61
    } else if (x1 == x2) {
slouken@3596
    62
        switch (blendMode) {
slouken@3596
    63
        case SDL_BLENDMODE_BLEND:
slouken@3596
    64
            VLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
    65
            break;
slouken@3596
    66
        case SDL_BLENDMODE_ADD:
slouken@3596
    67
            VLINE(Uint16, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
    68
            break;
slouken@3596
    69
        default:
slouken@3596
    70
            VLINE(Uint16, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
    71
            break;
slouken@3596
    72
        }
slouken@3596
    73
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    74
        switch (blendMode) {
slouken@3596
    75
        case SDL_BLENDMODE_BLEND:
slouken@3596
    76
            DLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
    77
            break;
slouken@3596
    78
        case SDL_BLENDMODE_ADD:
slouken@3596
    79
            DLINE(Uint16, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
    80
            break;
slouken@3596
    81
        default:
slouken@3596
    82
            DLINE(Uint16, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
    83
            break;
slouken@3596
    84
        }
slouken@3596
    85
    } else {
slouken@3596
    86
        switch (blendMode) {
slouken@3596
    87
        case SDL_BLENDMODE_BLEND:
slouken@3596
    88
            AALINE(x1, y1, x2, y2,
slouken@3596
    89
                   DRAW_SETPIXELXY2_BLEND_RGB, DRAW_SETPIXELXY2_BLEND_RGB,
slouken@3596
    90
                   draw_end);
slouken@3596
    91
            break;
slouken@3596
    92
        case SDL_BLENDMODE_ADD:
slouken@3596
    93
            AALINE(x1, y1, x2, y2,
slouken@3596
    94
                   DRAW_SETPIXELXY2_ADD_RGB, DRAW_SETPIXELXY2_ADD_RGB,
slouken@3596
    95
                   draw_end);
slouken@3596
    96
            break;
slouken@3596
    97
        default:
slouken@3596
    98
            AALINE(x1, y1, x2, y2,
slouken@3596
    99
                   DRAW_SETPIXELXY2_RGB, DRAW_SETPIXELXY2_BLEND_RGB,
slouken@3596
   100
                   draw_end);
slouken@3596
   101
            break;
slouken@3596
   102
        }
slouken@2898
   103
    }
slouken@2898
   104
}
slouken@2896
   105
slouken@3596
   106
static void
slouken@3596
   107
SDL_BlendLine_RGB555(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   108
                     SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3594
   109
                     SDL_bool draw_end)
slouken@2898
   110
{
slouken@3596
   111
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   112
    unsigned r, g, b, a, inva;
slouken@2898
   113
slouken@3596
   114
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   115
        r = DRAW_MUL(_r, _a);
slouken@3596
   116
        g = DRAW_MUL(_g, _a);
slouken@3596
   117
        b = DRAW_MUL(_b, _a);
slouken@3596
   118
        a = _a;
slouken@3596
   119
    } else {
slouken@3596
   120
        r = _r;
slouken@3596
   121
        g = _g;
slouken@3596
   122
        b = _b;
slouken@3596
   123
        a = _a;
slouken@2898
   124
    }
slouken@3596
   125
    inva = (a ^ 0xff);
slouken@2896
   126
slouken@3596
   127
    if (y1 == y2) {
slouken@3596
   128
        switch (blendMode) {
slouken@3596
   129
        case SDL_BLENDMODE_BLEND:
slouken@3596
   130
            HLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB555, draw_end);
slouken@3596
   131
            break;
slouken@3596
   132
        case SDL_BLENDMODE_ADD:
slouken@3596
   133
            HLINE(Uint16, DRAW_SETPIXEL_ADD_RGB555, draw_end);
slouken@3596
   134
            break;
slouken@3596
   135
        default:
slouken@3596
   136
            HLINE(Uint16, DRAW_SETPIXEL_RGB555, draw_end);
slouken@3596
   137
            break;
slouken@3596
   138
        }
slouken@3596
   139
    } else if (x1 == x2) {
slouken@3596
   140
        switch (blendMode) {
slouken@3596
   141
        case SDL_BLENDMODE_BLEND:
slouken@3596
   142
            VLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB555, draw_end);
slouken@3596
   143
            break;
slouken@3596
   144
        case SDL_BLENDMODE_ADD:
slouken@3596
   145
            VLINE(Uint16, DRAW_SETPIXEL_ADD_RGB555, draw_end);
slouken@3596
   146
            break;
slouken@3596
   147
        default:
slouken@3596
   148
            VLINE(Uint16, DRAW_SETPIXEL_RGB555, draw_end);
slouken@3596
   149
            break;
slouken@3596
   150
        }
slouken@3596
   151
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   152
        switch (blendMode) {
slouken@3596
   153
        case SDL_BLENDMODE_BLEND:
slouken@3596
   154
            DLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB555, draw_end);
slouken@3596
   155
            break;
slouken@3596
   156
        case SDL_BLENDMODE_ADD:
slouken@3596
   157
            DLINE(Uint16, DRAW_SETPIXEL_ADD_RGB555, draw_end);
slouken@3596
   158
            break;
slouken@3596
   159
        default:
slouken@3596
   160
            DLINE(Uint16, DRAW_SETPIXEL_RGB555, draw_end);
slouken@3596
   161
            break;
slouken@3596
   162
        }
slouken@3596
   163
    } else {
slouken@3596
   164
        switch (blendMode) {
slouken@3596
   165
        case SDL_BLENDMODE_BLEND:
slouken@3596
   166
            AALINE(x1, y1, x2, y2,
slouken@3596
   167
                   DRAW_SETPIXELXY_BLEND_RGB555, DRAW_SETPIXELXY_BLEND_RGB555,
slouken@3596
   168
                   draw_end);
slouken@3596
   169
            break;
slouken@3596
   170
        case SDL_BLENDMODE_ADD:
slouken@3596
   171
            AALINE(x1, y1, x2, y2,
slouken@3596
   172
                   DRAW_SETPIXELXY_ADD_RGB555, DRAW_SETPIXELXY_ADD_RGB555,
slouken@3596
   173
                   draw_end);
slouken@3596
   174
            break;
slouken@3596
   175
        default:
slouken@3596
   176
            AALINE(x1, y1, x2, y2,
slouken@3596
   177
                   DRAW_SETPIXELXY_RGB555, DRAW_SETPIXELXY_BLEND_RGB555,
slouken@3596
   178
                   draw_end);
slouken@3596
   179
            break;
slouken@3596
   180
        }
slouken@2899
   181
    }
slouken@2899
   182
}
slouken@2899
   183
slouken@3596
   184
static void
slouken@3596
   185
SDL_BlendLine_RGB565(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   186
                     SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   187
                     SDL_bool draw_end)
slouken@2898
   188
{
slouken@3596
   189
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   190
    unsigned r, g, b, a, inva;
slouken@2896
   191
slouken@3596
   192
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   193
        r = DRAW_MUL(_r, _a);
slouken@3596
   194
        g = DRAW_MUL(_g, _a);
slouken@3596
   195
        b = DRAW_MUL(_b, _a);
slouken@3596
   196
        a = _a;
slouken@3596
   197
    } else {
slouken@3596
   198
        r = _r;
slouken@3596
   199
        g = _g;
slouken@3596
   200
        b = _b;
slouken@3596
   201
        a = _a;
slouken@3596
   202
    }
slouken@3596
   203
    inva = (a ^ 0xff);
slouken@3596
   204
slouken@3596
   205
    if (y1 == y2) {
slouken@2898
   206
        switch (blendMode) {
slouken@2898
   207
        case SDL_BLENDMODE_BLEND:
slouken@3596
   208
            HLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB565, draw_end);
slouken@2898
   209
            break;
slouken@2898
   210
        case SDL_BLENDMODE_ADD:
slouken@3596
   211
            HLINE(Uint16, DRAW_SETPIXEL_ADD_RGB565, draw_end);
slouken@2898
   212
            break;
slouken@2898
   213
        default:
slouken@3596
   214
            HLINE(Uint16, DRAW_SETPIXEL_RGB565, draw_end);
slouken@2898
   215
            break;
slouken@2898
   216
        }
slouken@3596
   217
    } else if (x1 == x2) {
slouken@2898
   218
        switch (blendMode) {
slouken@2898
   219
        case SDL_BLENDMODE_BLEND:
slouken@3596
   220
            VLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB565, draw_end);
slouken@3596
   221
            break;
slouken@3596
   222
        case SDL_BLENDMODE_ADD:
slouken@3596
   223
            VLINE(Uint16, DRAW_SETPIXEL_ADD_RGB565, draw_end);
slouken@3596
   224
            break;
slouken@3596
   225
        default:
slouken@3596
   226
            VLINE(Uint16, DRAW_SETPIXEL_RGB565, draw_end);
slouken@3596
   227
            break;
slouken@3596
   228
        }
slouken@3596
   229
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   230
        switch (blendMode) {
slouken@3596
   231
        case SDL_BLENDMODE_BLEND:
slouken@3596
   232
            DLINE(Uint16, DRAW_SETPIXEL_BLEND_RGB565, draw_end);
slouken@2898
   233
            break;
slouken@2898
   234
        case SDL_BLENDMODE_ADD:
slouken@3596
   235
            DLINE(Uint16, DRAW_SETPIXEL_ADD_RGB565, draw_end);
slouken@2898
   236
            break;
slouken@2898
   237
        default:
slouken@3596
   238
            DLINE(Uint16, DRAW_SETPIXEL_RGB565, draw_end);
slouken@2898
   239
            break;
slouken@2898
   240
        }
slouken@3596
   241
    } else {
slouken@3596
   242
        switch (blendMode) {
slouken@3596
   243
        case SDL_BLENDMODE_BLEND:
slouken@3596
   244
            AALINE(x1, y1, x2, y2,
slouken@3596
   245
                   DRAW_SETPIXELXY_BLEND_RGB565, DRAW_SETPIXELXY_BLEND_RGB565,
slouken@3596
   246
                   draw_end);
slouken@3596
   247
            break;
slouken@3596
   248
        case SDL_BLENDMODE_ADD:
slouken@3596
   249
            AALINE(x1, y1, x2, y2,
slouken@3596
   250
                   DRAW_SETPIXELXY_ADD_RGB565, DRAW_SETPIXELXY_ADD_RGB565,
slouken@3596
   251
                   draw_end);
slouken@3596
   252
            break;
slouken@3596
   253
        default:
slouken@3596
   254
            AALINE(x1, y1, x2, y2,
slouken@3596
   255
                   DRAW_SETPIXELXY_RGB565, DRAW_SETPIXELXY_BLEND_RGB565,
slouken@3596
   256
                   draw_end);
slouken@3596
   257
            break;
slouken@3596
   258
        }
slouken@2898
   259
    }
slouken@2898
   260
}
slouken@2896
   261
slouken@3596
   262
static void
slouken@3596
   263
SDL_BlendLine_RGB4(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   264
                   SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3594
   265
                   SDL_bool draw_end)
slouken@2898
   266
{
slouken@3596
   267
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   268
    unsigned r, g, b, a, inva;
slouken@3596
   269
slouken@3596
   270
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   271
        r = DRAW_MUL(_r, _a);
slouken@3596
   272
        g = DRAW_MUL(_g, _a);
slouken@3596
   273
        b = DRAW_MUL(_b, _a);
slouken@3596
   274
        a = _a;
slouken@3596
   275
    } else {
slouken@3596
   276
        r = _r;
slouken@3596
   277
        g = _g;
slouken@3596
   278
        b = _b;
slouken@3596
   279
        a = _a;
slouken@3596
   280
    }
slouken@3596
   281
    inva = (a ^ 0xff);
slouken@2896
   282
slouken@3596
   283
    if (y1 == y2) {
slouken@3596
   284
        switch (blendMode) {
slouken@3596
   285
        case SDL_BLENDMODE_BLEND:
slouken@3596
   286
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
   287
            break;
slouken@3596
   288
        case SDL_BLENDMODE_ADD:
slouken@3596
   289
            HLINE(Uint32, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
   290
            break;
slouken@3596
   291
        default:
slouken@3596
   292
            HLINE(Uint32, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
   293
            break;
slouken@3596
   294
        }
slouken@3596
   295
    } else if (x1 == x2) {
slouken@3596
   296
        switch (blendMode) {
slouken@3596
   297
        case SDL_BLENDMODE_BLEND:
slouken@3596
   298
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
   299
            break;
slouken@3596
   300
        case SDL_BLENDMODE_ADD:
slouken@3596
   301
            VLINE(Uint32, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
   302
            break;
slouken@3596
   303
        default:
slouken@3596
   304
            VLINE(Uint32, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
   305
            break;
slouken@3596
   306
        }
slouken@3596
   307
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   308
        switch (blendMode) {
slouken@3596
   309
        case SDL_BLENDMODE_BLEND:
slouken@3596
   310
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB, draw_end);
slouken@3596
   311
            break;
slouken@3596
   312
        case SDL_BLENDMODE_ADD:
slouken@3596
   313
            DLINE(Uint32, DRAW_SETPIXEL_ADD_RGB, draw_end);
slouken@3596
   314
            break;
slouken@3596
   315
        default:
slouken@3596
   316
            DLINE(Uint32, DRAW_SETPIXEL_RGB, draw_end);
slouken@3596
   317
            break;
slouken@3596
   318
        }
slouken@3596
   319
    } else {
slouken@2898
   320
        switch (blendMode) {
slouken@2898
   321
        case SDL_BLENDMODE_BLEND:
slouken@3596
   322
            AALINE(x1, y1, x2, y2,
slouken@3596
   323
                   DRAW_SETPIXELXY4_BLEND_RGB, DRAW_SETPIXELXY4_BLEND_RGB,
slouken@3596
   324
                   draw_end);
slouken@3596
   325
            break;
slouken@3596
   326
        case SDL_BLENDMODE_ADD:
slouken@3596
   327
            AALINE(x1, y1, x2, y2,
slouken@3596
   328
                   DRAW_SETPIXELXY4_ADD_RGB, DRAW_SETPIXELXY4_ADD_RGB,
slouken@3596
   329
                   draw_end);
slouken@3596
   330
            break;
slouken@3596
   331
        default:
slouken@3596
   332
            AALINE(x1, y1, x2, y2,
slouken@3596
   333
                   DRAW_SETPIXELXY4_RGB, DRAW_SETPIXELXY4_BLEND_RGB,
slouken@3596
   334
                   draw_end);
slouken@3596
   335
            break;
slouken@3596
   336
        }
slouken@3596
   337
    }
slouken@3596
   338
}
slouken@3596
   339
slouken@3596
   340
static void
slouken@3596
   341
SDL_BlendLine_RGBA4(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   342
                    SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   343
                    SDL_bool draw_end)
slouken@3596
   344
{
slouken@3596
   345
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   346
    unsigned r, g, b, a, inva;
slouken@3596
   347
slouken@3596
   348
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   349
        r = DRAW_MUL(_r, _a);
slouken@3596
   350
        g = DRAW_MUL(_g, _a);
slouken@3596
   351
        b = DRAW_MUL(_b, _a);
slouken@3596
   352
        a = _a;
slouken@3596
   353
    } else {
slouken@3596
   354
        r = _r;
slouken@3596
   355
        g = _g;
slouken@3596
   356
        b = _b;
slouken@3596
   357
        a = _a;
slouken@3596
   358
    }
slouken@3596
   359
    inva = (a ^ 0xff);
slouken@3596
   360
slouken@3596
   361
    if (y1 == y2) {
slouken@3596
   362
        switch (blendMode) {
slouken@3596
   363
        case SDL_BLENDMODE_BLEND:
slouken@3596
   364
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_RGBA, draw_end);
slouken@3596
   365
            break;
slouken@3596
   366
        case SDL_BLENDMODE_ADD:
slouken@3596
   367
            HLINE(Uint32, DRAW_SETPIXEL_ADD_RGBA, draw_end);
slouken@3596
   368
            break;
slouken@3596
   369
        default:
slouken@3596
   370
            HLINE(Uint32, DRAW_SETPIXEL_RGBA, draw_end);
slouken@3596
   371
            break;
slouken@3596
   372
        }
slouken@3596
   373
    } else if (x1 == x2) {
slouken@3596
   374
        switch (blendMode) {
slouken@3596
   375
        case SDL_BLENDMODE_BLEND:
slouken@3596
   376
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_RGBA, draw_end);
slouken@3596
   377
            break;
slouken@3596
   378
        case SDL_BLENDMODE_ADD:
slouken@3596
   379
            VLINE(Uint32, DRAW_SETPIXEL_ADD_RGBA, draw_end);
slouken@3596
   380
            break;
slouken@3596
   381
        default:
slouken@3596
   382
            VLINE(Uint32, DRAW_SETPIXEL_RGBA, draw_end);
slouken@3596
   383
            break;
slouken@3596
   384
        }
slouken@3596
   385
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   386
        switch (blendMode) {
slouken@3596
   387
        case SDL_BLENDMODE_BLEND:
slouken@3596
   388
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_RGBA, draw_end);
slouken@3596
   389
            break;
slouken@3596
   390
        case SDL_BLENDMODE_ADD:
slouken@3596
   391
            DLINE(Uint32, DRAW_SETPIXEL_ADD_RGBA, draw_end);
slouken@3596
   392
            break;
slouken@3596
   393
        default:
slouken@3596
   394
            DLINE(Uint32, DRAW_SETPIXEL_RGBA, draw_end);
slouken@3596
   395
            break;
slouken@3596
   396
        }
slouken@3596
   397
    } else {
slouken@3596
   398
        switch (blendMode) {
slouken@3596
   399
        case SDL_BLENDMODE_BLEND:
slouken@3596
   400
            AALINE(x1, y1, x2, y2,
slouken@3596
   401
                   DRAW_SETPIXELXY4_BLEND_RGBA, DRAW_SETPIXELXY4_BLEND_RGBA,
slouken@3596
   402
                   draw_end);
slouken@2898
   403
            break;
slouken@2898
   404
        case SDL_BLENDMODE_ADD:
slouken@3596
   405
            AALINE(x1, y1, x2, y2,
slouken@3596
   406
                   DRAW_SETPIXELXY4_ADD_RGBA, DRAW_SETPIXELXY4_ADD_RGBA,
slouken@3596
   407
                   draw_end);
slouken@3596
   408
            break;
slouken@3596
   409
        default:
slouken@3596
   410
            AALINE(x1, y1, x2, y2,
slouken@3596
   411
                   DRAW_SETPIXELXY4_RGBA, DRAW_SETPIXELXY4_BLEND_RGBA,
slouken@3596
   412
                   draw_end);
slouken@3596
   413
            break;
slouken@3596
   414
        }
slouken@3596
   415
    }
slouken@3596
   416
}
slouken@3596
   417
slouken@3596
   418
static void
slouken@3596
   419
SDL_BlendLine_RGB888(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   420
                     SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   421
                     SDL_bool draw_end)
slouken@3596
   422
{
slouken@3596
   423
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   424
    unsigned r, g, b, a, inva;
slouken@3596
   425
slouken@3596
   426
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   427
        r = DRAW_MUL(_r, _a);
slouken@3596
   428
        g = DRAW_MUL(_g, _a);
slouken@3596
   429
        b = DRAW_MUL(_b, _a);
slouken@3596
   430
        a = _a;
slouken@3596
   431
    } else {
slouken@3596
   432
        r = _r;
slouken@3596
   433
        g = _g;
slouken@3596
   434
        b = _b;
slouken@3596
   435
        a = _a;
slouken@3596
   436
    }
slouken@3596
   437
    inva = (a ^ 0xff);
slouken@3596
   438
slouken@3596
   439
    if (y1 == y2) {
slouken@3596
   440
        switch (blendMode) {
slouken@3596
   441
        case SDL_BLENDMODE_BLEND:
slouken@3596
   442
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB888, draw_end);
slouken@3596
   443
            break;
slouken@3596
   444
        case SDL_BLENDMODE_ADD:
slouken@3596
   445
            HLINE(Uint32, DRAW_SETPIXEL_ADD_RGB888, draw_end);
slouken@2898
   446
            break;
slouken@3596
   447
        default:
slouken@3596
   448
            HLINE(Uint32, DRAW_SETPIXEL_RGB888, draw_end);
slouken@3596
   449
            break;
slouken@3596
   450
        }
slouken@3596
   451
    } else if (x1 == x2) {
slouken@3596
   452
        switch (blendMode) {
slouken@3596
   453
        case SDL_BLENDMODE_BLEND:
slouken@3596
   454
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB888, draw_end);
slouken@3596
   455
            break;
slouken@3596
   456
        case SDL_BLENDMODE_ADD:
slouken@3596
   457
            VLINE(Uint32, DRAW_SETPIXEL_ADD_RGB888, draw_end);
slouken@3596
   458
            break;
slouken@3596
   459
        default:
slouken@3596
   460
            VLINE(Uint32, DRAW_SETPIXEL_RGB888, draw_end);
slouken@3596
   461
            break;
slouken@3596
   462
        }
slouken@3596
   463
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   464
        switch (blendMode) {
slouken@3596
   465
        case SDL_BLENDMODE_BLEND:
slouken@3596
   466
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_RGB888, draw_end);
slouken@3596
   467
            break;
slouken@3596
   468
        case SDL_BLENDMODE_ADD:
slouken@3596
   469
            DLINE(Uint32, DRAW_SETPIXEL_ADD_RGB888, draw_end);
slouken@3596
   470
            break;
slouken@3596
   471
        default:
slouken@3596
   472
            DLINE(Uint32, DRAW_SETPIXEL_RGB888, draw_end);
slouken@3596
   473
            break;
slouken@3596
   474
        }
slouken@3596
   475
    } else {
slouken@3596
   476
        switch (blendMode) {
slouken@3596
   477
        case SDL_BLENDMODE_BLEND:
slouken@3596
   478
            AALINE(x1, y1, x2, y2,
slouken@3596
   479
                   DRAW_SETPIXELXY_BLEND_RGB888, DRAW_SETPIXELXY_BLEND_RGB888,
slouken@3596
   480
                   draw_end);
slouken@3596
   481
            break;
slouken@3596
   482
        case SDL_BLENDMODE_ADD:
slouken@3596
   483
            AALINE(x1, y1, x2, y2,
slouken@3596
   484
                   DRAW_SETPIXELXY_ADD_RGB888, DRAW_SETPIXELXY_ADD_RGB888,
slouken@3596
   485
                   draw_end);
slouken@3596
   486
            break;
slouken@2898
   487
        default:
slouken@3596
   488
            AALINE(x1, y1, x2, y2,
slouken@3596
   489
                   DRAW_SETPIXELXY_RGB888, DRAW_SETPIXELXY_BLEND_RGB888,
slouken@3596
   490
                   draw_end);
slouken@3596
   491
            break;
slouken@3596
   492
        }
slouken@3596
   493
    }
slouken@3596
   494
}
slouken@3596
   495
slouken@3596
   496
static void
slouken@3596
   497
SDL_BlendLine_ARGB8888(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   498
                       SDL_BlendMode blendMode, Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a,
slouken@3596
   499
                       SDL_bool draw_end)
slouken@3596
   500
{
slouken@3596
   501
    const SDL_PixelFormat *fmt = dst->format;
slouken@3596
   502
    unsigned r, g, b, a, inva;
slouken@3596
   503
slouken@3596
   504
    if (blendMode == SDL_BLENDMODE_BLEND || blendMode == SDL_BLENDMODE_ADD) {
slouken@3596
   505
        r = DRAW_MUL(_r, _a);
slouken@3596
   506
        g = DRAW_MUL(_g, _a);
slouken@3596
   507
        b = DRAW_MUL(_b, _a);
slouken@3596
   508
        a = _a;
slouken@3596
   509
    } else {
slouken@3596
   510
        r = _r;
slouken@3596
   511
        g = _g;
slouken@3596
   512
        b = _b;
slouken@3596
   513
        a = _a;
slouken@3596
   514
    }
slouken@3596
   515
    inva = (a ^ 0xff);
slouken@3596
   516
slouken@3596
   517
    if (y1 == y2) {
slouken@3596
   518
        switch (blendMode) {
slouken@3596
   519
        case SDL_BLENDMODE_BLEND:
slouken@3596
   520
            HLINE(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888, draw_end);
slouken@3596
   521
            break;
slouken@3596
   522
        case SDL_BLENDMODE_ADD:
slouken@3596
   523
            HLINE(Uint32, DRAW_SETPIXEL_ADD_ARGB8888, draw_end);
slouken@3596
   524
            break;
slouken@3596
   525
        default:
slouken@3596
   526
            HLINE(Uint32, DRAW_SETPIXEL_ARGB8888, draw_end);
slouken@3596
   527
            break;
slouken@3596
   528
        }
slouken@3596
   529
    } else if (x1 == x2) {
slouken@3596
   530
        switch (blendMode) {
slouken@3596
   531
        case SDL_BLENDMODE_BLEND:
slouken@3596
   532
            VLINE(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888, draw_end);
slouken@3596
   533
            break;
slouken@3596
   534
        case SDL_BLENDMODE_ADD:
slouken@3596
   535
            VLINE(Uint32, DRAW_SETPIXEL_ADD_ARGB8888, draw_end);
slouken@3596
   536
            break;
slouken@3596
   537
        default:
slouken@3596
   538
            VLINE(Uint32, DRAW_SETPIXEL_ARGB8888, draw_end);
slouken@2898
   539
            break;
slouken@2898
   540
        }
slouken@3596
   541
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
   542
        switch (blendMode) {
slouken@3596
   543
        case SDL_BLENDMODE_BLEND:
slouken@3596
   544
            DLINE(Uint32, DRAW_SETPIXEL_BLEND_ARGB8888, draw_end);
slouken@3596
   545
            break;
slouken@3596
   546
        case SDL_BLENDMODE_ADD:
slouken@3596
   547
            DLINE(Uint32, DRAW_SETPIXEL_ADD_ARGB8888, draw_end);
slouken@3596
   548
            break;
slouken@3596
   549
        default:
slouken@3596
   550
            DLINE(Uint32, DRAW_SETPIXEL_ARGB8888, 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_ARGB8888, DRAW_SETPIXELXY_BLEND_ARGB8888,
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_ARGB8888, DRAW_SETPIXELXY_ADD_ARGB8888,
slouken@3596
   563
                   draw_end);
slouken@3596
   564
            break;
slouken@3596
   565
        default:
slouken@3596
   566
            AALINE(x1, y1, x2, y2,
slouken@3596
   567
                   DRAW_SETPIXELXY_ARGB8888, DRAW_SETPIXELXY_BLEND_ARGB8888,
slouken@3596
   568
                   draw_end);
slouken@3596
   569
            break;
slouken@3596
   570
        }
slouken@2898
   571
    }
slouken@2898
   572
}
slouken@2888
   573
slouken@3596
   574
typedef void (*BlendLineFunc) (SDL_Surface * dst,
slouken@3596
   575
                               int x1, int y1, int x2, int y2,
slouken@4929
   576
                               SDL_BlendMode blendMode,
slouken@3596
   577
                               Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@3596
   578
                               SDL_bool draw_end);
slouken@3596
   579
slouken@3596
   580
static BlendLineFunc
slouken@3596
   581
SDL_CalculateBlendLineFunc(const SDL_PixelFormat * fmt)
slouken@3596
   582
{
slouken@3596
   583
    switch (fmt->BytesPerPixel) {
slouken@3596
   584
    case 2:
slouken@3596
   585
        if (fmt->Rmask == 0x7C00) {
slouken@3596
   586
            return SDL_BlendLine_RGB555;
slouken@3596
   587
        } else if (fmt->Rmask == 0xF800) {
slouken@3596
   588
            return SDL_BlendLine_RGB565;
slouken@3596
   589
        } else {
slouken@3596
   590
            return SDL_BlendLine_RGB2;
slouken@3596
   591
        }
slouken@3596
   592
        break;
slouken@3596
   593
    case 4:
slouken@3596
   594
        if (fmt->Rmask == 0x00FF0000) {
slouken@3596
   595
            if (fmt->Amask) {
slouken@3596
   596
                return SDL_BlendLine_ARGB8888;
slouken@3596
   597
            } else {
slouken@3596
   598
                return SDL_BlendLine_RGB888;
slouken@3596
   599
            }
slouken@3596
   600
        } else {
slouken@3596
   601
            if (fmt->Amask) {
slouken@3596
   602
                return SDL_BlendLine_RGBA4;
slouken@3596
   603
            } else {
slouken@3596
   604
                return SDL_BlendLine_RGB4;
slouken@3596
   605
            }
slouken@3596
   606
        }
slouken@3596
   607
    }
slouken@3596
   608
    return NULL;
slouken@3596
   609
}
slouken@3596
   610
slouken@2888
   611
int
slouken@2888
   612
SDL_BlendLine(SDL_Surface * dst, int x1, int y1, int x2, int y2,
slouken@4929
   613
              SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@2888
   614
{
slouken@3596
   615
    BlendLineFunc func;
slouken@3596
   616
slouken@3596
   617
    if (!dst) {
slouken@3596
   618
        SDL_SetError("SDL_BlendLine(): Passed NULL destination surface");
slouken@3596
   619
        return -1;
slouken@3596
   620
    }
slouken@3596
   621
slouken@3596
   622
    func = SDL_CalculateBlendLineFunc(dst->format);
slouken@3596
   623
    if (!func) {
slouken@2888
   624
        SDL_SetError("SDL_BlendLine(): Unsupported surface format");
slouken@3596
   625
        return -1;
slouken@2888
   626
    }
slouken@2888
   627
slouken@2888
   628
    /* Perform clipping */
slouken@3596
   629
    /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@2910
   630
    if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3596
   631
        return 0;
slouken@2896
   632
    }
slouken@2898
   633
slouken@3596
   634
    func(dst, x1, y1, x2, y2, blendMode, r, g, b, a, SDL_TRUE);
slouken@3596
   635
    return 0;
slouken@2888
   636
}
slouken@2888
   637
slouken@3536
   638
int
slouken@3536
   639
SDL_BlendLines(SDL_Surface * dst, const SDL_Point * points, int count,
slouken@4929
   640
               SDL_BlendMode blendMode, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
slouken@3536
   641
{
slouken@3536
   642
    int i;
slouken@3536
   643
    int x1, y1;
slouken@3536
   644
    int x2, y2;
slouken@3596
   645
    SDL_bool draw_end;
slouken@3596
   646
    BlendLineFunc func;
slouken@3536
   647
slouken@3536
   648
    if (!dst) {
slouken@3596
   649
        SDL_SetError("SDL_BlendLines(): Passed NULL destination surface");
slouken@3536
   650
        return -1;
slouken@3536
   651
    }
slouken@3536
   652
slouken@3596
   653
    func = SDL_CalculateBlendLineFunc(dst->format);
slouken@3536
   654
    if (!func) {
slouken@3596
   655
        SDL_SetError("SDL_BlendLines(): Unsupported surface format");
slouken@3596
   656
        return -1;
slouken@3536
   657
    }
slouken@3536
   658
slouken@3536
   659
    for (i = 1; i < count; ++i) {
slouken@3536
   660
        x1 = points[i-1].x;
slouken@3536
   661
        y1 = points[i-1].y;
slouken@3536
   662
        x2 = points[i].x;
slouken@3536
   663
        y2 = points[i].y;
slouken@3536
   664
slouken@3536
   665
        /* Perform clipping */
slouken@3536
   666
        /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@3536
   667
        if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3536
   668
            continue;
slouken@3536
   669
        }
slouken@3536
   670
slouken@3596
   671
        /* Draw the end if it was clipped */
slouken@3596
   672
        draw_end = (x2 != points[i].x || y2 != points[i].y);
slouken@3596
   673
slouken@3596
   674
        func(dst, x1, y1, x2, y2, blendMode, r, g, b, a, draw_end);
slouken@3594
   675
    }
slouken@3594
   676
    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@3596
   677
        SDL_BlendPoint(dst, points[count-1].x, points[count-1].y,
slouken@3596
   678
                       blendMode, r, g, b, a);
slouken@3536
   679
    }
slouken@3596
   680
    return 0;
slouken@3536
   681
}
slouken@3536
   682
slouken@2888
   683
/* vi: set ts=4 sw=4 expandtab: */