home *** CD-ROM | disk | FTP | other *** search
/ Shareware Supreme Volume 6 #1 / swsii.zip / swsii / 215 / DDJ11A92.ZIP / GRPPROG.ASC < prev    next >
Text File  |  1992-11-04  |  11KB  |  360 lines

  1. _GRAPHICS PROGRAMMING COLUMN_
  2. by Michael Abrash
  3.  
  4. [LISTING ONE]
  5.  
  6. /* Run-length slice line drawing implementation for mode 0x13, the VGA's
  7. 320x200 256-color mode. Not optimized! Tested with Borland C++ 3.0 in 
  8. the small model. */
  9.  
  10. #include <dos.h>
  11.  
  12. #define SCREEN_WIDTH    320
  13. #define SCREEN_SEGMENT  0xA000
  14.  
  15. void DrawHorizontalRun(char far **ScreenPtr, int XAdvance, int RunLength,
  16.                        int Color);
  17. void DrawVerticalRun(char far **ScreenPtr, int XAdvance, int RunLength,
  18.                        int Color);
  19. /* Draws a line between the specified endpoints in color Color. */
  20. void LineDraw(int XStart, int YStart, int XEnd, int YEnd, int Color)
  21. {
  22.    int Temp, AdjUp, AdjDown, ErrorTerm, XAdvance, XDelta, YDelta;
  23.    int WholeStep, InitialPixelCount, FinalPixelCount, i, RunLength;
  24.    char far *ScreenPtr;
  25.  
  26.    /* We'll always draw top to bottom, to reduce the number of cases we have to
  27.    handle, and to make lines between the same endpoints draw the same pixels */
  28.    if (YStart > YEnd) {
  29.       Temp = YStart;
  30.       YStart = YEnd;
  31.       YEnd = Temp;
  32.       Temp = XStart;
  33.       XStart = XEnd;
  34.       XEnd = Temp;
  35.    }
  36.    /* Point to the bitmap address first pixel to draw */
  37.    ScreenPtr = MK_FP(SCREEN_SEGMENT, YStart * SCREEN_WIDTH + XStart);
  38.  
  39.    /* Figure out whether we're going left or right, and how far we're
  40.       going horizontally */
  41.    if ((XDelta = XEnd - XStart) < 0)
  42.    {
  43.       XAdvance = -1;
  44.       XDelta = -XDelta;
  45.    }
  46.    else
  47.    {
  48.       XAdvance = 1;
  49.    }
  50.    /* Figure out how far we're going vertically */
  51.    YDelta = YEnd - YStart;
  52.  
  53.    /* Special-case horizontal, vertical, and diagonal lines, for speed
  54.       and to avoid nasty boundary conditions and division by 0 */
  55.    if (XDelta == 0)
  56.    {
  57.       /* Vertical line */
  58.       for (i=0; i<=YDelta; i++)
  59.       {
  60.          *ScreenPtr = Color;
  61.          ScreenPtr += SCREEN_WIDTH;
  62.       }
  63.       return;
  64.    }
  65.    if (YDelta == 0)
  66.    {
  67.       /* Horizontal line */
  68.       for (i=0; i<=XDelta; i++)
  69.       {
  70.          *ScreenPtr = Color;
  71.          ScreenPtr += XAdvance;
  72.       }
  73.       return;
  74.    }
  75.    if (XDelta == YDelta)
  76.    {
  77.       /* Diagonal line */
  78.       for (i=0; i<=XDelta; i++)
  79.       {
  80.          *ScreenPtr = Color;
  81.          ScreenPtr += XAdvance + SCREEN_WIDTH;
  82.       }
  83.       return;
  84.    }
  85.  
  86.    /* Determine whether the line is X or Y major, and handle accordingly */
  87.    if (XDelta >= YDelta)
  88.    {
  89.       /* X major line */
  90.       /* Minimum # of pixels in a run in this line */
  91.       WholeStep = XDelta / YDelta;
  92.  
  93.       /* Error term adjust each time Y steps by 1; used to tell when one
  94.          extra pixel should be drawn as part of a run, to account for
  95.          fractional steps along the X axis per 1-pixel steps along Y */
  96.       AdjUp = (XDelta % YDelta) * 2;
  97.  
  98.       /* Error term adjust when the error term turns over, used to factor
  99.          out the X step made at that time */
  100.       AdjDown = YDelta * 2;
  101.  
  102.       /* Initial error term; reflects an initial step of 0.5 along the Y
  103.          axis */
  104.       ErrorTerm = (XDelta % YDelta) - (YDelta * 2);
  105.  
  106.       /* The initial and last runs are partial, because Y advances only 0.5
  107.          for these runs, rather than 1. Divide one full run, plus the
  108.          initial pixel, between the initial and last runs */
  109.       InitialPixelCount = (WholeStep / 2) + 1;
  110.       FinalPixelCount = InitialPixelCount;
  111.  
  112.       /* If the basic run length is even and there's no fractional
  113.          advance, we have one pixel that could go to either the initial
  114.          or last partial run, which we'll arbitrarily allocate to the
  115.          last run */
  116.       if ((AdjUp == 0) && ((WholeStep & 0x01) == 0))
  117.       {
  118.          InitialPixelCount--;
  119.       }
  120.      /* If there're an odd number of pixels per run, we have 1 pixel that can't
  121.      be allocated to either the initial or last partial run, so we'll add 0.5
  122.      to error term so this pixel will be handled by the normal full-run loop */
  123.       if ((WholeStep & 0x01) != 0)
  124.       {
  125.          ErrorTerm += YDelta;
  126.       }
  127.       /* Draw the first, partial run of pixels */
  128.       DrawHorizontalRun(&ScreenPtr, XAdvance, InitialPixelCount, Color);
  129.       /* Draw all full runs */
  130.       for (i=0; i<(YDelta-1); i++)
  131.       {
  132.          RunLength = WholeStep;  /* run is at least this long */
  133.          /* Advance the error term and add an extra pixel if the error
  134.             term so indicates */
  135.          if ((ErrorTerm += AdjUp) > 0)
  136.          {
  137.             RunLength++;
  138.             ErrorTerm -= AdjDown;   /* reset the error term */
  139.          }
  140.          /* Draw this scan line's run */
  141.          DrawHorizontalRun(&ScreenPtr, XAdvance, RunLength, Color);
  142.       }
  143.       /* Draw the final run of pixels */
  144.       DrawHorizontalRun(&ScreenPtr, XAdvance, FinalPixelCount, Color);
  145.       return;
  146.    }
  147.    else
  148.    {
  149.       /* Y major line */
  150.  
  151.       /* Minimum # of pixels in a run in this line */
  152.       WholeStep = YDelta / XDelta;
  153.  
  154.       /* Error term adjust each time X steps by 1; used to tell when 1 extra
  155.          pixel should be drawn as part of a run, to account for
  156.          fractional steps along the Y axis per 1-pixel steps along X */
  157.       AdjUp = (YDelta % XDelta) * 2;
  158.  
  159.       /* Error term adjust when the error term turns over, used to factor
  160.          out the Y step made at that time */
  161.       AdjDown = XDelta * 2;
  162.  
  163.       /* Initial error term; reflects initial step of 0.5 along the X axis */
  164.       ErrorTerm = (YDelta % XDelta) - (XDelta * 2);
  165.  
  166.       /* The initial and last runs are partial, because X advances only 0.5
  167.          for these runs, rather than 1. Divide one full run, plus the
  168.          initial pixel, between the initial and last runs */
  169.       InitialPixelCount = (WholeStep / 2) + 1;
  170.       FinalPixelCount = InitialPixelCount;
  171.  
  172.       /* If the basic run length is even and there's no fractional advance, we
  173.          have 1 pixel that could go to either the initial or last partial run,
  174.          which we'll arbitrarily allocate to the last run */
  175.       if ((AdjUp == 0) && ((WholeStep & 0x01) == 0))
  176.       {
  177.          InitialPixelCount--;
  178.       }
  179.       /* If there are an odd number of pixels per run, we have one pixel
  180.          that can't be allocated to either the initial or last partial
  181.          run, so we'll add 0.5 to the error term so this pixel will be
  182.          handled by the normal full-run loop */
  183.       if ((WholeStep & 0x01) != 0)
  184.       {
  185.          ErrorTerm += XDelta;
  186.       }
  187.       /* Draw the first, partial run of pixels */
  188.       DrawVerticalRun(&ScreenPtr, XAdvance, InitialPixelCount, Color);
  189.  
  190.       /* Draw all full runs */
  191.       for (i=0; i<(XDelta-1); i++)
  192.       {
  193.          RunLength = WholeStep;  /* run is at least this long */
  194.          /* Advance the error term and add an extra pixel if the error
  195.             term so indicates */
  196.          if ((ErrorTerm += AdjUp) > 0)
  197.          {
  198.             RunLength++;
  199.             ErrorTerm -= AdjDown;   /* reset the error term */
  200.          }
  201.          /* Draw this scan line's run */
  202.          DrawVerticalRun(&ScreenPtr, XAdvance, RunLength, Color);
  203.       }
  204.       /* Draw the final run of pixels */
  205.       DrawVerticalRun(&ScreenPtr, XAdvance, FinalPixelCount, Color);
  206.       return;
  207.    }
  208. }
  209. /* Draws a horizontal run of pixels, then advances the bitmap pointer to
  210.    the first pixel of the next run. */
  211. void DrawHorizontalRun(char far **ScreenPtr, int XAdvance,
  212.    int RunLength, int Color)
  213. {
  214.    int i;
  215.    char far *WorkingScreenPtr = *ScreenPtr;
  216.  
  217.    for (i=0; i<RunLength; i++)
  218.    {
  219.       *WorkingScreenPtr = Color;
  220.       WorkingScreenPtr += XAdvance;
  221.    }
  222.    /* Advance to the next scan line */
  223.    WorkingScreenPtr += SCREEN_WIDTH;
  224.    *ScreenPtr = WorkingScreenPtr;
  225. }
  226. /* Draws a vertical run of pixels, then advances the bitmap pointer to
  227.    the first pixel of the next run. */
  228. void DrawVerticalRun(char far **ScreenPtr, int XAdvance,
  229.    int RunLength, int Color)
  230. {
  231.    int i;
  232.    char far *WorkingScreenPtr = *ScreenPtr;
  233.  
  234.    for (i=0; i<RunLength; i++)
  235.    {
  236.       *WorkingScreenPtr = Color;
  237.       WorkingScreenPtr += SCREEN_WIDTH;
  238.    }
  239.    /* Advance to the next column */
  240.    WorkingScreenPtr += XAdvance;
  241.    *ScreenPtr = WorkingScreenPtr;
  242. }
  243.  
  244.  
  245. [LISTING TWO]
  246.  
  247. /* Sample line-drawing program. Adapted from code that appeared in the
  248.    _On Graphics_ column in Programmer's Journal. Tested with 
  249.    Borland C++ 3.0 in the small model. */
  250.  
  251. #include <dos.h>
  252.  
  253. #define GRAPHICS_MODE   0x13
  254. #define TEXT_MODE       0x03
  255. #define BIOS_VIDEO_INT  0x10
  256. #define X_MAX           320      /* working screen width */
  257. #define Y_MAX           200      /* working screen height */
  258.  
  259. extern void LineDraw(int XStart, int YStart, int XEnd, int YEnd, int Color);
  260.  
  261. /* Subroutine to draw a rectangle full of vectors, of the specified
  262.  * length and color, around the specified rectangle center.  */
  263. void VectorsUp(XCenter, YCenter, XLength, YLength, Color)
  264. int XCenter, YCenter;   /* center of rectangle to fill */
  265. int XLength, YLength;   /* distance from center to edge of rectangle */
  266. int Color;              /* color to draw lines in */
  267. {
  268.    int WorkingX, WorkingY;
  269.  
  270.    /* Lines from center to top of rectangle */
  271.    WorkingX = XCenter - XLength;
  272.    WorkingY = YCenter - YLength;
  273.    for ( ; WorkingX < ( XCenter + XLength ); WorkingX++ )
  274.    {
  275.       LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
  276.    }
  277.    /* Lines from center to right of rectangle */
  278.    WorkingX = XCenter + XLength - 1;
  279.    WorkingY = YCenter - YLength;
  280.    for ( ; WorkingY < ( YCenter + YLength ); WorkingY++ )
  281.    {
  282.       LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
  283.    }
  284.    /* Lines from center to bottom of rectangle */
  285.    WorkingX = XCenter + XLength - 1;
  286.    WorkingY = YCenter + YLength - 1;
  287.    for ( ; WorkingX >= ( XCenter - XLength ); WorkingX-- )
  288.    {
  289.       LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
  290.    }
  291.    /* Lines from center to left of rectangle */
  292.    WorkingX = XCenter - XLength;
  293.    WorkingY = YCenter + YLength - 1;
  294.    for ( ; WorkingY >= ( YCenter - YLength ); WorkingY-- )
  295.    {
  296.       LineDraw(XCenter, YCenter, WorkingX, WorkingY, Color);
  297.    }
  298. }
  299. /* Sample program to draw four rectangles full of lines.  */
  300. int main()
  301. {
  302.    union REGS regs;
  303.  
  304.    /* Set graphics mode */
  305.    regs.x.ax = GRAPHICS_MODE;
  306.    int86(BIOS_VIDEO_INT, ®s, ®s);
  307.  
  308.    /* Draw each of four rectangles full of vectors */
  309.    VectorsUp(X_MAX / 4, Y_MAX / 4, X_MAX / 4, Y_MAX / 4, 1);
  310.    VectorsUp(X_MAX * 3 / 4, Y_MAX / 4, X_MAX / 4, Y_MAX / 4, 2);
  311.    VectorsUp(X_MAX / 4, Y_MAX * 3 / 4, X_MAX / 4, Y_MAX / 4, 3);
  312.    VectorsUp(X_MAX * 3 / 4, Y_MAX * 3 / 4, X_MAX / 4, Y_MAX / 4, 4);
  313.  
  314.    /* Wait for a key to be pressed */
  315.    getch();
  316.  
  317.    /* Back to text mode */
  318.    regs.x.ax = TEXT_MODE;
  319.    int86(BIOS_VIDEO_INT, ®s, ®s);
  320. }
  321.  
  322.  
  323.  
  324. Example 1:
  325.  
  326. for (i=0; i<RunLength; i++)
  327. {
  328.    *WorkingScreenPtr = Color;
  329.    if (XDelta > 0)
  330.    {
  331.       WorkingScreenPtr++;
  332.    }
  333.    else
  334.    {
  335.       WorkingScreenPtr--;
  336.    }
  337. }
  338.  
  339.  
  340.  
  341.  
  342. Example 2:
  343.  
  344.  
  345. if (XDelta > 0)
  346. {
  347.    for (i=0; i<RunLength; i++)
  348.    {
  349.       *WorkingScreenPtr++ = Color;
  350.    }
  351. }
  352. else
  353. {
  354.    for (i=0; i<RunLength; i++)
  355.    {
  356.       *WorkingScreenPtr-- = Color;
  357.    }
  358. }
  359.  
  360.