/*---------------------------------------------------------------------------
  Shinobi2 Sample
  COPYRIGHT (C) SEGA ENTERPRISES, LTD.
---------------------------------------------------------------------------*/

#include	<shinobi.h>
#include	"Nj.h"
#include	"TexMain.h"

/* テクスチャリストの設定 */
NJS_TEXNAME texname[10];
NJS_TEXLIST texlist = {texname,3};
NJS_TEXINFO info;

NJS_TEXTURE_VTX vtx[4];
Int tex_max;

/*---------------------------------------------------------------------------
		テクスチャロードメイン
		load texture main
---------------------------------------------------------------------------*/
Sint32 TexLoadMain(void)
{
	GDFS fp;
	Sint32 file_size;
	Sint32 file_sect;
	Uint32 *yuvbuffer;

	/* ファイルのオープン */
	if((fp = gdFsOpen("yuv2.bin",NULL)) == NULL){
		return USER_EXIT;
	}
	gdFsGetFileSize(fp,&file_size);
	
	/* 最大何セクタあるか計算しておく */
	file_sect = gdFsCalcSctSize(file_size);
	
	yuvbuffer = syMalloc(file_sect*2048);
	if(yuvbuffer == NULL)	return USER_EXIT;
	
	if(gdFsRead(fp,file_sect,yuvbuffer) != GDD_ERR_OK){
		syFree(yuvbuffer);
		return USER_EXIT;
	}
	
	gdFsClose(fp);

	njSetTextureName(&texname[0],"Bra.pvr",0,NJD_TEXATTR_GLOBALINDEX);
	njSetTextureName(&texname[1],"Bra12.pvr",1,NJD_TEXATTR_GLOBALINDEX);

	njSetTextureInfo(&info,NULL,NJD_TEXFMT_YUV_422|NJD_TEXFMT_STRIDE,512,256);
	njSetTextureName(&texname[2],&info,2,NJD_TEXATTR_TYPE_SURFACE|NJD_TEXATTR_GLOBALINDEX);

	if(nuLoadTexture(&TEXLIST) < 0){
		return USER_EXIT;
	}
	njSetTexture(&TEXLIST);

	njSetSizeYUV420(320,240);
	if(njLoadTextureYUV420Num(2,yuvbuffer) < 0){
		return USER_EXIT;
	}

	syFree(yuvbuffer);
	return USER_CONTINUE;
}

void InitDrawTexture(void)
{
	Float polysize = 256.f;
	Float x0 = 192.f,y0 = 132.f;

	vtx[0].x = x0;
	vtx[0].y = y0;
	vtx[0].z = 0.1f;
	vtx[1].x = x0;
	vtx[1].y = y0 + polysize;
	vtx[1].z = 0.1f;
	vtx[2].x = x0 + polysize;
	vtx[2].y = y0;
	vtx[2].z = 0.1f;
	vtx[3].x = x0 + polysize;
	vtx[3].y = y0 + polysize;
	vtx[3].z = 0.1f;
	vtx[0].col = 0x80FFFFFF;
	vtx[1].col = 0x80FFFFFF;
	vtx[2].col = 0x80FFFFFF;
	vtx[3].col = 0x80FFFFFF;
	vtx[0].col = 0x80FFFFFF;
	vtx[1].col = 0x80FFFFFF;
	vtx[2].col = 0x80FFFFFF;
	vtx[3].col = 0x80FFFFFF;
	njTextureFilterMode(NJD_TEXTUREFILTER_POINT_SAMPLE);
	
	tex_max = TEXLIST.nbTexture;
}

void DrawTexture(void)
{
	static Float ratio = 1.f;
	static Int tex = 0;
	static Float u = 0.f,v = 0.f;
	static Int filter = 0;
	Float u0,v0,u1,v1;


	if(Per->press & PDD_DGT_TY){
		filter ^= 1;
		if(filter)
			njTextureFilterMode(NJD_TEXTUREFILTER_BILINEAR);
		else
			njTextureFilterMode(NJD_TEXTUREFILTER_POINT_SAMPLE);
	}
	
	if(Per->press & PDD_DGT_TA){
		tex++;
		if(tex > (tex_max - 1))
			tex = 0;
	}

	if(Per->press & PDD_DGT_TB){
		tex--;
		if(tex < 0)
			tex = (tex_max - 1);
	}
	
	if(Per->on & PDD_DGT_TR){
		  ratio -= 0.01f;
		  if(ratio < 0){
		  	ratio = 0.f;
		  }
	}

	if(Per->on & PDD_DGT_TL){
		  ratio += 0.01f;
		  if(ratio > 1.f){
		  	ratio = 1.f;
		  }
	}

	if(Per->on & PDD_DGT_KR){
		u += 0.01f;
	}

	if(Per->on & PDD_DGT_KL){
		u -= 0.01f;
		if(u  < 0.f){
			u = 0.f;
		}
		
	}

	if(Per->on & PDD_DGT_KD){
		v += 0.01f;
	}

	if(Per->on & PDD_DGT_KU){
		v -= 0.01f;
		if(v  < 0.f){
			v = 0.f;
		}
		
	}

	if((u + ratio) > 1.f){
		u = 1.f - ratio;
	}

	if((v + ratio) > 1.f){
		v = 1.f - ratio;
	}

	u0 	= u;
	u1	= u + ratio;
	v0 	= v;
	v1	= v + ratio;


	vtx[0].u = u0;
	vtx[1].u = u0;
	vtx[2].u = u1;
	vtx[3].u = u1;
	vtx[0].v = v0;
	vtx[1].v = v1;
	vtx[2].v = v0;
	vtx[3].v = v1;

	njSetTextureNum(tex);
	njDrawTextureEx(vtx,4,FALSE);
#if DEBUG	
	if(filter)
		njPrint( NJM_LOCATION( 2,4 ), "Y      :BILINEAR");
	else
		njPrint( NJM_LOCATION( 2,4 ), "Y      :POINT SAMPLE");

	njPrint( NJM_LOCATION( 2,5 ), "L <-> R:ZOOM");
	njPrint( NJM_LOCATION( 2,6 ), "U,D,L,R:TRANSLATE");
	njPrint( NJM_LOCATION( 2,7 ), "A,B    :TEXCHANGE");
#endif
}