【问题标题】:Handling an update loop using C++ Chrono?使用 C++ Chrono 处理更新循环?
【发布时间】:2013-02-09 06:17:09
【问题描述】:

我肯定对新的 C++ chrono 库有点迷失了。

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

engine.Update()
engine.Render()

这些都是很长的操作,很难说它们有多长。

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

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

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

我在 cmets 中直接在相关行旁边标记了我的问题,我将在下面重复。

头文件:

class MyClass
{
private:
    typedef std::chrono::high_resolution_clock Clock;
    Clock::time_point mLastEndTime;
    milliseconds mDeltaTime;
}

简化的更新循环

// 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();
}

主要问题是: 我的 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?

我很抱歉让大家感到困惑。我觉得这个 chrono 库是个白痴。大多数帮助站点、参考资料,甚至直接代码本身都很难阅读和理解我所应用的内容。非常欢迎指出我应该如何搜索解决方案或代码!

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

【问题讨论】:

  • 至于min/max函数,很容易为它们创建简单的重载函数。
  • @JoachimPileborg 你能详细说明一下吗?
  • 为什么你有一个“忙循环”? “投票是邪恶的”。如果你想要延迟,为什么不设置一个计时器呢?
  • @MintyAnt 哦,这是一个很好的理由,也是解决常见问题的常用方法。但这与不以 100% 运行处理器无关......
  • 不,kMaxDeltatime 具有正确的值 (16)。我不确定为什么您的程序会陷入循环。这是我根据您的原始示例汇总的一些代码,它按预期工作:ideone.com/rYe3sL

标签: c++ milliseconds chrono


【解决方案1】:

在使用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!

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

typedef duration<long,std::ratio<1,60>> sixtieths_of_a_sec;
constexpr auto kMaxDeltatime = sixtieths_of_a_sec{1};

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

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

auto newEndTime = Clock::now();
auto frameTime = newEndTime - mLastEndTime;
mLastEndTime = newEndTime;

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

改为使用:

while (frameTime > milliseconds(0))

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?

最好编写始终使用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);

或者:

auto milliseconds = mDeltaTime/milliseconds(1);

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

typedef std::common_type<decltype(frameTime),decltype(kMaxDeltatime)>::type common_duration;
auto mDeltaTime = std::min<common_duration>(frameTime, kMaxDeltatime); 

【讨论】:

  • 我知道这已经过去很久了,但是 std::common_type 调用并不能在 Linux 上使用 clang++ 为我编译。它说“不存在名为“type”的成员”,这导致 min 调用也无法编译。
  • 这与我认为搞砸编译器的初始化列表有关。
猜你喜欢
  • 1970-01-01
  • 2013-08-13
  • 2018-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
相关资源
最近更新 更多