home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / Source / GPCHAP02 / PROG2_1.CPP next >
Encoding:
C/C++ Source or Header  |  2002-04-24  |  4.2 KB  |  173 lines

  1. // PROG2_1.CPP - A simple console based game to illustrate
  2. // a generic game loop
  3.  
  4. // INCLUDES ///////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <ctype.h> 
  9. #include <conio.h>
  10. #include <windows.h>
  11. #include <time.h>
  12.  
  13. // DEFINES ////////////////////////////////////////////////
  14.  
  15. #define MAX_X        77  // maximum x position for player
  16. #define SCROLL_POS   24  // the point that scrolling occurs
  17.  
  18. // PROTOTYPES /////////////////////////////////////////////
  19.  
  20. void Init_Graphics(void);
  21. inline void Set_Color(int fcolor, int bcolor);
  22. inline void Draw_String(int x,int y, char *string);
  23.  
  24. // GLOBALS ////////////////////////////////////////////////
  25.  
  26. CONSOLE_SCREEN_BUFFER_INFO con_info;   // holds screen info
  27.  
  28. HANDLE hconsole;         // handle to console
  29. int    game_running = 1; // state of game, 0=done, 1=run
  30.  
  31. // FUNCTIONS //////////////////////////////////////////////
  32.  
  33. void Init_Graphics(void)
  34. {
  35. // this function initializes the console graphics engine
  36.  
  37. COORD console_size = {80,25}; // size of console
  38.  
  39. // seed the random number generator with time
  40. srand((unsigned)time(NULL));
  41.  
  42. // open i/o channel to console screen
  43. hconsole=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ,
  44.          FILE_SHARE_READ | FILE_SHARE_WRITE,
  45.          0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
  46.  
  47. // make sure we are in 80x25
  48. SetConsoleScreenBufferSize(hconsole,console_size);
  49.  
  50. // get details for console screen                       
  51. GetConsoleScreenBufferInfo(hconsole,&con_info);
  52.  
  53. } // end Init_Graphics
  54.  
  55. ///////////////////////////////////////////////////////////
  56.  
  57. inline void Set_Color(int fcolor, int bcolor=0) 
  58. {
  59. // this function sets the color of the console output
  60. SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) | 
  61.                         fcolor));
  62.  
  63. } // Set_Color
  64.  
  65. ///////////////////////////////////////////////////////////
  66.  
  67. inline void Draw_String(int x,int y, char *string)
  68. {
  69. // this function draws a string at the given x,y
  70.  
  71. COORD cursor_pos; // used to pass coords
  72.  
  73. // set printing position
  74. cursor_pos.X = x;
  75. cursor_pos.Y = y;
  76. SetConsoleCursorPosition(hconsole,cursor_pos);
  77.  
  78. // print the string in current color
  79. printf("%s",string);
  80.  
  81. } // end Draw_String
  82.  
  83. ///////////////////////////////////////////////////////////
  84.  
  85. inline void Clear_Screen(void)
  86. {
  87. // this function clears the screen
  88.  
  89. // set color to white on black
  90. Set_Color(15,0);
  91.  
  92. // clear the screen
  93. for (int index=0; index<=25; index++)
  94.     Draw_String(0, SCROLL_POS,"\n");
  95.  
  96. } // end Clear_Screen
  97.  
  98. // MAIN GAME LOOP /////////////////////////////////////////
  99.  
  100. void main(void)
  101. {
  102. char key;            // player input data
  103. int  player_x = 40;  // player's x position
  104.  
  105. // SECTION: initialization
  106.  
  107. // set up the console text graphics system
  108. Init_Graphics();
  109.  
  110. // clear the screen
  111. Clear_Screen();
  112.  
  113. // SECTION: main event loop, this is where all the action  
  114. // takes place, the general loop is erase-move-draw
  115.  
  116. while(game_running)
  117.      {
  118.      // SECTION: erase all the objects or clear screen
  119.  
  120.      // nothing to erase in our case   
  121.  
  122.      // SECTION: get player input
  123.      if (kbhit())
  124.         {
  125.         // get keyboard data, and filter it
  126.         key = toupper(getch());
  127.  
  128.         // is player trying to exit, if so exit
  129.         if (key=='Q' || key==27)
  130.            game_running = 0; 
  131.  
  132.         // is player moving left        
  133.         if (key=='A')
  134.            player_x--;
  135.            
  136.         // is player moving right
  137.         if (key=='S')
  138.            player_x++;
  139.  
  140.         } // end if   
  141.  
  142.      // SECTION: game logic and further processing
  143.      
  144.      // make sure player stays on screen 
  145.      if (++player_x > MAX_X)
  146.         player_x=MAX_X;     
  147.  
  148.      if (--player_x < 0)
  149.         player_x=0;     
  150.  
  151.      // SECTION: draw everything
  152.  
  153.      // draw next star at random position
  154.      Set_Color(15,0);
  155.      Draw_String(rand()%80, SCROLL_POS,".\n");
  156.      
  157.      // draw player 
  158.      Set_Color(rand()%15,0);
  159.      Draw_String(player_x,0,"<-*->");
  160.      Draw_String(0,0,"");   
  161.  
  162.      // SECTION: synchronize to a constant frame rate
  163.      Sleep(40);   
  164.  
  165.      } // end while
  166.  
  167. // SECTION: shutdown and bail
  168. Clear_Screen();
  169.  
  170. printf("\nG A M E  O V E R \n\n");
  171.  
  172. } // end main
  173.