I want to write a Game Loop and measure the time it took to render. At the end of the loop I want the Thread to sleep for a certain amount of time to not exhaust the CPU. Currently I want to run the loop 60 times a second.
Here is the minimal example I came up with:
#include <iostream>
#include <Windows.h>
class Clock {
public:
Clock() : m_Start(), m_Elapsed(0) {}
double AbsoluteTime() {
LARGE_INTEGER current_time;
QueryPerformanceCounter(¤t_time);
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return static_cast<double>(current_time.QuadPart) / frequency.QuadPart;
}
void Start() {
m_Start = AbsoluteTime();
}
double GetElapsed() {
double current_time = AbsoluteTime();
m_Elapsed = current_time - m_Start;
return m_Elapsed;
}
private:
double m_Start;
double m_Elapsed;
};
int main() {
Clock clock;
Clock fpsClock;
int frameCount = 0;
while (true) {
clock.Start();
// Do some rendering stuff....
double elapsed = clock.GetElapsed();
double delay = (1.0 / 60.0) - elapsed;
if (delay > 0) {
Sleep(static_cast<DWORD>(delay * 1000));
}
frameCount++;
if (fpsClock.GetElapsed() > 1.0) {
std::cout << "FPS: " << frameCount << std::endl;
fpsClock.Start();
frameCount = 0;
}
}
return 0;
}
However when executing this code all I get is 33 Frames printing every second. I am really not sure why this happens. If I dont use the Sleep method at all I get over 4600 frames a second. So it cant be a performance issue. Has anybody has encountered this before or knows how to fix it?