Frame limiting

BlitzMax Forums/BlitzMax Programming/Frame limiting

In fact your code should really be this since you are not rendering the gfx every frame, only when screenupdate reaches a certain threshold. You need to pass screenupdate as the delta.

Graphics 800,600,0

AutoMidHandle True

Type DeltaTime
	Global DeltaTList:TList

	Field OldTime:Double
	Field ElapsedTime:Double
	Field Delta:Double
	Field dFps:Double
	Field dDelta:Double
	Field CurrFps:Double
	Field FPSCounter:Double
	Field CurrTime:Double
	Field CheckTime:Double
	
		Method New ()
            	If DeltaTList = Null Then DeltaTList = New TList
            	DeltaTList.AddLast Self
    	End Method		
		
		Method Destroy ()
            	DeltaTList.Remove Self
        End Method

	
		Function Create:DeltaTime(Fps:Double)
			Local dTime:DeltaTime = New DeltaTime
			dTime.Delta = 1
			dTime.dFps = Fps
			dTime.dDelta = 1000/Fps
			Return dTime
		End Function
		
		Function Update()
			Local dTime:DeltaTime
			For dTime:DeltaTime = EachIn DeltaTList
				dTime.CurrTime = MilliSecs()
				dTime.ElapsedTime = dTime.CurrTime - dTime.OldTime
				dTime.OldTime = dTime.CurrTime
				
				If dTime.CurrTime > dTime.CheckTime
					dTime.CheckTime = dTime.CurrTime + 1000
					dTime.CurrFps = dTime.FPSCounter
					dTime.FPSCounter = 0
				Else
					dTime.FPSCounter:+1
				EndIf
			Next
			
		End Function
		
		Function FPS(dTime:DeltaTime)
			Return dTime.CurrFps
		End Function
		
		Function CurrDelta:Double(dTime:DeltaTime)
			Return dTime.ElapsedTime/dTime.dDelta
		End Function
		
End Type


SetMaskColor(255,0,255)
'Global blob:TImage = LoadImage("C:\blitzmax\samples\simonh\fireworks\spark.png")


Global Delta1:DeltaTime = DeltaTime.Create(35.0) ' Normal 35 FPS
Local  Delta:Double

Global ScreenUpdate:Double
Global bx:Double=10

While Not KeyHit(KEY_ESCAPE)

	

	
	
	DeltaTime.Update()
	
	Delta = DeltaTime.CurrDelta(Delta1)
	
	If Delta > 1 Then Delta = 1 ' Delta should never be bigger than 1.
	
	' -------------------------------GFX FUNCTIONS ---------------------------
	ScreenUpdate = ScreenUpdate + Delta

	
	If ScreenUpdate > 0.30 Then
		Cls
	
		RenderGFX(ScreenUpdate)
		ScreenUpdate = 0
		Flip
	EndIf	
	
	' ------------------------------------------------------------------------
	
	FlushMem
	
Wend


Function RenderGFX(Delta:Double=1)
		DrawOval bx,100,10,10

			bx=bx+(Delta * 2)
	
		If bx>799 Then bx = 0
End Function