40 lines
1.3 KiB
C++
40 lines
1.3 KiB
C++
#pragma once // Zabezpieczenie przed wielokrotnym dołączeniem
|
|
#include <chrono>
|
|
|
|
class FPSCounter {
|
|
public:
|
|
FPSCounter() :
|
|
frameCount(0),
|
|
currentFPS(0.0),
|
|
currentFrameTime(0.0),
|
|
lastTime(std::chrono::high_resolution_clock::now()) {
|
|
}
|
|
|
|
// Zwraca true, jeśli minęła sekunda i zaktualizowano dane
|
|
bool update() {
|
|
frameCount++;
|
|
auto currentTime = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> elapsed = currentTime - lastTime;
|
|
|
|
if (elapsed.count() >= 1.0) {
|
|
currentFPS = frameCount / elapsed.count();
|
|
// Obliczamy czas klatki w ms
|
|
currentFrameTime = 1000.0 / (currentFPS == 0 ? 1 : currentFPS);
|
|
|
|
frameCount = 0;
|
|
lastTime = currentTime;
|
|
return true; // Zgłaszamy, że dane się zmieniły
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// --- TEGO BRAKOWAŁO W TWOIM KODZIE ---
|
|
double getFPS() const { return currentFPS; }
|
|
double getFrameTime() const { return currentFrameTime; }
|
|
|
|
private:
|
|
long long frameCount;
|
|
double currentFPS; // Przechowuje ostatnio obliczone FPS
|
|
double currentFrameTime; // Przechowuje czas klatki
|
|
std::chrono::time_point<std::chrono::high_resolution_clock> lastTime;
|
|
}; |