Display FPS in Game

Share your advanced PureBasic knowledge/code with the community.
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Display FPS in Game

Post by J. Baker »

Code: Select all

;before Repeat

  FPSCounter = 0
  OneSecond = 1
  
;after Repeat
  
  Repeat
    
    If FlipBuffers()
       FPSCounter +1
    EndIf
     
    If OneSecond = 1
        StartTime = ElapsedMilliseconds()
        OneSecond = 0
    EndIf   
    
      ElapsedTime = ElapsedMilliseconds()-StartTime
    
    If ElapsedTime >= 1000
        FinalFPS = FPSCounter 
         FPSCounter = 0
        OneSecond = 1
    EndIf
     
      StartDrawing(ScreenOutput())
       DrawingMode(#PB_2DDrawing_Default)
        DrawText(10, 0, "FPS " + Str(FinalFPS), RGB(255, 255, 255))
      StopDrawing()
Last edited by J. Baker on Fri Sep 03, 2010 2:10 pm, edited 1 time in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Display FPS in Game

Post by infratec »

Hi J.

hmmm,

first I would replace

Code: Select all

If ElapsedTime = 1000
with

Code: Select all

If ElapsedTime >= 1000
I think it is clear why.

But in general I think it is better to use a window timer for waiting one second.

Bernd
User avatar
J. Baker
Addict
Addict
Posts: 2185
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Display FPS in Game

Post by J. Baker »

You are correct, my mistake. I updated the code.

I don't think AddWindowTimer() works in a full screen game as there's no WindowEvent().
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply