/*
	Ninja Sample
	
	COPYRIGHT (C) SEGA ENTERSRISES,LTD.

	Torus Sample

	1997.10.20
*/
#include <Ninjawin.h>

#define	OBJECT	object_torus_torus8_torus8
extern NJS_OBJECT OBJECT[];

NJS_TEXNAME	tex1[1];
NJS_TEXLIST	tlist1 = {tex1,1};

#define	NUM		10000

NJS_MATRIX	matrix[128];
NJS_VERTEX_BUF	vbuf[NUM];

NJS_TEXMEMLIST	tex[10];

NJS_VIEW	view;
NJS_LIGHT	light;

Sint32	xx,yy,zz = 0;
Sint32	xx1,yy1,zz1 = 0;
Sint32	xx2,yy2,zz2 = 0;
Sint32	xx3,yy3,zz3 = 0;

Float	dx,dy = 0.0f;

Int	i = 0;
Uint32	time;
long	poly,wpoly=0;
long	disp,wdisp=0;
long	frame,wframe=0;
long	count = 0;


NJS_ARGB	argb1,argb2,argb3,argb4,argb5;

NJS_PERIPHERAL *per;

void njUserInit(void)
{

	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer( 700000, 0, 10000, 0 );
	njInitMatrix( matrix, 128, 0 );
	njInit3D( vbuf, NUM );
	njInitView( &view );
	njCreateLight( &light, NJD_SPEC_DIR );
//	njCreateLight( &light, NJD_DIR_LIGHT|NJD_SIMPLE_LIGHT );
	njSetLightIntensity( &light, 1.0f, 2.0f, 0.5f );
	njSetView( &view );
	njInitTexture( tex, 10 );
	njSetTextureName(&tex1[0],"009.pvr",0,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
	njLoadTexture( &tlist1 );
	njSetTexture( &tlist1 );
	njSetTextureNum(0);
	njClipZ( -2.0f, -100.f );

	njControl3D( 
		NJD_CONTROL_3D_CONSTANT_ATTR | 
//		NJD_CONTROL_3D_NO_CLIP_CHECK |
		NJD_CONTROL_3D_OFFSET_MATERIAL 
		);
//	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV/* | NJD_FILTER_BILINEAR*/);
//	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_IGNORE_SPECULAR);
//	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_USE_FLAT);
	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_IGNORE_LIGHT);

	time = GetCurrentTime();

	njSetBackColor(0x00000000,0x00000000,0x000000FF);

	argb1.a = 0.f;
	argb1.r = -0.4f;
	argb1.g = 0.f;
	argb1.b = 0.f;

	argb2.a = 0.f;
	argb2.r = 0.f;
	argb2.g = -0.4f;
	argb2.b = 0.f;

	argb3.a = 0.f;
	argb3.r = -0.4f;
	argb3.g = -0.4f;
	argb3.b = 1.f;

	argb4.a = 0.f;
	argb4.r = 0.f;
	argb4.g = 0.f;
	argb4.b = 0.f;


}

Uint32	sx1,sx2=0;
Uint32	sy1,sy2=0;
Uint32	sz1,sz2=0;


#define	XXX	7.f
#define	YYY	4.f
#define	ZZZ	-30.f


Sint32 njUserMain(void)
{
	per = njGetPeripheral(NJD_PORT_SYSKEYBOARD);

	if(per->push & NJD_DGT_TA )
		njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV);
	if(per->push & NJD_DGT_TB )
		njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_IGNORE_LIGHT);
	if(per->push & NJD_DGT_TC )
		njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_USE_FLAT);
	if(per->push & NJD_DGT_TX )
		njSetConstantAttr( 0xffffffff, 0);
	if(per->push & NJD_DGT_TY )
		njSetConstantAttr( 0xffffffff, NJD_FLAG_IGNORE_LIGHT);
	if(per->push & NJD_DGT_TZ )
		njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_USE_FLAT | NJD_FLAG_IGNORE_SPECULAR);
	if(per->push & NJD_DGT_ST ) 
		dx = dy = 0.0f;


	if( per->on & NJD_DGT_KL )	dx -= 0.2f;
	if( per->on & NJD_DGT_KR )	dx += 0.2f;
	if( per->on & NJD_DGT_KU )	dy += 0.2f;
	if( per->on & NJD_DGT_KD )	dy -= 0.2f;

	njClearMatrix();
	njTranslate( NULL, XXX+dx, YYY+dy, ZZZ );
	njRotateXYZ( NULL, xx,yy,zz );
	njTranslate( NULL, -2.2f, 0.f, 0.f );

	njSetConstantMaterial( &argb1 );
	njFastDrawObject( OBJECT );

	njClearMatrix();
	njTranslate( NULL, XXX+dx, YYY+dy, ZZZ );
	njRotateXYZ( NULL, xx,yy,zz );
	njTranslate( NULL, 2.2f, 0.f, 0.f );
	njRotateX( NULL, 0x4000 );


	njSetConstantMaterial( &argb2 );
	njFastDrawObject( OBJECT );


	xx += 257;
	yy += 179;
	zz += 193;



	njClearMatrix();
	njTranslate( NULL, -XXX+dx, -YYY+dy, ZZZ );
	njRotateXYZ( NULL, yy,zz,xx );
	njTranslate( NULL, -2.2f, 0.f, 0.f );


	njSetConstantMaterial( &argb1 );
	njFastDrawObject( OBJECT );


	njClearMatrix();
	njTranslate( NULL, -XXX+dx, -YYY+dy, ZZZ );
	njRotateXYZ( NULL, yy,zz,xx );
	njTranslate( NULL, 2.2f, 0.f, 0.f );
	njRotateX( NULL, 0x4000 );

	njSetConstantMaterial( &argb4 );
	njFastDrawObject( OBJECT );


	xx += 257;
	yy += 179;
	zz += 193;


	njClearMatrix();
	njTranslate( NULL, -XXX+dx, YYY+dy, ZZZ );
	njRotateXYZ( NULL, zz,xx,yy );
	njTranslate( NULL, -2.2f, 0.f, 0.f );

	njSetConstantMaterial( &argb2 );
	njFastDrawObject( OBJECT );


	njClearMatrix();
	njTranslate( NULL, -XXX+dx, YYY+dy, ZZZ );
	njRotateXYZ( NULL, zz,xx,yy );
	njTranslate( NULL, 2.2f, 0.f, 0.f );
	njRotateX( NULL, 0x4000 );


	njSetConstantMaterial( &argb4 );
	njFastDrawObject( OBJECT );

	xx += 257;
	yy += 179;
	zz += 193;


	njClearMatrix();
	njTranslate( NULL, XXX+dx, -YYY+dy, ZZZ );
	njRotateXYZ( NULL, zz,yy,xx );
	njTranslate( NULL, -2.2f, 0.f, 0.f );

	njSetConstantMaterial( &argb3 );
	njFastDrawObject( OBJECT );


	njClearMatrix();
	njTranslate( NULL, XXX+dx, -YYY+dy, ZZZ );
	njRotateXYZ( NULL, zz,yy,xx );
	njTranslate( NULL, 2.2f, 0.f, 0.f );
	njRotateX( NULL, 0x4000 );

	njSetConstantMaterial( &argb4 );
	njFastDrawObject( OBJECT );

	xx += 257;
	yy += 179;
	zz += 193;


	wpoly += _nj_calc_polygon_;
	wdisp += _nj_draw_polygon_;
	wframe++;
	if( GetCurrentTime() > (time + 5000) ) {
		poly = wpoly;
		disp = wdisp;
		frame= wframe/5;
		wpoly = 0;
		wdisp = 0;
		wframe = 0;
		time = GetCurrentTime();
	}

	njPrintC(NJM_LOCATION(4,4),"Vertex");
	njPrintC(NJM_LOCATION(4,5),"Calc Poly");
	njPrintC(NJM_LOCATION(4,6),"Disp Poly");
	njPrintD(NJM_LOCATION(14,4),_nj_calc_vertex_,8);
	njPrintD(NJM_LOCATION(14,5),_nj_calc_polygon_,8);
	njPrintD(NJM_LOCATION(14,6),_nj_draw_polygon_,8);
	njPrintD(NJM_LOCATION(24,5),poly/5,8);
	njPrintD(NJM_LOCATION(24,6),disp/5,8);
	njPrintC(NJM_LOCATION(32,5),"/s");
	njPrintC(NJM_LOCATION(32,6),"/s");
	njPrintD(NJM_LOCATION(6,8),frame,3);
	njPrintC(NJM_LOCATION(10,8),"frame/s");

	return NJD_USER_CONTINUE;
}

void njUserExit(void)
{
	njExitTexture();
	njExitSystem();
}
