Exemplo n.º 1
0
bool Simulation::Run( void ) {
	bool quit = false;
	Input inputs;
	int fpsCount = 0; // for FPS calculations
	int fpsTotal= 0; // for FPS calculations
	Uint32 fpsTS = 0; // timestamp of last FPS printing

	// Grab the camera and give it coordinates
	Camera *camera = Camera::Instance();
	camera->Focus(0, 0);

	// Generate a starfield
	Starfield starfield( OPTION(int, "options/simulation/starfield-density") );

	// Create a spritelist
	SpriteManager sprites;

	Player *player = Player::Instance();

	// Set player model based on simulation xml file settings
	player->SetModel( models->GetModel( playerDefaultModel ) );
	sprites.Add( player->GetSprite() );

	// Focus the camera on the sprite
	camera->Focus( player->GetSprite() );

	// Add the planets
	planets->RegisterAll( &sprites );

	// Start the Lua Universe
	Lua::SetSpriteList( &sprites );
	Lua::Load("Resources/Scripts/universe.lua");

	// Start the Lua Scenarios
	Lua::Run("Start()");

	// Ensure correct drawing order
	sprites.Order();
	
	// Create the hud
	Hud::Hud();

	Hud::Alert( "Captain, we don't have the power! Pow = %d", 3 );

	fpsTS = Timer::GetTicks();
	// main game loop
	while( !quit ) {
		quit = inputs.Update();
		
		if( !paused ) {
			Lua::Update();
			// Update cycle
			starfield.Update();
			camera->Update();
			sprites.Update();
			camera->Update();
			Hud::Update();
			UI::Run(); // runs only a few loops
			
			// Keep this last (I think)
			Timer::Update();
		}

		// Erase cycle
		Video::Erase();
		
		// Draw cycle
		starfield.Draw();
		sprites.Draw();
		Hud::Draw( sprites );
		UI::Draw();
		Video::Update();
		
		// Don't kill the CPU (play nice)
		Timer::Delay();
		
		Coordinate playerPos = player->GetWorldPosition();

		// Counting Frames
		fpsCount++;
		fpsTotal++;

		// Update the fps once per second
		if( (Timer::GetTicks() - fpsTS) >1000 ) { 
			Simulation::currentFPS = static_cast<float>(1000.0 *
					((float)fpsCount / (Timer::GetTicks() - fpsTS)));
			fpsTS = Timer::GetTicks();
			fpsCount = 0;
		}
	}

	Log::Message("Average Framerate: %f Frames/Second", 1000.0 *((float)fpsTotal / Timer::GetTicks() ) );
	return true;
}