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

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

#define	OBJECT	 object_chie_Torus_Knot01_Torus_Knot01

extern	NJS_CNK_OBJECT	OBJECT[];


/*** Variables ***/

/* プレイ履歴ライブラリ用ワーク */
/* Work area for Play log library */
Uint32 Work1[0x10000L / sizeof(Uint32)];
Uint32 Work2[60 / sizeof(Uint32)];
Uint32 Work3[0x10000L / sizeof(Uint32)];
Uint32 Work4[0x8000L / sizeof(Uint32)];

Sint32  scene;
Sint32  vcnt;
Sint32  fade;
Sint32  cary;
Sint32  est;
Sint32  net_f;

#define SCENE_TIME  (60*60*10)  /* 32400 */
#define WRITE_TIME  ((SCENE_TIME)/3)

/* Model */
static	Float	zzz = -400.f;
static	Float	xxx = 0.f;
static	Float	yyy = 0.f;

static	Sint32	xx = 0;
static	Sint32	yy = 0;
static	Sint32	zz = 0;

/*---------------------------------------------------------------------------
		初期化
		initialize
---------------------------------------------------------------------------*/
Sint32	UserInit(void)
{
	Sint32 i;

	syRtcInit();

	/* To read the per-info correctly */
	/* (Insert this sequence before "ufInitSystem" ) */
	for ( i = 0; i < 3; i++ ){
		njWaitVBlank();
		pdExecPeripheralServer();
	}

	/* Init the Play log Library */
	ufInitSystem((Uint8*)Work1, (Uint8*)Work2, NULL, NULL);
	ufAutoSynch(0);

	/* Init Variables */
	scene = 0;
	vcnt = 0;
	fade = 255;
	cary = 0;
	est = 0;
	net_f = 0;

	{
		static UFS_AllPlayHistory plyhst;
		
		if (ufReadAllHead(NULL, &plyhst) == TRUE) {
			if (plyhst.GameEst != 0xff) est = plyhst.GameEst;
			if (plyhst.GameCarry != 0xff) cary = plyhst.GameCarry;
		}
	}

	return USER_CONTINUE;
}

/* Sub functions */
void DrawScreenMask(Sint32 step)
{
	static NJS_POLYGON_VTX poly[4];
	Sint32 type;
	
	step &= 0xff;
	
	poly[0].x = 0.0f;
	poly[0].y = 0.0f;
	poly[0].z = 65535.0f;
	poly[0].col = (step << 24) + 0x0;
	
	poly[1].x = 0.0f;
	poly[1].y = 480.f;
	poly[1].z = 65535.0f;
	poly[1].col = (step << 24) + 0x0;
	
	poly[2].x = 640.f;
	poly[2].y = 0.0f;
	poly[2].z = 65535.0f;
	poly[2].col = (step << 24) + 0x0;
	
	poly[3].x = 640.f;
	poly[3].y = 480.0f;
	poly[3].z = 65535.0f;
	poly[3].col = (step << 24) + 0x0;
	
	if (step == 255) type = FALSE;
	else type = TRUE;
	
	njDrawPolygon( poly, 4, type );
}

Sint32 WritePlayTime(void)
{
	ufChangeWorkBuffer( NULL,  NULL, Work3, Work4);
	ufPlayTime(UFD_PLAY, est, cary, NULL);
	
	if (cary >= 254) cary = 0;
	else cary++;
	
	est = (rand() & 0xff);
	if (est == 255) est = 0;
}

/*---------------------------------------------------------------------------
		終了処理
		exit
---------------------------------------------------------------------------*/
Sint32	UserExit(void)
{
	/* write PlayTime before Exit */
	WritePlayTime();
	ufSynch();
	return 0;
}

/*---------------------------------------------------------------------------
		メイン
		main
---------------------------------------------------------------------------*/
Sint32	UserMain(void)
{
	/* Draw Torus */
	njPushMatrixEx();	

	njTranslate( NULL, xxx, yyy, zzz );
	njRotateXYZ( NULL, xx,yy,zz );

	njCnkWireDrawObject( OBJECT );

	njPopMatrixEx();

	if( Per->on & PDD_DGT_TL ) zzz -= 10;
	if( Per->on & PDD_DGT_TR ) zzz += 10;

	if ( scene == 0 ){
		xx += 219;
		yy += 197;
		zz += 359;
	} else {
		xx += 219 * 3;
		yy += 197 * 3;
	}

	syRtcExecServer();
	
	njPrintC(NJM_LOCATION(2,3), "Play log Library Test");
	vcnt++;
	
	njPrint(NJM_LOCATION(2,5), "EST : %d", est);
	njPrint(NJM_LOCATION(2,6), "CARY: %d", cary);
	njPrint(NJM_LOCATION(2,7), "NET : %s", (net_f == ON) ? "ON" : "OFF");
	
	if (Per->press & PDD_DGT_TX) {
		if (net_f == OFF) {
			SYS_RTC_DATE date;
			Uint32 cnt;
			
			/* get a substitute of network time */
			syRtcGetDate( &date );
			syRtcDateToCount( &date, &cnt );
			
			ufConnect(cnt);
		} else {
			ufDisConnect();
		}
		
		net_f ^= 1;
	}

	switch (scene) {
	case 0:
		/* scene 0 */
		njSetBackColor(0x00000060,0x00000060,0x00000060);

		if (fade < 255) DrawScreenMask(255 - fade);

		/* Write PlayTime 3 times per scene */
		if ((vcnt % WRITE_TIME) == WRITE_TIME - 1) WritePlayTime();
		
		if (vcnt >= SCENE_TIME) {
			if (--fade <= 0) {
				/* change the scene */
				scene = 1;
				vcnt = 0;
				fade = 255;
				
				WritePlayTime();
				ufSynch();
			}
		}
		break;
	
	case 1:
	default:
		/* scene 1 */
		njSetBackColor(0x00600000,0x00600000,0x00600000);

		if (fade < 255) DrawScreenMask(255 - fade);

		/* Write PlayTime 3 times per scene */
		if ((vcnt % WRITE_TIME) == WRITE_TIME - 1) WritePlayTime();

		if (vcnt >= SCENE_TIME) {
			if (--fade <= 0) {
				/* change the scene */
				scene = 0;
				vcnt = 0;
				fade = 255;
				
				WritePlayTime();
				ufSynch();
			}
		}
		break;
	}

	return USER_CONTINUE;
}
/*-------------------------------------------------------------------------*/
