使用C++ Chrono处理更新循环?

Min*_*Ant 8 c++ milliseconds c++-chrono

我对新的C++ chrono库肯定有点失落.

这里我有一个更新循环.它运行两个操作:

engine.Update()
engine.Render()
Run Code Online (Sandbox Code Playgroud)

这些都是长期操作,很难说它们有多长.

因此,我们测量他们花了多长时间,然后进行一些计算并找出在调用渲染之前逐渐调用更新的最佳方法.

为此,我正在使用C++ 11的Chrono功能.我选择它是因为它听起来很划算:更准确,更依赖于平台.我发现现在我遇到的问题比现在更多.

以下是我的代码,以及我的主要问题.非常需要任何有关问题或正确操作方法的帮助!

我直接在相关行旁边的注释中标记了我的问题,我将在下面重复这些问题.

头文件:

class MyClass
{
private:
    typedef std::chrono::high_resolution_clock Clock;
    Clock::time_point mLastEndTime;
    milliseconds mDeltaTime;
}
Run Code Online (Sandbox Code Playgroud)

简化的更新循环

// time it took last loop
milliseconds frameTime;
// The highest we'll let that time go. 60 fps = 1/60, and in milliseconds, * 1000
const milliseconds kMaxDeltatime((int)((1.0f / 60.0f) * 1000.0f)); // It's hard to tell, but this seems to come out to some tiny number, not what I expected!
while (true)
{
    // How long did the last update take?
    frameTime = duration_cast<milliseconds>(Clock::now() - mLastEndTime); // Is this the best way to get the delta time, with a duration cast?
    // Mark the last update time
    mLastEndTime = Clock::now();

    // Don't update everything with the frameTime, keep it below our maximum fps.
    while (frameTime.count() > 0) // Is this the best way to measure greater than 0 milliseconds?
    {
        // Determine the minimum time. Our frametime, or the max delta time?
        mDeltaTime = min(frameTime, kMaxDeltatime);

        // Update our engine.
        engine->Update((long)mDeltaTime.count()); // From here, it's so much easier to deal with code in longs. Is this the best way to shove a long through my code?

        // Subtract the delta time out of the total update time 
        frameTime -= mDeltaTime;
    }
    engine->Render();
}
Run Code Online (Sandbox Code Playgroud)

主要问题是:我的mDeltaTime总是很小.它基本上停留在几乎无限的循环中.这是因为kMaxDeltatime非常小,但如果我的目标是每秒60帧,那么我不是计算正确的毫秒数吗?

以下是列出的所有问题:

const milliseconds kMaxDeltatime((int)((1.0f / 60.0f) * 1000.0f)); // It's hard to tell, but this seems to come out to some tiny number, not what I expected!

frameTime = duration_cast<milliseconds>(Clock::now() - mLastEndTime); // Is this the best way to get the delta time, with a duration cast?

while (frameTime.count() > 0) // Is this the best way to measure greater than 0 milliseconds?

engine->Update((long)mDeltaTime.count()); // From here, it's so much easier to deal with code in longs. Is this the best way to shove a long through my code?
Run Code Online (Sandbox Code Playgroud)

我很抱歉这些混乱的人.我觉得这个计时库是个白痴.大多数帮助网站,或参考资料,甚至直接代码本身都非常令人困惑,无法阅读和理解我正在应用它.我非常欢迎指向我应该如何搜索解决方案或代码的指针!

编辑:Joachim指出std :: min/max工作正常毫秒!更新了代码以反映变化.

bam*_*s53 20

使用时std::chrono,应尽可能避免使用持续时间或将持续时间转换为原始积分值.相反,你应该坚持使用自然持续时间并利用持续时间类型提供的类型安全性.

以下是一系列具体建议.对于每个推荐,我将引用原始代码的行,然后显示我将如何重写这些行.


const milliseconds kMaxDeltatime((int)((1.0f / 60.0f) * 1000.0f)); // It's hard to tell, but this seems to come out to some tiny number, not what I expected!
Run Code Online (Sandbox Code Playgroud)

没有理由用手动转换常量进行这种计算.相反,你可以这样做:

typedef duration<long,std::ratio<1,60>> sixtieths_of_a_sec;
constexpr auto kMaxDeltatime = sixtieths_of_a_sec{1};
Run Code Online (Sandbox Code Playgroud)
frameTime = duration_cast<milliseconds>(Clock::now() - mLastEndTime); // Is this the best way to get the delta time, with a duration cast?
Run Code Online (Sandbox Code Playgroud)

您可以将值保留为其本机类型:

auto newEndTime = Clock::now();
auto frameTime = newEndTime - mLastEndTime;
mLastEndTime = newEndTime;
Run Code Online (Sandbox Code Playgroud)
while (frameTime.count() > 0) // Is this the best way to measure greater than 0 milliseconds?
Run Code Online (Sandbox Code Playgroud)

而是使用:

while (frameTime > milliseconds(0))
Run Code Online (Sandbox Code Playgroud)
engine->Update((long)mDeltaTime.count()); // From here, it's so much easier to deal with code in longs. Is this the best way to shove a long through my code?
Run Code Online (Sandbox Code Playgroud)

最好编写chrono::duration始终使用类型的代码,而不是使用泛型整数类型,但如果你真的需要获得泛型整数类型(例如,如果你必须传递long给第三方API)那么你可以做就像是:

auto mDeltaTime = ... // some duration type

long milliseconds = std::chrono::duration_cast<std::duration<long,std::milli>>(mDeltaTime).count();
third_party_api(milliseconds);
Run Code Online (Sandbox Code Playgroud)

要么:

auto milliseconds = mDeltaTime/milliseconds(1);
Run Code Online (Sandbox Code Playgroud)

要获得delta,您应该执行以下操作:

typedef std::common_type<decltype(frameTime),decltype(kMaxDeltatime)>::type common_duration;
auto mDeltaTime = std::min<common_duration>(frameTime, kMaxDeltatime); 
Run Code Online (Sandbox Code Playgroud)