1.  
  2. #include "game.h"
  3. //#include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main (int argc, char* args[] )
  8. {
  9.     bool quit = false;
  10.     bool cap = true;    // flag - cap the frame rate or not
  11.  
  12.     // event structure
  13.     SDL_Event event;
  14.  
  15.      // Load the Config data from file (to have dynamic screen size, etc)
  16.      load_Config("setup.cfg");
  17.  
  18.     if (!Game_Init(0))
  19.         return 0;
  20.  
  21.     //Keep track of the frame count
  22.     int frame = 0; //Timer used to calculate the frames per second
  23.     CPTimer fps,update; //Timer used to update the caption Timer update;
  24.  
  25.     //Start the update timer
  26.     update.start();
  27.     //Start the frame timer
  28.     fps.start();
  29.  
  30.     double old_fps = 0;
  31.  
  32.     while ( quit == false )
  33.     {
  34.         while ( SDL_PollEvent( &event ) )
  35.         {
  36.             if ( event.type == SDL_QUIT )
  37.             {
  38.                 quit = true;
  39.             }
  40.             if ( event.type == SDL_KEYDOWN )
  41.             {
  42.                 // check if enter key pressed
  43.                 if ( event.key.keysym.sym == SDLK_RETURN )
  44.                 {
  45.                     if (cap)
  46.                     {
  47.                         //The frame rate as a string
  48.                         std::stringstream caption;
  49.  
  50.                         //Calculate the frames per second and create the string
  51.                         #ifdef USE_OPENGL
  52.                         caption << "BugHunt 2942AD (SDL + OpenGL)";
  53.                         #else
  54.                         caption << "BugHunt 2942AD (SDL)";
  55.                         #endif
  56.        
  57.                         //Reset the caption
  58.                         SDL_WM_SetCaption( caption.str().c_str(), NULL );
  59.                     }
  60.                     // toggle cap on/off
  61.                     cap = (! cap );
  62.                 }
  63.                 if ( event.key.keysym.sym == SDLK_ESCAPE )
  64.                 {
  65.                     quit = true;
  66.                 }
  67.             }
  68.         } // while poll event
  69.  
  70.           // frame rate cap done, increment the frame and process the game loop
  71.         frame++;
  72.         Game_Run(0);
  73.  
  74.         if (( update.get_ticks() > 1000) && (cap))
  75.         {
  76.           int temp = 100 * frame / ( fps.get_ticks() / 1000.f);
  77.           if (temp != old_fps)
  78.             {
  79.                 old_fps = temp;
  80.                 float FPS = (float)temp / 100;
  81.                 //The frame rate as a string
  82.                 std::stringstream caption;
  83.    
  84.                 //Calculate the frames per second and create the string
  85.                 caption << "BugHunt 2942AD  (SDL) ~Avg FPS: " << FPS;
  86.    
  87.                 //Reset the caption
  88.                 SDL_WM_SetCaption( caption.str().c_str(), NULL );
  89.    
  90.                 // reset the counter for updates
  91.                 update.start();
  92.             }
  93.         } // if (caps)
  94.  
  95.         // if we want to cap the frame rate
  96.         if ( cap )
  97.         {
  98.  
  99.         // each frame should get a slice of ticks
  100.         if ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
  101.         {
  102.             //Sleep the remaining frame time
  103.             SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
  104.         }
  105.  
  106.         }
  107.     } // while ! quit
  108.  
  109.     Game_End(0);
  110.    
  111.         cout << "Game over :: exiting" << endl;
  112.  
  113.     return 0;
  114. }