Placeholder for line drawing algorithm (current code doesn't work)
authorSam Lantinga <slouken@libsdl.org>
Sat, 20 Dec 2008 13:54:19 +0000
changeset 28901863c8b59658
parent 2889 67f84eb26ea1
child 2891 3e7856518a34
Placeholder for line drawing algorithm (current code doesn't work)
src/video/SDL_drawline.c
     1.1 --- a/src/video/SDL_drawline.c	Sat Dec 20 13:53:54 2008 +0000
     1.2 +++ b/src/video/SDL_drawline.c	Sat Dec 20 13:54:19 2008 +0000
     1.3 @@ -24,8 +24,62 @@
     1.4  #include "SDL_video.h"
     1.5  #include "SDL_blit.h"
     1.6  
     1.7 +#define ABS(x) (x < 0 ? -x : x)
     1.8  
     1.9 -int
    1.10 +#define SWAP(x, y) (x ^= y ^= x ^= y)
    1.11 +
    1.12 +#define BRESENHAM(x0, y0, x1, y1, op, color) \
    1.13 +{ \
    1.14 +    int deltax, deltay, steep, error, xstep, ystep, x, y; \
    1.15 + \
    1.16 +    deltax = ABS(x1 - x0); \
    1.17 +    deltay = ABS(y1 - y0); \
    1.18 +    steep = deltay > deltax; \
    1.19 +    error = deltax / 2; \
    1.20 +    if (steep) { \
    1.21 +        SWAP(x0, y0); \
    1.22 +        SWAP(x1, y1); \
    1.23 +    } \
    1.24 +    y = y0; \
    1.25 +    if (x0 > x1) { \
    1.26 +        xstep = -1; \
    1.27 +        deltax = -deltax; \
    1.28 +    } else { \
    1.29 +        xstep = 1; \
    1.30 +    } \
    1.31 +    if (y0 < y1) { \
    1.32 +        ystep = 1; \
    1.33 +    } else { \
    1.34 +        ystep = -1; \
    1.35 +    } \
    1.36 +    if (!steep) { \
    1.37 +        for (x = x0; x != x1; x += xstep) { \
    1.38 +            op(x, y, color); \
    1.39 +            error -= deltay; \
    1.40 +            if (error < 0) { \
    1.41 +                y = y + ystep; \
    1.42 +                error += deltax; \
    1.43 +            } \
    1.44 +        } \
    1.45 +    } else { \
    1.46 +        for (x = x0; x != x1; x += xstep) { \
    1.47 +            op(y, x, color); \
    1.48 +            error -= deltay; \
    1.49 +            if (error < 0) { \
    1.50 +                y = y + ystep; \
    1.51 +                error += deltax; \
    1.52 +            } \
    1.53 +        } \
    1.54 +    } \
    1.55 +}
    1.56 +
    1.57 +#define SETPIXEL(x, y, type, bpp, color) \
    1.58 +    *(type *)(dst->pixels + y * dst->pitch + x * bpp) = (type) color
    1.59 +
    1.60 +#define SETPIXEL1(x, y, color) SETPIXEL(x, y, Uint8, 1, color);
    1.61 +#define SETPIXEL2(x, y, color) SETPIXEL(x, y, Uint16, 2, color);
    1.62 +#define SETPIXEL4(x, y, color) SETPIXEL(x, y, Uint32, 4, color);
    1.63 +
    1.64  SDL_DrawLine(SDL_Surface * dst, int x1, int y1, int x2, int y2, Uint32 color)
    1.65  {
    1.66      /* This function doesn't work on surfaces < 8 bpp */
    1.67 @@ -41,8 +95,21 @@
    1.68         }
    1.69       */
    1.70  
    1.71 -    SDL_Unsupported();
    1.72 -    return -1;
    1.73 +    switch (dst->format->BytesPerPixel) {
    1.74 +    case 1:
    1.75 +        BRESENHAM(x1, y1, x2, y2, SETPIXEL1, color);
    1.76 +        break;
    1.77 +    case 2:
    1.78 +        BRESENHAM(x1, y1, x2, y2, SETPIXEL2, color);
    1.79 +        break;
    1.80 +    case 3:
    1.81 +        SDL_Unsupported();
    1.82 +        return -1;
    1.83 +    case 4:
    1.84 +        BRESENHAM(x1, y1, x2, y2, SETPIXEL4, color);
    1.85 +        break;
    1.86 +    }
    1.87 +    return 0;
    1.88  }
    1.89  
    1.90  /* vi: set ts=4 sw=4 expandtab: */