diff --git a/Core/Console.cpp b/Core/Console.cpp index 01b44770..cbd67a7b 100644 --- a/Core/Console.cpp +++ b/Core/Console.cpp @@ -10,7 +10,6 @@ shared_ptr Console::Instance(new Console()); uint32_t Console::Flags = 0; -uint32_t Console::CurrentFPS = 0; Console::Console() { @@ -161,19 +160,12 @@ bool Console::CheckFlag(int flag) return (Console::Flags & flag) == flag; } -uint32_t Console::GetFPS() -{ - return Console::CurrentFPS; -} - void Console::Run() { Timer clockTimer; - Timer fpsTimer; - uint32_t lastFrameCount = 0; double elapsedTime = 0; double targetTime = 16.6666666666666666; - + _runLock.Acquire(); _stopLock.Acquire(); @@ -224,17 +216,6 @@ void Console::Run() break; } } - - if(fpsTimer.GetElapsedMS() > 1000) { - uint32_t frameCount = PPU::GetFrameCount(); - if((int32_t)frameCount - (int32_t)lastFrameCount < 0) { - Console::CurrentFPS = 0; - } else { - Console::CurrentFPS = (int)(std::round((double)(frameCount - lastFrameCount) / (fpsTimer.GetElapsedMS() / 1000))); - } - lastFrameCount = frameCount; - fpsTimer.Reset(); - } } _apu->StopAudio(); _stopLock.Release(); diff --git a/Core/Console.h b/Core/Console.h index 5701a8da..c00ae9fa 100644 --- a/Core/Console.h +++ b/Core/Console.h @@ -22,7 +22,6 @@ class Console private: static shared_ptr Instance; static uint32_t Flags; - static uint32_t CurrentFPS; SimpleLock _pauseLock; SimpleLock _runLock; SimpleLock _stopLock; @@ -71,7 +70,6 @@ class Console static bool CheckFlag(int flag); static void SetFlags(int flags); static void ClearFlags(int flags); - static uint32_t GetFPS(); static shared_ptr GetInstance(); }; diff --git a/Windows/Renderer.cpp b/Windows/Renderer.cpp index e37e9afe..3cc40047 100644 --- a/Windows/Renderer.cpp +++ b/Windows/Renderer.cpp @@ -439,7 +439,19 @@ namespace NES } else { //Draw FPS counter if(CheckFlag(UIFlags::ShowFPS)) { - string fpsString = string("FPS: ") + std::to_string(Console::GetFPS()); + + if(_fpsTimer.GetElapsedMS() > 1000) { + //Update fps every sec + if(_frameCount - _lastFrameCount < 0) { + _currentFPS = 0; + } else { + _currentFPS = (int)(std::round((double)(_frameCount - _lastFrameCount) / (_fpsTimer.GetElapsedMS() / 1000))); + } + _lastFrameCount = _frameCount; + _fpsTimer.Reset(); + } + + string fpsString = string("FPS: ") + std::to_string(_currentFPS); DrawOutlinedString(fpsString, 256 * 4 - 80, 13, Colors::AntiqueWhite, 1.0f); } } @@ -556,6 +568,8 @@ namespace NES _frameLock.Acquire(); memcpy(_nextFrameBuffer, frameBuffer, 256 * 240 * 4); _frameLock.Release(); + + _frameCount++; } void Renderer::TakeScreenshot(string romFilename) diff --git a/Windows/Renderer.h b/Windows/Renderer.h index a0bd4f91..d5485fb1 100644 --- a/Windows/Renderer.h +++ b/Windows/Renderer.h @@ -6,6 +6,7 @@ #include "../Utilities/PNGWriter.h" #include "../Utilities/FolderUtilities.h" #include "../Utilities/SimpleLock.h" +#include "../Utilities/Timer.h" using namespace DirectX; @@ -46,6 +47,10 @@ namespace NES { uint8_t* _nextFrameBuffer = nullptr; SimpleLock _frameLock; + Timer _fpsTimer; + uint32_t _frameCount = 0; + uint32_t _lastFrameCount = 0; + uint32_t _currentFPS = 0; unique_ptr _font; unique_ptr _smallFont; @@ -84,7 +89,7 @@ namespace NES { void DrawToasts(); void DrawToast(shared_ptr toast, int posIndex); void RemoveOldToasts(); - + public: Renderer(HWND hWnd); ~Renderer();