Requested in
http://forum.thegamecreators.com/?m=forum_view&t=203658&b=41
This is code to display a graph on screen showing the frame rate for the last couple of hundred frames.
Its a snippet from an existing project so uses some globals which contain information from system calls. These should be apparent from the name or how they are used.
Call this at the start of the app.
type GDebugType
fileLogging
debugText
debugString as String
graphHeight as float
graphWidth as float
endtype
function Debug_Init()
global GDebug as GDebugType
GDebug.fileLogging = 0
GDebug.debugText = 0
GDebug.debugString = ""
` Graph Sprites
thisWidth# = getVirtualWidth()
thisHeight# = getVirtualHeight()
if thisWidth# > thisHeight#
thisMin# = thisHeight#
else
thisMin# = thisWidth#
endif
` Set Size of Graph
GDebug.graphHeight = thisMin# * 0.1
GDebug.graphWidth = thisMin#
` Set Size of Sprites
thisSize# = thisMin# * 0.005
thisHalf# = thisSize# * 0.5
` Create Template Sprite
thisSprite = createSprite( 0 )
setSpriteSize( thisSprite , thisSize# , thisSize# )
setSpriteOffset( thisSprite , thisHalf# , thisHalf# )
setSpriteDepth( thisSprite , 5 )
fixSpriteToScreen( thisSprite , 1 )
setSpriteColor( thisSprite , 127 , 255 , 127 , 127 )
` Create Raay for Sprites
dim GDebugSprite[ 201 ]
thisX# = 0.0
for thisPos = 0 to 199
GDebugSprite[ thisPos ] = cloneSprite( thisSprite )
setSpritePositionByOffset( thisSprite , thisX# , GDebug.graphHeight )
thisX# = thisX# + thisSize#
next thisPos
setSpriteSize( thisSprite , thisMin# , thisSize# )
setSpriteColor( thisSprite , 127 , 127 , 255 , 127 )
setSpriteDepth( thisSprite , 6 )
setSpritePosition( thisSprite , 0 , GDebug.graphHeight )
GDebugSprite[ 200 ] = clonesprite( thisSprite )
GDebugSprite[ 201 ] = thisSprite
endfunction
Then call this at the end of every loop.
function Debug_LoopEnd()
print( " loopCount: " + str( GMain.loopCount ) )
print( "loopStartTime: " + str( GMain.loopStartTime ) )
print( "lastFrameTime: " + str( GMain.lastFrameTime ) )
print( " lastFPS: " + str( GMain.lastFPS ) )
print( " Screen FPS: " + str( ScreenFPS() ) )
print( " Resolution: " + str( GDisplay.vWidth ) + " x " + str( GDisplay.vHeight ) )
if GMain.state < 0 or GMain.state >= countStringTokens( STATE_STRING , "," )
thisState$ = "Unknown"
else
thisState$ = getStringToken( STATE_STRING , "," , GMain.state + 1 )
endif
print( " State: " + thisState$ + " (" + str( GMain.stateStep ) + ")" )
` Timing Graph
for thisPos = 0 to 198
setSpriteY( GDebugSprite[ thisPos ] , getSpriteY( GDebugSprite[ thisPos + 1 ] ))
next thisPos
thisY# = ( 1.0 - GMain.lastFPS / 100.0 ) * GDebug.graphHeight
setSpriteY( GDebugSprite[ 199 ] , thisY# )
setSpriteY( GDebugSprite[ 200 ] , thisY# )
endfunction
It's part of a thing I'm doing for the Community Wiki
The contents of the global GMain should be apparent, but here's the definition in case you want to duplicate it;
type GMainType
loopCount
loopStartTime as float
lastFrameTime as float
lastFPS as float
state
stateStep
stateStartTime as float
endtype
function Main_Init()
global GMain as GMainType
...
it is set each loop with a call to
function Main_LoopStart()
GMain.loopStartTime = timer()
GMain.LastFrameTime = getFrameTime()
inc GMain.loopCount
if GMain.LastFrameTime = 0.0
GMain.LastFPS = 99.99
else
GMain.LastFPS = 1.0 / GMain.LastFrameTime
endif
Display_LoopStart()
Touch_LoopStart()
Debug_LoopStart()
endfunction
I'll let you figure out GDisplay yourself.
NB:
The size of the graph is the shorter of the two resolution mesurements, so for a 640 x 480 display ( or a 480 x 640 display ) it will draw it 480 wide.
That way you don't have to move or resize it if the orientation of the device changes