Code archives/Algorithms/TFrameManager
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Limits how many frames will be drawn a second. Pretty simple. :)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 | |||||
SuperStrict Framework BRL.GLMax2D Import BRL.Retro Include "TFrameManager.bmx" Graphics 800,600 'CREATE FRAME MANAGER AND SPECIFY TARGET FPS. Global fpsmanager:TFrameManager=CreateFrameManager(30) Repeat If AppTerminate() Exit If KeyHit(KEY_ESCAPE) Exit 'UPDATE FRAME MANAGER AND CHECK TO SEE IF WE SHOULD DRAW. fpsmanager.Update() If fpsmanager.ReadyToDraw() 'ALL UPDATING GOES HERE! Cls SetBlend ALPHABLEND SetScale 1,1 SetAlpha 1.0 SetRotation 0 SetColor 255,255,255 DrawRect MouseX(),MouseY(),32,32 DrawText "FPS: "+fpsmanager.GetFPS(),0,0 'ALWAYS FLIP SINCE WE ARE CONTROLLING WHEN WE DRAW NOW. Flip(False) EndIf Forever fpsmanager=Null End |
Comments
| ||
1/28/10 Updated to include an FPS counter. See example for demonstration. |
Code Archives Forum