display fps on screen

Started by zelda64bit, August 07, 2021, 20:20:50

Previous topic - Next topic

zelda64bit

Hello.

there is some way to get the fps from the game so that you can display them on the screen.

Midimaster

Code (BlitzMax) Select
SuperStrict
Graphics 800,600
Print GraphicsHertz()
...back from Egypt

zelda64bit

Thank you Midimaster. ;) Although it does not do what I wanted, I give example to explain myself.

SuperStrict
AutoMidHandle(True) 'centra origen en el medio

Type Tjugador
Field x:Int
Field y:Int
Field rotacion:Int
Field imagen:TImage = LoadImage("00.png")

Function Create:Tjugador()
Local jugador:Tjugador = New Tjugador
SeedRnd(MilliSecs()) 
jugador.x = Rand(32, 608)
jugador.y = Rand(100, 450)
Return jugador
EndFunction

Method pintar()
rotacion:+ 1
SetScale(0.5,0.5)
SetRotation(rotacion)
DrawImage(imagen,x,y)
EndMethod

EndType

Global objetos:Int

Graphics(640,480)


Global lista_jugador:TList = CreateList()

While Not KeyDown(key_escape)
SetScale(2,2)
SetRotation(0)
DrawText("frames: "+GraphicsHertz(),100,32)
DrawText("objetos: "+objetos,320,32)

If(GraphicsHertz() > 58) Then
Local jugador:Tjugador = Tjugador.Create()
ListAddLast(lista_jugador,jugador)
objetos:+1
EndIf

For Local j:Tjugador = EachIn lista_jugador
j.pintar()
Next

Flip()
Cls()
EndWhile



In the example I create objects while it is greater than 58 fps, what happens is that the fps do not go down for more objects that appear on the screen and I can not see the performance that bliztmax has on my computer.

Midimaster

As long as you do games in a window the GraphicsHertz() is related to the Vsync that Window uses. It does not go down but is constant 60Hz.

Every time when you use the command Flip() your screen will be sent to the window. With only the word Flip() this happens too at 60Hz. Means, that independent of what msec you used before the next While/Wend-loop will start 16msec later. Flip() without parameter pauses your app until the vsync comes.

You could use a Flip 0 to speed up your performance. This means sent your screen to the window, but dont wait for the vsync.

If you want to know how often the While/Wend iterated simply use a counter:Int in combination with a Millisecs()-time.
Code (BlitzMax) Select

While Not KeyDown(key_escape)
Global Counter:Int, TimeStamp:Int
If TimeStamp<Millisecs()
TimeStamp = Millisecs()+1000
Print "FPS=" + Counter
Counter=0
Endif
Counter = Counter +1
SetRotation(0)
DrawText("frames: "+GraphicsHertz(),100,32)
DrawText("objetos: "+objetos,320,32)
.....



Another way would be to measure the time between While-command and Wend command:
Code (BlitzMax) Select

While Not KeyDown(key_escape)
Local  TimeStamp:Int =  Millisecs()
' now your code:
SetRotation(0)
DrawText("frames: "+GraphicsHertz(),100,32)
DrawText("objetos: "+objetos,320,32)
.....
Print "Time needed:" + (Millisecs()-TimeStamp)
Flip()
Wend


If you need it more precise you could use my MicroTimer code snipplet I pubished here last week. This enables measurments below 1msec. So it is possible to get usefull result when you try only to find out the performance of a single function or a hand full of code lines. See here:

https://www.syntaxbomb.com/blitzmax-blitzmax-ng/usleep/msg347051757/#msg347051757
...back from Egypt

zelda64bit

Thanks for the help,I'll try it to see if it works. ;)