FPS Counter & Color Graph

Monkey Forums/Monkey Code/FPS Counter & Color Graph

Paul - Taiphoz(Posted 2014) [#1]
Create a new instance
Global fps:emProfile = new emProfile(10,100,True,4)


Throw a call to fps.Render() at the bottom of your apps main Render method and it will do the rest.

It's got a few display options to allow you to display fps, graph, graph & fps etc.

Import ember

'summary:Profiler used to visible an fps graph.
class emProfile
	Private
	
		CONST ProfileRed:int =1
		CONST ProfileOrange:int =2
		CONST ProfileYellow:int =3
		CONST ProfileGreen:int =4
		field rgb:int
	
		field x:Float
		field y:Float
	
		field frames:int[200]
		field startTime:Int
		
		field visible:bool
		field displayLevel:Int
		field colorMode:bool
		field width:Int
		
		
		field targetFPS:int
		field targetMinFPS:int
		field minFPS:Int
		field maxFPS:Int
		
		field fps:Int
		field totalFPS:int
		field fpsCount:int
		field lastFps:int

		Field updateTime:Int
	
	Public
				
	method new(_x:float=10,_y:float=100,_visible:bool=false,_display:int=4,_color:bool=false,_size:int=200)		
		self.width=_size
		for local i:int = 0 to _size -1
			self.frames[i]=0
		next
		
		self.startTime=Millisecs()
		
		self.x=_x
		self.y=_y
		
		self.visible = _visible
		self.displayLevel=_display
		self.colorMode=_color
		
		self.targetFPS=60
		self.targetMinFPS=29
		
		self.minFPS=60
		self.maxFPS=0
	End
	
	method SetX:void(_x:int)
		self.x=_x
	end method

	method SetY:void(_x:int)
		self.x=_y
	end method
	
	method Show:void()
		self.visible=true
	end Method

	Method SetTargetFps:void(_value:int)
		self.targetFPS=_value
	end method
	
	Method SetTargetMinFps:void(_value:int)
		self.targetMinFPS=_value
	end method
	
	Method SetColorMode:void()
		if self.colorMode =true
			self.colorMode=false
		Else	
			self.colorMode=true
		Endif
	end Method
	
	method Hide:void()
		self.visible=false
	end Method
	
	method Toggle:void()
		select self.visible
			case True
				self.visible=false
			case False
				self.visible=true
		end select
	end method
	
	'summary:add a frame to the graph, providing the current frame rate.
	method AddFrame:void(_value:int)
		self.fps = _value
		
		for local a:int = 199 to 1 step -1
			self.frames[a]=self.frames[a-1]
		next
		self.frames[0] = self.fps

		if self.fps<self.minFPS then self.minFPS=self.fps
		if self.fps>self.maxFPS then self.maxFPS=self.fps
	end method
	
	Method Update:void()
		If Millisecs() - self.startTime >= 1000
			self.totalFPS = self.fpsCount
			self.startTime = Millisecs()
			self.AddFrame(self.totalFPS)
			self.fpsCount = 0
		Else
			self.fpsCount+=1
		End
	End Method
	
	'summary:Draw thew graph
	method Render:void()

		if self.visible
			self.Update()
			
			SetColor(0,0,0)
			DrawRect(self.x,self.y-self.targetFPS,self.width,self.targetFPS)
			SetColor(255,255,255)
			
			select self.displayLevel
				case 0 ' text only
					DrawText("Max:"+self.maxFPS,self.x,self.y)
					DrawText("Min:"+self.minFPS,self.x+50,self.y)				
					DrawText("Fps:"+self.fps,self.x+100,self.y)
				case 1 ' graph only
					for local a:int = 0 to 199
						DrawLine(self.x+a,self.y,self.x+a,self.y-self.frames[a])			
					next
				case 2 'text and graph
					for local a:int = 0 to 199
						DrawLine(self.x+a,self.y,self.x+a,self.y-self.frames[a])			
					next
				
					DrawText("Max:"+self.maxFPS,self.x,self.y)
					DrawText("Min:"+self.minFPS,self.x+50,self.y)				
					DrawText("Fps:"+self.fps,self.x+100,self.y)
				case 3 'coloured graph
					local range:int = self.targetFPS / 4
					'Print "Range ="+range
					local alt:int = 255/60
					for local a:int = 0 to 199
						if self.colorMode =true then SetColor(255-(alt*frames[a]),alt*frames[a],0)
						DrawLine(self.x+a,self.y,self.x+a,self.y-self.frames[a])			
						if self.colorMode =true then SetColor(255,255,255)
					next					
				case 4 ' coloured graph with text
					local range:int = self.targetFPS / 4
					'Print "Range ="+range
					local alt:int = 255/60
					for local a:int = 0 to 199
						if self.colorMode =true then SetColor(255-(alt*frames[a]),alt*frames[a],0)
						DrawLine(self.x+a,self.y,self.x+a,self.y-self.frames[a])			
						if self.colorMode =true then SetColor(255,255,255)
					next					
									
					DrawText("Max:"+self.maxFPS,self.x,self.y)
					DrawText("Min:"+self.minFPS,self.x+50,self.y)				
					DrawText("Fps:"+self.fps,self.x+100,self.y)
			end select
		endif
			
	end Method
		
	Method ClearHistory:void()
		for local a:int = 199 to 1 step -1
			self.frames[a]=0
		next
		self.minFPS=self.targetFPS
		self.maxFPS=0		
	end Method
	
end class



John McCubbin(Posted 2014) [#2]
Pretty sweet, you might want add void to the methods and declare any variables though, that way it will work with Strict (which most people will be using I imagine)


Paul - Taiphoz(Posted 2014) [#3]
Should just be the voids I missed everything should be declared.


Paul - Taiphoz(Posted 2014) [#4]
You know whats really odd, I'm pulling that into my project via an include and the main project is strict, I wonder why that never got flagged by any of the IDE's I use ..


John McCubbin(Posted 2014) [#5]
Spotted one: The _value input for AddFrame()

Aye that is weird, you need to be more strict with it :P


Paul - Taiphoz(Posted 2014) [#6]
Indeed.

I wonder, if it might be of value to log the update rate as well, glfw can do flip false cant it ?


therevills(Posted 2014) [#7]
Interesting... might add it to Diddy ;)


Paul - Taiphoz(Posted 2014) [#8]
go for it , I'm sure you could improve on it as well ..