Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Skip frames to keep FPS constant — Gideros Forum

Skip frames to keep FPS constant

totebototebo Member
edited February 2016 in General questions
I found this in the docs:

"You can also use setFps(-60) or setFps(-30) to maintain an apparent frame rate of 60 or 30 fps by skipping frames if the system notices it is running slow. So for example, it updates the game logic 60 times per second but redraws the screen 30 times. The game is playable but will not be so smooth on slow devices. (currently available on WinRT)"

http://docs.giderosmobile.com/reference/gideros/Application/setFps#Application:setFps

How involved would it be to add this to iOS and Android?
My Gideros games: www.totebo.com

Comments

  • Currently we have an enter frame event, which is dispatched every time device is ready to draw new frame.
    So if fps drop, enter frame gets called more rarely.

    There was an idea to introduce second parallel game loop, like tick, which would run provided FPS times per second, no matter if real FPS drops down due to performance.

    But actually when thinking about this, it is probably does not make sense to have both game loops, does it?

    It would probably be better to have a setting to switch behavior of enter frame to one or another. Right?

    Likes: totebo, antix

    +1 -1 (+2 / -0 )Share on Facebook
  • Yeah, that makes sense. It would be great if this could be done at runtime, so you can "save" old devices when they start running slowly.
    My Gideros games: www.totebo.com
  • SinisterSoftSinisterSoft Maintainer
    edited February 2016
    You can do this at run time:http://docs.giderosmobile.com/reference/gideros/Application/setFps#Application:setFps

    You can also get a delta when the event starts - so you multiply changes by 2 if the frame was skipped:
    local frameRate=application:getFps()
     
    .
    local playerX=0
    local speed=4
    .
     
    function gameLoop(e)
    	local skip=event.deltaTime*frameRate
    	if skip>1.5 then
    		skip=2
    	else
    		skip=1
    	end
    .
    .
    	playerX=playerX+(speed*skip)
    .
    .
    .
    end
     
    stage:addEventListener(Event.ENTER_FRAME,gameLoop)
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • john26john26 Maintainer
    You can see how it works for WinRT here:

    https://github.com/gideros/gideros/blob/master/winrt/gideros/gideros.Shared/giderosapi.cpp#L1001

    There is an inner loop which runs as long as the computer thinks it is not keeping up. The inner loop runs extra ENTER_FRAME events while the outer loop redraws the scene. So provided the ENTER_FRAME is quicker than the screen draw this will work, but it will be a disaster if it's the other way round and the code doesn't check. Having said that, in my experience the screen redraw is always the bottleneck in practice.

    I got the idea from this website which also discusses other ways to do the game loop:
    http://www.koonsolo.com/news/dewitters-gameloop/

    I'm using the one he calls "Constant Game Speed with Maximum FPS"

    So yes, its definitely possible to implement on Android and iOS. But I didn't write those so need to dig down to see how they operate! I think it would be good to have this. But don't expect too much: it makes the game playable on an old device but it will be jerky.

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited February 2016
    I had it run slower than the draw loop - so there was massive slowdown as a result. imho it should be the same as the other versions to be consistent (and so use the delta to correct frame skips), maybe with negative frame rate doing this method instead?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
Sign In or Register to comment.