about fps control

BlitzMax Forums/BlitzMax Beginners Area/about fps control

i want that my game always works at the same 'speed'.

i think have limited the fps to 60 just by using : Graphics 1024,768,32, 60. When i display the fps into my game have 59-60FPS. *

Is it a good method or is it better to use
Graphics 1024,768,32, 75 and next use an fps limiter code set to 60 fps ? (If this is better where to find an example)

(*) note that i've tried to set the graphics parameter to 75, i've 74-75 fps displayed into the game)

Thanks for your help !

This isn't a good practice.
You should work with FPS independet calculation technics (using the time difference from now and the last calculation) which scale the modification value to the time frame they need to be applied (normally this means * timedifference / 1000.0 on all calculations that need to be frame independet)

.

thats tweening ... never used it so far ...

i'm searching an example, but nothing easy to understand or adapted to bmax for the moment !

There's been loads of discussions and code for this in Bmax. Search for Delta Time

ok.

note that i already use a lot of 'If MilliSecs() > timer1 + 100' to control frame animation, shoot movement updates...

Thats no problem :-)
delta time works great with time (or even real event) based updating

here a code from Indiepath. Should i now 'paste' my main loop 'inside' the RenderGFX() function ?
i'm very bad with fps limitation !!!

initial post : http://www.blitzbasic.com/Community/posts.php?topic=49306&hl=delta%20time

Graphics 800,600,32,NOSYNC

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(60) ' 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)
	
	DrawText DeltaTime.FPS(Delta1)+" FPS",10,10

		If bx>799 Then bx = 0
End Function


Finally i've used this from Sushimasta. Perhaps not the best solution, but it's easy to understand and add to my initial program ! Hope this works fine !

Strict

Global fps:Float = 60
Global time:Float = 1000 / fps

Local timer:Float = Millisecs() + time

While(Not Keyhit(1))
    Local ms:Float = Millisecs()
    If(ms >= timer)
        timer = ms + time
        'Update()
        'Render()
    Endif


Don't limit your update loop like this. Use delta timing.
It's easier than you think :)

Here's how I calculate smooth rotation and movement in my dungeon app:

 Local diff:Float = Float(MilliSecs () - old_time)
 old_time = MilliSecs()
 Local rot_speed:Float = 0.4 * diff
 Local move_speed:Float = 0.015 * diff	


So now I just add rot_speed to rotation if the user wishes to rotate and move_speed to the movement if the user wishes to move forwards or backwards.

The trick is you want smooth movement across systems of varying speed, but you want the display to update as much as possible at the same time.