home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_14 / graph14.c next >
Encoding:
C/C++ Source or Header  |  1994-07-20  |  6.1 KB  |  278 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include "graph3.h"  // include our graphics stuff
  17. #include "graph4.h"
  18. #include "graph5.h"
  19. #include "graph6.h"
  20. #include "graph14.h"
  21.  
  22. // F U N C T I O N S /////////////////////////////////////////////////////////
  23.  
  24. void Draw_Polygon_DB(polygon_ptr poly)
  25. {
  26. // this function draws a polygon into the double buffer without clipping
  27. // caller should make sure that vertices are within bounds of clipping
  28. // rectangle, also the polygon will always be unfilled regardless
  29. // of the fill flag
  30.  
  31. int index,xo,yo;
  32.  
  33. // extract local origin
  34.  
  35. xo = poly->lxo;
  36. yo = poly->lyo;
  37.  
  38. // draw polygon
  39.  
  40. for (index=0; index<poly->num_vertices-1; index++)
  41.     {
  42.  
  43.     Bline_DB(xo+(int)poly->vertices[index].x,yo+(int)poly->vertices[index].y,
  44.           xo+(int)poly->vertices[index+1].x,yo+(int)poly->vertices[index+1].y,
  45.           poly->b_color);
  46.  
  47.     } // end for index
  48.  
  49.     // close polygon?
  50.  
  51.     if (!poly->closed)
  52.        return;
  53.  
  54.     Bline_DB(xo+(int)poly->vertices[index].x,yo+(int)poly->vertices[index].y,
  55.           xo+(int)poly->vertices[0].x,yo+(int)poly->vertices[0].y,
  56.           poly->b_color);
  57.  
  58.  
  59. } // end Draw_Polygon_DB
  60.  
  61. //////////////////////////////////////////////////////////////////////////////
  62.  
  63. void Draw_Polygon_Clip_DB(polygon_ptr poly)
  64. {
  65.  
  66. // this function draws a polygon into the double buffer with clipping
  67. // also the polygon will always be unfilled regardless
  68. // of the fill flag in the polygon structure
  69.  
  70. int index,  // loop index
  71.     xo,yo,  // local origin
  72.     x1,y1,  // end points of current line being processed
  73.     x2,y2;
  74.  
  75. // extract local origin
  76.  
  77. xo = poly->lxo;
  78. yo = poly->lyo;
  79.  
  80. // draw polygon
  81.  
  82. for (index=0; index<poly->num_vertices-1; index++)
  83.     {
  84.  
  85.     // extract the line
  86.  
  87.     x1 = (int)poly->vertices[index].x+xo;
  88.     y1 = (int)poly->vertices[index].y+yo;
  89.  
  90.     x2 = (int)poly->vertices[index+1].x+xo;
  91.     y2 = (int)poly->vertices[index+1].y+yo;
  92.  
  93.     // clip line to viewing screen and draw unless line is totally invisible
  94.  
  95.     if (Clip_Line(&x1,&y1,&x2,&y2))
  96.        {
  97.        // line was clipped and now can be drawn
  98.        Bline_DB(x1,y1,x2,y2,poly->b_color);
  99.  
  100.        } // end if draw line
  101.  
  102.     } // end for index
  103.  
  104.     // close polygon?        // close polygon
  105.  
  106.     if (!poly->closed)
  107.        return;
  108.  
  109.     // extract the line
  110.  
  111.     x1 = (int)poly->vertices[index].x+xo;
  112.     y1 = (int)poly->vertices[index].y+yo;
  113.  
  114.     x2 = (int)poly->vertices[0].x+xo;
  115.     y2 = (int)poly->vertices[0].y+yo;
  116.  
  117.     // clip line to viewing screen and draw unless line is totally invisible
  118.  
  119.     if (Clip_Line(&x1,&y1,&x2,&y2))
  120.        {
  121.  
  122.        // line was clipped and now can be drawn
  123.  
  124.        Bline_DB(x1,y1,x2,y2,poly->b_color);
  125.  
  126.        } // end if draw line
  127.  
  128. } // end Draw_Polygon_Clip_DB
  129.  
  130. //////////////////////////////////////////////////////////////////////////////
  131.  
  132. void Bline_DB(int xo, int yo, int x1,int y1, unsigned char color)
  133. {
  134. // this function uses Bresenham's algorithm IBM (1965) to draw a line from
  135. // (xo,yo) - (x1,y1) into the double buffer
  136.  
  137. int dx,             // difference in x's
  138.     dy,             // difference in y's
  139.     x_inc,          // amount in pixel space to move during drawing
  140.     y_inc,          // amount in pixel space to move during drawing
  141.     error=0,        // the discriminant i.e. error i.e. decision variable
  142.     index;          // used for looping
  143.  
  144.  
  145.  
  146. unsigned char far *vb_start = double_buffer; // directly access the double
  147.                                              // buffer for speed
  148.  
  149.  
  150. // SECTION 1 /////////////////////////////////////////////////////////////////
  151.  
  152. // pre-compute first pixel address in video buffer
  153. // use shifts for multiplication
  154.  
  155.  
  156. vb_start = vb_start + ((unsigned int)yo<<6) +
  157.                       ((unsigned int)yo<<8) +
  158.                       (unsigned int)xo;
  159.  
  160. // compute deltas
  161.  
  162. dx = x1-xo;
  163. dy = y1-yo;
  164.  
  165. // SECTION 2 /////////////////////////////////////////////////////////////////
  166.  
  167. // test which direction the line is going in i.e. slope angle
  168.  
  169. if (dx>=0)
  170.    {
  171.    x_inc = 1;
  172.  
  173.    } // end if line is moving right
  174. else
  175.    {
  176.    x_inc = -1;
  177.    dx    = -dx;  // need absolute value
  178.  
  179.    } // end else moving left
  180.  
  181. // SECTION 3 /////////////////////////////////////////////////////////////////
  182.  
  183. // test y component of slope
  184.  
  185. if (dy>=0)
  186.    {
  187.    y_inc = 320; // 320 bytes per line
  188.  
  189.    } // end if line is moving down
  190. else
  191.    {
  192.    y_inc = -320;
  193.    dy    = -dy;  // need absolute value
  194.  
  195.    } // end else moving up
  196.  
  197. // SECTION 4 /////////////////////////////////////////////////////////////////
  198.  
  199. // now based on which delta is greater we can draw the line
  200.  
  201. if (dx>dy)
  202.    {
  203.  
  204.    // draw the line
  205.  
  206.    for (index=0; index<=dx; index++)
  207.        {
  208.        // set the pixel
  209.  
  210.        *vb_start = color;
  211.  
  212.        // adjust the discriminate
  213.  
  214.        error+=dy;
  215.  
  216.        // test if error overflowed
  217.  
  218.        if (error>dx)
  219.           {
  220.  
  221.           error-=dx;
  222.  
  223.           // move to next line
  224.  
  225.           vb_start+=y_inc;
  226.  
  227.           } // end if error overflowed
  228.  
  229.        // move to the next pixel
  230.  
  231.        vb_start+=x_inc;
  232.  
  233.        } // end for
  234.  
  235.    } // end if |slope| <= 1
  236. else
  237.    {
  238.  
  239. // SECTION 5 /////////////////////////////////////////////////////////////////
  240.  
  241.    // draw the line
  242.  
  243.    for (index=0; index<=dy; index++)
  244.        {
  245.        // set the pixel
  246.  
  247.        *vb_start = color;
  248.  
  249.        // adjust the discriminate
  250.  
  251.        error+=dx;
  252.  
  253.        // test if error overflowed
  254.  
  255.        if (error>0)
  256.           {
  257.  
  258.           error-=dy;
  259.  
  260.           // move to next line
  261.  
  262.           vb_start+=x_inc;
  263.  
  264.           } // end if error overflowed
  265.  
  266.        // move to the next pixel
  267.  
  268.        vb_start+=y_inc;
  269.  
  270.        } // end for
  271.  
  272.    } // end else |slope| > 1
  273.  
  274. } // end Bline_DB
  275.  
  276. ///////////////////////////////////////////////////////////////////////////////
  277.  
  278.