pmc/sdl.cc
changeset 4 236f8f747073
child 9 c3fecc82ade6
equal deleted inserted replaced
3:820ed7fb9314 4:236f8f747073
       
     1 /***
       
     2  *
       
     3  * $Id: sdl.cc,v 1.1.1.1 2008-04-28 17:33:22 mbroeker Exp $
       
     4  * $Source: /development/cpp/pmc/sdl.cc,v $
       
     5  *
       
     6  */
       
     7 
       
     8 #include <SDL/SDL.h>
       
     9 
       
    10 #define max(a,b) (((a) > (b)) ? (a) : (b))
       
    11 #define min(a,b) (((a) < (b)) ? (a) : (b))
       
    12 #define abs(a) (((a)<0) ? -(a) : (a))
       
    13 #define sign(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)
       
    14 
       
    15 void drawLine (SDL_Surface * s, int x1, int y1, int x2, int y2, Uint32 color)
       
    16 {
       
    17     int d;
       
    18     int x;
       
    19     int y;
       
    20     int ax;
       
    21     int ay;
       
    22     int sx;
       
    23     int sy;
       
    24     int dx;
       
    25     int dy;
       
    26 
       
    27     Uint8 *lineAddr;
       
    28     Sint32 yOffset;
       
    29 
       
    30     dx = x2 - x1;
       
    31     ax = abs (dx) << 1;
       
    32     sx = sign (dx);
       
    33 
       
    34     dy = y2 - y1;
       
    35     ay = abs (dy) << 1;
       
    36     sy = sign (dy);
       
    37     yOffset = sy * s->pitch;
       
    38 
       
    39     x = x1;
       
    40     y = y1;
       
    41 
       
    42     lineAddr = ((Uint8 *) s->pixels) + (y * s->pitch);
       
    43     if (ax > ay) {              /* x dominant */
       
    44         d = ay - (ax >> 1);
       
    45         for (;;) {
       
    46             *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
       
    47 
       
    48             if (x == x2) {
       
    49                 return;
       
    50             }
       
    51             if (d >= 0) {
       
    52                 y += sy;
       
    53                 lineAddr += yOffset;
       
    54                 d -= ax;
       
    55             }
       
    56             x += sx;
       
    57             d += ay;
       
    58         }
       
    59     } else {                    /* y dominant */
       
    60         d = ax - (ay >> 1);
       
    61         for (;;) {
       
    62             *((Uint16 *) (lineAddr + (x << 1))) = (Uint16) color;
       
    63 
       
    64             if (y == y2) {
       
    65                 return;
       
    66             }
       
    67             if (d >= 0) {
       
    68                 x += sx;
       
    69                 d -= ay;
       
    70             }
       
    71             y += sy;
       
    72             lineAddr += yOffset;
       
    73             d += ax;
       
    74         }
       
    75     }
       
    76 }