在 c++ 中调整帧速率的简单方法是什么?
What's the simples way of adjusting frame rate in c++?
我有一个while循环,它使用openGL在窗口上显示内容,但与在其他计算机上运行的方式相比,动画太快了,所以我需要在循环中有一些东西,它只允许在上次显示后显示1/40秒,我该怎么做?(我是c++noob)
您需要在循环开始时检查时间,在完成所有渲染和更新逻辑后,在循环结束时再次检查时间,然后Sleep()
检查所用时间与目标帧时间之间的差异(每秒40帧,25毫秒)。
这是我在C++和SDL库中使用的一些代码。基本上,您需要有一个函数在循环开始时启动计时器(StartFpsTimer()),以及一个函数根据您想要的恒定帧速率等待足够的时间直到下一帧到期(WaitTtillNextFrame())。
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();
}
相关文章:
- 一种在C++中读取TXT配置文件的简单方法
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 将时间戳打印到流的最简单方法
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- C++ 将静态方法转换为简单方法
- 将一系列数字映射到 CPP 中的值的简单方法
- 引用基类模板的成员变量的简单方法
- 学习数据结构和算法的简单方法
- boost::variant - 对变体应用算术的最简单方法
- 创建可以遍历 std::map 值的通用模板迭代器的最简单方法是什么?
- 在C++中将算术类型转换为 std::array 的最简单方法<uint8_t>
- 使用新运算符C++创建多维数组的简单方法
- C++-将ASCII字符数组中可能的数值转换为字符的最简单方法
- OpenSSL模块化最大数据类型大小的简单方法
- 获取当前按键的简单方法
- 多个 if-else 测试的更简单方法
- 为具有多个参数的函数创建 SWIG 类型图的更简单方法?