diff --git a/include/game-engine.h b/include/game-engine.h index c71d2987dd9e17120930bc0d58725890f159b0b9..2e84c008bb50488897766e07b9e9e4e880f51f4c 100644 --- a/include/game-engine.h +++ b/include/game-engine.h @@ -50,7 +50,7 @@ class GameEngine SaveManager _saveManager; GameState _state = Uninitialised; size_t _frame = 0; - std::time_t _frametime = 0; + size_t _frametime = 0; EntityManager _entityManager; Logger& _logger; int m_mX = 0; diff --git a/include/window.h b/include/window.h index c7f27ce826fcb52d0089f50a8e3ae268659aa4d5..946a56f1cfb6fb85109334cc4a37fa56702953cb 100644 --- a/include/window.h +++ b/include/window.h @@ -12,6 +12,7 @@ #include "logger.h" #include "colour.h" #include "maths/vec2.h" +#include "game-engine.h" @@ -37,6 +38,8 @@ enum KeyPressSurfaces class Window { + friend class GameEngine; + public: static SDL_Renderer* renderer; static bool sdlIsInit; @@ -55,6 +58,8 @@ class Window bool isClosed() { return this->_toClose; } void spawn(); + protected: + double fps = 0; private: bool _toClose; bool _hasInit; diff --git a/src/game-engine.cpp b/src/game-engine.cpp index 44b5ce8cb57e03cee12ab7a09fcfe5da4c5a2b73..ec1de5a9587a50405125d2736f297559c42c85c3 100644 --- a/src/game-engine.cpp +++ b/src/game-engine.cpp @@ -55,18 +55,25 @@ void GameEngine::update() this->handleInput(); this->_entityManager.update(); - std::chrono::system_clock::time_point now = std::chrono::high_resolution_clock::now(); + const std::chrono::time_point<std::chrono::high_resolution_clock> now + = std::chrono::high_resolution_clock::now(); + const auto cTime = now.time_since_epoch().count() / 1000000; + const auto fTime = this->_frametime; - if ( ++this->_frame % 1 == 0 ) { - std::time_t cTime = std::chrono::high_resolution_clock::to_time_t(now); - // size_t delTime = cTime - this->_frametime; + + ++this->_frame; + // std::cout << now << "|"<< this->_frametime << std::endl; + if ( cTime - fTime > 25 && this->_frame > 10 ) + { + double fps = (double) this->_frame / (cTime - fTime) * 1000; + // printf( "Frame: %i at %.2f fps\n", this->_frame, fps ); this->_frametime = cTime; - // std::cout << cTime << std::endl; - // printf( "Frame: %i at %.2f fps\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime ); - // printf( "Frame: %i at %.2f fps over %i s\n", this->_frame, delTime == 0 ? 0.0 : 1.0 / delTime, delTime ); + this->_frame = 0; + this->_window->fps = fps; } this->_window->update(); } + void GameEngine::handleInput() { SDL_Event e; diff --git a/src/window.cpp b/src/window.cpp index 68f69dc59981ce92864655a6f24506db524c4ee0..ec74985bff9bd692e2b1b9c2bc7117773cce78ee 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -121,6 +121,7 @@ void Window::loadMedia() this->_waterTicks[ 3 ].w = 100; this->_waterTicks[ 3 ].h = 100; } + if( this->_textureSprite->loadFromFile( "assets/texture_test.png" ) ) { this->_textureSprite->setAlpha( 128 ); @@ -224,10 +225,9 @@ void Window::update() { // SDL_RenderCopy( this->_renderer, this->_activeTexture, NULL, NULL ); overlay = this->_createViewport( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ); - this->_fontFile->renderText( std::to_string(this->_frame) ); + this->_fontFile->renderText( std::to_string(this->fps) ); this->_fontFile->render( this->_renderer, {50,50} ); - ++this->_frame; SDL_RenderPresent( this->_renderer ); } @@ -283,6 +283,7 @@ void Window::spawn() if( Window::renderer == NULL ) { this->_renderer = SDL_CreateRenderer( this->_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ); + // this->_renderer = SDL_CreateRenderer( this->_window, -1, SDL_RENDERER_ACCELERATED ); if( this->_renderer == NULL ) { printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );