src/render/software/SDL_drawline.c
changeset 5163 d72793305335
parent 3697 f7b03b6838cb
child 5166 4d39eeaad00b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/render/software/SDL_drawline.c	Thu Feb 03 02:45:29 2011 -0800
     1.3 @@ -0,0 +1,210 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include "SDL_draw.h"
    1.28 +#include "SDL_drawline.h"
    1.29 +
    1.30 +
    1.31 +static void
    1.32 +SDL_DrawLine1(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
    1.33 +              SDL_bool draw_end)
    1.34 +{
    1.35 +    if (y1 == y2) {
    1.36 +        //HLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
    1.37 +        int length;
    1.38 +        int pitch = (dst->pitch / dst->format->BytesPerPixel);
    1.39 +        Uint8 *pixel;
    1.40 +        if (x1 <= x2) {
    1.41 +            pixel = (Uint8 *)dst->pixels + y1 * pitch + x1;
    1.42 +            length = draw_end ? (x2-x1+1) : (x2-x1);
    1.43 +        } else {
    1.44 +            pixel = (Uint8 *)dst->pixels + y1 * pitch + x2;
    1.45 +            if (!draw_end) {
    1.46 +                ++pixel;
    1.47 +            }
    1.48 +            length = draw_end ? (x1-x2+1) : (x1-x2);
    1.49 +        }
    1.50 +        SDL_memset(pixel, color, length);
    1.51 +    } else if (x1 == x2) {
    1.52 +        VLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
    1.53 +    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
    1.54 +        DLINE(Uint8, DRAW_FASTSETPIXEL1, draw_end);
    1.55 +    } else {
    1.56 +        BLINE(x1, y1, x2, y2, DRAW_FASTSETPIXELXY1, draw_end);
    1.57 +    }
    1.58 +}
    1.59 +
    1.60 +static void
    1.61 +SDL_DrawLine2(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
    1.62 +              SDL_bool draw_end)
    1.63 +{
    1.64 +    if (y1 == y2) {
    1.65 +        HLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
    1.66 +    } else if (x1 == x2) {
    1.67 +        VLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
    1.68 +    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
    1.69 +        DLINE(Uint16, DRAW_FASTSETPIXEL2, draw_end);
    1.70 +    } else {
    1.71 +        Uint8 _r, _g, _b, _a;
    1.72 +        const SDL_PixelFormat * fmt = dst->format;
    1.73 +        SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
    1.74 +        if (fmt->Rmask == 0x7C00) {
    1.75 +            AALINE(x1, y1, x2, y2,
    1.76 +                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB555,
    1.77 +                   draw_end);
    1.78 +        } else if (fmt->Rmask == 0xF800) {
    1.79 +            AALINE(x1, y1, x2, y2,
    1.80 +                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY_BLEND_RGB565,
    1.81 +                   draw_end);
    1.82 +        } else {
    1.83 +            AALINE(x1, y1, x2, y2,
    1.84 +                   DRAW_FASTSETPIXELXY2, DRAW_SETPIXELXY2_BLEND_RGB,
    1.85 +                   draw_end);
    1.86 +        }
    1.87 +    }
    1.88 +}
    1.89 +
    1.90 +static void
    1.91 +SDL_DrawLine4(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color,
    1.92 +              SDL_bool draw_end)
    1.93 +{
    1.94 +    if (y1 == y2) {
    1.95 +        HLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
    1.96 +    } else if (x1 == x2) {
    1.97 +        VLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
    1.98 +    } else if (ABS(x1 - x2) == ABS(y1 - y2)) {
    1.99 +        DLINE(Uint32, DRAW_FASTSETPIXEL4, draw_end);
   1.100 +    } else {
   1.101 +        Uint8 _r, _g, _b, _a;
   1.102 +        const SDL_PixelFormat * fmt = dst->format;
   1.103 +        SDL_GetRGBA(color, fmt, &_r, &_g, &_b, &_a);
   1.104 +        if (fmt->Rmask == 0x00FF0000) {
   1.105 +            if (!fmt->Amask) {
   1.106 +                AALINE(x1, y1, x2, y2,
   1.107 +                       DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_RGB888,
   1.108 +                       draw_end);
   1.109 +            } else {
   1.110 +                AALINE(x1, y1, x2, y2,
   1.111 +                       DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY_BLEND_ARGB8888,
   1.112 +                       draw_end);
   1.113 +            }
   1.114 +        } else {
   1.115 +            AALINE(x1, y1, x2, y2,
   1.116 +                   DRAW_FASTSETPIXELXY4, DRAW_SETPIXELXY4_BLEND_RGB,
   1.117 +                   draw_end);
   1.118 +        }
   1.119 +    }
   1.120 +}
   1.121 +
   1.122 +typedef void (*DrawLineFunc) (SDL_Surface * dst,
   1.123 +                              int x1, int y1, int x2, int y2,
   1.124 +                              Uint32 color, SDL_bool draw_end);
   1.125 +
   1.126 +static DrawLineFunc
   1.127 +SDL_CalculateDrawLineFunc(const SDL_PixelFormat * fmt)
   1.128 +{
   1.129 +    switch (fmt->BytesPerPixel) {
   1.130 +    case 1:
   1.131 +        if (fmt->BitsPerPixel < 8) {
   1.132 +            break;
   1.133 +        }
   1.134 +        return SDL_DrawLine1;
   1.135 +    case 2:
   1.136 +        return SDL_DrawLine2;
   1.137 +    case 4:
   1.138 +        return SDL_DrawLine4;
   1.139 +    }
   1.140 +    return NULL;
   1.141 +}
   1.142 +
   1.143 +int
   1.144 +SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
   1.145 +{
   1.146 +    DrawLineFunc func;
   1.147 +
   1.148 +    if (!dst) {
   1.149 +        SDL_SetError("SDL_DrawLine(): Passed NULL destination surface");
   1.150 +        return -1;
   1.151 +    }
   1.152 +
   1.153 +    func = SDL_CalculateDrawLineFunc(dst->format);
   1.154 +    if (!func) {
   1.155 +        SDL_SetError("SDL_DrawLine(): Unsupported surface format");
   1.156 +        return -1;
   1.157 +    }
   1.158 +
   1.159 +    /* Perform clipping */
   1.160 +    /* FIXME: We don't actually want to clip, as it may change line slope */
   1.161 +    if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
   1.162 +        return 0;
   1.163 +    }
   1.164 +
   1.165 +    func(dst, x1, y1, x2, y2, color, SDL_TRUE);
   1.166 +    return 0;
   1.167 +}
   1.168 +
   1.169 +int
   1.170 +SDL_DrawLines(SDL_Surface * dst, const SDL_Point * points, int count,
   1.171 +              Uint32 color)
   1.172 +{
   1.173 +    int i;
   1.174 +    int x1, y1;
   1.175 +    int x2, y2;
   1.176 +    SDL_bool draw_end;
   1.177 +    DrawLineFunc func;
   1.178 +
   1.179 +    if (!dst) {
   1.180 +        SDL_SetError("SDL_DrawLines(): Passed NULL destination surface");
   1.181 +        return -1;
   1.182 +    }
   1.183 +
   1.184 +    func = SDL_CalculateDrawLineFunc(dst->format);
   1.185 +    if (!func) {
   1.186 +        SDL_SetError("SDL_DrawLines(): Unsupported surface format");
   1.187 +        return -1;
   1.188 +    }
   1.189 +
   1.190 +    for (i = 1; i < count; ++i) {
   1.191 +        x1 = points[i-1].x;
   1.192 +        y1 = points[i-1].y;
   1.193 +        x2 = points[i].x;
   1.194 +        y2 = points[i].y;
   1.195 +
   1.196 +        /* Perform clipping */
   1.197 +        /* FIXME: We don't actually want to clip, as it may change line slope */
   1.198 +        if (!SDL_IntersectRectAndLine(&dst->clip_rect, &x1, &y1, &x2, &y2)) {
   1.199 +            continue;
   1.200 +        }
   1.201 +
   1.202 +        /* Draw the end if it was clipped */
   1.203 +        draw_end = (x2 != points[i].x || y2 != points[i].y);
   1.204 +
   1.205 +        func(dst, x1, y1, x2, y2, color, draw_end);
   1.206 +    }
   1.207 +    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
   1.208 +        SDL_DrawPoint(dst, points[count-1].x, points[count-1].y, color);
   1.209 +    }
   1.210 +    return 0;
   1.211 +}
   1.212 +
   1.213 +/* vi: set ts=4 sw=4 expandtab: */