Skip to content
Snippets Groups Projects
Commit 34334322 authored by Alfred Burgess's avatar Alfred Burgess
Browse files

moved update loop to game engine and added an FPS counter

parent cc5b5df7
Branches
Tags
No related merge requests found
......@@ -4,6 +4,10 @@
#include <map>
#include <string>
#include <vector>
#include <chrono>
#include <ctime>
// Return
#include "logger.h"
#include "entity-manager.h"
......@@ -29,6 +33,8 @@ class GameEngine
bool isRunning;
void handleInput();
private:
size_t _frame = 0;
std::time_t _frametime = 0;
EntityManager _entityManager;
Logger& _logger;
......
......@@ -10,7 +10,7 @@ GameEngine::~GameEngine()
{
this->_logger.info( "Cleaning GameEngine Object" );
}
void GameEngine::close(){}
void GameEngine::close(){ this->isRunning = false; }
void GameEngine::init()
{
this->isRunning = false;
......@@ -24,9 +24,23 @@ Scene* GameEngine::currentScene()
void GameEngine::run()
{
this->isRunning = true;
this->_frame = 0;
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t _frametime = std::chrono::system_clock::to_time_t(now);
while( this->isRunning ) { this->update(); }
}
void GameEngine::update()
{
this->_entityManager.update();
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
if( ++this->_frame > 10000 ) { this->close(); }
else if ( this->_frame % 1000 == 0 ) {
std::time_t cTime = std::chrono::system_clock::to_time_t(now);
size_t delTime = cTime - this->_frametime;
this->_frametime = cTime;
printf( "Frame: %i at %.2f fps\n", this->_frame / 1000, delTime == 0 ? 0.0 : 1.0 / delTime );
}
}
void GameEngine::handleInput() { }
......@@ -40,26 +40,7 @@ int main( int argc, char* argv[] ) {
GameEngine* game = new GameEngine();
// Main Loop
while( state != Exit )
{
if( ++tickCount > 1000000 )
{
// Exit
state = Exit;
}
// Update Entities
game->update();
// Process Input
// Movement Updates
// Collision Updates
// Update Game State
// Render Game
_frame++;
}
game->run();
userSettings.set( "exit", "teset" );
std::cout << userSettings;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment