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?
The proper way is with a delta (difference between lastTime and now), something like this:
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:
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)