1

I try to cap the animation at 30 fps. So I design the functions below to achieve the goal. Unfortunately, the animation doesn't behave as fast as no condition checking for setFPSLimit() function when I set 60 fps (DirectX caps game application at 60 fps by default). How should I fix it to make it work?

getGameTime() function counts the time like stopwatch in millisecond when game application starts.

//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;
}

With fps limit http://www.youtube.com/watch?v=i3VDOMqI6ic

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

With No fps limit 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;
    }
user
  • 291
  • 2
  • 7
  • 18
  • The question is not clear. The function looks OK. The static that depends on the input is not a good idea. if you change from 60 to 30 fps during the game it won't work. Does getGameTime returns milliseconds ? if so you should do float timeDelay = 1000.f / fpsLimit; – a.lasram Jul 10 '13 at 01:46
  • Yes but float timeDelay = 1000.f / fpsLimit doesn't work. I already updated the question. If my question is still unclear, tell me again. I will try to make it as clear as possible. – user Jul 10 '13 at 03:58
  • 1
    Alternativly to cap your framerate, you could control your animationspeed with the timedelta of the frame, so it would be independend from the framerate. (http://stackoverflow.com/questions/5508922/whats-the-usual-way-of-controlling-frame-rate) Maybe following article is interesting for you too: http://www.koonsolo.com/news/dewitters-gameloop/ – Gnietschow Jul 10 '13 at 09:15
  • You said DirectX caps game application at 60 fps by default, any documentation about this? just curious. – zdd Jul 16 '13 at 12:38

0 Answers0