vk-book/shared/UtilsFPS.h
2025-05-23 21:13:53 -04:00

43 lines
877 B
C++

#pragma once
#include <assert.h>
#include <stdio.h>
class FramesPerSecondCounter
{
public:
explicit FramesPerSecondCounter(float avgInterval = 0.5f)
: avgInterval_(avgInterval)
{
assert(avgInterval > 0.0f);
}
bool tick(float deltaSeconds, bool frameRendered = true)
{
if (frameRendered)
numFrames_++;
accumulatedTime_ += deltaSeconds;
if (accumulatedTime_ > avgInterval_) {
currentFPS_ = static_cast<float>(numFrames_ / accumulatedTime_);
if (printFPS_)
printf("FPS: %.1f\n", currentFPS_);
numFrames_ = 0;
accumulatedTime_ = 0;
return true;
}
return false;
}
inline float getFPS() const { return currentFPS_; }
bool printFPS_ = true;
public:
float avgInterval_ = 0.5f;
unsigned int numFrames_ = 0;
double accumulatedTime_ = 0;
float currentFPS_ = 0.0f;
};