home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_06 / transfx.c < prev    next >
Text File  |  1994-05-17  |  8KB  |  340 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"
  17. #include "graph4.h"
  18.  
  19. // D E F I N E S /////////////////////////////////////////////////////////////
  20.  
  21. #define NUM_WORMS 320
  22.  
  23. // S T R U C T U R E S //////////////////////////////////////////////////////
  24.  
  25. typedef struct worm_typ
  26.         {
  27.         int y;       // current y position of worm
  28.         int color;   // color of worm
  29.         int speed;   // speed of worm
  30.         int counter; // counter
  31.  
  32.         } worm, *worm_ptr;
  33.  
  34. // G L O B A L S /////////////////////////////////////////////////////////////
  35.  
  36. pcx_picture screen_fx; // our test screen
  37.  
  38. //////////////////////////////////////////////////////////////////////////////
  39.  
  40. void Fade_Lights(void)
  41. {
  42. // this functions fades the lights by slowly decreasing the color values
  43. // in all color registers
  44.  
  45. int pal_reg,index;
  46. RGB_color color;
  47.  
  48. for (index=0; index<30; index++)
  49.     {
  50.  
  51.     for (pal_reg=1; pal_reg<255; pal_reg++)
  52.         {
  53.         // get the color to fade
  54.  
  55.         Get_Palette_Register(pal_reg,(RGB_color_ptr)&color);
  56.  
  57.         if (color.red   > 5) color.red-=3;
  58.         else
  59.            color.red = 0;
  60.  
  61.         if (color.green > 5) color.green-=3;
  62.         else
  63.            color.green = 0;
  64.         if (color.blue  > 5) color.blue-=3;
  65.         else
  66.            color.blue = 0;
  67.  
  68.         // set the color to a diminished intensity
  69.  
  70.         Set_Palette_Register(pal_reg,(RGB_color_ptr)&color);
  71.  
  72.         } // end for pal_reg
  73.  
  74.     // wait a bit
  75.  
  76.     Delay(2);
  77.  
  78.     } // end fade for
  79.  
  80. } // end Fade_Lights
  81.  
  82. //////////////////////////////////////////////////////////////////////////////
  83.  
  84. void Disolve(void)
  85. {
  86. // disolve screen by ploting zillions of black pixels
  87.  
  88. unsigned long index;
  89.  
  90. for (index=0; index<=300000; index++,Plot_Pixel_Fast(rand()%320, rand()%200, 0));
  91.  
  92. } // end Disolve
  93.  
  94. //////////////////////////////////////////////////////////////////////////////
  95.  
  96. void Melt(void)
  97. {
  98.  
  99. // this function "melts" the screen by moving little worms at different speeds
  100. // down the screen.  These worms change to the color thy are eating
  101.  
  102. int index,ticks=0;
  103.  
  104. worm worms[NUM_WORMS]; // the array of worms used to make the screen melt
  105.  
  106. // initialize the worms
  107.  
  108. for (index=0; index<160; index++)
  109.     {
  110.  
  111.     worms[index].color   = Get_Pixel(index,0);
  112.     worms[index].speed   = 3 + rand()%9;
  113.     worms[index].y       = 0;
  114.     worms[index].counter = 0;
  115.  
  116.     // draw the worm
  117.  
  118.     Plot_Pixel_Fast((index<<1),0,(char)worms[index].color);
  119.     Plot_Pixel_Fast((index<<1),1,(char)worms[index].color);
  120.     Plot_Pixel_Fast((index<<1),2,(char)worms[index].color);
  121.  
  122.  
  123.     Plot_Pixel_Fast((index<<1)+1,0,(char)worms[index].color);
  124.     Plot_Pixel_Fast((index<<1)+1,1,(char)worms[index].color);
  125.     Plot_Pixel_Fast((index<<1)+1,2,(char)worms[index].color);
  126.  
  127.     } // end index
  128.  
  129. // do screen melt
  130.  
  131. while(++ticks<1800)
  132.      {
  133.  
  134.      // process each worm
  135.  
  136.      for (index=0; index<320; index++)
  137.          {
  138.          // is it time to move worm
  139.  
  140.          if (++worms[index].counter == worms[index].speed)
  141.             {
  142.             // reset counter
  143.  
  144.             worms[index].counter = 0;
  145.  
  146.             worms[index].color = Get_Pixel(index,worms[index].y+4);
  147.  
  148.             // has worm hit bottom?
  149.  
  150.             if (worms[index].y < 193)
  151.                {
  152.  
  153.                Plot_Pixel_Fast((index<<1),worms[index].y,0);
  154.                Plot_Pixel_Fast((index<<1),worms[index].y+1,(char)worms[index].color);
  155.                Plot_Pixel_Fast((index<<1),worms[index].y+2,(char)worms[index].color);
  156.                Plot_Pixel_Fast((index<<1),worms[index].y+3,(char)worms[index].color);
  157.  
  158.                Plot_Pixel_Fast((index<<1)+1,worms[index].y,0);
  159.                Plot_Pixel_Fast((index<<1)+1,worms[index].y+1,(char)worms[index].color);
  160.                Plot_Pixel_Fast((index<<1)+1,worms[index].y+2,(char)worms[index].color);
  161.                Plot_Pixel_Fast((index<<1)+1,worms[index].y+3,(char)worms[index].color);
  162.  
  163.                worms[index].y++;
  164.  
  165.                } // end if worm isn't at bottom yet
  166.  
  167.             } // end if time to move worm
  168.  
  169.          } // end index
  170.  
  171.      // accelerate melt
  172.  
  173.      if (!(ticks % 500))
  174.         {
  175.  
  176.         for (index=0; index<160; index++)
  177.             worms[index].speed--;
  178.  
  179.         } // end if time to accelerate melt
  180.  
  181.      } // end while
  182.  
  183. } // end Melt
  184.  
  185. //////////////////////////////////////////////////////////////////////////////
  186.  
  187. void Sheer(void)
  188. {
  189. // this program "sheers" the screen for lack of a better word.
  190.  
  191. long index;
  192. int x,y;
  193.  
  194. // select starting point of sheers
  195.  
  196. x=rand()%320;
  197. y=rand()%200;
  198.  
  199. // do it a few times to make sure whole screen is destroyed
  200.  
  201. for (index=0; index<100000; index++)
  202.     {
  203.  
  204.     // move sheers
  205.  
  206.     x+=17; // note the use of prime numbers
  207.     y+=13;
  208.  
  209.     // test if sheers are of boundaries, if so roll them over
  210.  
  211.     if (x>319)
  212.        x = x - 319;
  213.  
  214.     if (y>199)
  215.        y = y - 199;
  216.  
  217.     // plot the pixel in black
  218.  
  219.     Plot_Pixel_Fast(x,y,0);
  220.  
  221.     } // end for index
  222.  
  223. } // end Sheer
  224.  
  225. // M A I N ///////////////////////////////////////////////////////////////////
  226.  
  227. void main(void)
  228. {
  229. int done=0,                       // exit flag
  230.     index=0;                      // current position in instruction string
  231.  
  232. static char instructions[256]=""; // holds the instructions string
  233.  
  234. char buffer[41];
  235.  
  236. // build up instruction string
  237.  
  238. strcat(instructions,"..................................................");
  239. strcat(instructions,"Press 1 to fade the lights, ");
  240. strcat(instructions,"Press 2 to disolve the screen, ");
  241. strcat(instructions,"Press 3 to melt the screen, ");
  242. strcat(instructions,"Press 4 to sheer the screen.");
  243. strcat(instructions,"..................................................");
  244.  
  245. // set video mode to 320x200 256 color mode
  246.  
  247. Set_Video_Mode(VGA256);
  248.  
  249. // load in a background picture
  250.  
  251. PCX_Init((pcx_picture_ptr)&screen_fx);
  252.  
  253. PCX_Load("screenfx.pcx", (pcx_picture_ptr)&screen_fx,1);
  254.  
  255. PCX_Show_Buffer((pcx_picture_ptr)&screen_fx);
  256.  
  257. PCX_Delete((pcx_picture_ptr)&screen_fx);
  258.  
  259. // main event loop
  260.  
  261. while(!done)
  262.      {
  263.  
  264.      // wait for a keyboard hit
  265.  
  266.      if (kbhit())
  267.         {
  268.  
  269.         // which special fx did user want to see
  270.  
  271.         switch(getch())
  272.               {
  273.               case '1':  // dim lights
  274.                       {
  275.  
  276.                       Fade_Lights();
  277.  
  278.                       } break;
  279.  
  280.               case '2': // disolve screen
  281.                       {
  282.                       Disolve();
  283.  
  284.                       } break;
  285.  
  286.               case '3': // melt screen
  287.                       {
  288.  
  289.                       Melt();
  290.  
  291.                       } break;
  292.  
  293.               case '4': // sheer screen
  294.                       {
  295.  
  296.                       Sheer();
  297.  
  298.                       } break;
  299.  
  300.               default:break;
  301.  
  302.               } // end switch
  303.  
  304.         // set exit flag
  305.  
  306.         done=1;
  307.  
  308.         } // end if keyboard hit */
  309.  
  310.      // extract the sub string to be displayed
  311.  
  312.      memcpy(buffer,&instructions[index],40);
  313.  
  314.      // put NULL teminator at end
  315.  
  316.      buffer[40]=0;
  317.  
  318.      Blit_String(0,23*8+6,15,buffer,0);
  319.  
  320.      // increment instruction index
  321.      // roll over if at end of instructions
  322.  
  323.      if (++index>180)
  324.         index=0;
  325.  
  326.      // assume that user can only read at 1,000,000 words a second
  327.  
  328.      Delay(2);
  329.  
  330.      } // end while
  331.  
  332. // go back to text mode
  333.  
  334. Set_Video_Mode(TEXT_MODE);
  335.  
  336. } // end main
  337.  
  338.  
  339.  
  340.