home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Game Programming Gurus / Tricks_of_the_Game_Programming_Gurus_SAMS_Publishing_1994.iso / source / chap_12 / stars.c < prev    next >
Text File  |  1994-02-18  |  4KB  |  185 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <dos.h>
  5. #include <bios.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <conio.h>
  9. #include <graph.h>
  10.  
  11.  
  12. // D E F I N E S /////////////////////////////////////////////////////////////
  13.  
  14. #define TIME_KEEPER_INT 0x1C
  15. #define NUM_STARS 50
  16.  
  17. // S T R U C T U R E S ///////////////////////////////////////////////////////
  18.  
  19. typedef struct star_typ
  20.         {
  21.         int x,y;    // position of star
  22.         int vel;    // x - component of star velocity
  23.         int color;  // color of star
  24.  
  25.         } star, *star_ptr;
  26.  
  27.  
  28. // G L O B A L S /////////////////////////////////////////////////////////////
  29.  
  30. void (_interrupt _far *Old_Isr)();  // holds old com port interrupt handler
  31.  
  32. unsigned char far *video_buffer = (char far *)0xA0000000L; // vram byte ptr
  33.  
  34. int star_first=1;  // flags first time into star field
  35.  
  36. star stars[NUM_STARS]; // the star field
  37.  
  38.  
  39. // F U N C T I O N S ////////////////////////////////////////////////////////
  40.  
  41. Plot_Pixel_Fast(int x,int y,unsigned char color)
  42. {
  43.  
  44. // plots the pixel in the desired color a little quicker using binary shifting
  45. // to accomplish the multiplications
  46.  
  47. // use the fact that 320*y = 256*y + 64*y = y<<8 + y<<6
  48.  
  49. video_buffer[((y<<8) + (y<<6)) + x] = color;
  50.  
  51. } // end Plot_Pixel_Fast
  52.  
  53. //////////////////////////////////////////////////////////////////////////////
  54.  
  55. void _interrupt _far Star_Int()
  56. {
  57.  
  58. // this function will create a panning 3-d star field with 3-planes, like
  59. // looking out of the Enterprise
  60.  
  61. // note: this function had better execute faster than 55.4 ms, otherwise it
  62. // will be called again re-entrantly and kaboom!
  63.  
  64. int index;
  65.  
  66. // test if we need to initialize star field i.e. first time function is being
  67. // called
  68.  
  69. if (star_first)
  70.    {
  71.    // reset first time
  72.    star_first=0;
  73.  
  74.    // initialize all the stars
  75.  
  76.    for (index=0; index<NUM_STARS; index++)
  77.        {
  78.        // initialize each star to a velocity, position and color
  79.  
  80.        stars[index].x     = rand()%320;
  81.        stars[index].y     = rand()%180;
  82.  
  83.        // decide what star plane the star is in
  84.  
  85.        switch(rand()%3)
  86.              {
  87.              case 0: // plane 1- the farthest star plane
  88.                   {
  89.                   // set velocity and color
  90.  
  91.                   stars[index].vel = 2;
  92.                   stars[index].color = 8;
  93.  
  94.                   } break;
  95.  
  96.              case 1: // plane 2-The medium distance star plane
  97.                   {
  98.  
  99.                   stars[index].vel = 4;
  100.                   stars[index].color = 7;
  101.  
  102.                   } break;
  103.  
  104.              case 2: // plane 3-The nearest star plane
  105.                   {
  106.  
  107.                   stars[index].vel = 6;
  108.                   stars[index].color = 15;
  109.  
  110.                   } break;
  111.  
  112.              } // end switch
  113.  
  114.        } // end for index
  115.  
  116.    } // end if first time
  117. else
  118.    { // must be nth time in, so do the usual
  119.  
  120.    // erase, move, draw
  121.  
  122.    for (index=0; index<NUM_STARS; index++)
  123.        {
  124.        // erase
  125.  
  126.        Plot_Pixel_Fast(stars[index].x,stars[index].y,0);
  127.  
  128.        // move
  129.  
  130.        if ( (stars[index].x+=stars[index].vel) >=320 )
  131.           stars[index].x = 0;
  132.  
  133.        // draw
  134.  
  135.        Plot_Pixel_Fast(stars[index].x,stars[index].y,stars[index].color);
  136.  
  137.        } // end for index
  138.  
  139.    } // end else
  140.  
  141. } // end Star_Int
  142.  
  143. // M A I N ///////////////////////////////////////////////////////////////////
  144.  
  145. main()
  146. {
  147. int num1, num2,c;
  148.  
  149. _setvideomode(_MRES256COLOR);
  150.  
  151. // install our ISR
  152.  
  153. Old_Isr = _dos_getvect(TIME_KEEPER_INT);
  154.  
  155. _dos_setvect(TIME_KEEPER_INT, Star_Int);
  156.  
  157. // wait for user to hit a key
  158.  
  159. _settextposition(23,0);
  160.  
  161. printf("Hit Q - to quit.");
  162. printf("\nHit E - to see something wonderful...");
  163.  
  164. // get the character
  165.  
  166. c = getch();
  167.  
  168. // does user feel adventurous
  169.  
  170. if (c=='e')
  171.    {
  172.    printf("\nLook stars in DOS, how can this be ?");
  173.  
  174.    exit(0);  // exit without fixing up old ISR
  175.    } // end if
  176.  
  177. // replace old ISR
  178.  
  179. _dos_setvect(TIME_KEEPER_INT, Old_Isr);
  180.  
  181. _setvideomode(_DEFAULTMODE);
  182.  
  183. } // end main
  184.  
  185.