1

我尝试将动画设置为 30 fps。所以我设计了下面的功能来实现这个目标。不幸的是,当我设置 60 fps(DirectX 将游戏应用程序默认设置为 60 fps)时,动画的运行速度不如 setFPSLimit() 函数没有条件检查那么快。我应该如何修复它以使其正常工作?

getGameTime() 函数像秒表一样计算游戏应用程序启动时的时间,以毫秒为单位。

//Called every time you need the current game time
float getGameTime()
{
    UINT64 ticks;
    float time;

    // This is the number of clock ticks since start
    if( !QueryPerformanceCounter((LARGE_INTEGER *)&ticks) )
        ticks = (UINT64)timeGetTime();

    // Divide by frequency to get the time in seconds
    time = (float)(__int64)ticks/(float)(__int64)ticksPerSecond;

    // Subtract the time at game start to get
    // the time since the game started
    time -= timeAtGameStart;

    return time;
}

有 fps 限制 http://www.youtube.com/watch?v=i3VDOMqI6ic

void update()
{
    if ( setFPSLimit(60) )
        updateAnimation();
}

没有 fps 限制http://www.youtube.com/watch?v=Rg_iKk78ews

   void update()
    {
        updateAnimation();
    }

bool setFPSLimit(float fpsLimit)
{
    // Convert fps to time
    static float timeDelay = 1 / fpsLimit; 

    // Measure time elapsed
    static float timeElapsed    = 0;

    float currentTime = getGameTime();
    static float totalTimeDelay = timeDelay + getGameTime();

    if( currentTime > totalTimeDelay)
    {
        totalTimeDelay = timeDelay + getGameTime();
        return true;
    }
    else
        return false;
    }
4

0 回答 0