#include <shinobi.h>
#include <njdef.h>

void main(void)
{
	njSetBorderColor( 0x00000000 );
        njUserInit();
        while (njUserMain() == NJD_USER_CONTINUE) {
                njWaitVSync();
        }
        njUserExit();
}

#define MAX_MATRIX_DEPTH 16
#define MAX_VERTEXES 10000

static NJS_MATRIX		*matrix;
static NJS_VERTEX_BUF	*vbuf;

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[] = {
	{ 256,256,				/* スプライトサイズ（テクスチャのサイズとは無関係 */
	                        /* sprite size */
	   64, 64,				/* スプライト中心 */
	                        /* sprite center coordinates */
	    0,  0,				/* 左上ＵＶ（テクスチャの大きさに関係なく０〜２５５）*/
	                        /* texture upper left UV coordinates  (0-255) */
	  255,255,				/* 右下ＵＶ（テクスチャの大きさに関係なく０〜２５５）*/
	                        /* texture lower right UV coordinates (0-255)  */
	    0,					/* テクスチャ番号  */
	                        /* texture number  */
		0					/* アトリビュート（現在サポートしていない）*/
		                    /* attribute (Currently unused) */
	},
};

#define	XXX	700

void njUserInit(void)
{
	sbInitSystem( NJD_RESOLUTION_320x240_NTSCNI_ANTI, NJD_FRAMEBUFFER_MODE_RGB565, 1 );

    /*matrixとvertexバッファの確保*/
    /* allocates the memory */
    matrix = (NJS_MATRIX *)syMalloc(sizeof (NJS_MATRIX) * MAX_MATRIX_DEPTH);
    vbuf = (NJS_VERTEX_BUF *)syMalloc(sizeof (NJS_VERTEX_BUF) * MAX_VERTEXES);

    /* テクスチャバッファの確保 */
    /* sets necessary memory for work area of a texture */ 
    njInitTextureBuffer((Sint8 *)vbuf,0x800*10);

    /* 頂点データ登録用バッファをアロケート */
    /* allocates the buffers for registration of vertex data */
    njInitVertexBuffer(-MAX_VERTEXES * 64 / 4, 0, MAX_VERTEXES * 64 / 4, 0, 0);
	njInitPrint( NULL, 0, NJD_TEXFMT_ARGB_1555 );
	njInitMatrix( matrix, MAX_MATRIX_DEPTH,0 );
	njInitView( &view );
	njSetView( &view );

	njInitTexture( tex, 32 );
	njLoadTexture( &texlist );

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

	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 NJD_USER_CONTINUE;
}

void njUserExit(void)
{
	njExitPrint();
	njExitTexture();
	syFree(matrix);
	syFree(vbuf);
	sbExitSystem();
}
