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

	Teapot Sample

	1997.10.20
	1997.11.07
*/

#include <Ninjawin.h>

extern NJS_OBJECT object_teapot_root[];

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


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

NJS_TEXMEMLIST	tex[10];

NJS_VIEW	view;
NJS_LIGHT	light;

Sint32	xx,yy,zz = 0;

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

NJS_PERIPHERAL *per,*per1;

void njUserInit(void)
{

	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer( 500000, 0, 500000, 0 );
	njInitMatrix( matrix, 128 , 1);
	njInit3D( vbuf, 4096 );
	njInitView( &view );
	njCreateLight( &light, NJD_SPEC_DIR );
	njSetLightIntensity( &light, 0.0f, 1.0f, 0.5f );
	njSetView( &view );

	njInitTexture( tex, 10 );
	njSetTexturePath("image\\");
	njSetTextureName(&tex1[0],"cafe128.pvr",1,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
	njLoadTexture( &tlist1 );
	njSetTexture( &tlist1 );

	njClipZ( -2.0f, -100.f );

	njControl3D( NJD_CONTROL_3D_CONSTANT_ATTR );
	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV|NJD_FILTER_BILINEAR);

	time = GetCurrentTime();
}

Sint32 njUserMain(void)
{

    static Int _flat = 0;
    static Int _spec = 1;

	per1 = njGetPeripheral(NJD_PORT_JOYSTICK1);
	per = njGetPeripheral(NJD_PORT_SYSKEYBOARD);

    if( per->push & NJD_DGT_TR ) {
        _flat = 1 - _flat;
	}
    if( per->push & NJD_DGT_TL ) {
        _spec = 1 - _spec;
	}
    if(_flat == 1){
        njPrintC(NJM_LOCATION(4,4),"Flat Shading");
        njPrintC(NJM_LOCATION(45,4),"Hit 'E' to get Gouraud Shading");
    	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_USE_FLAT|NJD_FILTER_BILINEAR);
    }
    else{
        njPrintC(NJM_LOCATION(4,4),"Gouraud Shading");
        njPrintC(NJM_LOCATION(45,4),"Hit 'E' to get Flat Shading");
		njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV|NJD_FILTER_BILINEAR);
    }
    if(_spec == 1){
        njPrintC(NJM_LOCATION(4,5),"Specular On");
        njPrintC(NJM_LOCATION(45,5),"Hit 'Q' to turn Off Specular");
        njSetLightIntensity( &light, 0.5f, 2.0f, 0.5f );
    }
    else{
        njPrintC(NJM_LOCATION(4,5),"Specular Off");
        njPrintC(NJM_LOCATION(45,5),"Hit 'Q' to turn On Specular");
        njSetLightIntensity( &light, 0.f, 2.f, 0.5f );
    }

	njSetAspect( 1.0f, 1.0f );

	njClearMatrix();
	njTranslate( NULL, 0.f, 0.f, -4.5f );
	njRotateXYZ( NULL, xx,yy,zz );
	njDrawObject( object_teapot_root );
	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,50),"Vertex");
	njPrintC(NJM_LOCATION(4,51),"Calc Poly");
	njPrintC(NJM_LOCATION(4,52),"Disp Poly");
	njPrintD(NJM_LOCATION(14,50),_nj_calc_vertex_,8);
	njPrintD(NJM_LOCATION(14,51),_nj_calc_polygon_,8);
	njPrintD(NJM_LOCATION(14,52),_nj_draw_polygon_,8);
	njPrintD(NJM_LOCATION(24,51),poly/5,8);
	njPrintD(NJM_LOCATION(24,52),disp/5,8);
	njPrintC(NJM_LOCATION(32,51),"/s");
	njPrintC(NJM_LOCATION(32,52),"/s");
	njPrintD(NJM_LOCATION(4,54),frame,3);
	njPrintC(NJM_LOCATION(9,54),"frame/s");
	njPrintD(NJM_LOCATION(24,54),count++,8);

	return NJD_USER_CONTINUE;
}

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