Hi everyone, out of curiosity yesterday I was trying out chrono header with the goal of creating a 60fps loop, the code below is what I have so far and I wanted your opinion about it, if it is the proper way to do it, how it is usually done or how I could improve on it .
Thanks
#include <iostream>
#include <chrono>
#include <cstdint>
using namespace std;
using namespace chrono;
using frame = duration<int32_t, ratio<1, 60>>;
using ms = duration<float, milli>;
int main()
{
time_point<steady_clock> fpsTimer(steady_clock::now());
frame FPS{};
while (true)
{
FPS = duration_cast<frame>(steady_clock::now() - fpsTimer);
if (FPS.count() >= 1)
{
fpsTimer = steady_clock::now();
cout << "LastFrame: " << duration_cast<ms>(FPS).count() << "ms | FPS: " << FPS.count() * 60 << endl;
}
}
return 0;
}
output:
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60
LastFrame: 16.6667ms | FPS: 60