Code archives/Algorithms/FPS/Delta Timing

This code has been declared by its author to be Public Domain code.

Download source code

FPS/Delta Timing by Nilium
(Posted 21 years ago)
There's pretty much nothing special about this, you simply Import it, call SetGameSpeed if necessary (defaults to 60fps) and whenever you call Flip the delta time will be acquired and the FPS will be incremented until one second has elapsed.

Basically:
To set the game speed call SetGameSpeed( fps! )
To get the delta time call GetTicks( )
To get the FPS call GetFPS( )

It should be accurate enough for most things.
SuperStrict

Private
Global deltat!=1!
Global ctime!=-1
Global ltime!
Global fps! = 1.0 / (1000!/60!)
Global fps_frames% = 60
Global fps_time% = 0
Global fpsTimer:TTimer = CreateTimer( 1 )

Public

Function SetGameSpeed( fps! )
    fps = 1.0/(1000!/fps)
    fps_frames = fps
End Function

Function GetTicks!( )
    Return deltat
End Function

Function GetFPS%( )
    Return fps_frames
End Function

Private
Function __flipHook:Object( i%, d:Object, c:Object )
    If i = EmitEventHook Then
        Local e:TEvent = TEvent( d )
        If Not e Then Return d
        If e.id = EVENT_TIMERTICK And e.source = fpsTimer Then
            fps_frames = fps_time
            fps_time = 0
        EndIf
    ElseIf i = FlipHook Then
        ltime = ctime
        ctime = Millisecs( )
        fps_time :+ 1
        If Int(ltime) = -1 Then Return d ' First flip?
        deltat = deltat*.005+((ctime-ltime)*fps)*.995
    EndIf
    Return d
End Function
AddHook( FlipHook, __flipHook, Null, 10000 )
AddHook( EmitEventHook, __flipHook, Null, 0 )

Comments

WarpZone
(Posted 21 years ago)
I'm having trouble getting it to work. :( What am I doing wrong?

'Test of Noel's DeltaTime implementation.
SuperStrict
Include "NoelDeltaTime.bmx"
SetGameSpeed(1)

graphics 640,480,1


SetBlend ALPHABLEND
SetAlpha 0.2
SetColor (255,255,255)
Global t:Int=0

While not KeyHit(KEY_ESCAPE)
	Cls
	DrawText "DrawRect Example",0,0
	For Local r:Int=t To t+500 Step 10
		SetRotation r
		SetScale r/5,r/5
		DrawRect r,r,2,2
	Next
	t=t+1
	If t=10 t=0
	Flip();	
Wend


This is just the drawrect example from the Blitz help with a call to SetGameSpeed tacked on the begining. As you can see, I'm trying to set it to 1fps (Just to test it and compare 60fps to 1fps.) No matter what number I use when I call it, it always seems to use the default of 60fps. A "Print GFPS()" in the main loop always returns 60, no matter what. What gives?

Diablo
(Posted 21 years ago)
you should use flip 0 (or flip false), heres some working code:
'Test of Noel's DeltaTime implementation.
SuperStrict

Private
Global deltat!=1!
Global ctime!=-1
Global ltime!
Global fps! = 1.0 / (1000!/60!)
Global fps_frames% = 60
Global fps_time% = 0
Global fpsTimer:TTimer = CreateTimer( 1 )

Public

Function SetGameSpeed( fps! )
    fps = 1.0/(1000!/fps)
    fps_frames = fps
End Function

Function GetTicks!( )
    Return deltat
End Function

Function GetFPS%( )
    Return fps_frames
End Function

Private
Function __flipHook:Object( i%, d:Object, c:Object )
    If i = EmitEventHook Then
        Local e:TEvent = TEvent( d )
        If Not e Then Return d
        If e.id = EVENT_TIMERTICK And e.source = fpsTimer Then
            fps_frames = fps_time
            fps_time = 0
        EndIf
    ElseIf i = FlipHook Then
        ltime = ctime
        ctime = MilliSecs( )
        fps_time :+ 1
        If Int(ltime) = -1 Then Return d ' First flip?
        deltat = deltat*.005+((ctime-ltime)*fps)*.995
    EndIf
    Return d
End Function
AddHook( FlipHook, __flipHook, Null, 10000 )
AddHook( EmitEventHook, __flipHook, Null, 0 )

SetGameSpeed(60)

Graphics 640,480,0


SetBlend ALPHABLEND

SetColor (255,255,255)
Global t# =0

While Not KeyHit(KEY_ESCAPE)
	Cls
	DrawText "DrawRect Example",0,0
	DrawText "fps = " + GetFPS(), 10, 20
	
	DrawOval 10 + t, 50, 40, 40
		
	SetAlpha 1
	
	t=t+(1! * GetTicks())
	'If t=10 t=0
	Flip 0
Wend



HOWVER - I dont see any difference using set game speed :(

Drakim
(Posted 19 years ago)
I think the reason game speed doesn't work is because:

Function SetGameSpeed( fps! )
    fps = 1.0/(1000!/fps)
    fps_frames = fps
End Function


Here he uses a parameter name "fps!", but he already has a global called "fps!" and he referes to them both as "fps" in the function. Try changing the name of one of them, and it might work.

tonyg
(Posted 19 years ago)
Isn't that taking the fps value sent by the user as a parm of the setgamespeed function to the global fps parm.

Nilium
(Posted 19 years ago)
Might be. I can't say I particularly care to fix this when the code I have in my engine works fine.

Also, in the case of both examples above, neither user has any idea what delta timing is.

Chroma
(Posted 19 years ago)
Posting non-working code in the archives is a bit pointless don't you think? And you particularly don't care to fix it to boot! Nice.

Nilium
(Posted 19 years ago)
I'm glad if it personally hinders your work.

markcw
(Posted 19 years ago)
Where's all your free code and why is your site empty?

Nilium
(Posted 19 years ago)
Because I switched hosts and don't feel like providing it.

markcw
(Posted 19 years ago)
I'm jealous.

Code Archives Forum