#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;

#define	NB_TEX	32
NJS_TEXMEMLIST	tex[NB_TEX];
NJS_VIEW	view;
PDS_PERIPHERAL *per;

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

static	NJS_TEXLIST	texlist = { texname, 1 };

static	NJS_TEXANIM	anim[] = {
	{ 256,256,				/* スプライトサイズ（テクスチャのサイズとは無関係 */
	   64, 64,				/* スプライト中心 */
	    0,  0,				/* 左上ＵＶ（テクスチャの大きさに関係なく０〜２５５）*/
	  255,255,				/* 右下ＵＶ（テクスチャの大きさに関係なく０〜２５５）*/
	    0,					/* テクスチャ番号  */
		0					/* アトリビュート（現在サポートしていない）*/
	},
};

#define	XXX	700

#define USE_NINDOWS

void njUserInit(void)
{
	sbInitSystem( NJD_RESOLUTION_320x240_NTSCNI_ANTI, NJD_FRAMEBUFFER_MODE_RGB565, 1 );
    /*matrixとvertexバッファの確保*/
    matrix = (NJS_MATRIX *)syMalloc(sizeof (NJS_MATRIX) * MAX_MATRIX_DEPTH);
    vbuf = (NJS_VERTEX_BUF *)syMalloc(sizeof (NJS_VERTEX_BUF) * MAX_VERTEXES);
    /* テクスチャバッファの確保 */
    njInitTextureBuffer((Sint8 *)vbuf,0x800*10);
    /* 頂点データ登録用バッファをアロケート */
    njInitVertexBuffer(-MAX_VERTEXES * 64 / 4, 0, MAX_VERTEXES * 64 / 4, 0, MAX_VERTEXES * 64 / 4);
	njInitPrint( NULL, 0, NJD_TEXFMT_ARGB_1555 );
	njPrintSize(16);
	njInitMatrix( matrix, MAX_MATRIX_DEPTH,0 );
	njInitView( &view );
	njSetView( &view );

	njInitTexture( tex, NB_TEX );
#ifdef USE_NINDOWS
	nwInitSystem(NB_TEX, NJD_PORT_B0);
#endif
	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;
Sint32 njUserMain(void)
{
	static Sint32 flag = 0;
	static Float distance = 0.1f;
	Int	xx,yy,zz;
	Float mm;

    per = (PDS_PERIPHERAL *) pdGetPeripheral(PDD_PORT_A0);
	if (per->press & NJD_DGT_TA) {
		flag ^= -1;
	}
	if (flag != 0) {
		njControl3D(0);
		njPrintC(NJM_LOCATION(4,4),"TRANSLUCENT");
	} else {
		njControl3D(NJD_CONTROL_3D_USE_PUNCHTHROUGH);
		njPrintC(NJM_LOCATION(4,4),"PUNCH THROUGH");
	}
	
	njClearMatrix();

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

	mm = distance * XXX + 128;
	for(xx=0;xx<10;xx++) {
		for(yy=0;yy<10;yy++) {
			for(zz=0;zz<10;zz++) {
				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_SCALE | NJD_SPRITE_ALPHA );
			}
		}
	}

	if (per->on & NJD_DGT_KU) {
		x += 211;
	} else if (per->on & NJD_DGT_KD) {
		x -= 211;
	}
	if (per->on & NJD_DGT_KR) {
		y += 137;
	} else if (per->on & NJD_DGT_KL) {
		y -= 137;
	}
	if (per->on & NJD_DGT_TR) {
		z += 197;
	} else if (per->on & NJD_DGT_TL) {
		z -= 197;
	}
	if (per->press & NJD_DGT_TB) {
		if (distance > 0.f) {
			distance -= 0.1;
		}
	} else if (per->press & NJD_DGT_TX) {
		if (distance < 1.f) {
			distance += 0.1;
		}
	}
#ifdef USE_NINDOWS
	return nwExecute();
#else
	return NJD_USER_CONTINUE;
#endif
}

void njUserExit(void)
{
#ifdef USE_NINDOWS
	nwExitSystem();
#endif
	njExitPrint();
	njExitTexture();
	syFree(matrix);
	syFree(vbuf);
	sbExitSystem();
}
