home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TUTORC.ZIP / TUT6.C < prev    next >
C/C++ Source or Header  |  1994-10-30  |  5KB  |  174 lines

  1. /*
  2.   tut6.c
  3.   10/30/94
  4.   from tutprog6.pas
  5.   Adapted from Denthor's tutprog6.pas
  6.   Translated into C, from Denthor's VGA Trainer, by
  7.   Steve Pinault, scp@ohm.att.com
  8.   I modified the parameters of the spiral to cover the whole screen.
  9.   It takes about 15 seconds to draw the spiral on my 486.
  10.   (Running under Windows it takes 25 seconds!)
  11.   Compiled with Microsoft Visual C++ 1.5 (Microsoft C 8.0)
  12.   To compile:
  13.   First compile the subroutines in tutsubs.c with the batch file 
  14.   cltutsub.bat
  15.   Then compile any of the tutor programs with the batch file
  16.   cltut.bat
  17.   Example: C:>cltutsub
  18.            C:>cltut tut6.c
  19.            to compile this program.
  20. */
  21.  
  22. #include "tutheadr.h"
  23.  
  24. float costbl[8000];
  25. float sintbl[8000];
  26. int  ncolors=40;
  27.  
  28. // DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD} 
  29. // Procedure NormCirc; 
  30. // This generates a spiral without using a lookup table } 
  31. void NormCirc()
  32. {
  33.   float deg,radius;
  34.   int x,y;
  35.   int x1,y1;
  36.   int loop1;
  37.   for(loop1=60;loop1>60-ncolors;loop1--)
  38.   {
  39.     deg=(float)-90.0;
  40.     radius=160+loop1;
  41.     while(radius>=0)
  42.     {
  43.       x=round(radius*(float)cos ((double)rad (deg)));
  44.       y=round(radius*(float)sin ((double)rad (deg)));
  45.       x1=x+160;y1=y+100;
  46.       if((x1>=0)&&(x1<320)&&(y1>=0)&&(y1<200))
  47.                 PutPixel (x1,y1,(char)(61-loop1),VGA);
  48.       deg=deg+(float)0.045; // Increase the degree so the circle is round 
  49.       radius-=(float)0.005; // Decrease the radius for a spiral effect 
  50.     }
  51.   }
  52. }
  53.  
  54. // DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  55. //  Procedure Setupvars;
  56. void Setupvars()
  57. {
  58.     float deg;
  59.     int loop1;
  60.     deg=(float)-90.0;
  61.     for(loop1=0;loop1<8000;loop1++)
  62.     {
  63.       deg=deg+(float)0.045;  // 360 degs / 0.045 degs = 8000 elements in array.
  64.       costbl[loop1]=(float)cos((double)rad(deg));
  65.       sintbl[loop1]=(float)sin((double)rad(deg));
  66.     }
  67. }
  68.  
  69. // DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  70. // Procedure LookupCirc;
  71. // This draws a spiral using a lookup table }
  72. void LookupCirc()
  73. {
  74.   float radius;
  75.   int x,y,pos; 
  76.   int x1,y1;
  77.   int loop1;
  78.   Cls (0,VGA);
  79.   Setupvars();
  80.   for(loop1=60;loop1>60-ncolors;loop1--)
  81.   {
  82.     pos=0;
  83.     radius=160+loop1;
  84.     while(radius>=0)
  85.     {
  86.       x=round(radius*costbl[pos]);  // Note how I am not recalculating sin}
  87.       y=round(radius*sintbl[pos]);  // and cos for each point.            }
  88.       x1=x+160;y1=y+100;
  89.       if((x1>=0)&&(x1<320)&&(y1>=0)&&(y1<200))
  90.                 PutPixel (x1,y1,(char)(61-loop1),VGA);
  91.       radius-=(float)0.005;
  92.       pos++;
  93.       if(pos==8000)pos=0;   // 
  94.     }
  95.   }
  96. }
  97.  
  98. // {DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD}
  99. // Procedure PalPlay;               
  100. // Moves the pallette down 1 color, working with colors 0 to number-1.
  101. void PalPlay6(int number)
  102. {
  103.     char Tmp[3];
  104.     int loop1;
  105.  
  106. // This copies color 0 from our virtual pallette to the Tmp variable:
  107.    _fmemmove(Tmp,Pall[0],3);
  108. // This moves the entire virtual pallette down one color:
  109.    _fmemmove(Pall[0],Pall[1],(number-1)*3);
  110. // This copies the Tmp variable to the bottom of the virtual pallette:
  111.    _fmemmove(Pall[number-1],Tmp,3);
  112.    WaitRetrace();
  113.    for(loop1=0;loop1<number;loop1++)
  114.      Pal((unsigned char)(loop1+1),Pall[loop1][0],Pall[loop1][1],Pall[loop1][2]);
  115. }
  116.  
  117. void main()
  118. {                                         
  119.   int c,loop1;
  120.   _clearscreen(_GCLEARSCREEN);
  121.   printf("Hi there! This program will demonstrate the usefullness of \n");
  122.   printf("pregenerated arrays, also known as lookup tables. The program\n");
  123.   printf("will first draw a spiral without using a lookup table, rotate\n");
  124.   printf("the pallette until a key is pressed, the calculate the lookup\n");
  125.   printf("table, then draw the same spiral using the lookup table.\n");
  126.   printf("\n");
  127.   printf("This is merely one example for the wide range of uses of a \n");
  128.   printf("lookup table.\n");
  129.   printf("\n");
  130.   printf("\n");
  131.   printf("  Hit any key to contine ...\n");
  132.   getch();
  133.   SetMCGA();
  134.   // Arrangement for ncolors = 40:
  135.   for(loop1=0;loop1<ncolors/2;loop1++)
  136.   {
  137.     Pall[loop1][0] = 3*loop1+3;
  138.     Pall[loop1][1] = 0;
  139.     Pall[loop1][2] = 0;
  140.   }
  141.   for(loop1=0;loop1<ncolors/2;loop1++)
  142.   {
  143.     Pall[ncolors/2+loop1][0] = 3*ncolors/2+3;
  144.     Pall[ncolors/2+loop1][1] = 3*loop1;
  145.     Pall[ncolors/2+loop1][2] = 0;
  146.   }
  147.    WaitRetrace();
  148.    for(loop1=0;loop1<ncolors;loop1++)
  149.      Pal ((char)(loop1+1),Pall[loop1][0],Pall[loop1][1],Pall[loop1][2]);
  150.         // This sets the true pallette to variable Pall }
  151.  
  152. //  NormCirc();   //      { This draws a spiral without lookups }
  153. //  while(1)      //  I've commented this out because it's too slow 
  154. //  {             //  with the expanded spiral!
  155. //     PalPlay6(ncolors);
  156. //     c=_bios_keybrd(_KEYBRD_READY);
  157. //     if(c)break;
  158. //  }
  159. //  getch();
  160. //  Blackout();
  161.   LookupCirc();    //   { This draws a spiral with lookups }
  162. //  getch();
  163. //  FadeUp();
  164.   while(1)
  165.   {
  166.      PalPlay6(ncolors);
  167.      c=_bios_keybrd(_KEYBRD_READY);
  168.      if(c)break;
  169.   }
  170.   getch();
  171.   FadeDown();
  172.   SetText();
  173. }
  174.