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

	Cache Sample

	1999.01.20
*/

#include <Shinobi.h>
#include <Nindows.h>
#define NUM 91

/* テクスチャの設定 

	ここで使用されているPVR形式のファイルにはグローバルインデックスが
	入っていません。グローバルインデックスが入っていない場合や入って
	いてもプログラム上で設定したい場合はNJD_TEXATTR_GLOBALINDEXを
	設定してください。

*/
/* setting of texture

    A global index does not exit in a file of PVR form used here.
    The case that there is not a global index or when want to establish it 
    on a program even if exist, use NJD_TEXATTR_GLOBALINDEX.
*/

/* GD-WorkShop では 以下のテクスチャはルートに置いてください */
/* Put texture with baing root directory of GD WorkShop */
static	NJS_TEXNAME	texname[] = {
	{"logo02_60.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   0},
	{"logo02_61.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   1},
	{"logo02_62.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   2},
	{"logo02_63.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   3},
	{"logo02_64.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   4},
	{"logo02_65.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   5},
	{"logo02_66.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   6},
	{"logo02_67.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   7},
	{"logo02_68.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   8},
	{"logo02_69.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,   9},
	{"logo02_70.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  10},
	{"logo02_71.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  11},
	{"logo02_72.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  12},
	{"logo02_73.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  13},
	{"logo02_74.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  14},
	{"logo02_75.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  15},
	{"logo02_76.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  16},
	{"logo02_77.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  17},
	{"logo02_78.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  18},
	{"logo02_79.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  19},
	{"logo02_80.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  20},
	{"logo02_81.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  21},
	{"logo02_82.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  22},
	{"logo02_83.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  23},
	{"logo02_84.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  24},
	{"logo02_85.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  25},
	{"logo02_86.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  26},
	{"logo02_87.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  27},
	{"logo02_88.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  28},
	{"logo02_89.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  29},
	{"logo02_90.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  30},
	{"logo02_91.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  31},
	{"logo02_92.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  32},
	{"logo02_93.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  33},
	{"logo02_94.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  34},
	{"logo02_95.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  35},
	{"logo02_96.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  36},
	{"logo02_97.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  37},
	{"logo02_98.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  38},
	{"logo02_99.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX,  39},
	{"logo02_100.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 40},
	{"logo02_101.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 41},
	{"logo02_102.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 42},
	{"logo02_103.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 43},
	{"logo02_104.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 44},
	{"logo02_105.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 45},
	{"logo02_106.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 46},
	{"logo02_107.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 47},
	{"logo02_108.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 48},
	{"logo02_109.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 49},
	{"logo02_110.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 50},
	{"logo02_111.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 51},
	{"logo02_112.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 52},
	{"logo02_113.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 53},
	{"logo02_114.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 54},
	{"logo02_115.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 55},
	{"logo02_116.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 56},
	{"logo02_117.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 57},
	{"logo02_118.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 58},
	{"logo02_119.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 59},
	{"logo02_120.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 60},
	{"logo02_121.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 61},
	{"logo02_122.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 62},
	{"logo02_123.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 63},
	{"logo02_124.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 64},
	{"logo02_125.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 65},
	{"logo02_126.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 66},
	{"logo02_127.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 67},
	{"logo02_128.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 68},
	{"logo02_129.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 69},
	{"logo02_130.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 70},
	{"logo02_131.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 71},
	{"logo02_132.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 72},
	{"logo02_133.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 73},
	{"logo02_134.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 74},
	{"logo02_135.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 75},
	{"logo02_136.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 76},
	{"logo02_137.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 77},
	{"logo02_138.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 78},
	{"logo02_139.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 79},
	{"logo02_140.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 80},
	{"logo02_141.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 81},
	{"logo02_142.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 82},
	{"logo02_143.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 83},
	{"logo02_144.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 84},
	{"logo02_145.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 85},
	{"logo02_146.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 86},
	{"logo02_147.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 87},
	{"logo02_148.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 88},
	{"logo02_149.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 89},
	{"logo02_150.pvr",NJD_TEXATTR_CACHE|NJD_TEXATTR_GLOBALINDEX, 90},
};

NJS_TEXLIST texlist = {texname,NUM};
NJS_TEXMEMLIST	tex[100];

NJS_PERIPHERAL *per,*per1;
Sint32 i,j;
NJS_POINT2COL pc;
NJS_POINT2	p[4];
NJS_COLOR	c[4];
NJS_COLOR	uv[4];
Sint8 *texbuf;
Sint8 *cachebuf;

void ReLoad(void);

void njUserInit(void)
{
	njSetBorderColor(0x00000000);
	sbInitSystem( NJD_RESOLUTION_640x480_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer( 800000, 0, 100000, 0 ,0);
	njInitPrint(NULL,0,0);
	
	/* Max Texture 128x128 Twiddled No Mipmap Texture*/
	texbuf = syMalloc(0x8800);
	njInitTextureBuffer(texbuf,0x8800);
	/* Cache Buffer (128x128x2byte+ 32(HeaderSize))x91=0x2D8B60*/
	cachebuf = syMalloc(0x2D8B60);
	njInitCacheTextureBuffer(cachebuf,0x2D8B60);
	
	/* Load logo02_60.pvr - logo02_150.pvr */
	njInitTexture( tex,100);
	njLoadTexture( &texlist );
	
	/* Texture Buffer Free*/
	syFree(texbuf);

	njSetTexture( &texlist );
	nwInitSystem(100,NJD_PORT_A0);
	
	njLoadCacheTextureNum(0);

	i=0;
	j=0;

	pc.p = p;
	pc.col = c;
	pc.tex = uv;
	pc.num = 4;

	p[0].x = 100;
	p[0].y = 100;
	uv[0].tex.u = 0;
	uv[0].tex.v = 0;
	c[0].color = 0xFFFFFFFF;
	
	p[3].x = 100;
	p[3].y = 300;
	uv[3].tex.u = 0;
	uv[3].tex.v = 255;
	c[3].color = 0xFFFFFFFF;
	
	p[1].x = 300;
	p[1].y = 100;
	uv[1].tex.u = 255;
	uv[1].tex.v = 0;
	c[1].color = 0xFFFFFFFF;

	p[2].x = 300;
	p[2].y = 300;
	uv[2].tex.u = 255;
	uv[2].tex.v = 255;
	c[2].color = 0xFFFFFFFF;
	njSetBackColor(0x000000FF,0x000000FF,0x00000000);
	njPrintSize(16);
	/* レンダリング終了した後、テクスチャをリロードする */
	/* After rendering, I reload texture. */
	njSetEORFunction(ReLoad);
}

Sint32 njUserMain(void)
{
	njSetTextureNum(i);
	njDrawPolygon2D(&pc,4,-100,NJD_USE_TEXTURE|NJD_FILL);

	njPrintC(NJM_LOCATION(2,3),"NINJA CACHE SAMPLE");

	return nwExecute();
}

void njUserExit(void)
{
	njExitPrint();
	nwExitSystem();
	njExitTexture();
	/* Cache Buffer Free */
	syFree(cachebuf);
	sbExitSystem();
}

/* テクスチャ入れ替え関数  */
/* reload texture function  */
void ReLoad(void)
{
	njReleaseTextureNum(i);

	if(j==0) i++;
	j++;
	if(j > 3) j = 0;
	if(i>90) i=0;
	
	njLoadCacheTextureNum(i);

}

