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

	Perticle Sample 1

	1999.4.1
*/

#include <shinobi.h>


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

NJS_POINT3	*p;

NJS_VIEW	view;

#define	RRR		600
#define	QQQ		1200

#define	ZZZZ	-2500.f
#define	NNNN	5000

static	Float	HHH = 1.0f;

void njUserInit(void)
{
	NJS_POINT3		*pp;
	NJS_POINT3		p0 = {0,0,1};
	register	Float	ff;
	register	int		i,j,k,l;

	njSetBorderColor( 0x00000000 );

	njSetTextureMemorySize( 10000 );

/*	sbInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1 );/* */
	sbInitSystem( NJD_RESOLUTION_640x480_NTSCNI_FF, NJD_FRAMEBUFFER_MODE_RGB565, 1 );/* */

/*	njSetAspect( 0.5f, 0.5f );*/

	njInitVertexBuffer( -1500000, 0, 100000, 0, 0 );
	njInitPrint(NULL,0,0);
	njPrintSize(12);
	njInitMatrix( matrix, 8 , 0);
	njInit3D( vbuf, 4096 );
	njInitView( &view );
	njSetView( &view );

	njSetBackColor(0x00000000,0x00000000,0x000000FF);

	p = (NJS_POINT3 *)syMalloc(sizeof (NJS_POINT3) * NNNN);
	pp = p;
	for( i=0; i<NNNN; i++ ) {
		njClearMatrix();
		njRotateXYZ( NULL, njRandom()*0xffff,njRandom()*0xffff,njRandom()*0xffff);
		njScale( NULL, njRandom()+0.1,njRandom()+0.1,njRandom()+0.1 );
		njCalcPoint( NULL, &p0, pp );
		pp++;
	}

}


static	Sint32	x=0,y=0,z=0,m=0;
static	Sint32	x1=0,y1=0,z1=0,m1=0;
static	Sint32	x2=0,y2=0,z2=0,m2=0;
static	Sint32	x3=0,y3=0,z3=0,m3=0;


Sint32 njUserMain(void)
{
	NJS_POINT3		poly[4];
	register	int		i,j,k,l;
	static	Angle	ang = 0;
	register	Float	ff;
	register	Float	ffx,ffy,ffz;
	register	Uint32 c;
	Float		f[20];
	Float		*pf;
	PDS_PERIPHERAL *per;
	NJS_COLOR	col1,col2,col3,col;
	
	per = pdGetPeripheral(PDD_PORT_A0);

	col1.argb.a = 0xff;
	col1.argb.r = njSin(x1)*100+100;
	col1.argb.g = njSin(y1)*100+100;
	col1.argb.b = njSin(z1)*100+100;
	col2.argb.a = 0xff;
	col2.argb.r = njSin(x2)*100+100;
	col2.argb.g = njSin(y2)*100+100;
	col2.argb.b = njSin(z2)*100+100;
	col3.argb.a = 0xff;
	col3.argb.r = njSin(x3)*100+100;
	col3.argb.g = njSin(y3)*100+100;
	col3.argb.b = njSin(z3)*100+100;

	col.argb.a = 0xff;
	col.argb.r = njSin(x)*100+100;
	col.argb.g = njSin(y)*100+100;
	col.argb.b = njSin(z)*100+100;

	/* Block 1 */
	njClearMatrix();
	njTranslate( NULL, 0.f, 0.f, ZZZZ );
	njRotateXYZ( NULL, x1,y1,z1);
	njPtclPolygonStart( col1.color );
	ffx = njSin(m1) * RRR + QQQ;
	ffy = njSin(m1) * RRR + QQQ;
	ffz = njSin(m1) * RRR + QQQ;
	njScale( NULL, ffx,ffy,ffz );
	njPtclDrawPolygon( p, NNNN, HHH );
	njPtclSpriteEnd();

	/* Block 2 */
	njClearMatrix();
	njTranslate( NULL, 0.f, 0.f, ZZZZ );
	njRotateXYZ( NULL, x2,y2,z2);
	njPtclPolygonStart( col2.color );
	ffx = njSin(m2) * RRR + QQQ;
	ffy = njSin(m2) * RRR + QQQ;
	ffz = njSin(m2) * RRR + QQQ;
	njScale( NULL, ffx,ffy,ffz );
	njPtclDrawPolygon( p, NNNN, HHH );
	njPtclSpriteEnd();

	/* Block 3 */
	njClearMatrix();
	njTranslate( NULL, 0.f, 0.f, ZZZZ );
	njRotateXYZ( NULL, x3,y3,z3);
	njPtclPolygonStart( col3.color );
	ffx = njSin(m3) * RRR + QQQ;
	ffy = njSin(m3) * RRR + QQQ;
	ffz = njSin(m3) * RRR + QQQ;
	njScale( NULL, ffx,ffy,ffz );
	njPtclDrawPolygon( p, NNNN, HHH );
	njPtclSpriteEnd();

	/* Block 4 */
	njClearMatrix();
	njTranslate( NULL, 0.f, 0.f, ZZZZ );
	njRotateXYZ( NULL, x,y,z);
	njPtclPolygonStart( col.color );
	ffx = njSin(m) * RRR + QQQ;
	ffy = njSin(m) * RRR + QQQ;
	ffz = njSin(m) * RRR + QQQ;
	njScale( NULL, ffx,ffy,ffz );
	njPtclDrawPolygon( p, NNNN, HHH );
	njPtclSpriteEnd();

	njSetBackColor( col1.color,col2.color,col3.color );

	x += 211;
	y += 137;
	z += 197;
	m += 271;

	m1 += 171;
	m2 += 231;
	m3 += 199;
	
	x1 += 315;
	y1 += 217;
	z1 += 129;

	x2 += 109;
	y2 += 193;
	z2 += 247;

	x3 += 139;
	y3 += 111;
	z3 += 213;

	njPrintC(NJM_LOCATION(4,4), "A BUTTON: UP SIZING    B BUTTON : DOWN SIZING");
	if( per->press & PDD_DGT_TA ) {
		HHH += 0.5f;
	} 
	else if( per->press & PDD_DGT_TB ) {
		HHH -= 0.5f;
	}
	
	if( HHH < 0.5f ) HHH = 0.5f;
	if( HHH > 5.0f ) HHH = 5.0f;
	njPrintC(NJM_LOCATION(4,4), "A BUTTON: UP SIZING    B BUTTON : DOWN SIZING");
	njPrintF(NJM_LOCATION(4,6), HHH, 1);

	return NJD_USER_CONTINUE;
}

void njUserExit(void)
{
	njExitPrint();
	sbExitSystem();
}
