home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter36 / l36-1.c next >
C/C++ Source or Header  |  1997-06-18  |  8KB  |  240 lines

  1. /* Run-length slice line drawing implementation for mode 0x13, the VGA's
  2. 320x200 256-color mode. Not optimized! 
  3.   Compile Borland C++ 4.02 in small model and link with L15-2.C
  4.   Checked by Jim Mischel 11/30/94 */
  5.  
  6. #include <dos.h>
  7.  
  8. #define SCREEN_WIDTH    320
  9. #define SCREEN_SEGMENT  0xA000
  10.  
  11. void DrawHorizontalRun(char far **ScreenPtr, int XAdvance, int RunLength,
  12.                        int Color);
  13. void DrawVerticalRun(char far **ScreenPtr, int XAdvance, int RunLength,
  14.                        int Color);
  15. /* Draws a line between the specified endpoints in color Color. */
  16. void LineDraw(int XStart, int YStart, int XEnd, int YEnd, int Color)
  17. {
  18.    int Temp, AdjUp, AdjDown, ErrorTerm, XAdvance, XDelta, YDelta;
  19.    int WholeStep, InitialPixelCount, FinalPixelCount, i, RunLength;
  20.    char far *ScreenPtr;
  21.  
  22.    /* We'll always draw top to bottom, to reduce the number of cases we have to
  23.    handle, and to make lines between the same endpoints draw the same pixels */
  24.    if (YStart > YEnd) {
  25.       Temp = YStart;
  26.       YStart = YEnd;
  27.       YEnd = Temp;
  28.       Temp = XStart;
  29.       XStart = XEnd;
  30.       XEnd = Temp;
  31.    }
  32.    /* Point to the bitmap address first pixel to draw */
  33.    ScreenPtr = MK_FP(SCREEN_SEGMENT, YStart * SCREEN_WIDTH + XStart);
  34.  
  35.    /* Figure out whether we're going left or right, and how far we're
  36.       going horizontally */
  37.    if ((XDelta = XEnd - XStart) < 0)
  38.    {
  39.       XAdvance = -1;
  40.       XDelta = -XDelta;
  41.    }
  42.    else
  43.    {
  44.       XAdvance = 1;
  45.    }
  46.    /* Figure out how far we're going vertically */
  47.    YDelta = YEnd - YStart;
  48.  
  49.    /* Special-case horizontal, vertical, and diagonal lines, for speed
  50.       and to avoid nasty boundary conditions and division by 0 */
  51.    if (XDelta == 0)
  52.    {
  53.       /* Vertical line */
  54.       for (i=0; i<=YDelta; i++)
  55.       {
  56.          *ScreenPtr = Color;
  57.          ScreenPtr += SCREEN_WIDTH;
  58.       }
  59.       return;
  60.    }
  61.    if (YDelta == 0)
  62.    {
  63.       /* Horizontal line */
  64.       for (i=0; i<=XDelta; i++)
  65.       {
  66.          *ScreenPtr = Color;
  67.          ScreenPtr += XAdvance;
  68.       }
  69.       return;
  70.    }
  71.    if (XDelta == YDelta)
  72.    {
  73.       /* Diagonal line */
  74.       for (i=0; i<=XDelta; i++)
  75.       {
  76.          *ScreenPtr = Color;
  77.          ScreenPtr += XAdvance + SCREEN_WIDTH;
  78.       }
  79.       return;
  80.    }
  81.  
  82.    /* Determine whether the line is X or Y major, and handle accordingly */
  83.    if (XDelta >= YDelta)
  84.    {
  85.       /* X major line */
  86.       /* Minimum # of pixels in a run in this line */
  87.       WholeStep = XDelta / YDelta;
  88.  
  89.       /* Error term adjust each time Y steps by 1; used to tell when one
  90.          extra pixel should be drawn as part of a run, to account for
  91.          fractional steps along the X axis per 1-pixel steps along Y */
  92.       AdjUp = (XDelta % YDelta) * 2;
  93.  
  94.       /* Error term adjust when the error term turns over, used to factor
  95.          out the X step made at that time */
  96.       AdjDown = YDelta * 2;
  97.  
  98.       /* Initial error term; reflects an initial step of 0.5 along the Y
  99.          axis */
  100.       ErrorTerm = (XDelta % YDelta) - (YDelta * 2);
  101.  
  102.       /* The initial and last runs are partial, because Y advances only 0.5
  103.          for these runs, rather than 1. Divide one full run, plus the
  104.          initial pixel, between the initial and last runs */
  105.       InitialPixelCount = (WholeStep / 2) + 1;
  106.       FinalPixelCount = InitialPixelCount;
  107.  
  108.       /* If the basic run length is even and there's no fractional
  109.          advance, we have one pixel that could go to either the initial
  110.          or last partial run, which we'll arbitrarily allocate to the
  111.          last run */
  112.       if ((AdjUp == 0) && ((WholeStep & 0x01) == 0))
  113.       {
  114.          InitialPixelCount--;
  115.       }
  116.       /* If there're an odd number of pixels per run, we have 1 pixel that can't
  117.          be allocated to either the initial or last partial run, so we'll add 0.5
  118.          to error term so this pixel will be handled by the normal full-run loop */
  119.          if ((WholeStep & 0x01) != 0)
  120.       {
  121.          ErrorTerm += YDelta;
  122.       }
  123.       /* Draw the first, partial run of pixels */
  124.       DrawHorizontalRun(&ScreenPtr, XAdvance, InitialPixelCount, Color);
  125.       /* Draw all full runs */
  126.       for (i=0; i<(YDelta-1); i++)
  127.       {
  128.          RunLength = WholeStep;  /* run is at least this long */
  129.          /* Advance the error term and add an extra pixel if the error
  130.             term so indicates */
  131.          if ((ErrorTerm += AdjUp) > 0)
  132.          {
  133.             RunLength++;
  134.             ErrorTerm -= AdjDown;   /* reset the error term */
  135.          }
  136.          /* Draw this scan line's run */
  137.          DrawHorizontalRun(&ScreenPtr, XAdvance, RunLength, Color);
  138.       }
  139.       /* Draw the final run of pixels */
  140.       DrawHorizontalRun(&ScreenPtr, XAdvance, FinalPixelCount, Color);
  141.       return;
  142.    }
  143.    else
  144.    {
  145.       /* Y major line */
  146.  
  147.       /* Minimum # of pixels in a run in this line */
  148.       WholeStep = YDelta / XDelta;
  149.  
  150.       /* Error term adjust each time X steps by 1; used to tell when 1 extra
  151.          pixel should be drawn as part of a run, to account for
  152.          fractional steps along the Y axis per 1-pixel steps along X */
  153.       AdjUp = (YDelta % XDelta) * 2;
  154.  
  155.       /* Error term adjust when the error term turns over, used to factor
  156.          out the Y step made at that time */
  157.       AdjDown = XDelta * 2;
  158.  
  159.       /* Initial error term; reflects initial step of 0.5 along the X axis */
  160.       ErrorTerm = (YDelta % XDelta) - (XDelta * 2);
  161.  
  162.       /* The initial and last runs are partial, because X advances only 0.5
  163.          for these runs, rather than 1. Divide one full run, plus the
  164.          initial pixel, between the initial and last runs */
  165.       InitialPixelCount = (WholeStep / 2) + 1;
  166.       FinalPixelCount = InitialPixelCount;
  167.  
  168.       /* If the basic run length is even and there's no fractional advance, we
  169.          have 1 pixel that could go to either the initial or last partial run,
  170.          which we'll arbitrarily allocate to the last run */
  171.       if ((AdjUp == 0) && ((WholeStep & 0x01) == 0))
  172.       {
  173.          InitialPixelCount--;
  174.       }
  175.       /* If there are an odd number of pixels per run, we have one pixel
  176.          that can't be allocated to either the initial or last partial
  177.          run, so we'll add 0.5 to the error term so this pixel will be
  178.          handled by the normal full-run loop */
  179.       if ((WholeStep & 0x01) != 0)
  180.       {
  181.          ErrorTerm += XDelta;
  182.       }
  183.       /* Draw the first, partial run of pixels */
  184.       DrawVerticalRun(&ScreenPtr, XAdvance, InitialPixelCount, Color);
  185.  
  186.       /* Draw all full runs */
  187.       for (i=0; i<(XDelta-1); i++)
  188.       {
  189.          RunLength = WholeStep;  /* run is at least this long */
  190.          /* Advance the error term and add an extra pixel if the error
  191.             term so indicates */
  192.          if ((ErrorTerm += AdjUp) > 0)
  193.          {
  194.             RunLength++;
  195.             ErrorTerm -= AdjDown;   /* reset the error term */
  196.          }
  197.          /* Draw this scan line's run */
  198.          DrawVerticalRun(&ScreenPtr, XAdvance, RunLength, Color);
  199.       }
  200.       /* Draw the final run of pixels */
  201.       DrawVerticalRun(&ScreenPtr, XAdvance, FinalPixelCount, Color);
  202.       return;
  203.    }
  204. }
  205. /* Draws a horizontal run of pixels, then advances the bitmap pointer to
  206.    the first pixel of the next run. */
  207. void DrawHorizontalRun(char far **ScreenPtr, int XAdvance,
  208.    int RunLength, int Color)
  209. {
  210.    int i;
  211.    char far *WorkingScreenPtr = *ScreenPtr;
  212.  
  213.    for (i=0; i<RunLength; i++)
  214.    {
  215.       *WorkingScreenPtr = Color;
  216.       WorkingScreenPtr += XAdvance;
  217.    }
  218.    /* Advance to the next scan line */
  219.    WorkingScreenPtr += SCREEN_WIDTH;
  220.    *ScreenPtr = WorkingScreenPtr;
  221. }
  222. /* Draws a vertical run of pixels, then advances the bitmap pointer to
  223.    the first pixel of the next run. */
  224. void DrawVerticalRun(char far **ScreenPtr, int XAdvance,
  225.    int RunLength, int Color)
  226. {
  227.    int i;
  228.    char far *WorkingScreenPtr = *ScreenPtr;
  229.  
  230.    for (i=0; i<RunLength; i++)
  231.    {
  232.       *WorkingScreenPtr = Color;
  233.       WorkingScreenPtr += SCREEN_WIDTH;
  234.    }
  235.    /* Advance to the next column */
  236.    WorkingScreenPtr += XAdvance;
  237.    *ScreenPtr = WorkingScreenPtr;
  238. }
  239.  
  240.