#include <Ninjawin.h>
#include <Nindows.h>

#define	VNUM	8192
NJS_MATRIX	matrix[128];
NJS_SPRITE	sprite;
NJS_TEXMEMLIST	tex[256];
NJS_VIEW	view;
NJS_VERTEX_BUF	vbuf[VNUM];
NJS_LIGHT	light;

#define	OBJECT	object_map_grid_grid
#define	TEXLIST	texlist_map_grid
#define	OBJECT2	object_segalogo_s1_s1

extern	NJS_OBJECT	OBJECT[];
extern	NJS_TEXLIST	TEXLIST;
extern	NJS_OBJECT	OBJECT2[];

void njUserInit(void)
{
	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer( 200000, 100000, 100000, 00000 );
	njInit3D( vbuf, VNUM );
	njInitMatrix( matrix, 128, 1 );
	njInitView( &view );
//	njCreateLight( &light, NJD_SPEC_DIR );
	njCreateLight( &light, NJD_DIR_LIGHT );
	njRotateLightX( &light, -0x4000 );
	njSetView( &view );
	njSetLightIntensity( &light, 1.0f, 2.0f, 0.5f );

	njInitTexture( tex, 256 );
	nwInitSystem(256);			//nwInitSystem function should be put immediately after njInitTexture
	njLoadTexture( &TEXLIST );
	njSetTexture( &TEXLIST );

		njControl3D(
			NJD_CONTROL_3D_MODEL_CLIP |
			NJD_CONTROL_3D_CONSTANT_TEXTURE_MATERIAL |
			NJD_CONTROL_3D_CONSTANT_ATTR );

		njSetConstantAttr( ~(0), NJD_FILTER_BILINEAR);

	njSetBackColor( 0xff000000,0xff000000,0xff0000ff );

	njClipZ( -1.f, -30000.f );
//	njSetPerspective( 0x6000 );
}

NJS_PERIPHERAL *per;
Sint32	rx,ry,rz = 0;
Float	x,y,z=0.f;
Sint32	rx2,ry2,rz2 = 0;
Float	x2,y2,z2=0.f;

Int	sw=0;

Sint32 njUserMain(void)
{

	per = njGetPeripheral(NJD_PORT_SYSKEYBOARD);

	njClearMatrix();

	njTranslate( NULL, x, y-500.f, z - 1000.f );
	njRotateXYZ( NULL, rx, ry, rz );
	njShadowObject( OBJECT, 0xffffffff,0xff4040ff ); /* this function not support yet */

	njClearMatrix();

	njTranslate( NULL, x2+600.f, y2+500.f, z2 -900.f );
	njRotateXYZ( NULL, rx2, ry2, rz2 );
	njScale(NULL,1.0f,1.0f,10.f);
	if(sw)	njSetModObject( OBJECT2 );
	else njFastDrawObject( OBJECT2 );

	if(per->on & NJD_DGT_TX) {
		if(per->on & NJD_DGT_KU) z += 20.f;
		if(per->on & NJD_DGT_KD) z -= 20.f;
		if(per->on & NJD_DGT_KL) x -= 20.f;
		if(per->on & NJD_DGT_KR) x += 20.f;
	}
	else if(per->on & NJD_DGT_TY) {
		if(per->on & NJD_DGT_KU) y += 20.f;
		if(per->on & NJD_DGT_KD) y -= 20.f;
		if(per->on & NJD_DGT_KL) x -= 20.f;
		if(per->on & NJD_DGT_KR) x += 20.f;
	}
	else if(per->on & NJD_DGT_TZ) {
		if(per->on & NJD_DGT_KU) rx += NJM_DEG_ANG(1.0f);
		if(per->on & NJD_DGT_KD) rx -= NJM_DEG_ANG(1.0f);
		if(per->on & NJD_DGT_KL) ry -= NJM_DEG_ANG(1.0f);
		if(per->on & NJD_DGT_KR) ry += NJM_DEG_ANG(1.0f);
	}

	if(per->push & NJD_DGT_ST) sw = 1-sw;

	if(per->on & NJD_DGT_TA) {
		if(per->on & NJD_DGT_KU) z2 += 10.f;
		if(per->on & NJD_DGT_KD) z2 -= 10.f;
	}
	else if(per->on & NJD_DGT_TB) {
		if(per->on & NJD_DGT_KU) y2 += 10.f;
		if(per->on & NJD_DGT_KD) y2 -= 10.f;
		if(per->on & NJD_DGT_KL) x2 -= 10.f;
		if(per->on & NJD_DGT_KR) x2 += 10.f;
	}
	else if(per->on & NJD_DGT_TC) {
		if(per->on & NJD_DGT_KU) rx2 += NJM_DEG_ANG(1.0f);
		if(per->on & NJD_DGT_KD) rx2 -= NJM_DEG_ANG(1.0f);
		if(per->on & NJD_DGT_KL) ry2 -= NJM_DEG_ANG(1.0f);
		if(per->on & NJD_DGT_KR) ry2 += NJM_DEG_ANG(1.0f);
	}

	return nwExecute();
}

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