What is the best way to calculate the current FPS (Frames Per Second)?
Couldn't find anything in the codearchives..
Couldn't find anything in the codearchives..
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
;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