At the end of the wiki (in an appendix), I could propose a lite version of regulation, universal without thresholds to adjust and not bringing significant CPU load (adapted to old PC), a version that I have already named 'regulateLite()'.
But this lite version is less precise (precision of the 'Sleep' keyword that is some ms quantification, instead of a few microseconds) and does not support high FPS because it cannot generate waiting times lower than the OS cycle period (corresponding to 'Sleep 1,1').
All-in-one 'regulateLite()' version:
Code: Select all
Function regulateLite(ByVal MyFps As Ulong) As Ulong
'' 'MyFps' : requested FPS value, in frames per second
'' function return : applied FPS value, in frames per second
Static As Double t1
Dim As Single tf = 1 / MyFps
Dim As Double t3 = t1 + tf
Dim As Double t2 = Timer
#if Not defined(__FB_WIN32__) And Not defined(__FB_LINUX__)
If t2 < t1 Then t1 -= 24 * 60 * 60
#endif
Dim As Single dt = (tf - (t2 - t1)) * 1000
Sleep Iif(dt > 1, dt, 1), 1
t2 = Timer
#if Not defined(__FB_WIN32__) And Not defined(__FB_LINUX__)
If t2 < t1 Then t1 -= 24 * 60 * 60
#endif
tf = 1 / (t2 - t1)
t1 = t2
Return tf
End Function
Example of use/test:
Code: Select all
'Declare Function _setTimer Lib "winmm" Alias "timeBeginPeriod"(ByVal As Ulong = 1) As Long
'_setTimer()
Function regulateLite(ByVal MyFps As Ulong) As Ulong
'' 'MyFps' : requested FPS value, in frames per second
'' function return : applied FPS value, in frames per second
Static As Double t1
Dim As Single tf = 1 / MyFps
Dim As Double t3 = t1 + tf
Dim As Double t2 = Timer
#if Not defined(__FB_WIN32__) And Not defined(__FB_LINUX__)
If t2 < t1 Then t1 -= 24 * 60 * 60
#endif
Dim As Single dt = (tf - (t2 - t1)) * 1000
Sleep Iif(dt > 1, dt, 1), 1
t2 = Timer
#if Not defined(__FB_WIN32__) And Not defined(__FB_LINUX__)
If t2 < t1 Then t1 -= 24 * 60 * 60
#endif
tf = 1 / (t2 - t1)
t1 = t2
Return tf
End Function
Screen 12
Dim As Ulong MyFPS = 10
Do
Static As Ulongint l
Static As Double dt
Static As Ulong fps
Screenlock
Cls
Color 11
Print "Called procedure : regulateLite ( " & MyFPS & " )"
Print
Print Using "Measured FPS : ###"; fps
Print Using "Applied delay : ###.### ms"; dt
Print
Print
Print
Color 14
Print "<+> : Increase FPS"
Print "<-> : Decrease FPS"
Print "<other key> : Quit"
Line (0, 80)-(639, 96), 7, B
Line (0, 80)-(l, 96), 7, BF
Screenunlock
l = (l + 1) Mod 640
Dim As String s = Inkey
Select Case s
Case "+"
If MyFPS < 100 Then MyFPS += 1
Case "-"
If MyFPS > 10 Then MyFPS -= 1
Case Chr(27)
Exit Do
End Select
dt = Timer
fps = regulateLite(MyFPS)
dt = (Timer - dt) * 1000
Loop