phoboslab / Ejecta

A Fast, Open Source JavaScript, Canvas & Audio Implementation for iOS
2.81k stars 322 forks source link

Cant have timers less than 34ms #17

Closed mrspeaker closed 12 years ago

mrspeaker commented 12 years ago

I was trying to run my game at a low FPS for my poor old 3Gs, but there seems to be some weirdness around the setTimeout timers:

setTimeout(..., 1000/10) = correctly runs 10 fps
setTimeout(..., 1000/30) = max fps (45-60) on my 3GS - thank you Ejecta!

If I remove this line from EJApp.m:

// Make sure short intervals (< 34ms) run each frame
if( interval < 0.034 ) {
    interval = 0;
}

and ask for 40fps I get 30 on my iPhone and on the simulator. I need to slow the fps down a bit... is it safe to omit this check?

Thanks!

phoboslab commented 12 years ago

Ejecta uses CADisplayLink to render frames in sync with the display's refresh rate, like all animations on the iPhone/iPad should. The iPhone's refresh rate is 60hz and thus Ejecta checks intervals and timeouts 60 times per second at most.

Trying to render an animation with 40fps on a 60hz display is generally a bad idea. It would render one frame, then skip the next display refresh, then render one frame, then skip two display refreshs, while the next frame would be rendered way to late; the animation would feel "jerky".

That's also the reason why console games are either "60hz" or "30hz" but never something in between. Rendering at constant 30fps looks way smoother than the stutter you'd get at 40fps.

CADisplayLink works exactly like requestAnimationFrame in the browser. It tries to be smart when scheduling frames - either running an animation at 60fps, or if that fails, drop down to 30fps.

So in short: don't do that. Just request an interval with 16ms or use requestAnimationFrame and let the iPhone decide if it's fast enough for 60fps or not.

With all that said, I should probably reduce the check to interval < 0.017, so you can run at 30fps if you really want to.

phoboslab commented 12 years ago

setInterval & setTimout now allow for 30fps. Everything below 18ms is still automatically scheduled for the next frame.

finscn commented 12 years ago

the game logic rate does not always equal to render rate。I think bind all loop to render rate is not a good way。