home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_05 / linedemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-13  |  5.8 KB  |  300 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.  
  19. // D E F I N E S /////////////////////////////////////////////////////////////
  20.  
  21. // S T R U C T U R E S ///////////////////////////////////////////////////////
  22.  
  23. // P R O T O T Y P E S ///////////////////////////////////////////////////////
  24.  
  25. void Bline(int xo, int yo, int x1,int y1, unsigned char color);
  26.  
  27. void Bounce(void);
  28.  
  29. // G L O B A L S  ////////////////////////////////////////////////////////////
  30.  
  31. // F U N C T I O N S /////////////////////////////////////////////////////////
  32.  
  33. void Bounce(void)
  34. {
  35. // this function makes use of the bline function to bounce a line around
  36.  
  37. int xo,yo,x1,y1,x2,y2,x3,y3;
  38. int dxo,dyo,dx1,dy1,dx2,dy2,dx3,dy3;
  39. long counter=0;
  40. int color=9;
  41.  
  42. // starting positions of lines
  43.  
  44. xo=x2=rand()%320;
  45. yo=y2=rand()%200;
  46. x1=x3=rand()%320;
  47. y1=y3=rand()%200;
  48.  
  49. // velocities of lines
  50.  
  51. dxo=dx2=2+rand()%5;
  52. dyo=dy2=3+rand()%5;
  53. dx1=dx3=2+rand()%5;
  54. dy1=dy3=2+rand()%5;
  55.  
  56. // animation loop
  57.  
  58. while(!kbhit())
  59.      {
  60.      // draw leader
  61.  
  62.      Bline(xo,yo,x1,y1,color);
  63.  
  64.      // move line
  65.  
  66.      if ((xo+=dxo)>=315 || xo<5)
  67.         dxo=-dxo;
  68.  
  69.      if ((yo+=dyo)>=195 || yo<5)
  70.         dyo=-dyo;
  71.  
  72.      if ((x1+=dx1)>=315 || x1<5)
  73.         dx1=-dx1;
  74.  
  75.      if ((y1+=dy1)>=195 || y1<5)
  76.         dy1=-dy1;
  77.  
  78.      // test if it's time to follow the leader
  79.  
  80.      if (++counter>50)
  81.         {
  82.  
  83.         Bline(x2,y2,x3,y3,0);
  84.  
  85.         // move line
  86.  
  87.         if ((x2+=dx2)>=315 || x2<5)
  88.            dx2=-dx2;
  89.  
  90.         if ((y2+=dy2)>=195 || y2<5)
  91.            dy2=-dy2;
  92.  
  93.         if ((x3+=dx3)>=315 || x3<5)
  94.            dx3=-dx3;
  95.  
  96.         if ((y3+=dy3)>=195 || y3<5)
  97.            dy3=-dy3;
  98.  
  99.         } // end if time to follow
  100.  
  101.      // wait a while so humans can see it
  102.  
  103.      Delay(1);
  104.  
  105.      // update color
  106.  
  107.      if (counter>250)
  108.         {
  109.         if (++color>=16)
  110.            color=1;
  111.         counter = 51;
  112.  
  113.         } // end if time to change color
  114.  
  115.      } // end while
  116.  
  117. } // end Bounce
  118.  
  119. //////////////////////////////////////////////////////////////////////////////
  120.  
  121. void Bline(int xo, int yo, int x1,int y1, unsigned char color)
  122. {
  123. // this function uses Bresenham's algorithm IBM (1965) to draw a line from
  124. // (xo,yo) - (x1,y1)
  125.  
  126. int dx,             // difference in x's
  127.     dy,             // difference in y's
  128.     x_inc,          // amount in pixel space to move during drawing
  129.     y_inc,          // amount in pixel space to move during drawing
  130.     error=0,        // the discriminant i.e. error i.e. decision variable
  131.     index;          // used for looping
  132.  
  133.  
  134.  
  135. unsigned char far *vb_start = video_buffer; // directly access the video
  136.                                             // buffer for speed
  137.  
  138.  
  139. // SECTION 1 /////////////////////////////////////////////////////////////////
  140.  
  141. // pre-compute first pixel address in video buffer
  142. // use shifts for multiplication
  143.  
  144.  
  145. vb_start = vb_start + ((unsigned int)yo<<6) +
  146.                       ((unsigned int)yo<<8) +
  147.                       (unsigned int)xo;
  148.  
  149. // compute deltas
  150.  
  151. dx = x1-xo;
  152. dy = y1-yo;
  153.  
  154. // SECTION 2 /////////////////////////////////////////////////////////////////
  155.  
  156. // test which direction the line is going in i.e. slope angle
  157.  
  158. if (dx>=0)
  159.    {
  160.    x_inc = 1;
  161.  
  162.    } // end if line is moving right
  163. else
  164.    {
  165.    x_inc = -1;
  166.    dx    = -dx;  // need absolute value
  167.  
  168.    } // end else moving left
  169.  
  170. // SECTION 3 /////////////////////////////////////////////////////////////////
  171.  
  172. // test y component of slope
  173.  
  174. if (dy>=0)
  175.    {
  176.    y_inc = 320; // 320 bytes per line
  177.  
  178.    } // end if line is moving down
  179. else
  180.    {
  181.    y_inc = -320;
  182.    dy    = -dy;  // need absolute value
  183.  
  184.    } // end else moving up
  185.  
  186. // SECTION 4 /////////////////////////////////////////////////////////////////
  187.  
  188. // now based on which delta is greater we can draw the line
  189.  
  190. if (dx>dy)
  191.    {
  192.  
  193.    // draw the line
  194.  
  195.    for (index=0; index<=dx; index++)
  196.        {
  197.        // set the pixel
  198.  
  199.        *vb_start = color;
  200.  
  201.        // adjust the discriminate
  202.  
  203.        error+=dy;
  204.  
  205.        // test if error overflowed
  206.  
  207.        if (error>dx)
  208.           {
  209.  
  210.           error-=dx;
  211.  
  212.           // move to next line
  213.  
  214.           vb_start+=y_inc;
  215.  
  216.           } // end if error overflowed
  217.  
  218.        // move to the next pixel
  219.  
  220.        vb_start+=x_inc;
  221.  
  222.        } // end for
  223.  
  224.    } // end if |slope| <= 1
  225. else
  226.    {
  227.  
  228. // SECTION 5 /////////////////////////////////////////////////////////////////
  229.  
  230.    // draw the line
  231.  
  232.    for (index=0; index<=dy; index++)
  233.        {
  234.        // set the pixel
  235.  
  236.        *vb_start = color;
  237.  
  238.        // adjust the discriminate
  239.  
  240.        error+=dx;
  241.  
  242.        // test if error overflowed
  243.  
  244.        if (error>0)
  245.           {
  246.  
  247.           error-=dy;
  248.  
  249.           // move to next line
  250.  
  251.           vb_start+=x_inc;
  252.  
  253.           } // end if error overflowed
  254.  
  255.        // move to the next pixel
  256.  
  257.        vb_start+=y_inc;
  258.  
  259.        } // end for
  260.  
  261.    } // end else |slope| > 1
  262.  
  263. } // end Bline
  264.  
  265. // M A I N ///////////////////////////////////////////////////////////////////
  266.  
  267. void main(void)
  268. {
  269. // set video mode to 320x200 256 color mode
  270.  
  271. Set_Video_Mode(VGA256);
  272.  
  273. // draw a couple lines
  274.  
  275. while(!kbhit())
  276.      {
  277.      // plot a line between a random start and end point
  278.  
  279.      Bline(rand()%320,rand()%200,rand()%320,rand()%200,rand()%256);
  280.  
  281.      } // end while
  282.  
  283. getch();
  284.  
  285. // clear the screen
  286.  
  287. Set_Video_Mode(VGA256);
  288.  
  289. // show off a little screen saver
  290.  
  291. Bounce();
  292.  
  293. // reset the video mode back to text
  294.  
  295. Set_Video_Mode(TEXT_MODE);
  296.  
  297.  
  298. } // end main
  299.  
  300.