/******************************************************************************************
	AM2 PS   (c) 1999-2001 ISL

	timer.c:		Debug timing functions
******************************************************************************************/

#include "global.h"
#include "main.h"
#include "timer.h"

char	timerActive = 0;
int 	globalTimer[TIMER_NUMTIMERS];
int 	activeTimer[TIMER_NUMTIMERS];
int 	prevTimer[TIMER_NUMTIMERS];
char	*timerName[TIMER_NUMTIMERS] = {TIMER_NAMES};
//char	timerValue1[TIMER_NUMTIMERS];
//char	timerValue2[TIMER_NUMTIMERS];
//char	timerValue3[TIMER_NUMTIMERS];


static void dispGraph(short x,short y, short w, short h, UBYTE r, UBYTE g, UBYTE b)
{
	POLY_F4 	*f4; 

	BEGINPRIM(f4, POLY_F4);
	setPolyF4(f4);
	setXY4(f4, x,y, x+w,y, x,y+h, x+w,y+h);
	setRGB0(f4, r,g,b);
	ENDPRIM(f4, 0, POLY_F4);
}


/**************************************************************************
	FUNCTION:	timerDisplay()
	PURPOSE:	Display timer readings if enabled
	PARAMETERS:	
	RETURNS:	
**************************************************************************/

void timerDisplay()
{
	int		loop, subtotal = 0;
	char	str[20];

	TIMER_START(TIMER_TIMERS);
	if (timerActive)
	{
		for(loop=1; loop<TIMER_NUMTIMERS; loop++)
		{
			fontPrint(font, -240,-120+loop*12, timerName[loop], (loop&1)?(90):(130),(loop&1)?(90):(130),(loop&1)?(90):(130));
			sprintf(str, "%4d", prevTimer[loop]);
			fontPrint(font, 80,-120+loop*12, str, (loop&1)?(90):(130),(loop&1)?(90):(130),(loop&1)?(90):(130));
			dispGraph(-60,-117+loop*12, (100*prevTimer[loop])/(1+prevTimer[TIMER_TOTAL]),4, (loop&1)?(100):(200),(loop&1)?(100):(200),0);
			dispGraph(-62,-118+loop*12, 104,6, 0,0,0);
			subtotal += prevTimer[loop];
		}
/*
		fontPrint(font, -240,-120+loop*12, "SUB-TOTAL", 0,128,128);
		sprintf(str, "%d (%d%%)", subtotal, (100*subtotal)/(1+prevTimer[TIMER_TOTAL]));
		fontPrint(font, 80,-120+loop*12, str, 0,128,128);
		loop++;
		fontPrint(font, -240,-120+loop*12, "OTHER", 0,150,150);
		sprintf(str, "%d (%d%%)", prevTimer[TIMER_TOTAL]-subtotal, (100*(prevTimer[TIMER_TOTAL]-subtotal))/(1+prevTimer[TIMER_TOTAL]));
		fontPrint(font, 80,-120+loop*12, str, 0,150,150);
		loop++;
*/
		fontPrint(font, -240,85, timerName[TIMER_TOTAL], 128,128,128);
		subtotal = (100*prevTimer[TIMER_TOTAL])/((PALMODE)?(620):(520));
		sprintf(str, "%d (%d%%)", prevTimer[TIMER_TOTAL], subtotal);
		fontPrint(font, 120,85, str, 128,128,128);
		if (subtotal>100)
			dispGraph(-50,86, subtotal,8, 255,0,0);
		else
			dispGraph(-50,86, subtotal,8, 128,0,0);
		dispGraph(-52,85, 104,10, 32,32,32);
	}
	TIMER_STOP(TIMER_TIMERS);
}
