Ooops
November 28, 2020, 02:17:01 PM

Author Topic: [bmx] TRL -Fixed rate update logic with Tweening by dmaz [ 1+ years ago ]  (Read 849 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : TRL -Fixed rate update logic with Tweening
Author : dmaz
Posted : 1+ years ago

Description : here's a zip of the mod, fully documented with an example in help.
<a href="http://www.ooeyug.com/files/dmaz.mod.zip" target="_blank">http://www.ooeyug.com/files/dmaz.mod.zip[/url]

make sure you rebuild documentation. Mac users will have to build modules as I didn't include Mac binaries in this zip.


Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Rem
  4. bbdoc: TRL - Fixed rate update logic with Tweening
  5. about:
  6. End Rem
  7. Module dmaz.TRL
  8.  
  9.  
  10. ModuleInfo "Version: 1.0"
  11. ModuleInfo "Author: David Maziarka"
  12. ModuleInfo "License: Public Domain 2007 David Maziarka"
  13.  
  14. ModuleInfo "History: 1.00 Release"
  15.  
  16.  
  17. Rem
  18. bbdoc: The TRL type
  19. End Rem
  20. Type TRL
  21.         Field gameFPS                   :Double = 60.0:Double
  22.         Field framePeriod               :Double = 1000.0:Double / gameFPS
  23.         Field frameTime         :Double = MilliSecs() - framePeriod
  24.  
  25.         Field gamelogicframes   :Int
  26.         Field catchUp           :Int = True
  27.        
  28.         Rem
  29.         bbdoc: This goes in the main loop and should be called every graphic frame.
  30.         about:
  31.         2 functions will be called back to, one that capture the old position
  32.         and one that performs the game update logic.
  33.         returns: tween number that should be used for drawing.
  34.         End Rem
  35.         Method Update:Double( captureCallback(), updateCallback() )
  36.                 Global frameElapsed:Double
  37.                
  38.                 Repeat
  39.                         frameElapsed = MilliSecs() - frameTime
  40.                 Until frameElapsed
  41.                 Local frameTicks:Int = frameElapsed / framePeriod
  42.                 Local frameTween:Double = Double(frameElapsed Mod framePeriod) / framePeriod
  43.  
  44.                 ' update game and world state
  45.                 If Not catchUp And frameTicks > 1 Then frameTicks = 1
  46.                 For Local frameLimit:Int = 1 To frameTicks
  47.                         If frameLimit = frameTicks
  48.                                 captureCallback()
  49.                         End If
  50.                         frameTime = frameTime + framePeriod
  51.                
  52.                         updateCallback()
  53.                         gamelogicframes = GetGameLogicFrames()
  54.                 Next
  55.  
  56.                 Return frameTween
  57.         End Method
  58.  
  59.  
  60.         Rem
  61.         bbdoc: Set the rate at which your "update" function will be run.
  62.         about:
  63.         Choose a logic rate at the start of your project, 30 or 60 work the best. For a game with lots of AI I'd go 30.
  64.         End Rem
  65.         Method SetUpdateRate( gameFPS:Double )
  66.                 Self.gameFPS = gameFPS
  67.                 framePeriod = 1000.0:Double / Self.gameFPS
  68.                 'ResetCounters
  69.         End Method
  70.  
  71.  
  72.        
  73.         Rem
  74.         bbdoc: Get the number of logic updates being called per second.
  75.         returns: Logic updates per second.
  76.         End Rem
  77.         Method GetUPS:Int()
  78.                 Return gamelogicframes
  79.         End Method
  80.  
  81.  
  82.         Rem
  83.         bbdoc: Get the actual frames being draw per second.
  84.         returns: Frames per second.
  85.         End Rem
  86.         Method GetFPS:Int()
  87.                 Global counter:Int
  88.                 Global time:Int
  89.                 Global framerate:Int
  90.        
  91.             counter=counter+1
  92.             If time=0 Then time=MilliSecs()
  93.             If time+1001<MilliSecs() Then
  94.                 framerate=counter
  95.                    counter=0
  96.                 time=MilliSecs()
  97.                 EndIf
  98.                
  99.                 Return framerate
  100.         End Method
  101.        
  102.  
  103.         Method GetGameLogicFrames:Int()
  104.                 Global counter:Int
  105.                 Global time:Int
  106.                 Global framerate:Int
  107.        
  108.             counter=counter+1
  109.             If time=0 Then time=MilliSecs()
  110.             If time+1001<MilliSecs() Then
  111.                 framerate=counter
  112.                    counter=0
  113.                 time=MilliSecs()
  114.                 EndIf
  115.                
  116.                 Return framerate
  117.         End Method      
  118.  
  119.  
  120.         Rem
  121.         bbdoc: Reset time counters.
  122.         End Rem
  123.         Method ResetCounters()
  124.                 framePeriod     = 1000 / gameFPS
  125.                 frameTime       = MilliSecs() - framePeriod
  126.         End Method
  127.  
  128.  
  129.         Rem
  130.         bbdoc: Get the number inbetween the old and the new value.
  131.         returns: The interpolated number.
  132.         End Rem
  133.         Method GetTween:Double( oldValue:Double, value:Double, tween:Double )
  134.                 Return oldValue + (value-oldValue)*tween
  135.         End Method
  136.  
  137. End Type


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal