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