如何为游戏循环设置计时器? [英] How to make timer for a game loop?

查看:125
本文介绍了如何为游戏循环设置计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对fps计数,并将其限制设置为60,但是我一直在通过Google查看一些代码,但我完全没有得到它.

I want to time fps count, and set it's limit to 60 and however i've been looking throught some code via google, I completly don't get it.

推荐答案

您不应尝试限制fps.这样做的唯一原因是,如果您不使用增量时间,并且希望每个帧的长度都相同.即使是最简单的游戏也无法保证.

You shouldn't try to limit the fps. The only reason to do so is if you are not using delta time and you expect each frame to be the same length. Even the simplest game cannot guarantee that.

不过,您可以将增量时间切成固定大小,然后保留其余部分.

You can however take your delta time and slice it into fixed sizes and then hold onto the remainder.

这是我最近编写的一些代码.尚未经过全面测试.

Here's some code I wrote recently. It's not thoroughly tested.

void GameLoop::Run()
{
    m_Timer.Reset();

    while(!m_Finished())
    {
        Time delta = m_Timer.GetDelta();
        Time frameTime(0);
        unsigned int loopCount = 0;

        while (delta > m_TickTime && loopCount < m_MaxLoops)
        {
            m_SingTick();
            delta -= m_TickTime;
            frameTime += m_TickTime;
            ++loopCount;
        }
        m_Independent(frameTime);

        // add an exception flag later.
        // This is if the game hangs
        if(loopCount >= m_MaxLoops)
        {
            delta %= m_TickTime;
        }

        m_Render(delta);
        m_Timer.Unused(delta);
    }
}

成员对象是Boost插槽,因此不同的代码可以使用不同的计时方法进行注册.独立插槽用于键映射或更改音乐之类的事情,不需要那么精确. SingTick对物理很有用,如果您知道每个滴答都一样,但又不想碰壁,会更容易.渲染采用增量,因此动画可以流畅运行,但必须记住在下一个SingTick上加以考虑.

The member objects are Boost slots so different code can register with different timing methods. The Independent slot is for things like key mapping or changing music Things that don't need to be so precise. SingTick is good for physics where it is easier if you know every tick will be the same but you don't want to run through a wall. Render takes the delta so animations run smooth, but must remember to account for it on the next SingTick.

希望有帮助.

这篇关于如何为游戏循环设置计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆