Question

A friend just asked me this interesting question and I had no answer to this.

He's making a game and sometimes he experiences lags in the frame rate. As if 10 or more frames get dropped.

The run loop function is called by CADisplayLink.

Is there a way to tell programmatically if frame rate was lagging? I'd just measure the time in the run loop function and then check if it's bigger than supposed to be. And if it is, remember that there was a lag.

Could be useful to test on various devices on the go. How would you go about tracking this without being connected to Xcode?

Was it helpful?

Solution

The proper way is with a delta (difference between lastTime and now), something like this:

    CFTimeInterval time;
    time = CACurrentMediaTime();
    if (lastTime == 0) {
        delta = 0.0;
    } else {
        delta = time - lastTime;
    }
    lastTime = time;

But he should use this delta for a lot more stuff in the game, framerate is always a fickle thing, so anything time sensitive should be offset by the delta.

Say you want to update the position of an object which has a speed, it should be something like this:

objectXPos.pos = delta/targetDelta*objectXSpeed;

Target Delta would be the perfect frame rate, say 60 fps would give you a targetDelta of 1/60=0.0167

This way game wont be affected by poor of better performance.

You should also implement a disaster mode, say the bloody thing stopped for a second or more, and you can opt to not update the game logic, in that case it's better than everything happening magically for the user. This part depends on the type a game, a second might be overkill, maybe 0.033 s is already too much (half the frame rate)

OTHER TIPS

Simple. Increment a integer frame count variable each frame render. Set a repeating NSTimer for every second or every ten seconds. In the timer callback, sample how much the frame counter has advanced in that 1 or 10 seconds and display the min/max/average in a hidden debug view, or append it a log file that can be fetched using Document sharing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top