Correct way to calculate FPS for your game

Monkey Forums/Monkey Programming/Correct way to calculate FPS for your game

Arabia(Posted 2013) [#1]
What is the correct way to do this? I'm guessing it's calculated in your OnUpdate() method. Anyone have a code snippet to do this correctly?

My game lags badly after a while when run as HTML5 but to me, it doesn't seem to be so bad or lag at all when run on my Android device - just want to make sure it's keeping close to the 60 fps that I've got it set to. If it lags on HTML5 it doesn't really worry me as I'm not going to release it on this platform anyway.


Markus(Posted 2013) [#2]
you can compare the Millisecs() between a update.
means memory the last time and
now-last is your frame time.

if you want 60 fps
each frame have 1000 ms/60 frames time for doing something = 16,66 ms one frame

vice versa,count the updates for 1000ms gone.


MikeHart(Posted 2013) [#3]
Use the usual check against Millisecs. And be aware that OnUpdate can be called more times than OnRender. The update rate you have set via SetUpdateRate is the goal for a Monkey app which it tries to achieve. To make sure an app is updated at the rate you set it, Mojo will skip OnRender calls when it detects that it might not be able to update the app enough.

So when you check your FPS inside OnUpdate, you get the FPS for updating the app. The FPS for rendering can be less.

I got the best results so far by setting an updateflag to TRUE inside OnRender. Inside OnUpdate I check against that flag. I only update the content, when the flag is set to true. At the end of OnUpdate, I set it to FALSE. This makes sure that I only update the app, once it got rendered.

Method OnUpdate:Int()
    If canUpdate = False Then Return 0
    '---- Update the game

    canUpdate = True
    Return 0
End

Method OnRender:Int()
    '---- Render your game
    canUpdate = True
    Return 0
End



Gerry Quinn(Posted 2013) [#4]
I use two floats in my main game window (the gameTime is also a float, calculated every update), and do the calculations during OnRender().

My code looks like:




The last statement gives me a moving average over a few frames, which smooths out variations. Replace 0.75 and 0.25 by other numbers adding to 1.0 for more or less smoothing.


Arabia(Posted 2013) [#5]
Thanks guys. Turns out the HTML5 prob was only caused by all the Print statements I left in while I test stuff out. I take it that when the code is converted for Android that Print statements are just ignored which is why it didn't suffer slow down on my tablet.

Getting consistent 60-66 fps now.


Gerry Quinn(Posted 2013) [#6]
Yeah, if you have Print statements in a loop (i.e. in OnUpdate or OnRender)it reprints them all from the start every frame, so eventually everything grinds to a halt.

If you need ongoing output, don't use Print, have it print on the app itself during OnRender.


Arabia(Posted 2013) [#7]
Yep, I will do that in future. For testing purposes it doesn't really worry me as long as I know that this is what's causing the slow down. Just a quick and nasty way to work out what's going on I guess. I'd left so many Print statements in during looping parts (OnUpdate OnRender) that it really did make it grind to a halt - down to around 20fps by the end.

As long as I know it's not slowing down because of my bad coding or the amount of graphic objects being drawn each time it renders.

Thanks for the assistance.


Gerry Quinn(Posted 2013) [#8]
Framerate is actually the perfect example of a case where you definitely should not use Print!


nikoniko(Posted 2014) [#9]
MikeHart wrote:
I got the best results so far by setting an updateflag to TRUE inside OnRender. Inside OnUpdate I check against that flag. I only update the content, when the flag is set to true. At the end of OnUpdate, I set it to FALSE. This makes sure that I only update the app, once it got rendered.


Is it correct?

I got low cpu loading when set global flag canUpdate in OnUpdate()/Update() method and check it and drop in OnRende/Render() method.


Global readyUpdate:Bool = True
..

Method OnUpdate():Void
 object.Move()
 readyUpdate = True

End Method

Method OnRender():Void
 if not readyUpdate then Return
 'do render
 Cls
 object.Render()
 
 readyUpdate = False

End Method





Goodlookinguy(Posted 2014) [#10]
For future readers: A generic FPS calculation.




nikoniko(Posted 2014) [#11]
nikoniko wrote:
Is it correct?


I see now that is correct. It does update after full render previous update.