I'm trying to implement a simple game loop using this_thread::sleep_until()
, but it doesn't seem to sleep at all. What could be the problem?
Here's the code:
// Duration of a single frame (1 / 60th of a second)
typedef std::chrono::duration<int, std::ratio<1, 60>> frame_duration;
// Typedef for fetching the delta time between two frames
typedef std::chrono::duration<float> fsec;
// Timer for a single second
float secTimer = 0.f;
// Frame counter
int frames = 0;
auto start_time = std::chrono::steady_clock::now();
// Main loop
while(running) {
// Get the delta since the last frame
fsec = std::chrono::steady_clock::now() - start_time;
float delta = fsec.count();
// Print the FPS once every second (should be around 60)
frames++;
secTimer += delta;
if(secTimer >= 1.f) {
secTimer -= 1.f;
std::cout << "FPS: " << frames << std::endl;
frames = 0;
}
// Get the current frame's start time
start_time = std::chrono::steady_clock::now();
// *** Game logic goes here ***
// Get the time until the next frame and sleep
auto end_time = start_time + frame_duration(1);
std::this_thread::sleep_until(end_time);
}
Aucun commentaire:
Enregistrer un commentaire