src/render/software/SDL_drawline.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
slouken@2888
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@2888
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@2888
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@2888
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2888
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@2888
    22
slouken@5226
    23
#if !SDL_RENDER_DISABLED
slouken@5226
    24
slouken@2898
    25
#include "SDL_draw.h"
slouken@5163
    26
#include "SDL_drawline.h"
slouken@5166
    27
#include "SDL_drawpoint.h"
slouken@5163
    28
slouken@2890
    29
slouken@3596
    30
static void
slouken@3596
    31
SDL_DrawLine1(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
slouken@3596
    32
              SDL_bool draw_end)
slouken@3596
    33
{
slouken@3596
    34
    if (y1 == y2) {
slouken@3671
    35
        int length;
slouken@3671
    36
        int pitch = (dst->pitch / dst->format->BytesPerPixel);
slouken@3671
    37
        Uint8 *pixel;
slouken@3671
    38
        if (x1 <= x2) {
slouken@3671
    39
            pixel = (Uint8 *)dst->pixels + y1 * pitch + x1;
slouken@3671
    40
            length = draw_end ? (x2-x1+1) : (x2-x1);
slouken@3671
    41
        } else {
slouken@3671
    42
            pixel = (Uint8 *)dst->pixels + y1 * pitch + x2;
slouken@3671
    43
            if (!draw_end) {
slouken@3671
    44
                ++pixel;
slouken@3671
    45
            }
slouken@3671
    46
            length = draw_end ? (x1-x2+1) : (x1-x2);
slouken@3671
    47
        }
slouken@3671
    48
        SDL_memset(pixel, color, length);
slouken@3596
    49
    } else if (x1 == x2) {
slouken@3596
    50
        VLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
slouken@3596
    51
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    52
        DLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
slouken@3596
    53
    } else {
slouken@3596
    54
        BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end);
slouken@3596
    55
    }
slouken@3596
    56
}
slouken@3596
    57
slouken@3596
    58
static void
slouken@3596
    59
SDL_DrawLine2(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
slouken@3596
    60
              SDL_bool draw_end)
slouken@3596
    61
{
slouken@3596
    62
    if (y1 == y2) {
slouken@3596
    63
        HLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
slouken@3596
    64
    } else if (x1 == x2) {
slouken@3596
    65
        VLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
slouken@3596
    66
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    67
        DLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
slouken@3596
    68
    } else {
slouken@3596
    69
        Uint8 _r, _g, _b, _a;
slouken@3596
    70
        const SDL_PixelFormat * fmt = dst->format;
slouken@3596
    71
        SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
slouken@3596
    72
        if (fmt->Rmask == 0x7C00) {
slouken@3596
    73
            AALINE(x1, y1, x2, y2,
slouken@3596
    74
                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB555,
slouken@3596
    75
                   draw_end);
slouken@3596
    76
        } else if (fmt->Rmask == 0xF800) {
slouken@3596
    77
            AALINE(x1, y1, x2, y2,
slouken@3596
    78
                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB565,
slouken@3596
    79
                   draw_end);
slouken@3596
    80
        } else {
slouken@3596
    81
            AALINE(x1, y1, x2, y2,
slouken@3596
    82
                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB,
slouken@3596
    83
                   draw_end);
slouken@3596
    84
        }
slouken@3596
    85
    }
slouken@3596
    86
}
slouken@3596
    87
slouken@3596
    88
static void
slouken@3596
    89
SDL_DrawLine4(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
slouken@3596
    90
              SDL_bool draw_end)
slouken@3596
    91
{
slouken@3596
    92
    if (y1 == y2) {
slouken@3596
    93
        HLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
slouken@3596
    94
    } else if (x1 == x2) {
slouken@3596
    95
        VLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
slouken@3596
    96
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    97
        DLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
slouken@3596
    98
    } else {
slouken@3596
    99
        Uint8 _r, _g, _b, _a;
slouken@3596
   100
        const SDL_PixelFormat * fmt = dst->format;
slouken@3596
   101
        SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
slouken@3596
   102
        if (fmt->Rmask == 0x00FF0000) {
slouken@3596
   103
            if (!fmt->Amask) {
slouken@3596
   104
                AALINE(x1, y1, x2, y2,
slouken@3596
   105
                       DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_RGB888,
slouken@3596
   106
                       draw_end);
slouken@3596
   107
            } else {
slouken@3596
   108
                AALINE(x1, y1, x2, y2,
slouken@3596
   109
                       DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_ARGB8888,
slouken@3596
   110
                       draw_end);
slouken@3596
   111
            }
slouken@3596
   112
        } else {
slouken@3596
   113
            AALINE(x1, y1, x2, y2,
slouken@3596
   114
                   DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB,
slouken@3596
   115
                   draw_end);
slouken@3596
   116
        }
slouken@3596
   117
    }
slouken@3596
   118
}
slouken@3596
   119
slouken@3596
   120
typedef void (*DrawLineFunc) (SDL_Surface * dst,
slouken@3596
   121
                              int x1, int y1, int x2, int y2,
slouken@3596
   122
                              Uint32 color, SDL_bool draw_end);
slouken@3596
   123
slouken@3596
   124
static DrawLineFunc
slouken@3596
   125
SDL_CalculateDrawLineFunc(const SDL_PixelFormat * fmt)
slouken@3596
   126
{
slouken@3596
   127
    switch (fmt->BytesPerPixel) {
slouken@3596
   128
    case 1:
slouken@3596
   129
        if (fmt->BitsPerPixel < 8) {
slouken@3596
   130
            break;
slouken@3596
   131
        }
slouken@3596
   132
        return SDL_DrawLine1;
slouken@3596
   133
    case 2:
slouken@3596
   134
        return SDL_DrawLine2;
slouken@3596
   135
    case 4:
slouken@3596
   136
        return SDL_DrawLine4;
slouken@3596
   137
    }
slouken@3596
   138
    return NULL;
slouken@3596
   139
}
slouken@2890
   140
slouken@2901
   141
int
slouken@2888
   142
SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
slouken@2888
   143
{
slouken@3596
   144
    DrawLineFunc func;
slouken@3596
   145
slouken@3536
   146
    if (!dst) {
icculus@7037
   147
        return SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
slouken@3536
   148
    }
slouken@3536
   149
slouken@3596
   150
    func = SDL_CalculateDrawLineFunc(dst->format);
slouken@3596
   151
    if (!func) {
icculus@7037
   152
        return SDL_SetError("SDL_DrawLine(): Unsupported surface format");
slouken@2888
   153
    }
slouken@2888
   154
slouken@2888
   155
    /* Perform clipping */
slouken@3536
   156
    /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@2910
   157
    if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3596
   158
        return 0;
slouken@2909
   159
    }
slouken@2888
   160
slouken@3596
   161
    func(dst, x1, y1, x2, y2, color, SDL_TRUE);
slouken@2890
   162
    return 0;
slouken@2888
   163
}
slouken@2888
   164
slouken@3536
   165
int
slouken@3536
   166
SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
slouken@3536
   167
              Uint32 color)
slouken@3536
   168
{
slouken@3536
   169
    int i;
slouken@3596
   170
    int x1, y1;
slouken@3596
   171
    int x2, y2;
slouken@3596
   172
    SDL_bool draw_end;
slouken@3596
   173
    DrawLineFunc func;
slouken@3536
   174
slouken@3536
   175
    if (!dst) {
icculus@7037
   176
        return SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
slouken@3536
   177
    }
slouken@3536
   178
slouken@3596
   179
    func = SDL_CalculateDrawLineFunc(dst->format);
slouken@3596
   180
    if (!func) {
icculus@7037
   181
        return SDL_SetError("SDL_DrawLines(): Unsupported surface format");
slouken@3536
   182
    }
slouken@3536
   183
slouken@3536
   184
    for (i = 1; i < count; ++i) {
slouken@3596
   185
        x1 = points[i-1].x;
slouken@3596
   186
        y1 = points[i-1].y;
slouken@3596
   187
        x2 = points[i].x;
slouken@3596
   188
        y2 = points[i].y;
slouken@3536
   189
slouken@3536
   190
        /* Perform clipping */
slouken@3536
   191
        /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@3536
   192
        if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3536
   193
            continue;
slouken@3536
   194
        }
slouken@3536
   195
slouken@3596
   196
        /* Draw the end if it was clipped */
slouken@3596
   197
        draw_end = (x2 != points[i].x || y2 != points[i].y);
slouken@3596
   198
slouken@3596
   199
        func(dst, x1, y1, x2, y2, color, draw_end);
slouken@3596
   200
    }
slouken@3596
   201
    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@3596
   202
        SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
slouken@3536
   203
    }
slouken@3536
   204
    return 0;
slouken@3536
   205
}
slouken@3536
   206
slouken@5226
   207
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   208
slouken@2888
   209
/* vi: set ts=4 sw=4 expandtab: */