FPS Framerate

Blitz3D Forums/Blitz3D Beginners Area/FPS Framerate

Tibit(Posted 2004) [#1]
What is the best way to calculate the current FPS (Frames Per Second)?

Couldn't find anything in the codearchives..


WolRon(Posted 2004) [#2]
Global Debug = True

Type FrameRate
	Field TargetFPS#
	Field SpeedFactor#
	Field TimeElapsed#
	Field FPS#
	Field FPS2#
	Field FPS3#
	Field FPS4#
	Field FPS5#
	Field CurrentTicks
	Field FrameDelay
End Type

Global no0# = 0.00000000001	;avoids division by zero
;for example; any time you perform a division, add no0# to the divisor
;quotient# = dividend# / (divisor# + no0#)

Global FL.FrameRate = New FrameRate

;initialize frame limiter
FL\TargetFPS# = 60	;set this to whatever FPS your code is based on
FL\FrameDelay = MilliSecs()


;place the following code inside of your game loop

;Set Speed Factor
FL\CurrentTicks = MilliSecs()
FL\SpeedFactor# = (FL\CurrentTicks - FL\FrameDelay) / (1000.0 / FL\TargetFPS#)
If FL\SpeedFactor# <= 0 Then FL\SpeedFactor# = no0#
FL\TimeElapsed# = (FL\CurrentTicks - FL\FrameDelay) / 1000.0
If Debug
	FL\FPS# = (FL\FPS2# + FL\FPS3# + FL\FPS4# + FL\FPS5# + FL\TargetFPS# / FL\SpeedFactor#) / 5
	FL\FPS5# = FL\FPS4# : FL\FPS4# = FL\FPS3# : FL\FPS3# = FL\FPS2# : FL\FPS2# = FL\FPS#
EndIf
FL\FrameDelay = FL\CurrentTicks


Here is some code that I use. It's code that figures out a SpeedFactor# for you to apply to all of your in-game movements (or you can use the TimeElapsed# variable depending on the situation).

It also averages the time it took (FPS) for the last five frames.
;SpeedFactor# contains a percentage of the last frame
;TimeElapsed# contains a percentage of the last second
;use whichever one applies to your code

;example usage of SpeedFactor#
;let's say your player normally moves 5 pixels (or units) per frame
If KeyDown(205)
	PlayerX = PlayerX + 5.0 * FL\SpeedFactor#
EndIf

;example usage of TimeElapsed#
;car simulator moves based on real-world time (in other words, not based on so many units per frame)
v# = v# + (FL\TimeElapsed# * a#)	;velocity (m/s) = velocity (m/s) + time (s) * acceleration (m/s^2)


;example FPS display
If Debug
	Text 0, 0, "FPS: " + Int(FL\FPS#)
EndIf



Tibit(Posted 2004) [#3]
Wolron I was hoping for something simple ;)
Still this seems quite good anyway.

Could you explain a little more in detail how this works?
FrameDelay? TargetFPS?
Shouldn't SpeedFactor be = 1 / Framerate or is it?
From what I understand this "checks" the FPS to determine a good speed for the game? It seems like I have to do it the opposite way in my case. That is to first determine the FPS (like 75) and then apply a speedfactor if the Fps goes below this (so the speed is constant even if the FPS is not).

Thanks for the reply!
//Wave~


WolRon(Posted 2004) [#4]
If that didn't answer your question then screw ya! :)

From what I understand this "checks" the FPS to determine a good speed for the game?
Yes. That is what it does.

I know you asked for a FPS calculator but I gave you this because not only does it calculate the FPS for you but it also makes your game speed constant on ALL machines. You should be using something like this in your code anyways :)


Tibit(Posted 2004) [#5]
TargetFPS is the FPS that the game will try to follow..

What is a good TargetFPS 60,75?

What different does it make in the end?

Can I run my game in a FPS of 500+? How to do that?
(Mine stops at 75 fps with my current FPS calc)


WolRon(Posted 2004) [#6]
(Mine stops at 75 fps with my current FPS calc)
This is most likely because you are using the Flip (True) command (the True is assumed) and you have your monitor's refresh rate set at 75.

Can I run my game in a FPS of 500+? How to do that?
Yes, just set TargetFPS to 500. However, in MOST situations, this is absolutely unnecessary. There would be very few scenarios where you would want to achieve something like that.
Usually, you will set it to something like 30,60,70,(100,120 maybe). But remember that your game will still only update as fast as the FLIP command is allowing it to. In other words, if the user has their monitors refresh rate set to 60 Hz and you are using the Flip (True) command, then your game will only update at 60 FPS regardless of how fast you would like your logic to run. You could allow it to run faster using the Flip (False) command but that may cause visual artifacts, such as tearing.

I normally set my games to run at 60 Hz.

What different does it make in the end?
I could go on and on about why you would or wouldn't want to set it at certain speeds, so I won't. Just suffice it to say that something between 60-120 is usually good because it's fast enough to create the illusion of fluid motion and it's slow enough that the computer can keep up.


Tibit(Posted 2004) [#7]
I didn't know about flip true! Thanks WolRon!


Kel(Posted 2004) [#8]
Wave,
I had to explain this render tweening thing...

when I implemented my own based on this code I was very confused, but if you take your calculator and go over the code you will see is easy to do yourself... I implemented my own version of it, differently, but it does the same thing.

Basically, you have the Fps in a second just calculated(with a little code you can do that or with blitz's timers), then take it, divide it by 1000 or 100 and you have a decimal number, then adjust this decimal to the moving entities speed just multiplying by the calculated speedfactor and you are done. this is basically what the code above does... it calculates actual FPS and Speedfactor at the same executing time.

anytime you dont understand formulas in code..take your calculator, check results and debug it graphically... and it will be clearer.

Hope this helps. cheers.


wizzlefish(Posted 2004) [#9]
I know Night GUI had a function called something like, "FindFPS"

You could download NGUI and see if you could find it. That's what I used for my Framerate functions.