C++;-对固定时间步长使用glfwGetTime() 在我的C++项目中用GLFWGETTIME()进行了研究和调试之后,我为我的项目做了一个游戏循环。就时间而言,我实际上只在Java中使用纳秒,在GLFW网站上,它声明该函数以秒为单位返回时间。如何使用glfwGetTime()创建固定时间步长循环
我现在所拥有的-C++;-对固定时间步长使用glfwGetTime() 在我的C++项目中用GLFWGETTIME()进行了研究和调试之后,我为我的项目做了一个游戏循环。就时间而言,我实际上只在Java中使用纳秒,在GLFW网站上,它声明该函数以秒为单位返回时间。如何使用glfwGetTime()创建固定时间步长循环,c++,loops,glfw,C++,Loops,Glfw,我现在所拥有的- while(!glfwWindowShouldClose(window)) { double now = glfwGetTime(); double delta = now - lastTime; lastTime = now; accumulator += delta; while(accumulator >= OPTIMAL_TIME)
while(!glfwWindowShouldClose(window))
{
double now = glfwGetTime();
double delta = now - lastTime;
lastTime = now;
accumulator += delta;
while(accumulator >= OPTIMAL_TIME) // OPTIMAL_TIME = 1 / 60
{
//tick
accumulator -= OPTIMAL_TIME;
}
}您所需要的只是这段代码,用于限制更新,但将渲染保持在可能的最高帧。代码基于此,对此进行了很好的解释。我所做的就是用GLFW和C++实现同样的原理。
static double limitFPS = 1.0 / 60.0;
double lastTime = glfwGetTime(), timer = lastTime;
double deltaTime = 0, nowTime = 0;
int frames = 0 , updates = 0;
// - While window is alive
while (!window.closed()) {
// - Measure time
nowTime = glfwGetTime();
deltaTime += (nowTime - lastTime) / limitFPS;
lastTime = nowTime;
// - Only update at 60 frames / s
while (deltaTime >= 1.0){
update(); // - Update function
updates++;
deltaTime--;
}
// - Render at maximum possible frames
render(); // - Render function
frames++;
// - Reset after one second
if (glfwGetTime() - timer > 1.0) {
timer ++;
std::cout << "FPS: " << frames << " Updates:" << updates << std::endl;
updates = 0, frames = 0;
}
}
静态双极限fps=1.0/60.0;
double lastTime=glfwGetTime(),计时器=lastTime;
双deltaTime=0,nowTime=0;
int frames=0,updates=0;
//-当窗口处于活动状态时
而(!window.closed()){
//-测量时间
nowTime=glfwGetTime();
deltaTime+=(nowTime-lastTime)/limitFPS;
lastTime=nowTime;
//-仅在60帧/秒时更新
而(deltaTime>=1.0){
update();//-update函数
更新++;
德尔塔梯姆;
}
//-以最大可能的帧进行渲染
render();//-render函数
frames++;
//-一秒钟后重置
如果(glfwGetTime()-timer>1.0){
定时器++;
std::无法跟踪您上次做某事的时间,并从现在的时间中减去该时间以获得增量。当增量>=您想要的步骤时,请再次执行。这就是我现在设置的方式,最好编辑问题并在那里添加代码。外部链接不可靠。哦,对了,很抱歉。