Frame limiting

BlitzMax Forums/BlitzMax Programming/Frame limiting

I'm trying to make a little template for using simply your
delta code :

The template :
Strict 

Include "Inc_Delta.Bmx"
Graphics 800,600,32,NOSYNC

AutoMidHandle True

' ---------------------------
' Init the delta time library
' ---------------------------
DLT_InitDelta(35.0,0.35)


Global bx:Float=10

' ---------
' Main loop
' ---------
While Not KeyHit(KEY_ESCAPE)
	' ------------------------------
	' Refresh the delta time library
	' ------------------------------
	DLT_RefreshDLT()

	' ---------------------------
	' Test for change the app FPS
	' ---------------------------
	If MouseDown(1) Then
		DLT_MyAppDelta.SetNewFPS(65.0)
	Else
		DLT_MyAppDelta.SetNewFPS(35.0)	
	EndIf
		
	' -------------------------------GFX FUNCTIONS ---------------------------
	If DLT_MyScreenUpdate > DLT_MyAppRefresh Then
		Cls
	
		DLT_UpdateScreen(DLT_MyScreenUpdate)
		DLT_MyScreenUpdate=DLT_MyScreenUpdate - DLT_MyAppRefresh
		
		Flip
	EndIf	
	' ------------------------------------------------------------------------
	
	FlushMem
Wend

' -------------------
' Clear the DeltaTime
' -------------------
DLT_ClearDLT()

' -------------------------------------
' Use this function to refresh your app
' -------------------------------------
Function DLT_UpdateScreen(DeltaTime:Float=1)
	' ----------------
	' A fantastic demo 
	' ----------------
	DrawOval bx,100,10,10

	bx=bx+1*DeltaTime
	If bx>799 Then bx = 0
	
	' ----------------------
	' Retrieve the FPS value
	' ----------------------
	Local DLT_GetDeltaFPS=DLT_MyAppDelta.GetDeltaFPS()
	Local DLT_GetRealFPS=DLT_MyAppDelta.GetRealFPS()
	
	DrawText "DELTA FPS : "+DLT_GetDeltaFPS+" / REAL FPS : "+DLT_GetRealFPS,10,10
End Function


Here is the include : Inc_Delta.bmx
Global DLT_DeltaTimeList:TList=CreateList()

Global DLT_MyAppDelta:DLT_DeltaTime
Global DLT_MyAppRefresh:Float
Global DLT_MyDeltaTime:Float
Global DLT_MyScreenUpdate:Float

' ----------------
' DeltaTime Object
' ----------------
Type DLT_DeltaTime
	Field OldTime:Int
	Field ElapsedTime:Int
	Field DeltaFps:Float
	Field DeltaPeriod:Float
	Field CurrentFps:Int
	Field CurrentTiming:Int
	Field FPSCounter:Int
	Field CheckTime:Int

	Field CurrentFrame:Int
	Field RenderTime:Int	
	Field RealFps:Int
	
	' -----------------------------
	' Create the delta time counter
	' -----------------------------
	Function Create:DLT_DeltaTime(Fps:Float)
		Local NewDelta:DLT_DeltaTime = New DLT_DeltaTime
		NewDelta.DeltaFps = Fps
		NewDelta.DeltaPeriod = 1000/Fps
		
		ListAddLast DLT_DeltaTimeList,NewDelta
		
		Return NewDelta
	End Function
	
	' -----------------------------
	' Update the delta time counter
	' -----------------------------
	Method Update()
		Self.CurrentTiming = MilliSecs()
		Self.ElapsedTime = Self.CurrentTiming - Self.OldTime
		Self.OldTime = Self.CurrentTiming
			
		If Self.CurrentTiming > Self.CheckTime
			Self.CheckTime = Self.CurrentTiming + 1000
			Self.CurrentFps = Self.FPSCounter
			Self.FPSCounter = 0
		Else
			Self.FPSCounter=Self.FPSCounter+1
		EndIf
	End Method
	
	' ---------------------------
	' Get the current delta value
	' ---------------------------
	Method GetCurrentDelta:Float()
		Return Float(Self.ElapsedTime)/Self.DeltaPeriod
	End Method

	' -------------------------
	' Get the current delta FPS
	' -------------------------		
	Method GetDeltaFPS()
		Return Self.CurrentFps
	End Method

	' ------------------------
	' Get the current real FPS
	' ------------------------	
	Method GetRealFPS()
		Self.CurrentFrame=Self.CurrentFrame+1
	
		If MilliSecs()-Self.RenderTime=>1000 Then
			Self.RealFps=CurrentFrame
			Self.CurrentFrame=0
	 		Self.RenderTime=MilliSecs()
		EndIf
	
		Return Self.RealFps
	End Method

	' ---------------------------------------------------------------------
	' This method allow the possibility to change dynamicly the current FPS 
	' ---------------------------------------------------------------------	
	Method SetNewFPS(NewFPS:Float)
		Self.DeltaFps = NewFPS
		Self.DeltaPeriod = 1000/Self.DeltaFps
	End Method	
	
	' ---------------------------------
	' This method destroy a delta timer
	' ---------------------------------
	Method Destroy()
		DLT_DeltaTimeList.Remove(Self)
	End Method
End Type

' ------------------
' Init the DeltaTime
' ------------------
Function DLT_InitDelta(DeltaFrameRate:Float=35.0,DeltaRefresh:Float=0.35)
	DLT_MyAppDelta=DLT_DeltaTime.Create(DeltaFramerate) ' Normal 35 FPS
	DLT_MyAppRefresh=DeltaRefresh
End Function

' ---------------------
' Refresh the DeltaTime
' ---------------------
Function DLT_RefreshDLT()
	' -----------------------------
	' Refresh the delta app counter
	' -----------------------------
	DLT_MyAppDelta.Update()
		
	' -----------------------------------
	' Delta should never be bigger than 1
	' -----------------------------------
	DLT_MyDeltaTime= DLT_MyAppDelta.GetCurrentDelta()
	If DLT_MyDeltaTime > 1 Then DLT_MyDeltaTime = 1 
	
	' ----------------------------
	' Update the screen update VAR
	' ----------------------------
	DLT_MyScreenUpdate = DLT_MyScreenUpdate + DLT_MyDeltaTime
End Function

' -------------------
' Clear the DeltaTime
' -------------------
Function DLT_ClearDLT()
	DLT_MyAppDelta.Destroy()
End Function


If you find some error tell me !

Cheers