Page 1 of 1

Need enlightenment on terminology, practice and purpose

Posted: Thu Jan 03, 2013 8:28 pm
by Nituvious
First, what is the proper way of recording frames per second? I'm using a small, incomplete profiler function but it feels inaccurate.

Code: Select all

Structure WORLD_STATE
	tick.l
	tickLast.l
	tickCurrent.l
	tickTime.l
	
	frameTimeCurrent.l
	frameTimeLast.l
	frameTimeDifference.l
	framesPerSecond.l
	frameHigh.l
	frameLow.l
EndStructure

#FRAME_DELAY = 1000
Global global_WorldTime.WORLD_STATE

; init
global_WorldTime\frameTimeCurrent	= ElapsedMilliseconds()
global_WorldTime\frameTimeLast		= ElapsedMilliseconds()
global_WorldTime\FrameLow					= 2147483647
global_WorldTime\frameHigh				= 0

Procedure GetWorldTime()
	global_WorldTime\frameTimeCurrent = ElapsedMilliseconds()
	global_WorldTime\frameTimeDifference = global_WorldTime\frameTimeCurrent - global_WorldTime\frameTimeLast
	If global_WorldTime\frameTimeDifference >= #FRAME_DELAY
		global_WorldTime\frameTimeLast = global_WorldTime\frameTimeCurrent
		If global_WorldTime\framesPerSecond > global_WorldTime\frameHigh
			global_WorldTime\frameHigh = global_WorldTime\framesPerSecond
		EndIf
		If global_WorldTime\framesPerSecond < global_WorldTime\FrameLow
			global_WorldTime\FrameLow = global_WorldTime\framesPerSecond
		EndIf
		Debug "FPS: " + Str(global_WorldTime\framesPerSecond) + ", High: " + Str(global_WorldTime\frameHigh) + ", Low: " + Str(global_WorldTime\FrameLow)
		global_WorldTime\framesPerSecond = 0
	Else
		global_WorldTime\framesPerSecond + 1
	EndIf
EndProcedure
Is there a difference between frames per second and frame rate? What exactly is tick counting and how to do you effectively utilize it?

Re: Need enlightenment on terminology, practice and purpose

Posted: Thu Jan 03, 2013 9:04 pm
by flood
Is there a difference between frames per second and frame rate?
They both mean the same thing
What exactly is tick counting and how to do you effectively utilize it?
In this case the 'tick' structure fields seems to not be used. But a tick represents an iteration of a timer. The amount of time since the previous tick is based on the resolution of the timer.

Here's a higher resolution FPS counter using delta timing. It can easily be modified to add framerate independent movement speed.

Code: Select all

Procedure.d FPS()
  Static FPSTimer.q,maxfreq.q
  Protected delta.d,nanoseconds.q, t.q
  If maxfreq=0
    If QueryPerformanceFrequency_(@maxfreq)
      maxfreq = maxfreq/1000000
    EndIf
  EndIf
  QueryPerformanceCounter_(@t)
  nanoseconds = t/maxfreq
  delta = (nanoseconds - FPSTimer) / 1000000
  FPSTimer = nanoseconds
  ProcedureReturn 1/delta
EndProcedure

Re: Need enlightenment on terminology, practice and purpose

Posted: Fri Jan 04, 2013 12:30 am
by LuCiFeR[SD]
if all you want is just a basic framecounter... This is all I use. I have put it into a working example, but as you can see, no need to overcomplicate things if you don't need to

Code: Select all

InitEngine3D()
InitSprite()
InitSprite3D()

Structure FPS
  FPS_Increment.i
  FPS_Start.i
  Current_FPS.i
EndStructure

Declare FrameCounter(*FPS.FPS)

Define FPS.FPS
FPS\FPS_Increment=0
FPS\FPS_Start=0
FPS\Current_FPS=0

Procedure FrameCounter(*FPS.FPS)
  ;This is our framecounter.  Nothing special, but accurate enough for our basic needs.
  
  *FPS.FPS\FPS_Increment+1  
  If ElapsedMilliseconds()-*FPS.FPS\FPS_Start=>1000
    *FPS.FPS\FPS_Start=ElapsedMilliseconds()
    *FPS.FPS\Current_FPS=*FPS\FPS_Increment
    *FPS.FPS\FPS_Increment=0
    
  EndIf
  ProcedureReturn *FPS.FPS\Current_FPS
EndProcedure 



Enumeration
  #Window
EndEnumeration

OpenWindow(#Window, 0, 0, 800, 600, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window), 0, 0, WindowWidth(#Window), WindowHeight(#Window), 0, 0, 0,#PB_Screen_SmartSynchronization) ; (try #PB_Screen_WaitSynchronization or #PB_Screen_NoSynchronization too)

Repeat
  
  Repeat
    
    Select WindowEvent()
      Case #PB_Event_CloseWindow
        End
      Case #Null
        Break
    EndSelect
    
  ForEver
  
  RenderWorld()
  
  SetWindowTitle(#Window, "Frames Per Second: "+Str(FrameCounter(@FPS.FPS)))
  
  FlipBuffers()
  
ForEver

Re: Need enlightenment on terminology, practice and purpose

Posted: Fri Jan 04, 2013 2:34 pm
by Nituvious
@flood
The tick thing in my struct was left in by mistake. It's from a failed implementation on my take of ticks.
I like your higher resolution code, however it doesn't work on Linux.

@LuCiFeR[SD]
Your example seems to cap at 60 fps, is this an Engine3D thing? Also, Engine3D already has a built in frame rate counter(Engine3DFrameRate(#PB_Engine3D_Current))

Re: Need enlightenment on terminology, practice and purpose

Posted: Fri Jan 04, 2013 3:07 pm
by LuCiFeR[SD]
Nituvious wrote: @LuCiFeR[SD]
Your example seems to cap at 60 fps, is this an Engine3D thing? Also, Engine3D already has a built in frame rate counter(Engine3DFrameRate(#PB_Engine3D_Current))
if you change the setWindowTitle() line to

Code: Select all

SetWindowTitle(#Window, "Frames Per Second: "+Str(FrameCounter(@FPS.FPS))+"Engine3D FPS: "+Engine3DFrameRate(#PB_Engine3D_Current))
apart from the engine3d one using a floating point, the results are similar. Although, not good enough it seems :) It's pretty much how I've done it for quite some time... maybe my math is out a little :)

Re: Need enlightenment on terminology, practice and purpose

Posted: Sun Jan 13, 2013 11:42 am
by Nituvious
So, I found the term I was looking for or trying to explain with 'tick rates'. Fixed time steps!
I also located an article on it but I lack the mental capacity to understand the integration process. http://gafferongames.com/game-physics/
Has anyone tried to implement this method into a game they've written in PureBasic?

Re: Need enlightenment on terminology, practice and purpose

Posted: Mon Feb 04, 2013 9:50 pm
by Thorium
Nituvious wrote:So, I found the term I was looking for or trying to explain with 'tick rates'. Fixed time steps!
I also located an article on it but I lack the mental capacity to understand the integration process. http://gafferongames.com/game-physics/
Has anyone tried to implement this method into a game they've written in PureBasic?
Actualy this articel does not speak about fixed time steps but delta time. Depending on your game it can be very tricky to implement FPS independant movement and game mechanics because you may need to interpolate to have reliable game mechanics like collision detection. A collision could happen in a frame not displayed.

Do a search for FPS independant, you sould find a lot of examples on the forum.

Btw. the FPS are capped at 60 because of your displayes refresh rate. Disable VSync to get bigger frame rates.