如何在C ++循环中限制FPS? [英] How to limit FPS in a loop with C++?
问题描述
我正在尝试使用带有chrono和线程的C ++在执行相交检查的循环中限制每秒的帧数.
I'm trying to limit the frames per second in a loop that is performing intersection checking, using C++ with chrono and thread.
这是我的代码:
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::system_clock::time_point lastFrame = std::chrono::system_clock::now();
while (true)
{
// Maintain designated frequency of 5 Hz (200 ms per frame)
now = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> delta = now - lastFrame;
lastFrame = now;
if (delta.count() < 200.0)
{
std::chrono::duration<double, std::milli> delta_ms(200.0 - delta.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
printf("Time: %f \n", delta.count());
// Perform intersection test
}
我遇到的问题是,delta的所有其他输出都显示出微不足道的数量,而不是我要达到的〜200 ms/帧:
The problem I'm having is that every other output of delta is showing miniscule amounts, rather than the ~200 ms / frame I'm aiming for:
Time: 199.253200
Time: 2.067700
Time: 199.420400
Time: 2.408100
Time: 199.494200
Time: 2.306200
Time: 199.586800
Time: 2.253400
Time: 199.864000
Time: 2.156500
Time: 199.293800
Time: 2.075500
Time: 201.787500
Time: 4.426600
Time: 197.304100
Time: 4.530500
Time: 198.457200
Time: 3.482000
Time: 198.365300
Time: 3.415400
Time: 198.467400
Time: 3.595000
Time: 199.730100
Time: 3.373400
关于这种情况为什么发生的任何想法?
Any thoughts as to why this is happening?
推荐答案
如果考虑代码的工作原理,您会发现它与您编写代码的方式完全一样.Delta由于代码中的逻辑错误而发生振荡.
If you think about how your code works, you'll find out that it works exactly how you wrote it. Delta oscillates because of a logical mistake in the code.
会发生这种情况:
- 我们从
delta == 0
开始. - 因为增量小于
200
,所以您将代码休眠200-delta(0)== 200
ms. - 现在,增量本身变得接近
200
(因为您已经测量了睡眠时间以及实际工作),并且您睡眠了200-delta(200)== 0
ms. - 此后,循环重复进行.
- We start with
delta == 0
. - Because the delta is smaller than
200
, you code sleeps200 - delta(0) == 200
ms. - Now, the delta itself becomes close to
200
(because you've measured that sleep time as well as an actual work) and you sleep200 - delta(200) == 0
ms. - After that the cycle repeats.
要解决此问题,您无需测量睡眠时间.
To fix the problem you need to not measure the sleep time.
这是可以完成的方法:
#include <iostream>
#include <cstdio>
#include <chrono>
#include <thread>
std::chrono::system_clock::time_point a = std::chrono::system_clock::now();
std::chrono::system_clock::time_point b = std::chrono::system_clock::now();
int main()
{
while (true)
{
// Maintain designated frequency of 5 Hz (200 ms per frame)
a = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> work_time = a - b;
if (work_time.count() < 200.0)
{
std::chrono::duration<double, std::milli> delta_ms(200.0 - work_time.count());
auto delta_ms_duration = std::chrono::duration_cast<std::chrono::milliseconds>(delta_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(delta_ms_duration.count()));
}
b = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> sleep_time = b - a;
// Your code here
printf("Time: %f \n", (work_time + sleep_time).count());
}
}
这段代码为我提供了稳定的增量序列:
This code gives me a steady sequence of deltas:
Time: 199.057206
Time: 199.053581
Time: 199.064718
Time: 199.053515
Time: 199.053307
Time: 199.053415
Time: 199.053164
Time: 199.053511
Time: 199.053280
Time: 199.053283
这篇关于如何在C ++循环中限制FPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!