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

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

/* テクスチャリストの設定 */
TEXTURE_START

TEXTURENAME textures_f40_gte_sh_null1_1[]
START
	TEXN( "GRAD_METALn" ),      /* 21000 32x32 565 TW MIP ?? ?? */
	TEXN( "GRAD_METAL00n" ),    /* 21002 32x32 565 TW MIP ?? ?? */
	TEXN( "bpr_logo" ),        /* 21011 128x32 565 RE ?? ?? */
	TEXN( "car_mesh3n" ),       /* 20998 32x32 565 TW MIP ?? ?? */
	TEXN( "f40_enginen" ),      /* 20999 64x64 565 TW MIP ?? ?? */
	TEXN( "f40_i_coolern" ),    /* 21001 64x64 565 TW MIP ?? ?? */
	TEXN( "f40_locka" ),       /* 21013 32x64 565 RE ?? ?? */
	TEXN( "f40_numbern" ),      /* 21010 64x64 565 TW MIP ?? ?? */
	TEXN( "f40_rearend" ),     /* 21012 256x128 565 RE ?? ?? */
	TEXN( "f4e_wheeln" ),       /* 20993 128x128 565 TW MIP ?? ?? */
	TEXN( "f4e_wheelan" ),      /* 20992 128x128 4444 TW MIP ?? ?? */
	TEXN( "fer_emblem_fan" ),   /* 21008 64x64 1555 TW MIP ?? ?? */
	TEXN( "fer_emblem_sbn" ),   /* 21009 64x64 4444 TW MIP ?? ?? */
	TEXN( "fer_emblem_syn" ),   /* 21007 64x64 4444 TW MIP ?? ?? */
	TEXN( "fer_logoa" ),       /* 21014 128x32 1555 RE ?? ?? */
	TEXN( "igolb" ),           /* 21004 128x32 1555 RE ?? ?? */
	TEXN( "igolw" ),           /* 21005 128x32 1555 RE ?? ?? */
	TEXN( "leaseplan_logo" ),  /* 21006 256x32 565 RE ?? ?? */
	TEXN( "mcl_h_light1_on" ), /* 21003 128x64 565 RE ?? ?? */
	TEXN( "mirror_ln" ),        /* 21021 128x128 565 TW MIP ?? ?? */
	TEXN( "mirror_rn" ),        /* 21020 128x128 565 TW MIP ?? ?? */
	TEXN( "pirelli" ),         /* 21016 128x32 1555 RE ?? ?? */
	TEXN( "recaro" ),          /* 20995 64x128 565 RE ?? ?? */
	TEXN( "recaro_b" ),        /* 20996 64x128 565 RE ?? ?? */
	TEXN( "ref_deepbluen" ),    /* 21017 128x128 565 TW MIP ?? ?? */
	TEXN( "ref_lemon_linen" ),  /* 21019 128x128 565 TW MIP ?? ?? */
	TEXN( "ref_sky_ts4n" ),     /* 21018 128x128 565 TW MIP ?? ?? */
	TEXN( "tex_f1_rampn" ),     /* 21022 64x64 565 TW MIP ?? ?? */
	TEXN( "tex_f1_ramp1n" ),    /* 21015 64x64 1555 TW MIP ?? ?? */
	TEXN( "tex_f1_ramp2n" ),    /* 20997 64x64 565 TW MIP ?? ?? */
	TEXN( "tier_patarnn" ),     /* 20994 128x128 565 TW MIP ?? ?? */
	TEXN( "tier_patarnb" ),    /* 20990 1024x128 565 RE ?? ?? */
	TEXN( "tier_sidean" ),      /* 20991 128x128 565 TW MIP ?? ?? */
END

TEXTURELIST texlist_f40_gte_sh_null1_1
START
TextureList textures_f40_gte_sh_null1_1,
TextureNum  33,
END

TEXTURE_END

NJS_TEXTURE_VTX vtx[4];
Int tex_max;
/*---------------------------------------------------------------------------
		テクスチャロードメイン
		load texture main
---------------------------------------------------------------------------*/
Sint32 TexLoadMain(void)
{
	if(nuLoadTexturePvmFile("f40.pvm",&TEXLIST) < 0){
		return USER_EXIT;
	}
	njSetTexture(&TEXLIST);
	
	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
}