Tweening + Delay on high FPS

BlitzMax Forums/BlitzMax Programming/Tweening + Delay on high FPS

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hello,

I have found a good tweening code that i changed a bit to count both UPS (Updates Per Second) and (Frames Per Second)
With this code, the engine always runs at a fixed game logic rate, so it is very handy!
Now, what else i want todo is cap the FPS if w exceed the UPS. Meening, If the UPS 35, we should never allow more then +-35FPS. If the UPS is 50, we should not allow more then +-50FPS.
Currently, my logic runs nice at 50UPS, because i fixed it. However, FPS runs over 120 FPS eating CPU.
As you can see i tried to delay (to save CPU) and reduce the FPS back to 50, but it works not as i hoped. Because it will toggle between 120FPS 50FPS 120FPS 50 FPS, etc... (which is logical :P)
I smell that i should do it timebased, but i dont yet really see how i can achieve that.

Graphics 800, 600,, , GRAPHICS_BACKBUFFER

Global UPDATE_FREQUENCY:Int = 50
Global update_time:Float = 1000 / UPDATE_FREQUENCY
Global t:Int, dt:Int
Global execution_time:Float = 0

t = MilliSecs() 
While Not KeyDown(KEY_ESCAPE) 
	dt = MilliSecs() - t
	t = MilliSecs() 
	execution_time:+dt
 
    While execution_time >= update_time
		'UPS.Update()
		'Update()
		execution_time:- update_time
	Wend
	
	'FPS.Update() 
	'Render(execution_time / update_time) 

	rem
	If FPS.count > 0 And UPS.count > 0 And FPS.count > UPS.count
		Delay(Float(FPS.count) / Float(UPS.count) * 10) 
	End If
	endrem
Wend