home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_4 / spheres.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  2.3 KB  |  106 lines

  1.  
  2. // SPHERES.C - A demo of mode Z page flipping
  3.  
  4. // I N C L U D E S ///////////////////////////////////////////////////////////
  5.  
  6. #include <io.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12. #include <fcntl.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #include <math.h>
  16. #include <string.h>
  17.  
  18. #include "black3.h"
  19. #include "black4.h"
  20.  
  21. // F U N C T I O N S /////////////////////////////////////////////////////////
  22.  
  23. void Draw_Sphere(int x0,int y0,int radius, int color)
  24. {
  25. // this function will draw a sphere in
  26.  
  27. int x1,y1,y2, // location coordinates
  28.     angle;   // used to track current angle
  29.  
  30. // interate thru 90 degrees and use symetry to draw circle
  31.  
  32. for (angle=0; angle<90; angle++)
  33.     {
  34.     // draw the sphere as a collection of vertical strips
  35.  
  36.     x1 = radius*cos(3.14159*(float)angle/(float)180);
  37.     y1 = -radius*sin(3.14159*(float)angle/(float)180)*1.66;
  38.  
  39.     // draw the next vertical strip
  40.  
  41.     for (y2=y0+y1; y2<y0-y1; y2++)
  42.         {
  43.         Write_Pixel_Z(x0+x1,y2,color);
  44.         Write_Pixel_Z(x0-x1,y2,color);
  45.         } // end for y2
  46.  
  47.     } // end for angle
  48.  
  49. } // end Draw_Sphere
  50.  
  51. // M A I N ////////////////////////////////////////////////////////////////////
  52.  
  53. void main(int argc, char **argv)
  54. {
  55. int index; // loop variables
  56.  
  57. // set the graphics mode to mode Z 320x400x256
  58.  
  59. Set_Mode_Z();
  60.  
  61. // clear out all of display memory, only page 1 was cleared during set_mode_z
  62.  
  63. Set_Working_Page_Mode_Z(PAGE_1);
  64. Fill_Screen_Z(0);
  65.  
  66. // set visual and working page to page 0
  67.  
  68. Set_Visual_Page_Mode_Z(PAGE_0);
  69. Set_Working_Page_Mode_Z(PAGE_0);
  70.  
  71. // draw some colored spheres on this page
  72.  
  73. for (index=0; index<50; index++)
  74.     {
  75.     Draw_Sphere(20+rand()%280,20+rand()%360,rand()%15, 34 + rand()%6);
  76.     } // end for index
  77.  
  78. // now draw grey spheres on page 1
  79.  
  80. Set_Working_Page_Mode_Z(PAGE_1);
  81.  
  82. for (index=0; index<50; index++)
  83.     {
  84.     Draw_Sphere(20+rand()%280,20+rand()%360,rand()%15, 16 + rand()%16);
  85.     } // end for index
  86.  
  87. // now toggle between pages
  88.  
  89. while(!kbhit())
  90.      {
  91.  
  92.      Set_Visual_Page_Mode_Z(PAGE_0);
  93.      Time_Delay(5);
  94.  
  95.      Set_Visual_Page_Mode_Z(PAGE_1);
  96.      Time_Delay(5);
  97.  
  98.      } // end while
  99.  
  100. // restore the video system to text
  101.  
  102. Set_Graphics_Mode(TEXT_MODE);
  103.  
  104. } // end main
  105.  
  106.