home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_2 / light.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  4.1 KB  |  217 lines

  1.  
  2. // LIGHT.C - An example of real-time event loops
  3.  
  4. // I N C L U D E S ////////////////////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <math.h>
  10. #include <graphics.h>
  11.  
  12. // D E F I N E S /////////////////////////////////////////////////////////////
  13.  
  14. // directions
  15.  
  16. #define NORTH   0
  17. #define EAST    1
  18. #define SOUTH   2
  19. #define WEST    3
  20.  
  21. // F U N C T I O N S /////////////////////////////////////////////////////////
  22.  
  23. void Delay(int clicks)
  24. {
  25. // this function uses the internal timer to delay a number of clock ticks
  26.  
  27. unsigned long far *clock = (unsigned long far *)0x0000046CL;
  28.  
  29. unsigned long now;
  30.  
  31. // get current time
  32.  
  33. now = *clock;
  34.  
  35. // wait for number of click to pass
  36.  
  37. while(abs(*clock - now) < clicks){}
  38.  
  39. } // end Delay
  40.  
  41. //////////////////////////////////////////////////////////////////////////////
  42.  
  43. void Draw_Game_Grid(void)
  44. {
  45. // this function draws a game grid out of horizontal and vertical lines
  46.  
  47. short x,y;
  48.  
  49. // the the line color to white
  50.  
  51. setcolor(15);
  52.  
  53. // draw the verical lines
  54.  
  55. for (x=0; x<320; x+=20)
  56.      {
  57.  
  58.      // position the pen and draw the line
  59.  
  60.      moveto(x,0);
  61.      lineto(x,199);
  62.  
  63.      } // end for x
  64.  
  65. // draw the horizontal lines
  66.  
  67. for (y=0; y<200; y+=20)
  68.      {
  69.  
  70.      // position the pen and draw the line
  71.  
  72.      moveto(0,y);
  73.      lineto(319,y);
  74.  
  75.      } // end for y
  76.  
  77. } // end Draw_Game_Grid
  78.  
  79. // M A I N ////////////////////////////////////////////////////////////////////
  80.  
  81. void main(void)
  82. {
  83.  
  84. int done=0,                    // main event loop exit flag
  85.      player_x         = 160,    // starting position and direction of player
  86.      player_y         = 150,
  87.      player_direction = NORTH;
  88.  
  89.      int driver=2, mode=2;
  90.  
  91. // SECTION 1 //////////////////////////////////////////////////////////////////
  92.  
  93. // set the graphics mode to mode 13h 320x200x256
  94.  
  95. initgraph(&driver,&mode,"");
  96.  
  97. // draw the game grid
  98.  
  99. Draw_Game_Grid();
  100.  
  101. // SECTION 2 //////////////////////////////////////////////////////////////////
  102.  
  103. // begin real time event loop
  104.  
  105. while(!done)
  106.      {
  107.  
  108. // SECTION 3 //////////////////////////////////////////////////////////////////
  109.  
  110.       // is the player steering his light cycle or trying to exit?
  111.  
  112.       if (kbhit())
  113.           {
  114.  
  115.         // test for <A>,<S> or <Q>
  116.  
  117.         switch(getch())
  118.               {
  119.  
  120.               case 'a': // turn left
  121.                    {
  122.  
  123.                          // turn 90 to the left
  124.  
  125.                          if (--player_direction<NORTH)
  126.                              player_direction=WEST;
  127.  
  128.  
  129.                    } break;
  130.  
  131.                   case 's': // turn right
  132.                    {
  133.  
  134.                    // turn 90 to the right
  135.  
  136.                    if (++player_direction>WEST)
  137.                              player_direction=NORTH;
  138.  
  139.                    } break;
  140.  
  141.                   case 'q': // quit game
  142.                          {
  143.                          // set exit flag to true
  144.  
  145.                    done=1;
  146.  
  147.                    } break;
  148.  
  149.               default:break; // do nothing
  150.  
  151.               } // end switch
  152.  
  153.           } // end if kbhit
  154.  
  155. // SECTION 4 //////////////////////////////////////////////////////////////////
  156.  
  157.       // at this point we need to move the light cycle in the direction its
  158.       // currently pointing
  159.  
  160.      switch(player_direction)
  161.            {
  162.  
  163.            case NORTH:
  164.                 {
  165.  
  166.                 if (--player_y<0)
  167.                          player_y = 199;
  168.  
  169.                 } break;
  170.  
  171.               case SOUTH:
  172.                      {
  173.                      if (++player_y>199)
  174.                    player_y = 0;
  175.  
  176.                 } break;
  177.  
  178.            case EAST:
  179.                 {
  180.                 if (++player_x>319)
  181.                    player_x = 0;
  182.  
  183.                      } break;
  184.  
  185.               case WEST:
  186.                      {
  187.                      if (--player_x<0)
  188.                          player_y = 319;
  189.  
  190.                 } break;
  191.  
  192.            default:break;
  193.  
  194.  
  195.            } // end switch direction
  196.  
  197. // SECTION 5 //////////////////////////////////////////////////////////////////
  198.  
  199.      // render the lightcyle
  200.  
  201.       putpixel((short)player_x,(short)player_y,(short)9);
  202.  
  203.       // wait a moment and lock the game to 18 fps
  204.  
  205.       Delay(1);
  206.  
  207.       } // end while
  208.  
  209. // SECTION 6 //////////////////////////////////////////////////////////////////
  210.  
  211. // restore the video mode back to text
  212.  
  213. closegraph();
  214.  
  215. } // end main
  216.  
  217.