diff --git a/Engine/Graphics/DebugUI.cpp b/Engine/Graphics/DebugUI.cpp index bf70a0d..e2a3c64 100644 --- a/Engine/Graphics/DebugUI.cpp +++ b/Engine/Graphics/DebugUI.cpp @@ -26,9 +26,9 @@ void DebugUI::DrawDebugData(std::shared_ptr context) { std::ostringstream outStrStream; outStrStream << std::fixed; - outStrStream << std::setprecision(0) << "CPU Usage: " << gDbgStats.cpu_usage << " % (cycle: " << std::setprecision(1) << gDbgStats.loop_time << " ms)" << std::endl; - outStrStream << std::setprecision(0) << "FPS: " << gDbgStats.fps << std::endl; - outStrStream << std::setprecision(1) << "RAM Usage: " << gDbgStats.ram_virt_usage << "MB(V) / " << gDbgStats.ram_phys_usage << "MB(P)"; + outStrStream << std::setprecision(0) << "CPU: " << gDbgStats.cpu_usage << "% (cycle time: " << std::setprecision(3) << gDbgStats.loop_time << "ms)" << std::endl; + outStrStream << std::setprecision(1) << "RAM: " << gDbgStats.ram_virt_usage << "MB(V) / " << gDbgStats.ram_phys_usage << "MB(P)" << std::endl; + outStrStream << std::setprecision(0) << "FPS: " << gDbgStats.fps; gDbgText.setString(outStrStream.str()); context->draw(gDbgText); diff --git a/Engine/Utils/Timers.hpp b/Engine/Utils/Timers.hpp index ec74d33..7a7e31a 100644 --- a/Engine/Utils/Timers.hpp +++ b/Engine/Utils/Timers.hpp @@ -21,13 +21,23 @@ public: } const double GetDeltaTime() { - return GetDeltaTime(false); + return GetDeltaTime(false, true); } - const double GetDeltaTime(bool reset) { + const double GetDeltaTimeUs() { + return GetDeltaTime(false, false); + } + const double GetDeltaTime(bool reset, bool msMode) { auto curr_time = std::chrono::high_resolution_clock::now(); - double ret = std::chrono::duration_cast(curr_time - t_previousDelta).count(); + double ret = 0; + + if (msMode) + ret = std::chrono::duration_cast(curr_time - t_previousDelta).count(); + else + ret = std::chrono::duration_cast(curr_time - t_previousDelta).count(); + if (reset) this->t_previousDelta = curr_time; + return ret; } diff --git a/Game.cpp b/Game.cpp index 59ff257..44f71b2 100644 --- a/Game.cpp +++ b/Game.cpp @@ -13,7 +13,7 @@ using std::make_shared; using std::make_unique; -#define TARGET_FPS (1000/60) +#define TARGET_FPS (60) std::unique_ptr mPerfsTimer = nullptr; @@ -55,14 +55,15 @@ GAME_STATUS Game::Tick(SysTimer& time) { static std::vector msTicksMonitoring; static double mPrevdelta = 0; static unsigned int mFrameCnt = 0; - double mDelta = time.GetDeltaTime(); + double mDelta = time.GetDeltaTimeUs(); + // Debug performance computations if (mDbgUI != nullptr && mPerfsTimer != nullptr) { - msTicksMonitoring.push_back((mDelta - mPrevdelta) / TARGET_FPS); + msTicksMonitoring.push_back((mDelta - mPrevdelta) / (1000000/TARGET_FPS)); if (mPerfsTimer->GetDeltaTime() >= 1000) { // Re-evaluate CPU usage every seconds (for FP'S') - double cpuUsage = std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), 0) / msTicksMonitoring.size(); + double cpuUsage = 100 * std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), (double)(0)) / msTicksMonitoring.size(); - mDbgUI->UpdateDebugData(cpuUsage, (mDelta - mPrevdelta), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000)); + mDbgUI->UpdateDebugData(cpuUsage, ((mDelta - mPrevdelta) / 1000), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000)); mFrameCnt = 0; msTicksMonitoring.clear(); @@ -71,14 +72,19 @@ GAME_STATUS Game::Tick(SysTimer& time) { } sf::Event event; - while (mMainWindow->pollEvent(event)) { // Wait for next frame (fixed FPS) + while (mMainWindow->pollEvent(event)) { if (event.type == sf::Event::Closed) mMainWindow->close(); } + // Update game stats and internal stuff Update(); - if (mDelta >= TARGET_FPS) { //TODO: Replace with global var and use dynamic window loader + // Ugly way to wait for next frame... Maybe create a separate thread for rendering? + //while(time.GetDeltaTime() < (1000/TARGET_FPS)) {} + + // Process to the final rendering + if (time.GetDeltaTime() >= (1000/TARGET_FPS)) { Render(); mFrameCnt++; time.Reset(); @@ -87,8 +93,7 @@ GAME_STATUS Game::Tick(SysTimer& time) { mPrevdelta = mDelta; } - sf::sleep(sf::milliseconds(TARGET_FPS - (mDelta - mPrevdelta))); - + // Return the current process stat to the head process (TODO: convert to thread) if (mMainWindow->isOpen()) return GAME_RUNNING; else