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

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

/* テクスチャリストの設定 */
NJS_TEXNAME test_texname[] = {
{"Tw",0,0},
{"Twmm",0,0},
{"Re",0,0},
{"TWRe",0,0},
{"St",0,0},
{"Vq",0,0},
{"Vqmm",0,0},
{"Pal4",0,NJM_BANKGLOBALINDEX(0,0)},
{"Pal4mm",0,NJM_BANKGLOBALINDEX(1,0)},
{"Pal8",0,NJM_BANKGLOBALINDEX(16,0)},
{"Pal8mm",0,NJM_BANKGLOBALINDEX(32,0)},
};
NJS_TEXLIST test_list = {test_texname,11};

NJS_TEXTURE_VTX vtx[4];
Int tex_max;

Uint32 palette[256];
NJS_TEXPALETTE pl = {palette,0,0,0,0};

/*---------------------------------------------------------------------------
		テクスチャロードメイン
		load texture main
---------------------------------------------------------------------------*/
Sint32 TexLoadMain(void)
{
	nuLoadPaletteFile("Pal4.pvp",&pl);
	njSetPaletteMode(pl.mode);
	njSetPaletteData(0,pl.count,palette);

	nuLoadPaletteFile("Pal4mm.pvp",&pl);
	njSetPaletteData(1*16,pl.count,palette);

	nuLoadPaletteFile("Pal8.pvp",&pl);
	njSetPaletteData(16*16,pl.count,palette);

	nuLoadPaletteFile("Pal8mm.pvp",&pl);
	njSetPaletteData(32*16,pl.count,palette);

	/* 
		固定領域にテクスチャをロードする
		ただし、SmallVQには対応していない
	*/
	if(LoadTexture(&TEXLIST) < 0){
		return USER_EXIT;
	}
	
	njSetTexture(&TEXLIST);
	njSetRenderWidth(128);
	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
}