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

	Draw2D Sample

	1997.12.08
*/
#include <Ninjawin.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 p[point_num];
NJS_COLOR col[point_num];
NJS_COLOR tex[point_num];
NJS_POINT2COL p2c;
NJS_TEXMEMLIST texmemlist[3];
Sint32 flag;

NJS_PERIPHERAL *per;

void   njUserInit(void)
{
	Int	pri = 0;

	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB555, 1 );
	njInitVertexBuffer(100000, 0, 100000, 0);
	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 = njGetPeripheral(NJD_PORT_JOYSTICK1);
	flag = NJD_DEV_JOYSTICK;
	if(per->id == NJD_DEV_NODEVICE) {
		per = njGetPeripheral(NJD_PORT_SYSKEYBOARD);
		flag = NJD_DEV_SYSKEYBOARD;
	}
	switch(mode){
		case 0:
			if(per->push & NJD_DGT_TX){
				return NJD_USER_EXIT;
			}
			Menu();
			break;

		case 1:										/* Point test */
			Pointtest();
				if(per->push & NJD_DGT_TB){
					lineflag ^= 1;
					break;
			}
			
			if(per->push & NJD_DGT_TX){
					mode = 0;
			
			}
			break;

		case 2:										/* Line test (Wheel) */
			Linetest();
			if(per->push & NJD_DGT_TB){
					lineflag ^= 1;
					break;
			}
			
			if(per->push & NJD_DGT_TX){
					mode = 0;
			
			}
			break;

		case 3:										/* Triangle test */
			switch(mode2){
			case 0:
				Menu2();
				break;
			case 1:									/* Triangle test (Fill)*/
				Triangletest();
				if(per->push & NJD_DGT_TX){
					mode2 = 0;
				}
				break;
			case 2:									/* Triangle test (Line)*/
				Triangletest2();
				if(per->push & NJD_DGT_TX){
					mode2 = 0;
				}
				break;
			case 3:									/* Triangle test (Oldwood.pvr)*/
				Triangletest3();
				if(per->push & 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->push & NJD_DGT_TX){
					mode3 = 0;
				}
				break;
			case 2:									/* Polygon test (Line)*/
				Polygontest2();
				if(per->push & NJD_DGT_TX){
					mode3 = 0;
				}
				break;
			case 3:									/* Polygon test (Wallmap.pvr)*/
				Polygontest3();
				if(per->push & NJD_DGT_TX){
					mode3 = 0;
				}
				break;
			}
			break;
	
		case 5:										/* Circle test (Cafe.pvr used)*/
			Circletest();
			if(per->push & NJD_DGT_TX){
				mode = 0;
			}
			break;

		}
		return NJD_USER_CONTINUE;
}

void njUserExit(void)
{
	njExitTexture();
	njExitSystem();
}

void Menu(void)
{
	njPrintC(NJM_LOCATION(10,5),"TEST NUMBER?");	/* Character Strings output	*/
    if (flag == NJD_DEV_JOYSTICK) {
	njPrintC(NJM_LOCATION(10,6)," POINT     U+A");
	njPrintC(NJM_LOCATION(10,7)," LINE      R+A");
	njPrintC(NJM_LOCATION(10,8)," TRIANGLE  D+A");
	njPrintC(NJM_LOCATION(10,9)," POLYGON   L+A");
	njPrintC(NJM_LOCATION(10,10)," CIRCLE    U+B");
	njPrintC(NJM_LOCATION(10,11)," EXIT      X");
	} else {
	njPrintC(NJM_LOCATION(10,5),"TEST NUMBER?");	/* Character Strings output	*/
	njPrintC(NJM_LOCATION(10,6)," POINT     UP    ARROW + Z");
	njPrintC(NJM_LOCATION(10,7)," LINE      RIGHT ARROW + Z");
	njPrintC(NJM_LOCATION(10,8)," TRIANGLE  DOWN  ARROW + Z");
	njPrintC(NJM_LOCATION(10,9)," POLYGON   LEFT  ARROW + Z");
	njPrintC(NJM_LOCATION(10,10)," CIRCLE    UP    ARROW + X");
	njPrintC(NJM_LOCATION(10,11)," EXIT      A");
	}

	if((per->on & NJD_DGT_TA)&&(per->on & NJD_DGT_KU)){
		mode = 1;
	}
	else if((per->on & NJD_DGT_TA)&&(per->on & NJD_DGT_KR)){
		mode = 2;
	}
	else if((per->on & NJD_DGT_TA)&&(per->on & NJD_DGT_KD)){
		mode = 3;
	}
	else if((per->on & NJD_DGT_TA)&&(per->on & NJD_DGT_KL)){
		mode = 4;
	}
	else if((per->on & NJD_DGT_TB)&&(per->on & NJD_DGT_KU)){
		mode = 5;
	} 
}

void Menu2(void)
{
	njPrintC(NJM_LOCATION(10,5),"TRIANGLE TEST NUMBER?");	/* Character Strings output	*/
    if (flag == NJD_DEV_JOYSTICK) {
	njPrintC(NJM_LOCATION(10,6)," FILL      U+C");
	njPrintC(NJM_LOCATION(10,7)," LINE      R+C");
	njPrintC(NJM_LOCATION(10,8)," TEXTURE   D+C");
	njPrintC(NJM_LOCATION(10,9)," EXIT      X");
	} else {
	njPrintC(NJM_LOCATION(10,6)," FILL      UP    ARROW + C");
	njPrintC(NJM_LOCATION(10,7)," LINE      RIGHT ARROW + C");
	njPrintC(NJM_LOCATION(10,8)," TEXTURE   DOWN  ARROW + C");
	njPrintC(NJM_LOCATION(10,9)," EXIT      A");
	}
	
	if(per->push & NJD_DGT_TX){
			mode = 0;
			mode2 = 0;
	}
	else if((per->on & NJD_DGT_TC)&&(per->on & NJD_DGT_KU)){
		mode2 = 1;
	}
	else if((per->on & NJD_DGT_TC)&&(per->on & NJD_DGT_KR)){
		mode2 = 2;
	}
	else if((per->on & NJD_DGT_TC)&&(per->on & NJD_DGT_KD)){
		mode2 = 3;
	}

	
}
void Menu3(void)
{
	njPrintC(NJM_LOCATION(10,5),"POLYGON TEST NUMBER?");	/* Character Strings output	*/
    if (flag == NJD_DEV_JOYSTICK) {
	njPrintC(NJM_LOCATION(10,6)," FILL      U+C");
	njPrintC(NJM_LOCATION(10,7)," LINE      R+C");
	njPrintC(NJM_LOCATION(10,8)," TEXTURE   D+C");
	njPrintC(NJM_LOCATION(10,9)," EXIT      X");
	} else {
	njPrintC(NJM_LOCATION(10,6)," FILL      UP    ARROW + C");
	njPrintC(NJM_LOCATION(10,7)," LINE      RIGHT ARROW + C");
	njPrintC(NJM_LOCATION(10,8)," TEXTURE   DOWN  ARROW + C");
	njPrintC(NJM_LOCATION(10,9)," EXIT      A");
	}

	if(per->push & NJD_DGT_TX){
			mode = 0;
			mode3 = 0;
	}
	else if((per->on & NJD_DGT_TC)&&(per->on & NJD_DGT_KU)){
		mode3 = 1;
	}
	else if((per->on & NJD_DGT_TC)&&(per->on & NJD_DGT_KR)){
		mode3 = 2;
	}
	else if((per->on & NJD_DGT_TC)&&(per->on & NJD_DGT_KD)){
		mode3 = 3;
	}

}

void Pointtest(void)
{
	Sint32 i;

	if(lineflag){
	    if (flag == NJD_DEV_JOYSTICK) {
			njPrintC(NJM_LOCATION(4,5)," POINT TEST    STOP->B      EXIT -> X");
		} else {
			njPrintC(NJM_LOCATION(4,5)," POINT TEST    STOP->X      EXIT -> A");
		}
		for(i=0;i<point_num;i++){
			p[i].x = ((Float)njRandom() * 640.f);
			p[i].y = ((Float)njRandom() * 480.f);
			col[i].color = (Uint32)(njRandom() * 0xFFFFFFFF);
		}
	}
	else{
	    if (flag == NJD_DEV_JOYSTICK) {
			njPrintC(NJM_LOCATION(4,5)," POINT TEST    START->B      EXIT -> X");
		} else {
			njPrintC(NJM_LOCATION(4,5)," POINT TEST    START->X      EXIT -> A");
		}
	}
	p2c.p = p;
	p2c.col = col;

	/* njDrawPoint2D(a,b,c,d) */	
	/* a = always use p (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){
	    if (flag == NJD_DEV_JOYSTICK) {
			njPrintC(NJM_LOCATION(4,5)," LINE TEST    STOP->B      EXIT -> X");
		} else {
			njPrintC(NJM_LOCATION(4,5)," LINE TEST    STOP->X      EXIT -> A");
		}
		for(i=0;i<point_num;i++){
			p[i].x = ((Float)njRandom() * 640.f);
			p[i].y = ((Float)njRandom() * 480.f);
			col[i].color = (Uint32)(njRandom() * 0xFFFFFFFF);
		}
	}
	else{
	    if (flag == NJD_DEV_JOYSTICK) {
			njPrintC(NJM_LOCATION(4,5)," LINE TEST    START->B      EXIT -> X");
		} else {
			njPrintC(NJM_LOCATION(4,5)," LINE TEST    START->X      EXIT -> A");
		}
	}
	p2c.p = p;
	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)
{
    if (flag == NJD_DEV_JOYSTICK) {
		njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(FILL)      EXIT -> X");
	} else {
		njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(FILL)      EXIT -> A");
	}
	p[0].x = 100;
	p[0].y = 300;
	col[0].color = 0xFFFF00AA;

	p[1].x = 150;
	p[1].y = 100;
	col[1].color = 0x0FFFF000;

	p[2].x = 200;
	p[2].y = 300;
	col[2].color = 0x00FFFF00;

	p[3].x = 250;
	p[3].y = 100;
	col[3].color = 0xF00FFF55;

	p[4].x = 300;
	p[4].y = 300;
	col[4].color = 0xFF00FF00;

	p[5].x = 350;
	p[5].y = 100;
	col[5].color = 0xFFF00F00;

	p2c.p = p;
	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)
{
    if (flag == NJD_DEV_JOYSTICK) {
		njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(LINE)      EXIT -> X");
	} else {
		njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(LINE)      EXIT -> A");
	}
	p[0].x = 100;
	p[0].y = 300;
	col[0].color = 0xFFFF00AA;

	p[1].x = 150;
	p[1].y = 100;
	col[1].color = 0x0FFFF000;

	p[2].x = 200;
	p[2].y = 300;
	col[2].color = 0x00FFFF00;

	p[3].x = 250;
	p[3].y = 100;
	col[3].color = 0xF00FFF55;

	p[4].x = 300;
	p[4].y = 300;
	col[4].color = 0xFF00FF00;

	p[5].x = 350;
	p[5].y = 100;
	col[5].color = 0xFFF00F00;

	p2c.p = p;
	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)
{
    if (flag == NJD_DEV_JOYSTICK) {
		njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(TEXTURE)      EXIT -> X");
	} else {
		njPrintC(NJM_LOCATION(4,5)," TRIANGLE TEST(TEXTURE)      EXIT -> A");
	}
	p[0].x = 320;
	p[0].y = 240;
	col[0].color = 0xffffffff;
	tex[0].tex.u = 128;
	tex[0].tex.v = 128;

	p[1].x = 240;
	p[1].y = 50;
	col[1].color = 0xffffffff;
	tex[1].tex.u = 0;
	tex[1].tex.v = 0;

	p[2].x = 400;
	p[2].y = 50;
	col[2].color = 0xffffffff;
	tex[2].tex.u = 255;
	tex[2].tex.v = 0;

	p[3].x = 400;
	p[3].y = 430;
	col[3].color = 0xffffffff;
	tex[3].tex.u = 255;
	tex[3].tex.v = 255;

	p[4].x = 240;
	p[4].y = 430;
	col[4].color = 0xffffffff;
	tex[4].tex.u = 0;
	tex[4].tex.v = 255;

	njSetTextureNum(1);

	p2c.p = p;
	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)
{
    if (flag == NJD_DEV_JOYSTICK) {
		njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(FILL)      EXIT -> X");
	} else {
		njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(FILL)      EXIT -> A");
	}
	p[0].x = 427;
	p[0].y = 116;
	col[0].color = 0xFF000055;

	p[1].x = 555;
	p[1].y = 200;
	col[1].color = 0xFFFFFF55;

	p[2].x = 555;
	p[2].y = 285;
	col[2].color = 0x00FF0055;

	p[3].x = 427;
	p[3].y = 371;
	col[3].color = 0xFFFFFF55;

	p[4].x = 300;
	p[4].y = 285;
	col[4].color = 0x0000FF55;

	p[5].x = 300;
	p[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 = p;
	p2c.col = col;
	njDrawPolygon2D(&p2c,6,-2.f,NJD_FILL);
}

void Polygontest2(void)
{
    if (flag == NJD_DEV_JOYSTICK) {
		njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(LINE)      EXIT -> X");
	} else {
		njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(LINE)      EXIT -> A");
	}
	p[0].x = 427;
	p[0].y = 116;
	col[0].color = 0xFF000055;

	p[1].x = 555;
	p[1].y = 200;
	col[1].color = 0xFFFFFF55;

	p[2].x = 555;
	p[2].y = 285;
	col[2].color = 0x00FF0055;

	p[3].x = 427;
	p[3].y = 371;
	col[3].color = 0xFFFFFF55;

	p[4].x = 300;
	p[4].y = 285;
	col[4].color = 0x0000FF55;

	p[5].x = 300;
	p[5].y = 200;
	col[5].color = 0xFFFFFF55;

	p2c.p = p;
	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)
{
    if (flag == NJD_DEV_JOYSTICK) {
		njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(TEXTURE)      EXIT -> X");
	} else {
		njPrintC(NJM_LOCATION(4,5)," POLYGON TEST(TEXTURE)      EXIT -> A");
	}
	p[0].x = 427;
	p[0].y = 116;
	col[0].color = 0xffffffff;
	tex[0].tex.u = 127;
	tex[0].tex.v = 0;

	p[1].x = 555;
	p[1].y = 200;
	col[1].color = 0xffffffff;
	tex[1].tex.u = 255;
	tex[1].tex.v = 84;

	p[2].x = 555;
	p[2].y = 285;
	col[2].color = 0xffffffff;
	tex[2].tex.u = 255;
	tex[2].tex.v = 169;

	p[3].x = 427;
	p[3].y = 371;
	col[3].color = 0xffffffff;
	tex[3].tex.u = 127;
	tex[3].tex.v = 255;

	p[4].x = 300;
	p[4].y = 285;
	col[4].color = 0xffffffff;
	tex[4].tex.u = 0;
	tex[4].tex.v = 169;

	p[5].x = 300;
	p[5].y = 200;
	col[5].color = 0xffffffff;
	tex[5].tex.u = 0;
	tex[5].tex.v = 84;

	njSetTextureNum(2);

	p2c.p = p;
	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;

    if (flag == NJD_DEV_JOYSTICK) {
		njPrintC(NJM_LOCATION(4,5)," CIRCLE TEST    EXIT-> X");
	} else {
		njPrintC(NJM_LOCATION(4,5)," CIRCLE TEST    EXIT-> A");
	}
	p[0].x = 0;
	p[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){
		p[1].x = (float)((x - 420)*4/3);
		p[1].y = (float)(x - 420);
		x += 1;
	}
	else if(420 < i && i <= 840){
		p[1].x = (float)((x - 420)*4/3);
		p[1].y = (float)(x - 420);
		x -= 1;
	}	
	else if(i > 840){
		i = 0;
	}
	i++;
	njSetTextureNum(0);

	p2c.p = p;
	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);
}
