FpsCounter.cpp 975 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "FpsCounter.h"
  4. #include <Urho3D/Math/MathDefs.h>
  5. #include <Urho3D/DebugNew.h>
  6. using namespace Urho3D;
  7. // Time without measurement
  8. static constexpr float WARM_UP_TIME = 5.f;
  9. void FpsCounter::Update(float timeStep)
  10. {
  11. if (timeStep < M_EPSILON)
  12. return;
  13. totalTime_ += timeStep;
  14. if (WARM_UP_TIME != 0.f && totalTime_ <= WARM_UP_TIME)
  15. return; // Waiting for the next frame
  16. resultNumFrames_++;
  17. resultTime_ += timeStep;
  18. resultFps_ = RoundToInt(resultNumFrames_ / resultTime_);
  19. frameCounter_++;
  20. timeCounter_ += timeStep;
  21. if (timeCounter_ >= 0.5f)
  22. {
  23. currentFps_ = RoundToInt(frameCounter_ / timeCounter_);
  24. frameCounter_ = 0;
  25. timeCounter_ = 0.f;
  26. }
  27. if (resultMinFps_ < 0 || currentFps_ < resultMinFps_)
  28. resultMinFps_ = currentFps_;
  29. if (currentFps_ > resultMaxFps_)
  30. resultMaxFps_ = currentFps_;
  31. }