#include <Nindows.h>

NJS_MATRIX	matrix[16];
NJS_SPRITE	sprite;
NJS_TEXMEMLIST	tex[32];
NJS_VIEW	view;

static	NJS_TEXNAME	texname[] = {
	{"tama2.pvr", NJD_TEXATTR_GLOBALINDEX, 0},
};

static	NJS_TEXLIST	texlist = { texname, 1 };

static	NJS_TEXANIM	anim[] = {
	{ 128,128,				// Sprite  sizeiirrelevant to texture size
	   64, 64,				// Sprite center
	    0,  0,				// Upper left UVi0 thru 255 irrelevant to texture size
	  255,255,				// Bottom right UVi0 thru 255 irrelevant to texture size
	    0,					// texture number  (Currently 
	    0,					// attribute (Currently unsupportedj
	},
};

#define	XXX	700

void njUserInit(void)
{
	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer( 100000, 0, 300000, 0 );
	njInitMatrix( matrix, 16,0 );
	njInitView( &view );
	njSetView( &view );

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

	sprite.tlist = &texlist;
	sprite.tanim = anim;
	sprite.ang = 0;
	sprite.sx = 1.0f;
	sprite.sy = 1.0f;
	sprite.p.x = 0.f;
	sprite.p.y = 0.f;
	sprite.p.z = 0.f;


//	njTextureFilterMode( NJD_TEXTUREFILTER_BILINEAR );
//	njSuperSampleMode( ON );
	njSetBackColor( 0xff000000,0xff000000,0xff0000ff );

	njClipZ( -100.f, -30000.f );
}

Sint32	x,y,z,m;
Sint32	r1,g1,b1,r2,g2,b2,r3,g3,b3=0;
Sint32 njUserMain(void)
{
	Int	xx,yy,zz;
	Float mm;
	NJS_ARGB	argb;
	NJS_COLOR	col1,col2,col3;

	njClearMatrix();

	njTranslate( NULL, 0.f, 0.f, -3000.f );
	njRotateXYZ( NULL, x,y,z);

	mm = njSin(m) * XXX + XXX+128;
	for(xx=0;xx<8;xx++) {
		for(yy=0;yy<8;yy++) {
			for(zz=0;zz<8;zz++) {
				argb.a = 1.0f;
				argb.r = (float)(xx / 7.f);
				argb.g = (float)(yy / 7.f);
				argb.b = (float)(zz / 7.f);
				njSetConstantMaterial( &argb );
				sprite.p.x = (float)(xx*mm - (mm*3.5));
				sprite.p.y = (float)(yy*mm - (mm*3.5));
				sprite.p.z = (float)(zz*mm - (mm*3.5));
				njDrawSprite3D( &sprite, 0, NJD_SPRITE_ANGLE | NJD_SPRITE_SCALE | NJD_SPRITE_COLOR  | NJD_SPRITE_ALPHA );
			}
		}
	}

	col1.argb.a = 0xff;
	col1.argb.r = (Uint8)(njSin(r1)*127+127);
	col1.argb.g = (Uint8)(njSin(g1)*127+127);
	col1.argb.b = (Uint8)(njSin(b1)*127+127);
	col2.argb.a = 0xff;
	col2.argb.r = (Uint8)(njSin(r2)*127+127);
	col2.argb.g = (Uint8)(njSin(g2)*127+127);
	col2.argb.b = (Uint8)(njSin(b2)*127+127);
	col3.argb.a = 0xff;
	col3.argb.r = (Uint8)(njSin(r3)*127+127);
	col3.argb.g = (Uint8)(njSin(g3)*127+127);
	col3.argb.b = (Uint8)(njSin(b3)*127+127);
	njSetBackColor( col1.color,col2.color,col3.color );
	r1+=137;
	g1+=217;
	b1+=193;
	r2+=237;
	g2+=111;
	b2+=173;
	r3+=191;
	g3+=129;
	b3+=201;

	x += 211;
	y += 137;
	z += 197;
	m += 171;
	sprite.ang += 500;

	return nwExecute();
}

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