November 28, 2020, 02:39:22 PM

Author Topic: [bmx] TFrameManager by Ked [ 1+ years ago ]  (Read 721 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] TFrameManager by Ked [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : TFrameManager
Author : Ked
Posted : 1+ years ago

Description : Limits how many frames will be drawn a second. Pretty simple. :)
Code: [Select]
Type TFrameManager
Field fps:Int
Field ftimer:Int
Field fdelay:Int
Field readydraw:Int=False

Field frametimer:Int
Field tframecount:Int
Field framecount:Int

Method Create:TFrameManager(maxframes:Int)
If maxframes<1 maxframes=1

fps=maxframes
ftimer=MilliSecs()
fdelay=Int(1000/maxframes)

frametimer=MilliSecs()
tframecount=0
framecount=0

Return Self
EndMethod

Method SetMaxFrames(maxframes:Int)
If maxframes<1 maxframes=1

fps=maxframes
ftimer=MilliSecs()
fdelay=Int(1000/maxframes)

frametimer=MilliSecs()
tframecount=0
framecount=0
EndMethod

Method Update()
Local newt:Int=MilliSecs()

If newt >= (ftimer+fdelay)
tframecount:+1
readydraw=True
ftimer=MilliSecs()
EndIf

If newt >= (frametimer+1000)
framecount=tframecount
tframecount=0
frametimer=MilliSecs()
EndIf
EndMethod

Method ReadyToDraw:Int()
If readydraw=True
readydraw=False
Return True
Else
Return False
EndIf
EndMethod

Method GetFPS:Int()
Return framecount
EndMethod

Method Delete()
fps=Null
ftimer=Null
fdelay=Null
readydraw=Null
frametimer=Null
tframecount=Null
framecount=Null
EndMethod
EndType

Function CreateFrameManager:TFrameManager(maxframes:Int=30)
Return New TFrameManager.Create(maxframes)
EndFunction


Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Framework BRL.GLMax2D
  4. Import BRL.Retro
  5.  
  6. Include "TFrameManager.bmx"
  7.  
  8. Graphics 800,600
  9.  
  10. 'CREATE FRAME MANAGER AND SPECIFY TARGET FPS.
  11. Global fpsmanager:TFrameManager=CreateFrameManager(30)
  12.  
  13. Repeat
  14.         If AppTerminate() Exit
  15.         If KeyHit(KEY_ESCAPE) Exit
  16.        
  17.         'UPDATE FRAME MANAGER AND CHECK TO SEE IF WE SHOULD DRAW.
  18.         fpsmanager.Update()
  19.         If fpsmanager.ReadyToDraw()
  20.                
  21.                 'ALL UPDATING GOES HERE!
  22.                
  23.                 Cls
  24.                
  25.                 SetBlend ALPHABLEND
  26.                 SetScale 1,1
  27.                 SetAlpha 1.0
  28.                 SetRotation 0
  29.                 SetColor 255,255,255
  30.                
  31.                 DrawRect MouseX(),MouseY(),32,32
  32.                
  33.                 DrawText "FPS: "+fpsmanager.GetFPS(),0,0
  34.                
  35.                 'ALWAYS FLIP SINCE WE ARE CONTROLLING WHEN WE DRAW NOW.
  36.                 Flip(False)
  37.                
  38.         EndIf
  39. Forever
  40. fpsmanager=Null
  41. End


Comments :


Ked(Posted 1+ years ago)

 1/28/10Updated to include an FPS counter. See example for demonstration.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal