Skip to main content
Spacebruce dot netlify dot app

Lazy gamemaker performance monitor

# Usage;

Perf = new PerfTester(SAMPLES); //Create, samples is how far history stretches back, in frames
Perf.Tick();  //Step event somewhere
Perf.Draw(x,y,width,height);  //Draw

# Script as follows;


function PerfTester(Samples) constructor
{
	MaxReal = 0;
	MinReal = 0;
		
	Pointer = 0;
	SamplesMax = Samples;
	
	BufferReal = array_create(SamplesMax + 1);
	BufferFPS = array_create(SamplesMax + 1);
	
	static Tick = function()	//Run once per frame, captures current speed
	{
		if(fps_real > 0 && fps_real != infinity)
		{
			BufferReal[Pointer] = fps_real;
			BufferReal[Pointer + 1] = 0;
			MaxReal = max(fps_real, MaxReal);
			MinReal = min(fps_real, MinReal);
		}
		if(fps > 0)
		{
			BufferFPS[Pointer] = fps;
			BufferFPS[Pointer + 1] = 0;
		}
		++Pointer;
		if(Pointer == SamplesMax)
		{
			Pointer = 0;
		}
	}
	
	static Draw = function(X,Y, Width, Height)
	{
		var FrameRate = game_get_speed(gamespeed_fps);
		var SampleWidth = Width / SamplesMax;
		var ScaleFPS = (Height - 2) / FrameRate;	//to do, get defined FPS
		var ScaleReal = (Height - 2) / MaxReal;
		var SecondWidth = Width * (FrameRate / SamplesMax);		
		
		static RenderGraph = function(SampleWidth,Height, Buffer, Colour, Scale)
		{		
			draw_primitive_begin(pr_linestrip);
			var last = -1;
			var drawnLast = false;
			for(var i = 0; i < SamplesMax; ++i)
			{
				var p = (Pointer + i) % SamplesMax;
				var value = Buffer[p];
				/*
					Optimisation - don't repeat identical points
					To keep things looking right, manually force drawing on First & Last sample
				*/
				if value != 0 && ((i == 0) || (i == (SamplesMax - 1)) || value != last)
				{
					if !drawnLast && (last > 0)		//On change draw last sample (Assuming it's in range)
						draw_vertex_color((i-1) * SampleWidth,  Height - (last * Scale), Colour, 1.0);
					draw_vertex_color(i * SampleWidth,  Height - (value * Scale), Colour, 1.0);
					drawnLast = true;
				}
				else
				{
					drawnLast = false;
				}
				last = value;
			}
			draw_primitive_end();
		}
		
		matrix_set(matrix_world, matrix_build(X,Y+1,0,0,0,0,1,1,1));
		draw_rectangle_color(-2,-2,Width+4,Height+4, c_gray,c_gray,c_gray,c_gray, false);		
		for(var w = 0; w < Width; w += SecondWidth)	//Mark down seconds
			draw_line(Width-w,-2,Width-w,Height+4);
			
		RenderGraph(SampleWidth,Height, BufferReal, c_green, ScaleReal);
		RenderGraph(SampleWidth,Height, BufferFPS, c_red, ScaleFPS);
		
		matrix_set(matrix_world, matrix_build_identity());
	}
}