Ooops
October 17, 2021, 10:26:07

Author Topic: [bmx] AppTiming module by JoshK [ 1+ years ago ]  (Read 586 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] AppTiming module by JoshK [ 1+ years ago ]
« on: June 29, 2017, 00:28:38 »
Title : AppTiming module
Author : JoshK
Posted : 1+ years ago

Description : Any time-sensitive routine should be modulated by the application speed.  This set of commands will make the task easier.

Call UpdateAppTime() once every game loop.  You can specify a framerate your application should run at.  The default value is 60.

Multiply every time-dependent value in your program with the function AppSpeed().  If the program is supposed to run at 60 FPS but it is running at 120, AppSpeed() will return 0.5.

AppTime() will return the same value until the next call to UpdateAppTime().  This ensures your entire game loop is always using uniform timing.

PauseApp() and ResumeApp() will stop and resume the application without effecting timing.

UPS() will return updates per second.  The value is calculated once per second and rounded to an integer.  This is not a precise measurement, but rather a visual approximation for printing to the screen.

Because of the limited resolution of the Millisecs() timer, the application will cap the UPS at 1000.  A delay will be called to ensure that errors do not occur.
If you use this code, your application will always run at the same speed regardless of framerate.


Code :
Code: BlitzMax
  1. Module leadwerks.apptiming
  2.  
  3. Private
  4.  
  5. Global AppTime_UPS
  6. Global AppTime_Iterator
  7. Global AppTime_CurrentTime
  8. Global AppTime_PauseStart=0
  9. Global AppTime_Speed:Float=1.0
  10. Global AppTime_DesiredLoopTime#=1000.0/60.0
  11. Global AppTime_LastUpdateTime=0
  12. Global AppTime_LastUPSTime
  13.  
  14. Public
  15.  
  16. Rem
  17. bbdoc:
  18. EndRem
  19. Function UpdateAppTime(framerate=60)
  20.         Local time
  21.         Local elapsed
  22.        
  23.         If AppTime_PauseStart Return
  24.        
  25.         AppTime_DesiredLoopTime=1000.0/framerate
  26.  
  27.         time=MilliSecs()
  28.  
  29.         If AppTime_LastUpdateTime=0
  30.                 AppTime_Speed#=1.0
  31.                 AppTime_LastUpdateTime=time
  32.                 AppTime_CurrentTime=time
  33.                 AppTime_LastUPSTime=time
  34.         Else
  35.                 elapsed=time-AppTime_LastUpdateTime
  36.                 If Not elapsed
  37.                         elapsed=1
  38.                         Delay 1
  39.                         time:+1
  40.                 EndIf
  41.                 AppTime_Speed=Float(elapsed)/Float(AppTime_DesiredLoopTime)
  42.                 AppTime_CurrentTime=time
  43.                 AppTime_LastUpdateTime=time
  44.         EndIf
  45.        
  46.         AppTime_Iterator:+1
  47.         If AppTime_CurrentTime-AppTime_LastUPSTime>=1000
  48.                 AppTime_UPS=Float(AppTime_Iterator)/(Float(AppTime_CurrentTime-AppTime_LastUPSTime)/1000.0)
  49.                 AppTime_LastUPSTime=AppTime_CurrentTime
  50.                 AppTime_Iterator=0
  51.         EndIf
  52.        
  53. EndFunction
  54.  
  55. Rem
  56. bbdoc:
  57. EndRem
  58. Function AppTime()
  59.         Return AppTime_CurrentTime
  60. EndFunction
  61.  
  62. Rem
  63. bbdoc:
  64. EndRem
  65. Function AppSpeed#()
  66.         Return AppTime_Speed
  67. EndFunction
  68.  
  69. Rem
  70. bbdoc:
  71. EndRem
  72. Function UPS:Int()
  73.         Return AppTime_UPS
  74. EndFunction
  75.  
  76. Rem
  77. bbdoc:
  78. EndRem
  79. Function PauseApp()
  80.         If AppTime_PauseStart Return
  81.         AppTime_PauseStart=MilliSecs()
  82. EndFunction
  83.  
  84. Rem
  85. bbdoc:
  86. EndRem
  87. Function ResumeApp()
  88.         If Not AppTime_PauseStart Return
  89.         If AppTime_LastUpdateTime
  90.                 Local elapsed=MilliSecs()-AppTime_PauseStart
  91.                 AppTime_LastUpdateTime:+elapsed
  92.         EndIf
  93.         AppTime_PauseStart=0
  94. EndFunction


Comments :


Dreamora(Posted 1+ years ago)

 Hmm, wouldn't it make more sense if UPS returns 0 if paused and the same for AppSpeed?Currently, it will remain on constant speed while paused even thought the world shouldn't change anymore ...Fixed it would look like this:
Code: [Select]
Module leadwerks.apptiming

Private

Global AppTime_UPS
Global AppTime_Iterator
Global AppTime_CurrentTime
Global AppTime_PauseStart=0
Global AppTime_Speed:Float=1.0
Global AppTime_DesiredLoopTime#=1000.0/60.0
Global AppTime_LastUpdateTime=0
Global AppTime_LastUPSTime
Global AppTime_DesiredFrequency% 'mainly to avoid float rounding errors

Public

Rem
bbdoc:
EndRem
Function UpdateAppTime(framerate=60)
Local time
Local elapsed

If AppTime_PauseStart Return
AppTime_DesiredFrequency = framerate
AppTime_DesiredLoopTime=1000.0/framerate

time=MilliSecs()

If AppTime_LastUpdateTime=0
AppTime_Speed#=1.0
AppTime_LastUpdateTime=time
AppTime_CurrentTime=time
AppTime_LastUPSTime=time
Else
elapsed=time-AppTime_LastUpdateTime
If Not elapsed
elapsed=1
Delay 1
time:+1
EndIf
AppTime_Speed=Float(elapsed)/Float(AppTime_DesiredLoopTime)
AppTime_CurrentTime=time
AppTime_LastUpdateTime=time
EndIf

AppTime_Iterator:+1
If AppTime_CurrentTime-AppTime_LastUPSTime>=1000
AppTime_UPS=Float(AppTime_Iterator)/(Float(AppTime_CurrentTime-AppTime_LastUPSTime)/1000.0)
AppTime_LastUPSTime=AppTime_CurrentTime
AppTime_Iterator=0
EndIf

EndFunction

Rem
bbdoc:
EndRem
Function AppTime()
Return AppTime_CurrentTime
EndFunction

Rem
bbdoc:
EndRem
Function AppSpeed#()
Return AppTime_Speed
EndFunction

Rem
bbdoc:
EndRem
Function UPS:Int()
Return AppTime_UPS
EndFunction

Rem
bbdoc:
EndRem
Function PauseApp()
If AppTime_PauseStart Return
AppTime_PauseStart=MilliSecs()
        AppTime_UPS = 0
        AppTime_Speed = 0
EndFunction

Rem
bbdoc:
EndRem
Function ResumeApp()
If Not AppTime_PauseStart Return
If AppTime_LastUpdateTime
Local elapsed=MilliSecs()-AppTime_PauseStart
AppTime_LastUpdateTime:+elapsed
EndIf
AppTime_PauseStart=0
        UpdateAppTime( AppTime_DesiredFrequency )
EndFunction



slenkar(Posted 1+ years ago)

 thanks leadwerks and dreamora, we need a standard (and easy) way to fix framerate


JoshK(Posted 1+ years ago)

 If the user is calling AppSpeed() and UPS() while the application is paused they are doing something wrong.  Pause and resume are for menus or GUI applications, where you stop the game loop and do some other unrelated stuff. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal