src/render/software/SDL_drawline.c
author Sam Lantinga
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6138 4c64952a58fb
child 7037 3fedf1f25b94
permissions -rw-r--r--
Happy New Year!
slouken@2888
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@2888
    21
#include "SDL_config.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
        //HLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
slouken@3671
    36
        int length;
slouken@3671
    37
        int pitch = (dst->pitch / dst->format->BytesPerPixel);
slouken@3671
    38
        Uint8 *pixel;
slouken@3671
    39
        if (x1 <= x2) {
slouken@3671
    40
            pixel = (Uint8 *)dst->pixels + y1 * pitch + x1;
slouken@3671
    41
            length = draw_end ? (x2-x1+1) : (x2-x1);
slouken@3671
    42
        } else {
slouken@3671
    43
            pixel = (Uint8 *)dst->pixels + y1 * pitch + x2;
slouken@3671
    44
            if (!draw_end) {
slouken@3671
    45
                ++pixel;
slouken@3671
    46
            }
slouken@3671
    47
            length = draw_end ? (x1-x2+1) : (x1-x2);
slouken@3671
    48
        }
slouken@3671
    49
        SDL_memset(pixel, color, length);
slouken@3596
    50
    } else if (x1 == x2) {
slouken@3596
    51
        VLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
slouken@3596
    52
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    53
        DLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
slouken@3596
    54
    } else {
slouken@3596
    55
        BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end);
slouken@3596
    56
    }
slouken@3596
    57
}
slouken@3596
    58
slouken@3596
    59
static void
slouken@3596
    60
SDL_DrawLine2(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
slouken@3596
    61
              SDL_bool draw_end)
slouken@3596
    62
{
slouken@3596
    63
    if (y1 == y2) {
slouken@3596
    64
        HLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
slouken@3596
    65
    } else if (x1 == x2) {
slouken@3596
    66
        VLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
slouken@3596
    67
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    68
        DLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
slouken@3596
    69
    } else {
slouken@3596
    70
        Uint8 _r, _g, _b, _a;
slouken@3596
    71
        const SDL_PixelFormat * fmt = dst->format;
slouken@3596
    72
        SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
slouken@3596
    73
        if (fmt->Rmask == 0x7C00) {
slouken@3596
    74
            AALINE(x1, y1, x2, y2,
slouken@3596
    75
                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB555,
slouken@3596
    76
                   draw_end);
slouken@3596
    77
        } else if (fmt->Rmask == 0xF800) {
slouken@3596
    78
            AALINE(x1, y1, x2, y2,
slouken@3596
    79
                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB565,
slouken@3596
    80
                   draw_end);
slouken@3596
    81
        } else {
slouken@3596
    82
            AALINE(x1, y1, x2, y2,
slouken@3596
    83
                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB,
slouken@3596
    84
                   draw_end);
slouken@3596
    85
        }
slouken@3596
    86
    }
slouken@3596
    87
}
slouken@3596
    88
slouken@3596
    89
static void
slouken@3596
    90
SDL_DrawLine4(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
slouken@3596
    91
              SDL_bool draw_end)
slouken@3596
    92
{
slouken@3596
    93
    if (y1 == y2) {
slouken@3596
    94
        HLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
slouken@3596
    95
    } else if (x1 == x2) {
slouken@3596
    96
        VLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
slouken@3596
    97
    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
slouken@3596
    98
        DLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
slouken@3596
    99
    } else {
slouken@3596
   100
        Uint8 _r, _g, _b, _a;
slouken@3596
   101
        const SDL_PixelFormat * fmt = dst->format;
slouken@3596
   102
        SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
slouken@3596
   103
        if (fmt->Rmask == 0x00FF0000) {
slouken@3596
   104
            if (!fmt->Amask) {
slouken@3596
   105
                AALINE(x1, y1, x2, y2,
slouken@3596
   106
                       DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_RGB888,
slouken@3596
   107
                       draw_end);
slouken@3596
   108
            } else {
slouken@3596
   109
                AALINE(x1, y1, x2, y2,
slouken@3596
   110
                       DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_ARGB8888,
slouken@3596
   111
                       draw_end);
slouken@3596
   112
            }
slouken@3596
   113
        } else {
slouken@3596
   114
            AALINE(x1, y1, x2, y2,
slouken@3596
   115
                   DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB,
slouken@3596
   116
                   draw_end);
slouken@3596
   117
        }
slouken@3596
   118
    }
slouken@3596
   119
}
slouken@3596
   120
slouken@3596
   121
typedef void (*DrawLineFunc) (SDL_Surface * dst,
slouken@3596
   122
                              int x1, int y1, int x2, int y2,
slouken@3596
   123
                              Uint32 color, SDL_bool draw_end);
slouken@3596
   124
slouken@3596
   125
static DrawLineFunc
slouken@3596
   126
SDL_CalculateDrawLineFunc(const SDL_PixelFormat * fmt)
slouken@3596
   127
{
slouken@3596
   128
    switch (fmt->BytesPerPixel) {
slouken@3596
   129
    case 1:
slouken@3596
   130
        if (fmt->BitsPerPixel < 8) {
slouken@3596
   131
            break;
slouken@3596
   132
        }
slouken@3596
   133
        return SDL_DrawLine1;
slouken@3596
   134
    case 2:
slouken@3596
   135
        return SDL_DrawLine2;
slouken@3596
   136
    case 4:
slouken@3596
   137
        return SDL_DrawLine4;
slouken@3596
   138
    }
slouken@3596
   139
    return NULL;
slouken@3596
   140
}
slouken@2890
   141
slouken@2901
   142
int
slouken@2888
   143
SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
slouken@2888
   144
{
slouken@3596
   145
    DrawLineFunc func;
slouken@3596
   146
slouken@3536
   147
    if (!dst) {
slouken@3596
   148
        SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
slouken@3536
   149
        return -1;
slouken@3536
   150
    }
slouken@3536
   151
slouken@3596
   152
    func = SDL_CalculateDrawLineFunc(dst->format);
slouken@3596
   153
    if (!func) {
slouken@2888
   154
        SDL_SetError("SDL_DrawLine(): Unsupported surface format");
slouken@3536
   155
        return -1;
slouken@2888
   156
    }
slouken@2888
   157
slouken@2888
   158
    /* Perform clipping */
slouken@3536
   159
    /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@2910
   160
    if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3596
   161
        return 0;
slouken@2909
   162
    }
slouken@2888
   163
slouken@3596
   164
    func(dst, x1, y1, x2, y2, color, SDL_TRUE);
slouken@2890
   165
    return 0;
slouken@2888
   166
}
slouken@2888
   167
slouken@3536
   168
int
slouken@3536
   169
SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
slouken@3536
   170
              Uint32 color)
slouken@3536
   171
{
slouken@3536
   172
    int i;
slouken@3596
   173
    int x1, y1;
slouken@3596
   174
    int x2, y2;
slouken@3596
   175
    SDL_bool draw_end;
slouken@3596
   176
    DrawLineFunc func;
slouken@3536
   177
slouken@3536
   178
    if (!dst) {
slouken@3596
   179
        SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
slouken@3536
   180
        return -1;
slouken@3536
   181
    }
slouken@3536
   182
slouken@3596
   183
    func = SDL_CalculateDrawLineFunc(dst->format);
slouken@3596
   184
    if (!func) {
slouken@3596
   185
        SDL_SetError("SDL_DrawLines(): Unsupported surface format");
slouken@3536
   186
        return -1;
slouken@3536
   187
    }
slouken@3536
   188
slouken@3536
   189
    for (i = 1; i < count; ++i) {
slouken@3596
   190
        x1 = points[i-1].x;
slouken@3596
   191
        y1 = points[i-1].y;
slouken@3596
   192
        x2 = points[i].x;
slouken@3596
   193
        y2 = points[i].y;
slouken@3536
   194
slouken@3536
   195
        /* Perform clipping */
slouken@3536
   196
        /* FIXME: We don't actually want to clip, as it may change line slope */
slouken@3536
   197
        if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
slouken@3536
   198
            continue;
slouken@3536
   199
        }
slouken@3536
   200
slouken@3596
   201
        /* Draw the end if it was clipped */
slouken@3596
   202
        draw_end = (x2 != points[i].x || y2 != points[i].y);
slouken@3596
   203
slouken@3596
   204
        func(dst, x1, y1, x2, y2, color, draw_end);
slouken@3596
   205
    }
slouken@3596
   206
    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@3596
   207
        SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
slouken@3536
   208
    }
slouken@3536
   209
    return 0;
slouken@3536
   210
}
slouken@3536
   211
slouken@5226
   212
#endif /* !SDL_RENDER_DISABLED */
slouken@5226
   213
slouken@2888
   214
/* vi: set ts=4 sw=4 expandtab: */