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

	Sample

	1997.10.20
	1997.11.07
	1997.12.10
*/
#ifndef	__SET4__
#include <Ninjawin.h>
#else
#include <shinobi.h>
#endif

#include <Nindows.h>

#define	ACTION	action_win2
#define	TEXLIST	texlist_man_run_uv_man_2
extern	NJS_ACTION	ACTION[];
NJS_TEXNAME	tex1[2];
NJS_TEXLIST	tlist1 = {tex1,2};

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

NJS_TEXMEMLIST	tex[10];

NJS_VIEW	view;
NJS_LIGHT	light;

Sint32	xx,yy,zz = 0;

Float	ff = 0.f;
Int	sw=0;

NJS_PERIPHERAL *per,*per1;
NJS_TEXTURE_VTX	poly[4];

void njUserInit(void)
{	
#ifndef	__SET4__
	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1 );
#else
	sbInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1 );
	njInitTextureBuffer((Sint8 *)vbuf,0x40800);
#endif

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

	njInitTexture( tex, 10 );
#ifndef	__SET4__
	nwInitSystem(256);			/*nwInitSystem function should be put immediately after njInitTexture	*/
#else
	nwInitSystem(256, NJD_PORT_A0);			/*nwInitSystem function should be put immediately after njInitTexture	*/
#endif

	njSetTextureName(&tex1[0],"cafe128.pvr",1,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
	/* setting to use frame buffer texture */
	njSetTextureName(&tex1[1],NULL,2,NJD_TEXATTR_TYPE_FRAMEBUFFER|NJD_TEXATTR_GLOBALINDEX);
	njLoadTexture( &tlist1 );
	njSetTexture( &tlist1 );
	
	njSetBackColor( 0xff000000,0xff000000,0xff000000 );

	njControl3D( NJD_CONTROL_3D_CONSTANT_ATTR );
    
	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_USE_FLAT|NJD_FILTER_BILINEAR);
/*	njSetConstantAttr( ~NJD_FLAG_USE_TEXTURE, 0);	*/

	njClipZ( -5.0f, -1000.f );

	poly[0].x = 50;
	poly[0].y = 110;
	poly[0].z = 0.0011f;
	poly[0].u = 0.001f;
	poly[0].v = 0.047875f;
	poly[0].col = 0xAFFFFFFF;
	
	poly[1].x = 50;
	poly[1].y = 340;
	poly[1].z = 0.0011f;
	poly[1].u = 0.001f;
	poly[1].v = 0.46875f;
	poly[1].col = 0xAFFFFFFF;
	
	poly[2].x = 370;
	poly[2].y = 110;
	poly[2].z = 0.0011f;
	poly[2].u = 0.625f;
	poly[2].v = 0.047875f;
	poly[2].col = 0xAFFFFFFF;

	poly[3].x = 370;
	poly[3].y = 340;
	poly[3].z = 0.0011f;
	poly[3].u = 0.625f;
	poly[3].v = 0.46875f;
	poly[3].col = 0xAFFFFFFF;

	/* setting to use frame buffer texture(VGA 640) */
	njSetRenderWidth(640);
}

Sint32 njUserMain(void)
{
    static Int _flat = 0;
    static Int _spec = 1;
	static Int count = 0;
	static Int ang_y = 0;

#ifndef	__SET4__
	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, 1.0f, 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 );
    }
#else
	per = njGetPeripheral(NJD_PORT_A0);
	
    if( per->press & NJD_DGT_TR ) {
        _flat = 1 - _flat;
	}
    if( per->press & NJD_DGT_TL ) {
        _spec = 1 - _spec;
	}
    if(_flat == 1){
        njPrintC(NJM_LOCATION(4,4),"Flat Shading");
        njPrintC(NJM_LOCATION(45,4),"Hit RIGHT TRIGGER 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 RIGHT TRIGGER 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 LEFT TRIGGER to turn Off Specular");
        njSetLightIntensity( &light, 1.0f, 2.0f, 0.5f );
    }
    else{
        njPrintC(NJM_LOCATION(4,5),"Specular Off");
        njPrintC(NJM_LOCATION(45,5),"Hit LEFT TRIGGER to turn On Specular");
        njSetLightIntensity( &light, 0.f, 2.f, 0.5f );
    }
#endif
/*	njSetConstantAttr( ~NJD_FLAG_USE_TEXTURE, 0);	*/

	njClearMatrix();
	njTranslate( NULL, 0.f, -10.f, -50.f );
	njRotateY( NULL, 0x8000 + NJM_DEG_ANG(ang_y));
	njAction( ACTION, ff );

	njDrawTexture(poly,4,2,TRUE);

	ff += 1.0f;
	if( ff >= (ACTION->motion->nbFrame-1) ) {
	    ff=0.0f;
	}
#ifndef	__SET4__
	if((per->on & NJD_DGT_ST)||(per1->on & NJD_DGT_ST)){
#else
	if(per->on & NJD_DGT_ST){
#endif
		return NJD_USER_EXIT;
	}

	ang_y += 1;

	return nwExecute();

/*	return NJD_USER_CONTINUE;	*/
}

void njUserExit(void)
{
	nwExitSystem();
	njExitTexture();
#ifndef	__SET4__
	njExitSystem();
#else
	sbExitSystem();
#endif
}
