/*---------------------------------------------------------------------------
  NINJA2 Sample CompactShape
  COPYRIGHT (C) SEGA ENTERPRISES, LTD.
---------------------------------------------------------------------------*/

#include	<shinobi.h>
#include	"Nj.h"

#define	OBJ_PT		object_4_pt_pt
#define	OBJ_GR		object_test_grid2_grid2
#define OBJ_SE		object_test_mizu_mizu
#define	OBJ_SK		object_test_sky_sky
#define	OBJ_BD		object_test2_Bip01_Bip01
#define OBJ_KM		object_test2_kami_kami

#define	TEX_PT		texlist_4_pt
#define	TEX_GR		texlist_test_grid2
#define	TEX_SK		texlist_test_sky
#define	TEX_SE		texlist_test_mizu
#define	TEX_BD		texlist_test2_Bip01
#define	TEX_KM		texlist_test2_kami

#define	MOT_BD	 	motion_test2_Bip01
#define	MOT_KM	 	motion_test2_kami
#define SHPMOT_BD	cshape_motion_test2_Bip01
#define SHPLST		cshapelist_test2_Bip01
#define SHAPE_KEY_NUM	31

#define CAM			cam_Camera02
#define CAM3		cam_Camera03
#define CAMMOT_3	motion_cam_Camera03

extern	NJS_CNK_OBJECT	OBJ_PT[];
extern	NJS_CNK_OBJECT	OBJ_GR[];
extern	NJS_CNK_OBJECT	OBJ_SK[];
extern	NJS_CNK_OBJECT	OBJ_SE[];
extern	NJS_CNK_OBJECT	OBJ_BD[];
extern	NJS_CNK_OBJECT	OBJ_KM[];

extern  NJS_TEXLIST TEX_PT[];
extern  NJS_TEXLIST TEX_GR[];
extern  NJS_TEXLIST TEX_SK[];
extern  NJS_TEXLIST TEX_SE[];
extern  NJS_TEXLIST TEX_BD[];
extern  NJS_TEXLIST TEX_KM[];

extern NJS_CAMERA	CAM[];
extern NJS_CAMERA	CAM3[];

extern	NJS_MOTION	MOT_BD[];
extern	NJS_MOTION	MOT_KM[];
extern	NJS_MOTION	SHPMOT_BD[];
extern	NJS_MOTION	CAMMOT_3[];

extern	NJS_CSHAPELIST SHPLST[];

static Float	*pShapeBuf;
static Float	ff = 0.f;
static int		incflg = 0;
static int		stopflg = 0; 
static int		camflg = 0;

static int		typeoffset=0;

static	NJS_VECTOR	light = { 0.577f, -0.577f, -0.577f };

void FogDraw(void);




Sint32	UserInit(void)
{
	switch(syVideoGetCableType()){
		case SYD_VIDEO_CABLE_TYPE_VGA:
			njPrintSize(8);
			typeoffset=25;
			break;
		default:
			njPrintSize(16);
			typeoffset=0;
			break;
	}

	njSetBackColor( 0x004444cc, 0x004444cc, 0x004444cc );
	
	if ( ( pShapeBuf = syMalloc( 16 * SHAPE_KEY_NUM )) == NULL ) {
		return	USER_EXIT;
	}

	njInitShape( pShapeBuf, SHAPE_KEY_NUM);
	njSetShapeList( SHPLST );

	njInitCameraEx();

	njCnkSetSimpleMultiLight(2);

	njCnkSetSimpleMultiLightVectorEx(NJD_CNK_SIMPLE_MULTILIGHT_1, light.x, light.y, light.z);
	njCnkSetSimpleMultiLightVectorEx(NJD_CNK_SIMPLE_MULTILIGHT_2,  -light.x, -light.y, -light.z);
	njCnkSetSimpleMultiLightColor(NJD_CNK_SIMPLE_MULTILIGHT_1, 1.0f, 0.7f, 0.6f);
	njCnkSetSimpleMultiLightColor(NJD_CNK_SIMPLE_MULTILIGHT_2, 0.2f, 0.1f, 0.0f);
	njCnkSetSimpleMultiAmbient(0.4f, 0.2f, 0.2f);
	njCnkSetSimpleMultiLightSwitch(NJD_CNK_SIMPLE_MULTILIGHT_1, NJD_LIGHT_ON);
	njCnkSetSimpleMultiLightSwitch(NJD_CNK_SIMPLE_MULTILIGHT_2, NJD_LIGHT_ON);

	njCnkSetEasyLightColor( 0.7f, 0.35f, 0.25f );
	njCnkSetEasyLightIntensity( 0.7f, 0.0f );
	njCnkSetEasyLight( light.x, light.y, light.z );

	if(nuLoadTexture( TEX_PT ) < 0){
		return USER_EXIT;
	}
	
	if(	nuLoadTexture( TEX_GR ) < 0){
		return USER_EXIT;
	}
	if(	nuLoadTexture( TEX_SK ) < 0){
		return USER_EXIT;
	}
	if(	nuLoadTexture( TEX_SE ) < 0){
		return USER_EXIT;
	}
	if(	nuLoadTexture( TEX_BD ) < 0){
		return USER_EXIT;
	}
	if(	nuLoadTexture( TEX_KM ) < 0){
		return USER_EXIT;
	}

	FogDraw();
	
	return USER_CONTINUE;
}

void FogDraw(void){ 
	NJS_FOG_TABLE fog;
	njSetFogColor(0xffff8866);
	njSetFogDensity(0x8010);
	njGenerateFogTable3(fog,-150.f,-2000.f);
	njSetFogTable(fog);
	njFogEnable();
}


Sint32	UserExit(void)
{
	return USER_CONTINUE;
}

Sint32	UserMain(void)
{
#if DEBUG
	if(stopflg==0){
		njPrintC(NJM_LOCATION(2,22+typeoffset),"[A]  Stop");
	}
	else{
		njPrintC(NJM_LOCATION(2,22+typeoffset),"[A]  Start");
	}
	njPrintC(NJM_LOCATION(2,23+typeoffset),"[B]  Change Camera");
	if(camflg==0){
		njPrintC(NJM_LOCATION(2,24+typeoffset),"[AnalogUDLR]  Light");
	}
	njPrintC(NJM_LOCATION(2,26+typeoffset),"NINJA2.1 Sample");
	njPrintC(NJM_LOCATION(2,27+typeoffset),"CompactShape+Envelope+Camera");
#endif

	if(Per->press&PDD_DGT_TA){
		stopflg^=1;
	}

	if(Per->press&PDD_DGT_TB){
		camflg^=1;
		if(camflg==0){
			njCnkSetSimpleMultiLightVectorEx(NJD_CNK_SIMPLE_MULTILIGHT_1, light.x, light.y, light.z);
			njCnkSetSimpleMultiLightVectorEx(NJD_CNK_SIMPLE_MULTILIGHT_2,  -light.x, -light.y, -light.z);
			njCnkSetSimpleMultiLightColor(NJD_CNK_SIMPLE_MULTILIGHT_1, 1.0f, 0.7f, 0.6f);
		 	njCnkSetSimpleMultiLightColor(NJD_CNK_SIMPLE_MULTILIGHT_2, 0.2f, 0.1f, 0.0f);
		 	njCnkSetSimpleMultiAmbient(0.4f, 0.2f, 0.2f);
			njCnkSetEasyLight( light.x, light.y, light.z );
		}
		else{
			njCnkSetSimpleMultiLightVectorEx(NJD_CNK_SIMPLE_MULTILIGHT_1, 0, 0, -1);
			njCnkSetSimpleMultiLightVectorEx(NJD_CNK_SIMPLE_MULTILIGHT_2, 0, 0, 1);
			njCnkSetSimpleMultiLightColor(NJD_CNK_SIMPLE_MULTILIGHT_1, 0.9f, 0.9f, 0.9f);
			njCnkSetSimpleMultiLightColor(NJD_CNK_SIMPLE_MULTILIGHT_2, 0.2f, 0.2f, 0.2f);
			njCnkSetSimpleMultiAmbient(0.4f, 0.4f, 0.4f);
			njCnkSetSimpleMultiLightMatrices();
			njCnkSetEasyLight( 0, 0, -1 );
		}
	}

	njPushMatrixEx();

	if(camflg==0){
		
		njSetCamera(CAM);
		njPushMatrixEx();
		njRotateXYZ( NULL, Per->y1*120+12360,-Per->x1*120-8880,0 );
		njCnkSetSimpleMultiLightMatrices();
		njPopMatrixEx();

		if(stopflg==0){
			if(incflg==1){
				if(OBJ_SE->pos[1] < 4.f){
					OBJ_SE->pos[1]+=0.04f;
				}else{
					incflg^=1;
				}
			}else{
				if(OBJ_SE->pos[1]>-10.f){
					OBJ_SE->pos[1]-=0.04f;
				}else{
					incflg^=1;
				}
			}
		}
		
		njSetTexture( TEX_PT );
		njCnkEasyDrawObject( OBJ_PT );
		njSetTexture( TEX_GR );
		njCnkEasyDrawObject( OBJ_GR );
		njSetTexture( TEX_SE );
		njCnkEasyDrawObject( OBJ_SE );
		njSetTexture( TEX_SK );
		njCnkEasyDrawObject( OBJ_SK );
	}else{
		njCameraMotion( CAM3, CAMMOT_3, ff ) ;

	}

	njSetTexture( TEX_BD );
	njCnkSimpleMultiDrawShapeMotion( OBJ_BD, MOT_BD, SHPMOT_BD, ff );

	njSetTexture( TEX_KM );
	njCnkEasyDrawMotion( OBJ_KM, MOT_KM,ff );
	njPopMatrixEx();
	
	if(stopflg==0){
		ff  +=0.5f;
    }
    
    if ( ff >= MOT_BD->nbFrame - 1 ) {
        ff = 0.0f;
    }

	return USER_CONTINUE;
}
