/*
Ninja Sample

  COPYRIGHT (C) SEGA ENTERSRISES,LTD.
  
	Draw2D Sample
	
	  1997.12.08
*/
#include <shinobi.h>
#include <sg_sycbl.h>

#define point_num 200

/* Setting Textures  */
NJS_TEXNAME texname[3];
NJS_TEXLIST texlist ={texname,3};

void Menu(void);
void Menu2(void);
void Menu3(void);
void Pointtest(void);
void Linetest(void);
void Triangletest(void);
void Triangletest2(void);
void Triangletest3(void);
void Polygontest(void);
void Polygontest2(void);
void Polygontest3(void);
void Circletest(void);

Sint32 mode=0;
Sint32 mode2=0;
Sint32 mode3=0;
Sint32 lineflag = 1;
NJS_POINT2 pnt[point_num];
NJS_COLOR col[point_num];
NJS_COLOR tex[point_num];
NJS_POINT2COL p2c;
NJS_TEXMEMLIST texmemlist[3];
Sint32 flag;

NJS_PERIPHERAL *per;
extern NJS_VERTEX_BUF	vbuf[4096];


void   njUserInit(void)
{
	Int	pri = 0;
	SYE_CBL_CABLE Cable;
		
	njSetBorderColor(0x00000000);
	
	/* Check the cable for NTSC or VGA */
	Cable = syCblCheckCable();
	
	switch( Cable ) 
	{
	case SYE_CBL_CABLE_VGA:
		sbInitSystem(NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1);
		break;
		
	case SYE_CBL_CABLE_NTSC:
		sbInitSystem(NJD_RESOLUTION_640x480_NTSCNI, NJD_FRAMEBUFFER_MODE_RGB565, 1); 
		break;
	}
	njInitVertexBuffer(100000, 0, 100000, 0, 0);
	njInitPrint( NULL, 0, NJD_TEXFMT_ARGB_1555 );
	
	/*	for Set4	*/
	njInitTextureBuffer((Sint8 *)vbuf,0x40800);
	
	njInitTexture(texmemlist,3);
	njSetTextureName(&texname[0], "Cafe.pvr" ,0, NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
	njSetTextureName(&texname[1], "Oldwood.pvr" ,1 ,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
	njSetTextureName(&texname[2], "Wallmap.pvr" ,2 ,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
	njLoadTexture(&texlist);
	njSetTexture(&texlist);
}

Sint32 njUserMain(void)
{
	per = (PDS_PERIPHERAL *) njGetPeripheral(NJD_PORT_A0);
	
	switch(mode)
	{
	case 0:
		Menu();
		break;
		
	case 1:										/* Point test */
		Pointtest();
		if(per->press & NJD_DGT_TB)
		{
			lineflag ^= 1;
			break;
		}
		
		if(per->press & NJD_DGT_TX)
		{
			mode = 0;
		}
		break;
		
	case 2:										/* Line test (Wheel) */
		Linetest();
		if(per->press & NJD_DGT_TB)
		{
			lineflag ^= 1;
			break;
		}
		
		if(per->press & NJD_DGT_TX)
		{
			mode = 0;
			
		}
		break;
		
	case 3:										/* Triangle test */
		switch(mode2)
		{
		case 0:
			Menu2();
			break;
		case 1:									/* Triangle test (Fill)*/
			Triangletest();
			if(per->press & NJD_DGT_TX)
			{
				mode2 = 0;
			}
			break;
		case 2:									/* Triangle test (Line)*/
			Triangletest2();
			if(per->press & NJD_DGT_TX)
			{
				mode2 = 0;
			}
			break;
		case 3:									/* Triangle test (Oldwood.pvr)*/
			Triangletest3();
			if(per->press & NJD_DGT_TX)
			{
				mode2 = 0;
			}
			break;
		}
		break;
		
		case 4:										/* Polygon test */
			switch(mode3)
			{
			case 0:
				Menu3();
				break;
			case 1:									/* Polygon test (Fill)*/
				Polygontest();
				if(per->press & NJD_DGT_TX)
				{
					mode3 = 0;
				}
				break;
			case 2:									/* Polygon test (Line)*/
				Polygontest2();
				if(per->press & NJD_DGT_TX)
				{
					mode3 = 0;
				}
				break;
			case 3:									/* Polygon test (Wallmap.pvr)*/
				Polygontest3();
				if(per->press & NJD_DGT_TX)
				{
					mode3 = 0;
				}
				break;
			}
			break;
			
			case 5:										/* Circle test (Cafe.pvr used)*/
				Circletest();
				if(per->press & NJD_DGT_TX)
				{
					mode = 0;
				}
				break;
				
		}
		return NJD_USER_CONTINUE;
}

void njUserExit(void)
{
	njExitPrint();
	njExitTexture();
	sbExitSystem();
}

void Menu(void)
{
	njPrintC(NJM_LOCATION(10,5),"TEST NUMBER?");	/* Character Strings output	*/
    
	njPrintC(NJM_LOCATION(10,6)," POINT     X");
	njPrintC(NJM_LOCATION(10,7)," LINE      Y");
	njPrintC(NJM_LOCATION(10,8)," TRIANGLE  LT");
	njPrintC(NJM_LOCATION(10,9)," POLYGON   A");
	njPrintC(NJM_LOCATION(10,10)," CIRCLE    B");
	
	
	
	if(per->press & NJD_DGT_TX)
	{
		mode = 1;
	}
	else if(per->press & NJD_DGT_TY)
	{
		mode = 2;
	}
	else if(per->press & NJD_DGT_TL)
	{
		mode = 3;
	}
	else if(per->press & NJD_DGT_TA)
	{
		mode = 4;
	}
	else if(per->press & NJD_DGT_TB)
	{
		mode = 5;
	} 
}

void Menu2(void)
{
	njPrintC(NJM_LOCATION(10,5),"TRIANGLE TEST NUMBER?");	/* Character Strings output	*/
	njPrintC(NJM_LOCATION(10,6)," FILL      U+RT");
	njPrintC(NJM_LOCATION(10,7)," LINE      R+RT");
	njPrintC(NJM_LOCATION(10,8)," TEXTURE   D+RT");
	
	if(per->press & NJD_DGT_TX)
	{
		mode = 0;
	}
	else if((per->on & NJD_DGT_TR)&&(per->on & NJD_DGT_KU))
	{
		mode2 = 1;
	}
	else if((per->on & NJD_DGT_TR)&&(per->on & NJD_DGT_KR))
	{
		mode2 = 2;
	}
	else if((per->on & NJD_DGT_TR)&&(per->on & NJD_DGT_KD))
	{
		mode2 = 3;
	}
	
	
}
void Menu3(void)
{
	njPrintC(NJM_LOCATION(10,5),"POLYGON TEST NUMBER?");	/* Character Strings output	*/
	njPrintC(NJM_LOCATION(10,6)," FILL      U+RT");
	njPrintC(NJM_LOCATION(10,7)," LINE      R+RT");
	njPrintC(NJM_LOCATION(10,8)," TEXTURE   D+RT");
	njPrintC(NJM_LOCATION(10,9)," EXIT      X");
	
	
	if(per->press & NJD_DGT_TX)
	{
		mode = 0;
	}
	else if((per->on & NJD_DGT_TR)&&(per->on & NJD_DGT_KU))
	{
		mode3 = 1;
	}
	else if((per->on & NJD_DGT_TR)&&(per->on & NJD_DGT_KR))
	{
		mode3 = 2;
	}
	else if((per->on & NJD_DGT_TR)&&(per->on & NJD_DGT_KD))
	{
		mode3 = 3;
	}
	
}

void Pointtest(void)
{
	Sint32 i;
	
	if(lineflag)
	{
		
		njPrintC(NJM_LOCATION(4,5)," POINT TEST    STOP->B      EXIT -> X");
		
		for(i=0;i<point_num;i++)
		{
			pnt[i].x = ((Float)njRandom() * 640.f);
			pnt[i].y = ((Float)njRandom() * 480.f);
			col[i].color = (Uint32)(njRandom() * 0xFFFFFFFF);
		}
	}
	else
	{    
		njPrintC(NJM_LOCATION(4,5)," POINT TEST    START->B      EXIT -> X");
	}
	p2c.p = pnt;
	p2c.col = col;
	
	/* njDrawPoint2D(a,b,c,d) */	
	/* a = always use pnt (setting the color and location for a point) */	
	/* b = number of points */	
	/* c = priority(Float)  0 thru 65535 */	
	/* d = attribute (normal version) */
	njDrawPoint2D(&p2c,point_num,-2.f,0|NJD_TRANSPARENT);
}

void Linetest(void)
{
	Sint32 i;
	
	if(lineflag)
	{    
		njPrintC(NJM_LOCATION(4,5)," LINE TEST    STOP->B      EXIT -> X");
		
		for(i=0;i<point_num;i++)
		{
			pnt[i].x = ((Float)njRandom() * 640.f);
			pnt[i].y = ((Float)njRandom() * 480.f);
			col[i].color = (Uint32)(njRandom() * 0xFFFFFFFF);
		}
	}
	
	else
	{    
		njPrintC(NJM_LOCATION(4,5)," LINE TEST    START->B      EXIT -> X");	
	}
	
	p2c.p = pnt;
	p2c.col = col;
	/* njDrawLine2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = number of lines */	
	/* c = priority(Float)  0 thru 65535 */	
	/* d = attribute (emission, transparent) */	
	njDrawLine2D(&p2c, point_num - 1,- 2.f,NJD_DRAW_FAN|NJD_TRANSPARENT);
}

void Triangletest(void)
{
    
	njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(FILL)      EXIT -> X");
	
	pnt[0].x = 100;
	pnt[0].y = 300;
	col[0].color = 0xFFFF00AA;
	
	pnt[1].x = 150;
	pnt[1].y = 100;
	col[1].color = 0x0FFFF000;
	
	pnt[2].x = 200;
	pnt[2].y = 300;
	col[2].color = 0x00FFFF00;
	
	pnt[3].x = 250;
	pnt[3].y = 100;
	col[3].color = 0xF00FFF55;
	
	pnt[4].x = 300;
	pnt[4].y = 300;
	col[4].color = 0xFF00FF00;
	
	pnt[5].x = 350;
	pnt[5].y = 100;
	col[5].color = 0xFFF00F00;
	
	p2c.p = pnt;
	p2c.col = col;
	
	/* njDrawTriangle2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = number of triangle */	
	/* c = priority (Float)  0 thru 65535 */	
	/* d = attribute (continuous, fill) */	
	njDrawTriangle2D(&p2c,4,-3.f,NJD_DRAW_CONNECTED|NJD_FILL/*|NJD_TRANSPARENT*/);
}

void Triangletest2(void)
{
    
	njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(LINE)      EXIT -> X");
	
	pnt[0].x = 100;
	pnt[0].y = 300;
	col[0].color = 0xFFFF00AA;
	
	pnt[1].x = 150;
	pnt[1].y = 100;
	col[1].color = 0x0FFFF000;
	
	pnt[2].x = 200;
	pnt[2].y = 300;
	col[2].color = 0x00FFFF00;
	
	pnt[3].x = 250;
	pnt[3].y = 100;
	col[3].color = 0xF00FFF55;
	
	pnt[4].x = 300;
	pnt[4].y = 300;
	col[4].color = 0xFF00FF00;
	
	pnt[5].x = 350;
	pnt[5].y = 100;
	col[5].color = 0xFFF00F00;
	
	p2c.p = pnt;
	p2c.col = col;
	
	/* njDrawTriangle2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = number of triangle */	
	/* c = priority (Float)  0 thru 65535 */	
	/* d = attribute (continuous, wire) */
	njDrawTriangle2D(&p2c,4,-3.f,NJD_DRAW_CONNECTED|NJD_TRANSPARENT);
}

void Triangletest3(void)
{
    
	njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(TEXTURE)      EXIT -> X");
	
	pnt[0].x = 320;
	pnt[0].y = 240;
	col[0].color = 0xffffffff;
	tex[0].tex.u = 128;
	tex[0].tex.v = 128;
	
	pnt[1].x = 240;
	pnt[1].y = 50;
	col[1].color = 0xffffffff;
	tex[1].tex.u = 0;
	tex[1].tex.v = 0;
	
	pnt[2].x = 400;
	pnt[2].y = 50;
	col[2].color = 0xffffffff;
	tex[2].tex.u = 255;
	tex[2].tex.v = 0;
	
	pnt[3].x = 400;
	pnt[3].y = 430;
	col[3].color = 0xffffffff;
	tex[3].tex.u = 255;
	tex[3].tex.v = 255;
	
	pnt[4].x = 240;
	pnt[4].y = 430;
	col[4].color = 0xffffffff;
	tex[4].tex.u = 0;
	tex[4].tex.v = 255;
	
	njSetTextureNum(1);
	
	p2c.p = pnt;
	p2c.col = col;
	p2c.tex = tex;
	
	/* njDrawTriangle2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = number of triangle */	
	/* c = priority (Float)  0 thru 65535 */	
	/* d = attribute (emission, fill, texture) */	
	njDrawTriangle2D(&p2c,2,-6.f,NJD_DRAW_FAN|NJD_FILL|NJD_USE_TEXTURE);
}

void Polygontest(void)
{
    
	njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(FILL)      EXIT -> X");
	
	pnt[0].x = 427;
	pnt[0].y = 116;
	col[0].color = 0xFF000055;
	
	pnt[1].x = 555;
	pnt[1].y = 200;
	col[1].color = 0xFFFFFF55;
	
	pnt[2].x = 555;
	pnt[2].y = 285;
	col[2].color = 0x00FF0055;
	
	pnt[3].x = 427;
	pnt[3].y = 371;
	col[3].color = 0xFFFFFF55;
	
	pnt[4].x = 300;
	pnt[4].y = 285;
	col[4].color = 0x0000FF55;
	
	pnt[5].x = 300;
	pnt[5].y = 200;
	col[5].color = 0xFFFFFF55;
	
	/* njDrawPolygon2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = polygon */	
	/* c = priority (Float)  0 thru 65535 */	
	/* d = attribute (fill) */	
	p2c.p = pnt;
	p2c.col = col;
	njDrawPolygon2D(&p2c,6,-2.f,NJD_FILL);
}

void Polygontest2(void)
{
    
	njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(LINE)      EXIT -> X");
	
	pnt[0].x = 427;
	pnt[0].y = 116;
	col[0].color = 0xFF000055;
	
	pnt[1].x = 555;
	pnt[1].y = 200;
	col[1].color = 0xFFFFFF55;
	
	pnt[2].x = 555;
	pnt[2].y = 285;
	col[2].color = 0x00FF0055;
	
	pnt[3].x = 427;
	pnt[3].y = 371;
	col[3].color = 0xFFFFFF55;
	
	pnt[4].x = 300;
	pnt[4].y = 285;
	col[4].color = 0x0000FF55;
	
	pnt[5].x = 300;
	pnt[5].y = 200;
	col[5].color = 0xFFFFFF55;
	
	p2c.p = pnt;
	p2c.col = col;
	
	/* njDrawPolygon2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = polygon */	
	/* c = priority (Float)  0 thru 65535 */	
	/* d = attribute (wire) */
	njDrawPolygon2D(&p2c,6,-2.f,0);
}

void Polygontest3(void)
{
    
	njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(TEXTURE)      EXIT -> X");
	
	pnt[0].x = 427;
	pnt[0].y = 116;
	col[0].color = 0xffffffff;
	tex[0].tex.u = 127;
	tex[0].tex.v = 0;
	
	pnt[1].x = 555;
	pnt[1].y = 200;
	col[1].color = 0xffffffff;
	tex[1].tex.u = 255;
	tex[1].tex.v = 84;
	
	pnt[2].x = 555;
	pnt[2].y = 285;
	col[2].color = 0xffffffff;
	tex[2].tex.u = 255;
	tex[2].tex.v = 169;
	
	pnt[3].x = 427;
	pnt[3].y = 371;
	col[3].color = 0xffffffff;
	tex[3].tex.u = 127;
	tex[3].tex.v = 255;
	
	pnt[4].x = 300;
	pnt[4].y = 285;
	col[4].color = 0xffffffff;
	tex[4].tex.u = 0;
	tex[4].tex.v = 169;
	
	pnt[5].x = 300;
	pnt[5].y = 200;
	col[5].color = 0xffffffff;
	tex[5].tex.u = 0;
	tex[5].tex.v = 84;
	
	njSetTextureNum(2);
	
	p2c.p = pnt;
	p2c.col = col;
	p2c.tex = tex;
	
	/* njDrawPolygon2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = polygon */	
	/* c = priority (Float)  0 thru 65535 */	
	/* d = attribute (texture) */	
	njDrawPolygon2D(&p2c,6,-2.f,NJD_FILL|NJD_USE_TEXTURE);
}

void Circletest(void)
{
	static Sint32 i = 0, x = 420;
	
    
	njPrintC(NJM_LOCATION(4,5)," CIRCLE TEST    EXIT-> X");
	
	pnt[0].x = 0;
	pnt[0].y = 0;
	col[0].color = 0xffffffff;
	tex[0].tex.u = 0;
	tex[0].tex.v = 0;
	
	col[1].color = 0xffffffff;
	tex[1].tex.u = 255;
	tex[1].tex.v = 255;
	
	if(i <= 420)
	{
		pnt[1].x = (float)((x - 420)*4/3);
		pnt[1].y = (float)(x - 420);
		x += 1;
	}
	else if(420 < i && i <= 840)
	{
		pnt[1].x = (float)((x - 420)*4/3);
		pnt[1].y = (float)(x - 420);
		x -= 1;
	}	
	else if(i > 840)
	{
		i = 0;
	}
	
	i++;
	njSetTextureNum(0);
	
	p2c.p = pnt;
	p2c.col = col;
	p2c.tex = tex;
	
	/* njDrawCircle2D(a,b,c,d) */	
	/* a = always use p (setting the color and location for a point) */	
	/* b = number of oval */	
	/* c = priority (Float)  0 thru 65535 */	
	/* d = attribute (inscribed circle in rectangle, fill, texture) */	
	njDrawCircle2D(&p2c, 1,-6.f,NJD_DRAW_INSCRIBED|NJD_FILL|NJD_USE_TEXTURE);
}
