TGC Codebase Backup



AGK Performance Timer by Jack

20th Apr 2017 16:44
Summary

This code allows you to see a simple text display with the current rendering/ main process time. It does even show you, if you hit performance limits.



Description

This code allows you to see a simple text display with the current rendering/ main process time. It does even show you, if you hit performance limits.It does also include some interesting coding methods, for example how to save a shader in the AGK code.Useful, if you want to debug performance killers.
You can combine this with a timer.
The window is toggleable with the Enter- Key.

Function list:

- CreateEngineTimerText(px,py,size#)
- UpdateEngineTimerText()
- SetEngineTimerVisible(vis)
- GetEngineTimerVisible()
- CreatePSShader(name$,ShaderArr as string[])



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    
/*
System Init
*/
// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "ProcedualLoading" )
SetWindowSize( 1920, 1080, 0)

// set display properties
SetVirtualResolution( 1920, 1080 )
SetOrientationAllowed( 1, 1, 1, 1 )
// SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetSyncRate(60,1)

/*
Create Shader String Array (will be saved to a file and loaded later)
*/

	Dim PS_GraphShader[25] as string = ["",
	"uniform sampler2D texture0;",
	"varying mediump vec2 uvVarying;",
	"uniform float plot_value;",
	"#define SCALE 2.",
	"#define D (.0125 * SCALE)",
	"vec3 plot(float f,float x,vec3 color)",
	"{",
	"	float y = smoothstep (f-D, f+D, x);",
	"	y *= 1.- y;	",
	"	return y * color * 5.;",
	"}",
	"void main()",
	"{",
	"vec2 t = uvVarying;",
	"// optional: center around origin by going to [-1, +1] and scale",
	"	 t = (t * 1. - .5) * SCALE;",
	"// plot the axes",
	"	vec3 col = vec3(0, 0, 0);",
	"	col += plot(0., t.y, vec3(1, 1, 1));",
	"	col += plot(t.x, 0.9, vec3(1, 1, 1));",
	"// plot function",
	"	col +=plot(plot_value*1.8-0.9,t.y,vec3(1, 0, 0));",
	"// output",
	"float a = col.r+col.g+col.b;",
	"gl_FragColor = vec4 (col,a);",
	"}",
	""]



/*
Time System Init
*/
type Timer_def
	FrameTime# as float
	MainProcessTime# as float
	SyncTime# as float
	dbg_txt as integer
	dbg_spr as integer
	dbg_shad as integer
endtype

Dim EngineTimer[0] as Timer_def

CreateEngineTimerText(GetVirtualWidth()-180,GetVirtualHeight()-80,16.0)


/*
Main Loop
*/
do
	
EngineTimer[0].FrameTime# = timer()    // Get Time

/*
here goes the main process of your app...
*/
if GetRawKeyPressed(27)=1 then end
if GetRawKeyPressed(13)=1 then SetEngineTimerVisible(1-GetEngineTimerVisible())
/*
main process finish
*/


/*
Rendering
*/

EngineTimer[0].MainProcessTime# = timer() - EngineTimer[0].FrameTime#
Sync()


/*
Update Final Timer
*/
EngineTimer[0].SyncTime# = timer() - EngineTimer[0].FrameTime#
UpdateEngineTimerText()





loop

// function to create the media of the Timer Text

function CreateEngineTimerText(px,py,size#)



	shad = CreatePSShader("PSGraphShader.ps",PS_GraphShader) 
	
	img = CreateImageColor(255,0,0,255)
	spr = CreateSprite(img)
	SetSpriteTransparency(spr,1)
	SetSpriteSize(spr,size#*2,size#*4)
	SetSpritePosition(spr,px,py)
	SetSpriteShader(spr,shad)
	EngineTimer[0].dbg_spr = spr
	EngineTimer[0].dbg_shad = shad
	
	txt = CreateText("")
	SetTextPosition(txt,px+GetSpriteWidth(spr)+5,py)
	SetTextSize(txt,size#)
	EngineTimer[0].dbg_txt = txt	
	
	
endfunction

// Update timer text
function UpdateEngineTimerText()
	txt = EngineTimer[0].dbg_txt
	
	
	if GetTextVisible(txt)=1
	
	mainline$="FPS: "+str(ScreenFPS())+chr(10)+"Process Time: "+str(EngineTimer[0].MainProcessTime#)+chr(10)+"Sync Time: "+str(EngineTimer[0].SyncTime#)+chr(10)
	mainlen = len(mainline$)
	free# = 100*(EngineTimer[0].SyncTime#-EngineTimer[0].MainProcessTime#)/EngineTimer[0].SyncTime#
	text$ = mainline$+str(free#,2)+" % Free"
	
	// color percent
	

	for i=mainlen to len(text$)
		
		r# = (100.0-free#)/100.0*255

		g# = free#/(100.0)*255
		SetTextCharColor(txt,i,r#,g#,0,255)
	next i
		SetShaderConstantByName(EngineTimer[0].dbg_shad,"plot_value",1.0-(free#/100.0),0,0,0)
	endif
	
	SetTextString(txt,text$)
endfunction



function SetEngineTimerVisible(vis)
	SetTextVisible(EngineTimer[0].dbg_txt,vis)
	SetSpriteVisible(EngineTimer[0].dbg_spr,vis)
endfunction

function GetEngineTimerVisible()
	out = GetTextVisible(EngineTimer[0].dbg_txt)
endfunction out





function CreatePSShader(name$,ShaderArr as string[])

	if GetFileExists(name$)=1 then DeleteFile(name$)
	file = OpenToWrite(name$)
	
		for i=0 to ShaderArr.length
			WriteLine(file,ShaderArr[i])
		next i
	
	CloseFile(file)
	shad = LoadSpriteShader(name$)
endfunction shad