/*--------------------------------------------------------------------------*/

#include "soh.h"
#include "ipx.h"
#include "mesh.h"
#include "part.h"
#include "born.h"
#include "light.h"
#include "trace.h"

#define KMDEBUG 0

#if KMDEBUG
#define _FLAT_
#define _FLAT_MODEL_
#define _STRICT_UNION_
#define _CLX_
#define _CLX2_
#include <kamui.h>
#include <kmsrvice.h>
KMISYSTEMMETRICS SYS;
#endif

extern void ScreenMask(void);
extern void ScrollGo(long);
extern void Fading(void);
long bgflag=0;
long msflag=0;
long htflag=0;
long dbflag=0;
long ltflag=0;

long GGG0;
long GGG1;
long GGG2;
long GGG3;
long CNT0;
long CNT1;
long CNT2;
long CNT3;
long NOW;
long BAK;
long AVR;
long NUM;
long SPR;
NJS_POINT2 clip[2];

long PER0;
long PER1;
long PER2;
long PER3;
long PER4;

static void TEST(void);
extern void ixPlayer(void);
extern void ixHitDisp(void);
/*--------------------------------------------------------------------------*/
void UserInit(void)
{
	
	ixInit();
	msInit();
	ptInit();
	boInit();
	ltInit();
	sdInit();
	
	CNT0=0;
	CNT1=0;
	NOW=0;
	BAK=0;
	AVR=0;
	sxSetBackColor( 0.f, 0.f, 0.1f );
	
	clip[0].x = 3;
	clip[0].y = 0;
	clip[1].x = 16;
	clip[1].y = 15;
/*	njUserClipping(NJD_CLIP_INSIDE,clip);*/
	
	njSetFogColor(0xffff0000);
	njSetFogDensity(0);
	njFogDisable();
}
/*--------------------------------------------------------------------------*/
long UserMain(void)
{
	long CT=syTmrGetCount();
	if (sxKeyStatus(0, SX_BTN_TL, SX_KEY_PRESS)) htflag=(htflag+1)%3;
	if (sxKeyStatus(0, SX_BTN_TR, SX_KEY_PRESS)) ltflag=1-ltflag;
	
	NUM=0;
	SPR=0;
	njPrintC(NJM_LOCATION(0,0), "A");
	njUserClipping(NJD_CLIP_INSIDE,clip);
	
	ixPlayer();
	ixMainLoop();
	Fading();
	if (htflag) ixHitDisp();
	ScrollGo(256);
	
	if (sxCheckSoftReset()) {
		ixInterrupt((OBJECT*)ixID2WW(0x100), INT_RESET);
		return 1;
	}
	
	njUserClipping(NJD_CLIP_DISABLE,clip);
	
	
	CNT0++;
	if (CNT0>=30) {
		AVR=CNT1;
		
		CNT0=0;
		CNT1=0;
	}
	njPrintC(NJM_LOCATION(0,1), "OBJ:"); njPrintD(NJM_LOCATION(4,1), SPR,3);
	njPrintC(NJM_LOCATION(0,2), "SPR:"); njPrintD(NJM_LOCATION(4,2), NUM,3);
	njPrintC(NJM_LOCATION(0,4), "AVR:"); njPrintD(NJM_LOCATION(4,4), AVR/10,4);
	njPrintC(NJM_LOCATION(0,5), "NOW:"); njPrintD(NJM_LOCATION(4,5), NOW/10,4);
	
#if KMDEBUG
	kmfGetInternalSystemMetrics(&SYS);
	njPrintC(NJM_LOCATION(14,1), "RENDER TO:"); njPrintD(NJM_LOCATION(24,1), SYS.RenderTimeOutCount,8);
	njPrintC(NJM_LOCATION(14,2), "VERTEX TO:"); njPrintD(NJM_LOCATION(24,2), SYS.VertexTimeOutCount,8);
	njPrintC(NJM_LOCATION(14,4), "OBJ OVERF:"); njPrintD(NJM_LOCATION(24,4), SYS.OBJOverflowCount  ,8);
	njPrintC(NJM_LOCATION(14,5), "ITP OVERF:"); njPrintD(NJM_LOCATION(24,5), SYS.ITPOverflowCount  ,8);
#endif
	
	if (PER0) {
		PER1++;
		PER2+=NOW/10;
		PER3 = syTmrCountToMicro(syTmrDiffCount(PER4, syTmrGetCount()))/10;
	} else {
		if (PER1!=0) {
			njPrintC(NJM_LOCATION(0,14), "SCORE");
			njPrintD(NJM_LOCATION(6,14), (PER1*1667-PER2)/PER1,5);
		}
	}
	njPrintC(NJM_LOCATION(0,10), "FRAME"); njPrintD(NJM_LOCATION(6,10), PER1    ,5);
	njPrintC(NJM_LOCATION(0,11), "TIME "); njPrintD(NJM_LOCATION(6,11), PER3/100,5);
	njPrintC(NJM_LOCATION(0,12), "BUSY "); njPrintD(NJM_LOCATION(6,12), PER2/100,5);
	
	NOW=syTmrCountToMicro(syTmrDiffCount(CT, syTmrGetCount()));
	if (CNT1<NOW) CNT1=NOW;
	BAK=NOW;
	
	return 0;
}
/*--------------------------------------------------------------------------*/
void UserExit(void)
{
	ixExit();
	msExit();
	ptExit();
	boExit();
	ltExit();
	sdExit();
}
/*--------------------------------------------------------------------------*/
long GetID(long grp,long ptn)
{
	u_long TEX=ixGetHeader(IXD_HEAD_TEXTBL);
	u_long PTN=ixGetHeader(IXD_HEAD_PTNTBL);
	PTNTBL *P;
	TEXTBL *T;
	
	PTN = ixGetAddress( PTN + grp*4 );
	P = (PTNTBL *)(PTN + sizeof(PTNTBL) * ptn);
	T = (TEXTBL *)(ixGetAddress( TEX + P->texno*4 ));
	
	return GETWORD(T,2);
}
/*--------------------------------------------------------------------------*/
