#include "game.h"
//#include <iomanip>
using namespace std;
int main (int argc, char* args[] )
{
bool quit = false;
bool cap = true; // flag - cap the frame rate or not
// event structure
SDL_Event event;
// Load the Config data from file (to have dynamic screen size, etc)
load_Config("setup.cfg");
if (!Game_Init(0))
return 0;
//Keep track of the frame count
int frame = 0; //Timer used to calculate the frames per second
CPTimer fps,update; //Timer used to update the caption Timer update;
//Start the update timer
update.start();
//Start the frame timer
fps.start();
double old_fps = 0;
while ( quit == false )
{
while ( SDL_PollEvent( &event ) )
{
if ( event.type == SDL_QUIT )
{
quit = true;
}
if ( event.type == SDL_KEYDOWN )
{
// check if enter key pressed
if ( event
.key.keysym
.sym
== SDLK_RETURN
)
{
if (cap)
{
//The frame rate as a string
std::stringstream caption;
//Calculate the frames per second and create the string
#ifdef USE_OPENGL
caption << "BugHunt 2942AD (SDL + OpenGL)";
#else
caption << "BugHunt 2942AD (SDL)";
#endif
//Reset the caption
SDL_WM_SetCaption( caption.str().c_str(), NULL );
}
// toggle cap on/off
cap = (! cap );
}
if ( event
.key.keysym
.sym
== SDLK_ESCAPE
)
{
quit = true;
}
}
} // while poll event
// frame rate cap done, increment the frame and process the game loop
frame++;
Game_Run(0);
if (( update.get_ticks() > 1000) && (cap))
{
int temp = 100 * frame / ( fps.get_ticks() / 1000.f);
if (temp != old_fps)
{
old_fps = temp;
float FPS = (float)temp / 100;
//The frame rate as a string
std::stringstream caption;
//Calculate the frames per second and create the string
caption << "BugHunt 2942AD (SDL) ~Avg FPS: " << FPS;
//Reset the caption
SDL_WM_SetCaption( caption.str().c_str(), NULL );
// reset the counter for updates
update.start();
}
} // if (caps)
// if we want to cap the frame rate
if ( cap )
{
// each frame should get a slice of ticks
if ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
//Sleep the remaining frame time
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
} // while ! quit
Game_End(0);
cout << "Game over :: exiting" << endl;
return 0;
}