在c ++中调整帧速率的简单方法是什么?

时间:2015-02-21 19:07:39

标签: c++ opengl animation while-loop frame-rate

我有一个使用openGL在窗口上显示内容的while循环,但是动画与在其他计算机上运行的动作相比太快了,所以我需要循环中的某些内容,这样才能在之前的1/40秒后显示显示,我该怎么做? (我' m c ++ noob)

2 个答案:

答案 0 :(得分:0)

您需要检查循环开始时的时间,在完成所有渲染和更新逻辑后再检查循环结束时的时间,然后Sleep()查看之间的差异。经过时间和目标帧时间(40 fps为25ms)。

答案 1 :(得分:0)

这是我在C ++中使用SDL库的一些代码。基本上你需要一个函数来在循环开始时启动一个计时器(StartFpsTimer())和一个函数,根据你想要的恒定帧速率等待下一帧到期的足够时间(WaitTillNextFrame() )。

m_oTimer对象是一个简单的计时器对象,您可以启动,停止,暂停。 GAME_ENGINE_FPS是您希望拥有的帧率。

// Sets the timer for the main loop
void StartFpsTimer()
{
    m_oTimer.Start();
}

// Waits till the next frame is due (to call the loop at regular intervals)
void WaitTillNextFrame()
{
    if(this->m_oTimer.GetTicks() < 1000.0 / GAME_ENGINE_FPS) {
        delay((1000.0 / GAME_ENGINE_FPS) - m_oTimer.GetTicks());
    }
}

while (this->IsRunning())
{
    // Starts the fps timer
    this->StartFpsTimer();

    // Input
    this->HandleEvents();

    // Logic
    this->Update();

    // Rendering
    this->Draw();

    // Wait till the next frame
    this->WaitTillNextFrame();
}