#include <GameOS\GameOS.hpp>
#include <stdlib.h>
#include <memory.h>
#include <GameOS\ToolOs.hpp>
//
//
//
// Testing shell for 3D routines
//
//
//

//#define FLIP_VIEWPORT			// 0,0 in bottom left when enabled, normally top right is 0,0


#define NumLines 300			// Number of points and lines


int Lines[NumLines][6];
HGOSFONT3D Font_Arial8;
DWORD Test;
int Polys;
int Seed;
double lastframe;
DWORD Average;
DWORD frame;
float TotalPolys;
double TotalFrameRate;
DWORD tex1=0,tex2,tex3,tex4,tex5,tex6,tex7,tex8,tex9,tex10,tex11,tex12,tex13,tex14,tex15,tex16,tex17;

unsigned int PixDouble = 0;
unsigned int zwrite = 0, zcompare = 0, dotex = 0, dogouraud = 0, doalphamodulate = 0, do11 = 0, dopersp = 1;
unsigned int dofog = 0, doalphainvalpha = 0;

DWORD ColorTable[8]={ 0xff000000, 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffff00, 0xff00ffff, 0xffff00ff, 0xffffffff };

gos_CycleData   goscdTriangleTime;

DWORD fogColor = 0x00007FFF;


//
//
DWORD AlphaTexture;
DWORD TestTexture;
DWORD TestMipMap;
DWORD WhichTexture=0;
DWORD BackGroundColor;
float Size;
float Step;
float SafeSize;
float ViewportMulX,ViewportAddX,ViewportMulY,ViewportAddY;
DWORD UseTexture;
const float Xpixels=(1.0f/640.0f);
const float Ypixels=(1.0f/480.0f);




void TestStackWalk()
{
	*(char*)0=0;
}


void DrawTriangleAt(float x0, float y0, float x1, float y1, float x2, float y2,
                    DWORD frgb1, DWORD frgb2, DWORD frgb3)
{
    int t1 = 0;
    float z = 0.5f;
	gos_VERTEX v[16*3];

	memset( v, 0, 48*sizeof(gos_VERTEX) );

	v[t1].argb		= 0xFFFF0000;
    v[t1+1].argb	= 0xff00ff00;
	v[t1+2].argb	= 0xff0000ff;

    v[t1].frgb = frgb1;
    v[t1+1].frgb = frgb2;
    v[t1+2].frgb = frgb3;

	v[t1].rhw		= 1.0;
	v[t1+1].rhw		= 1.0;
	v[t1+2].rhw		= 1.0;

	v[t1+1].v		= 1.0;
	v[t1+2].u		= 1.0;
	v[t1+2].v		= 1.0;

	v[t1].x		= (x0*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
	v[t1].y		= (y0*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
    v[t1].z = z;

	v[t1+1].x	= (x1*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
	v[t1+1].y	= (y1*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
    v[t1+1].z = z;

	v[t1+2].x	= (x2*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
	v[t1+2].y	= (y2*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
    v[t1+2].z = z;

	gos_DrawTriangles( v, 3 );
}


    
void DoQuad( DWORD l, DWORD t, DWORD r, DWORD b, float w, bool TestAddress=false )
{
	gos_VERTEX pVertices[10];

	r=(DWORD)(l+(r-l)*w);
	b=(DWORD)(t+(b-t)*w);

	gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );

    pVertices[0].x		= ((l*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[0].y		= ((t*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[0].z		= 0.1f;
	pVertices[0].rhw	= w;
	pVertices[0].argb	= TestAddress ? 0xffffffff : 0x000000ff;
	pVertices[0].frgb	= 0;

	pVertices[1].x		= ((l*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[1].y		= ((b*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[1].z		= 0.1f;
	pVertices[1].rhw	= w;
	pVertices[1].argb	= TestAddress ? 0xffffffff : 0x0000ff00;
	pVertices[1].frgb	= 0;

	pVertices[2].x		= ((r*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[2].y		= ((b*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[2].z		= 0.1f;
	pVertices[2].rhw	= w;
	pVertices[2].argb	= TestAddress ? 0xffffffff : 0xffff0000;
	pVertices[2].frgb	= 0;

	pVertices[3].x		= ((r*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[3].y		= ((t*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[3].z		= 0.1f;
	pVertices[3].rhw	= w;
	pVertices[3].argb	= TestAddress ? 0xffffffff : 0xffffffff;
	pVertices[3].frgb	= 0;

	if( TestAddress )
	{
		pVertices[0].u		= -2.0f;
		pVertices[0].v		= -2.0f;
		pVertices[1].u		= -2.0f;
		pVertices[1].v		= 2.0f;
		pVertices[2].u		= 2.0f;
		pVertices[2].v		= 2.0f;
		pVertices[3].u		= 2.0f;
		pVertices[3].v		= -2.0f;
	}
	else
	{
		pVertices[0].u		= 0.0f;
		pVertices[0].v		= 0.0f;
		pVertices[1].u		= 0.0f;
		pVertices[1].v		= 1.0f;
		pVertices[2].u		= 1.0f;
		pVertices[2].v		= 1.0f;
		pVertices[3].u		= 1.0f;
		pVertices[3].v		= 0.0f;
	}

	gos_DrawQuads( pVertices, 4 );
}

void tDrawLine( int X0, int Y0, int X1, int Y1, DWORD Color )
{
	gos_VERTEX v[2];

	memset( v,0,sizeof(v) );
	v[0].rhw		= 1.0f;
	v[1].rhw		= 1.0f;

	v[0].x		= ((float)X0) * (PixDouble ? 0.5f : 1.0f);
	v[0].y		= ((float)Y0) * (PixDouble ? 0.5f : 1.0f);;
	v[0].argb	= Color;

	v[1].x		= ((float)X1) * (PixDouble ? 0.5f : 1.0f);;
	v[1].y		= ((float)Y1) * (PixDouble ? 0.5f : 1.0f);;
	v[1].argb	= Color;

	gos_DrawLines( v, 2 );
}				

void tDrawPoint( int X0, int Y0, DWORD Color )
{
	gos_VERTEX v[1];

	memset( v,0,sizeof(v) );
	v[0].rhw		= 1.0f;

	v[0].x		= ((float)X0) * (PixDouble ? 0.5f : 1.0f);;
	v[0].y		= ((float)Y0) * (PixDouble ? 0.5f : 1.0f);;
	v[0].argb	= Color;

	gos_DrawPoints( v, 1 );
}				


void DofQuad( DWORD l, DWORD t, DWORD r, DWORD b, float w, DWORD Fog=0xffffffff )
{
	gos_VERTEX pVertices[10];

	r=(DWORD)(l+(r-l)*w);
	b=(DWORD)(t+(b-t)*w);

	gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );

	pVertices[0].x		= ((l*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[0].y		= ((t*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);
	pVertices[0].z		= 1.0f-w;
	pVertices[0].rhw	= w;
	pVertices[0].u		= 0.0;
	pVertices[0].v		= 0.0;
	pVertices[0].argb	= 0xffffffff;
	pVertices[0].frgb	= Fog;

	pVertices[1].x		= ((l*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);;
	pVertices[1].y		= ((b*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);;
	pVertices[1].z		= 1.0f-w;
	pVertices[1].rhw	= w;
	pVertices[1].u		= 0.0;
	pVertices[1].v		= 1.0;
	pVertices[1].argb	= 0xffffffff;
	pVertices[1].frgb	= Fog;

	pVertices[2].x		= ((r*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);;
	pVertices[2].y		= ((b*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);;
	pVertices[2].z		= 1.0f-w;
	pVertices[2].rhw	= w;
	pVertices[2].u		= 1.0;
	pVertices[2].v		= 1.0;
	pVertices[2].argb	= 0xffffffff;
	pVertices[2].frgb	= Fog;

	pVertices[3].x		= ((r*Xpixels)*ViewportMulX + ViewportAddX-0.5f) * (PixDouble ? 0.5f : 1.0f);;
	pVertices[3].y		= ((t*Ypixels)*ViewportMulY + ViewportAddY-0.5f) * (PixDouble ? 0.5f : 1.0f);;
	pVertices[3].z		= 1.0f-w;
	pVertices[3].rhw	= w;
	pVertices[3].u		= 1.0;
	pVertices[3].v		= 0.0;
	pVertices[3].argb	= 0xffffffff;
	pVertices[3].frgb	= Fog;

	gos_DrawQuads( pVertices, 4 );
}








//
//
//
void __stdcall UpdateRenderers()
{
//
// Only allow pixel doubling in software
//
	if( Environment.Renderer!=3 )
		PixDouble=0;		
//
// Always use the same random numbers
//
	gos_srand( Seed );
//
// Make viewport the whole screen
//
	#ifdef FLIP_VIEWPORT
	gos_SetupViewport( 1,1.0,1,BackGroundColor, 1.0, 0.0, 0.0, 1.0 );
	#else
	gos_SetupViewport( 1,1.0,1,BackGroundColor, 0.0, 0.0, 1.0, 1.0 );
	#endif

    // enable pixel doubling if desired
    gos_SetRenderState(gos_State_MonoEnable, 0 | (PixDouble ? 0x100 : 0));

//
// 128 Pixel triangles
//
	if( Test==0 )
	{
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=Polys;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d - 128 Pixel %s Triangles",Polys,UseTexture ? "Textured" : "" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_MonoEnable, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

		for( int t0=0; t0<Polys; t0+=16 )
		{
			for( int t1=0; t1<48; t1+=3 )
			{
				float xo=0.2f+gos_rand()*(0.6f/32768.0f);
				float yo=0.2f+gos_rand()*(0.6f/32768.0f);
			
				v[t1].argb		= 0xffff0000;
				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= 1.0;
				v[t1+1].rhw		= 1.0;
				v[t1+2].rhw		= 1.0;

				v[t1+1].v		= 1.0;
				v[t1+2].u		= 1.0;
				v[t1+2].v		= 1.0;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+1].x	= ((xo+Xpixels*16)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);;
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);;

				v[t1+2].x	= ((xo+Xpixels*16)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*16)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
			}
			gos_DrawTriangles( v, 48 );
		}
	}
//
// 32768 Pixel triangles
//
	if( Test==7 )
	{
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=Polys/20;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d - 32768 Pixel %s Triangles",Polys/20,UseTexture ? "Textured" : "" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_MonoEnable, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

		for( int t0=0; t0<Polys/20; t0+=16 )
		{
			for( int t1=0; t1<48; t1+=3 )
			{
				float xo=0.1f+gos_rand()*(0.4f/32768.0f);
				float yo=0.1f+gos_rand()*(0.25f/32768.0f);
			
				v[t1].argb		= 0xffff0000;
				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= 1.0;
				v[t1+1].rhw		= 1.0;
				v[t1+2].rhw		= 1.0;

				v[t1+1].v		= 1.0;
				v[t1+2].u		= 1.0;
				v[t1+2].v		= 1.0;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+1].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+2].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*256)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
			}
			gos_DrawTriangles( v, 48 );
		}
	}
//
// Testing alpha modes
//
	if( Test==1 )
	{
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "Alpha modes" );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		extern DWORD ValidatePasses;
		extern DWORD ValidateResult;
		char Tex10,Tex11,Tex1I,TexAI;

		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulateAlpha );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, TestTexture );
		gos_SetRenderState( gos_State_Perspective, 0);

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		Tex10=ValidateResult ? '0' : '1';
		DoQuad( 64+128*0,20, 64+128*0+256,256, 0.5 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneOne );
		Tex11=ValidateResult ? '0' : '1';
		DoQuad( 64+128*1,20, 64+128*1+256,256, 0.5 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneInvAlpha );
		Tex1I=ValidateResult ? '0' : '1';
		DoQuad( 64+128*2,20, 64+128*2+256,256, 0.5 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
		TexAI=ValidateResult ? '0' : '1';
		DoQuad( 64+128*3,20, 64+128*3+256,256, 0.5 );

		gos_SetRenderState( gos_State_Texture, 0 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		Tex10=ValidateResult ? '0' : '1';
		DoQuad( 64+128*0,256, 64+128*0+256,512, 0.5 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneOne );
		Tex11=ValidateResult ? '0' : '1';
		DoQuad( 64+128*1,256, 64+128*1+256,512, 0.5 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneInvAlpha );
		Tex1I=ValidateResult ? '0' : '1';
		DoQuad( 64+128*2,256, 64+128*2+256,512, 0.5 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
		TexAI=ValidateResult ? '0' : '1';
		DoQuad( 64+128*3,256, 64+128*3+256,512, 0.5 );

		gos_TextSetPosition( 64+128*0+10,150 );
		gos_TextDraw("OneZero %c",Tex10 );

		gos_TextSetPosition( 64+128*1+10,150 );
		gos_TextDraw("OneOne %c",Tex11 );

		gos_TextSetPosition( 64+128*2+10,150 );
		gos_TextDraw("OneInvAlpha %c",Tex1I );

		gos_TextSetPosition( 64+128*3+10,150 );
		gos_TextDraw("AlphaInvAlpha %c",TexAI );

		gos_TextSetPosition( 64+128*0+10,406 );
		gos_TextDraw("OneZero %c",Tex10 );

		gos_TextSetPosition( 64+128*1+10,406 );
		gos_TextDraw("OneOne %c",Tex11 );

		gos_TextSetPosition( 64+128*2+10,406 );
		gos_TextDraw("OneInvAlpha %c",Tex1I );

		gos_TextSetPosition( 64+128*3+10,406 );
		gos_TextDraw("AlphaInvAlpha %c",TexAI );
		gos_SetRenderState( gos_State_Perspective, 1);
	}

//
// Testing filtering modes
//
	if( Test==2 )
	{
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "Point, BiLinear and Trilinear filtering" );
		gos_TextSetPosition( 380,440 );
		gos_TextDraw( "F - Fog on/off" );
		gos_TextSetPosition( 380,450 );
		gos_TextDraw( "M - Different Texture" );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);

		gos_SetRenderState( gos_State_AlphaTest, 0 );
		gos_SetRenderState( gos_State_Perspective, 0 );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_Texture, WhichTexture ? TestMipMap : TestTexture );
		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulateAlpha );
		gos_SetRenderState( gos_State_Filter, gos_FilterBiLinear );

		DofQuad( 12,40, 268,296, 1.0, 0 );                  // full fog
		DofQuad( 278,40, 534,296, 0.5, 0x40000000 );        // three quarters fog
		DofQuad( 416,40, 672,296, 0.25, 0x80000000 );		// Half fog
		DofQuad( 490,40, 746,296, 0.125 );					// No fog

		SafeSize=Size;

		gos_SetRenderState( gos_State_ZCompare, 0 );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		DofQuad( (DWORD)(10+128*(1.0-SafeSize)), (DWORD)(214+128*(1.0-SafeSize)), (DWORD)(266+128*(1.0-SafeSize)), (DWORD)(470+128*(1.0-SafeSize)), SafeSize );

		gos_SetRenderState( gos_State_Filter, gos_FilterBiLinear );
		DofQuad( (DWORD)(192+128*(1.0-SafeSize)), (DWORD)(214+128*(1.0-SafeSize)), (DWORD)(448+128*(1.0-SafeSize)), (DWORD)(470+128*(1.0-SafeSize)), SafeSize );

		gos_SetRenderState( gos_State_Filter, gos_FilterTriLinear );
		DofQuad( (DWORD)(374+128*(1.0-SafeSize)), (DWORD)(214+128*(1.0-SafeSize)), (DWORD)(630+128*(1.0-SafeSize)), (DWORD)(470+128*(1.0-SafeSize)), SafeSize );

		gos_SetRenderState( gos_State_ZCompare, 1 );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_Perspective, 1);
	}


//
// Test TEXT
//
	if( Test==3 )
	{
		char TestString[]="This is a test string of TEXT, 0123456789 !@#$%^&*()_+-= ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "Text Test" );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );
//
//
//
		gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
//
// Proportional
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 20,40 );
		gos_TextDraw( TestString );
//
// Non-Proportional
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 0, 0, 0 );
		gos_TextSetPosition( 20,60 );
		gos_TextDraw( TestString );
//
// Bold
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 1, 0 );
		gos_TextSetPosition( 20,80 );
		gos_TextDraw( TestString );
//
// Italic
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 1 );
		gos_TextSetPosition( 20,100 );
		gos_TextDraw( TestString );
//
// Bold+Italic
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 1, 1 );
		gos_TextSetPosition( 20,120 );
		gos_TextDraw( TestString );
//
// 0.9* size
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -7, 0, 1, 0, 0 );
		gos_TextSetPosition( 20,140 );
		gos_TextDraw( TestString );
//
// 2.0* size
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -16, 0, 1, 0, 0 );
		gos_TextSetPosition( 20,160 );
		gos_TextDraw( TestString );
//
// 3.0* size
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -24, 0, 1, 0, 0 );
		gos_TextSetPosition( 20,190 );
		gos_TextDraw( TestString );
//
// 3.0* size
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -24, 0, 1, 1, 1 );
		gos_TextSetPosition( 20,230 );
		gos_TextDraw( TestString );
//
// Regions
//
		gos_TextSetRegion( 48, 270, 398, 290 );
		gos_TextDrawBackground( 48, 270, 398, 290, 0xff00c000 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 20, 265 );
		gos_TextDraw( TestString );
		gos_TextSetPosition( 20, 275 );
		gos_TextDraw( TestString );
		gos_TextSetPosition( 20, 285 );
		gos_TextDraw( TestString );
//
// Text Size
//
		char TestLenString[]="This is a test of lengths OK?";
		DWORD Width,Height;
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextStringLength( &Width, &Height, TestLenString );
		gos_TextSetRegion( 20, 295, 20+Width, 295+Height );
		gos_TextDrawBackground( 20, 295, 20+Width, 295+Height, 0xffc00000 );
		gos_TextSetPosition( 20, 295 );
		gos_TextDraw( TestLenString );
//
// Text Size
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -16, 0, 1, 1, 1 );
		gos_TextStringLength( &Width, &Height, TestLenString );
		gos_TextSetRegion( 20, 315, 20+Width, 315+Height );
		gos_TextDrawBackground( 20, 315, 20+Width, 315+Height, 0xffc00000 );
		gos_TextSetPosition( 20, 315 );
		gos_TextDraw( TestLenString );
//
// Text Size with line breaks
//
		char TestLenString1[]="This is\na test of lengths\nOK?";
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextStringLength( &Width, &Height, TestLenString1 );
		gos_TextSetRegion( 20, 345, 20+Width, 345+Height );
		gos_TextDrawBackground( 20, 345, 20+Width, 345+Height, 0xff0000c0 );
		gos_TextSetPosition( 20, 345 );
		gos_TextDraw( TestLenString1 );
//
// Text word wrap
//
		char TestLenString2[]="This is a test of word wrapping to see if it works correctly ok?";
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 1, 1, 0, 0 );
		Width=128;
		Height=80;
		gos_TextSetRegion( 420, 295, 420+Width, 295+Height );
		gos_TextDrawBackground( 420, 295, 420+Width, 295+Height, 0xff0000c0 );
		gos_TextSetPosition( 420, 295 );
		gos_TextDraw( TestLenString2 );
//
// Text Size with control codes
//
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetRegion( 0, 0, Environment.screenWidth, Environment.screenHeight );
		gos_TextSetPosition( 20, 395 );
		gos_TextDraw( "Testing  //color /test / ok /color=ffff0000RED/color=ff00ff00GREEN/color=ff0000ffBLUE/color=80ffffff ALPHA /color=ffffffff/bold=1BOLD/bold=0 /proportional=0Fixed Space/proportional=1 /italic=1Italic/italic=0  Normal text" );
	}
//
// 1 Pixel triangles
//
	if( Test==4 )
	{
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=Polys*2;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d - 1 Pixel %s Triangles",Polys*2 ,UseTexture ? "Textured" : "" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_MonoEnable, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

		for( int t0=0; t0<Polys*2; t0+=16 )
		{
			for( int t1=0; t1<48; t1+=3 )
			{
				float xo=0.2f+gos_rand()*(0.6f/32768.0f);
				float yo=0.2f+gos_rand()*(0.6f/32768.0f);
			
				v[t1].argb		= 0xffff0000;
				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= 1.0;
				v[t1+1].rhw		= 1.0;
				v[t1+2].rhw		= 1.0;

				v[t1+1].v		= 1.0;
				v[t1+2].u		= 1.0;
				v[t1+2].v		= 1.0;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+1].x	= ((xo+Xpixels*1)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+2].x	= ((xo+Xpixels*1)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*1)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

			}
			gos_DrawTriangles( v, 48 );
		}
	}

//
// 1 Pixel triangles in 16 vertex indexed array
//
	if( Test==8 )
	{
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[18];
		WORD Indices[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d - 1 Pixel %s Tris as 16 triangles in 18 vertex array",Polys*4 ,UseTexture ? "Textured" : "" );

		WORD vertex=0;
		for( int t1=0;t1<16;t1++ )
		{
			Indices[t1*3]=vertex;
			Indices[t1*3+1]=(WORD)(vertex+1);
			Indices[t1*3+2]=(WORD)(vertex+2);
			vertex++;
		}

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=Polys*4;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_MonoEnable, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate );

		memset( v, 0, sizeof(v) );

		for( int t0=0; t0<Polys*4; t0+=16 )
		{
			float xo=0.2f+gos_rand()*(0.6f/32768.0f);
			float yo=0.2f+gos_rand()*(0.6f/32768.0f);
			float skipx=0.0f;
			float skipy=0.0f;
			DWORD Color=0xffff0000;

			float uv=1.0;			

			for( int t1=0; t1<18; t1++ )
			{
				v[t1].argb		= Color;
				v[t1].rhw		= 1.0f;
				v[t1].u			= 1.0f-uv;
				v[t1].v			= uv;
				uv=1.0f-uv;
				Color^=0x00ffffff;

				v[t1].x		= ((xo+skipx*Xpixels)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo+skipy*Ypixels)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				skipx+=1.0f;
				skipy=1.0f-skipy;
			}
			gos_RenderIndexedArray( v, 18, Indices, 48 );
		}
	}

//
// Testing modulate modes
//
	if( Test==9 )
	{
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "Modulate / Addressing modes" );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );
		gos_SetRenderState( gos_State_Perspective, 0);
//
// Test modulate modes
//
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, TestTexture );
		gos_SetRenderState( gos_State_MonoEnable, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud );
		gos_SetRenderState( gos_State_Perspective, 0 );

		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendDecal );
		DoQuad( 64+128*0,20, 64+128*0+256,256, 0.5 );

		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate );
		DoQuad( 64+128*1,20, 64+128*1+256,256, 0.5 );

		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulateAlpha );
		DoQuad( 64+128*2,20, 64+128*2+256,256, 0.5 );

		gos_SetRenderState( gos_State_Texture, AlphaTexture );

		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendDecal );
		DoQuad( 64+128*0,256, 64+128*0+256,512, 0.5 );

		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate );
		DoQuad( 64+128*1,256, 64+128*1+256,512, 0.5 );

		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulateAlpha );
		DoQuad( 64+128*2,256, 64+128*2+256,512, 0.5 );
//
// Test wrap and clamp
//
		gos_SetRenderState( gos_State_Texture, TestTexture );
		gos_SetRenderState( gos_State_TextureAddress, gos_TextureWrap );
		DoQuad( 64+128*3,20, 64+128*3+256,256, 0.5, true );
		gos_SetRenderState( gos_State_TextureAddress, gos_TextureClamp );
		DoQuad( 64+128*3,256, 64+128*3+256,512, 0.5, true );
		gos_SetRenderState( gos_State_TextureAddress, gos_TextureWrap );

		gos_TextSetPosition( 64+128*0+10,150 );
		gos_TextDraw( "Decal" );

		gos_TextSetPosition( 64+128*1+10,150 );
		gos_TextDraw( "Modulate" );

		gos_TextSetPosition( 64+128*2+10,150 );
		gos_TextDraw( "ModulateAlpha" );

		gos_TextSetPosition( 64+128*3+10,150 );
		gos_TextDraw( "Wrap" );

		gos_TextSetPosition( 64+128*0+10,406 );
		gos_TextDraw( "Decal" );

		gos_TextSetPosition( 64+128*1+10,406 );
		gos_TextDraw( "Modulate" );

		gos_TextSetPosition( 64+128*2+10,406 );
		gos_TextDraw( "ModulateAlpha" );

		gos_TextSetPosition( 64+128*3+10,406 );
		gos_TextDraw( "Clamp" );
		gos_SetRenderState( gos_State_Perspective, 1);
	}

	if( Test==10 )
	{
        int lpolys = 5000, trisize = 10;
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d Medium (%dx%d) Triangles Benchmark", lpolys, trisize, trisize );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );

        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

        StartCycleTiming( &goscdTriangleTime );

		gos_SetRenderState(gos_State_Texture, tex1 );

        gos_SetRenderState(gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);
        gos_SetRenderState(gos_State_MonoEnable, 1 | (PixDouble ? 0x100 : 0));

		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=lpolys;
		TotalFrameRate+=framerate;
		Average=(DWORD)(TotalPolys/TotalFrameRate);
		TotalPolys=0;
		TotalFrameRate=0.0;
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? 2 : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_AlphaTest, 0 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

        gos_srand( 1 );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

		for( int t0=0; t0<lpolys; t0+=16 )
		{
			for( int t1=0; t1<48; t1+=6 )
			{
				float xo=0.1f+gos_rand()*(0.8f/32768.0f);
				float yo=0.1f+gos_rand()*(0.8f/32768.0f);
			
				v[t1].argb		= 0xffff0000;
				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= (float)0.38911;
				v[t1+1].rhw		= (float)0.54037;
				v[t1+2].rhw		= (float)0.65915;

                v[t1].frgb      = 0xff000000;
                v[t1+1].frgb    = 0xff000000;
                v[t1+2].frgb    = 0xff000000;

				v[t1+1].v		= 0.5;
				v[t1+2].u		= 0.5;
				v[t1+2].v		= 0.5;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo+Ypixels*trisize/2.0f)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+1].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+2].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				xo=0.1f+gos_rand()*(0.8f/32768.0f);
				yo=0.1f+gos_rand()*(0.8f/32768.0f);
			
				v[t1+3].argb		= 0xffff0000;
				v[t1+4].argb	= 0xff00ff00;
				v[t1+5].argb	= 0xff0000ff;
				v[t1+3].rhw		= (float)0.38911;
				v[t1+4].rhw		= (float)0.54037;
				v[t1+5].rhw		= (float)0.65915;

                v[t1+3].frgb    = 0xff000000;
                v[t1+4].frgb    = 0xff000000;
                v[t1+5].frgb    = 0xff000000;

				v[t1+4].v		= 0.5;
				v[t1+5].u		= 0.5;
				v[t1+5].v		= 0.5;

				v[t1+3].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+3].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+4].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+4].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+5].x	= ((xo+Xpixels*trisize/2.0f)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+5].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

			}
			gos_DrawTriangles( v, 48 );
		}

		gos_SetRenderState(gos_State_ShadeMode, gos_ShadeFlat);
        gos_SetRenderState(gos_State_TextureMapBlend, gos_BlendDecal);
        gos_SetRenderState(gos_State_MonoEnable, 0 | (PixDouble ? 0x100 : 0));
        gos_SetRenderState( gos_State_Fog, 0 );

        EndCycleTiming( &goscdTriangleTime );
    }

	if( Test==11 )
	{
        int lpolys = 50000, trisize = 2;
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d Small (%dx%d) Triangles Benchmark", lpolys, trisize, trisize );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );

        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

        StartCycleTiming( &goscdTriangleTime );

		gos_SetRenderState( gos_State_Texture, tex1 );

        gos_SetRenderState(gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);
        gos_SetRenderState(gos_State_MonoEnable, 1 | (PixDouble ? 0x100 : 0));

		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=lpolys;
		TotalFrameRate+=framerate;
		Average=(DWORD)(TotalPolys/TotalFrameRate);
		TotalPolys=0;
		TotalFrameRate=0.0;
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? 2 : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_AlphaTest, 0 );

        gos_srand( 1 );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

		for( int t0=0; t0<lpolys; t0+=16 )
		{
			for( int t1=0; t1<48; t1+=6 )
			{
				float xo=0.1f+gos_rand()*(0.8f/32768.0f);
				float yo=0.1f+gos_rand()*(0.8f/32768.0f);
			
				v[t1].argb		= 0xffff0000;
				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= (float)0.38911;
				v[t1+1].rhw		= (float)0.54037;
				v[t1+2].rhw		= (float)0.65915;

                v[t1].frgb      = 0xff000000;
                v[t1+1].frgb    = 0xff000000;
                v[t1+2].frgb    = 0xff000000;

				v[t1+1].v		= 0.5;
				v[t1+2].u		= 0.5;
				v[t1+2].v		= 0.5;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo+Ypixels*trisize/2.0f)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+1].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+2].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				xo=0.1f+gos_rand()*(0.8f/32768.0f);
				yo=0.1f+gos_rand()*(0.8f/32768.0f);
			
				v[t1+3].argb		= 0xffff0000;
				v[t1+4].argb	= 0xff00ff00;
				v[t1+5].argb	= 0xff0000ff;
				v[t1+3].rhw		= (float)0.38911;
				v[t1+4].rhw		= (float)0.54037;
				v[t1+5].rhw		= (float)0.65915;

                v[t1+3].frgb    = 0xff000000;
                v[t1+4].frgb    = 0xff000000;
                v[t1+5].frgb    = 0xff000000;

				v[t1+4].v		= 0.5;
				v[t1+5].u		= 0.5;
				v[t1+5].v		= 0.5;

				v[t1+3].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+3].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+4].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+4].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+5].x	= ((xo+Xpixels*trisize/2.0f)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+5].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

			}
			gos_DrawTriangles( v, 48 );
		}

		gos_SetRenderState( gos_State_Perspective, 1);

		gos_SetRenderState(gos_State_ShadeMode, gos_ShadeFlat);
        gos_SetRenderState(gos_State_TextureMapBlend, gos_BlendDecal);
        gos_SetRenderState(gos_State_MonoEnable, 0 | (PixDouble ? 0x100 : 0));
        gos_SetRenderState( gos_State_Fog, 0 );

        EndCycleTiming( &goscdTriangleTime );
    }

	if( Test==12 )
	{
        int lpolys = 50, trisize = 256;
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d Large (%dx%d) Triangles Benchmark", lpolys, trisize, trisize );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );

        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

        StartCycleTiming( &goscdTriangleTime );

		gos_SetRenderState( gos_State_Texture, tex1 );

        gos_SetRenderState(gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);
        gos_SetRenderState(gos_State_MonoEnable, 1 | (PixDouble ? 0x100 : 0));

		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=lpolys;
		TotalFrameRate+=framerate;
		Average=(DWORD)(TotalPolys/TotalFrameRate);
		TotalPolys=0;
		TotalFrameRate=0.0;
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_AlphaTest, 0 );

        int Texlist[16];
        Texlist[0] = tex1;
        Texlist[1] = tex2;
        Texlist[2] = tex3;
        Texlist[3] = tex4;
        Texlist[4] = tex5;
        Texlist[5] = tex6;
        Texlist[6] = tex7;
        Texlist[7] = tex8;

		memset( v, 0, 30*sizeof(gos_VERTEX) );

        int x = 0;

        gos_srand( 1 );

		for( int t0=0; t0<lpolys; t0+=1 )
		{
			for( int t1=0; t1<3; t1+=3 )
			{
                x++;
                if (x>7)
                    x=0;

                gos_SetRenderState( gos_State_Texture, UseTexture ? Texlist[x] : 0 );

				float xo=0.1f+gos_rand()*(0.2f/32768.0f);
				float yo=0.1f+gos_rand()*(0.2f/32768.0f);
			
				v[t1].argb		= 0xffff0000;
				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= (float)0.38911;
				v[t1+1].rhw		= (float)0.54037;
				v[t1+2].rhw		= (float)0.65915;

                v[t1].frgb      = 0xff000000;
                v[t1+1].frgb    = 0xff000000;
                v[t1+2].frgb    = 0xff000000;

				v[t1].u		    = 0.5f;
				v[t1].v		    = 0.0f;
				v[t1+1].u		= 1.0f;
				v[t1+1].v		= 1.0f;
				v[t1+2].u		= 0.0f;
				v[t1+2].v		= 1.0f;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo+Ypixels*trisize/2.0f)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+1].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+2].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				xo=0.1f+gos_rand()*(0.8f/32768.0f);
				yo=0.1f+gos_rand()*(0.8f/32768.0f);
			
				v[t1+3].argb		= 0xffff0000;
				v[t1+4].argb	= 0xff00ff00;
				v[t1+5].argb	= 0xff0000ff;
				v[t1+3].rhw		= (float)0.38911;
				v[t1+4].rhw		= (float)0.54037;
				v[t1+5].rhw		= (float)0.65915;

                v[t1+3].frgb    = 0xff000000;
                v[t1+4].frgb    = 0xff000000;
                v[t1+5].frgb    = 0xff000000;

				v[t1+4].v		= 0.5;
				v[t1+5].u		= 0.5;
				v[t1+5].v		= 0.5;

				v[t1+3].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+3].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+4].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+4].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+5].x	= ((xo+Xpixels*trisize/2.0f)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+5].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

			}
			gos_DrawTriangles( v, 3 );
		}

		gos_SetRenderState(gos_State_ShadeMode, gos_ShadeFlat);
        gos_SetRenderState(gos_State_TextureMapBlend, gos_BlendDecal);
        gos_SetRenderState(gos_State_MonoEnable, 0 | (PixDouble ? 0x100 : 0));
        gos_SetRenderState( gos_State_Fog, 0 );

        EndCycleTiming( &goscdTriangleTime );
    }

	if( Test==13 )
	{
        int lpolys = 50, trisize = 256;
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d Large (%dx%d) Triangles Benchmark", lpolys, trisize, trisize );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );

        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

        StartCycleTiming( &goscdTriangleTime );

		gos_SetRenderState( gos_State_Texture, tex1 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

        gos_SetRenderState(gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);
        gos_SetRenderState(gos_State_MonoEnable, 1 | (PixDouble ? 0x100 : 0));

		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=lpolys;
		TotalFrameRate+=framerate;
		Average=(DWORD)(TotalPolys/TotalFrameRate);
		TotalPolys=0;
		TotalFrameRate=0.0;
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_AlphaTest, 0 );

        int Texlist[16];
        Texlist[0] = AlphaTexture;
        Texlist[1] = AlphaTexture;
        Texlist[2] = AlphaTexture;
        Texlist[3] = AlphaTexture;
        Texlist[4] = AlphaTexture;
        Texlist[5] = AlphaTexture;
        Texlist[6] = AlphaTexture;
        Texlist[7] = AlphaTexture;

		memset( v, 0, 30*sizeof(gos_VERTEX) );

        int x = 0;

        gos_srand( 1 );

		for( int t0=0; t0<lpolys; t0+=1 )
		{
			for( int t1=0; t1<3; t1+=3 )
			{
                x++;
                if (x>7)
                    x=0;

                gos_SetRenderState( gos_State_Texture, UseTexture ? Texlist[x] : 0 );

				float xo=0.1f+gos_rand()*(0.2f/32768.0f);
				float yo=0.1f+gos_rand()*(0.2f/32768.0f);
			
				v[t1].argb		= 0xffff0000;
				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= (float)0.38911;
				v[t1+1].rhw		= (float)0.54037;
				v[t1+2].rhw		= (float)0.65915;

                v[t1].frgb      = 0xff000000;
                v[t1+1].frgb    = 0xff000000;
                v[t1+2].frgb    = 0xff000000;

				v[t1].u		    = 0.0f;
				v[t1].v		    = 0.5f;
				v[t1+1].u		= 1.0f;
				v[t1+1].v		= 0.0f;
				v[t1+2].u		= 1.0f;
				v[t1+2].v		= 1.0f;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo+Ypixels*trisize/2.0f)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+1].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+2].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				xo=0.1f+gos_rand()*(0.8f/32768.0f);
				yo=0.1f+gos_rand()*(0.8f/32768.0f);
			
				v[t1+3].argb		= 0xffff0000;
				v[t1+4].argb	= 0xff00ff00;
				v[t1+5].argb	= 0xff0000ff;
				v[t1+3].rhw		= (float)0.38911;
				v[t1+4].rhw		= (float)0.54037;
				v[t1+5].rhw		= (float)0.65915;

                v[t1+3].frgb    = 0xff000000;
                v[t1+4].frgb    = 0xff000000;
                v[t1+5].frgb    = 0xff000000;

				v[t1+4].u		= 0.5;
				v[t1+5].u		= 0.5;
				v[t1+5].v		= 0.5;

				v[t1+3].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+3].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+4].x	= ((xo+Xpixels*trisize)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+4].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

				v[t1+5].x	= ((xo+Xpixels*trisize/2.0f)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+5].y	= ((yo+Ypixels*trisize)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);

			}
			gos_DrawTriangles( v, 3 );
		}

		gos_SetRenderState(gos_State_ShadeMode, gos_ShadeFlat);
        gos_SetRenderState(gos_State_TextureMapBlend, gos_BlendDecal);
        gos_SetRenderState(gos_State_MonoEnable, 0 | (PixDouble ? 0x100 : 0));
        gos_SetRenderState( gos_State_Fog, 0 );

        EndCycleTiming( &goscdTriangleTime );
    }

	if( Test==14 )
	{
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "Perspective texture mapping" );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);

		gos_SetRenderState( gos_State_AlphaTest, 0 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_AlphaTest, 0 );
		gos_SetRenderState( gos_State_Texture, TestTexture );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
        gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendDecal );

        float      uadjust = (float)0;
        float      vadjust = (float)0;

        {
    	    gos_VERTEX v[16*3];

		    v[0].x      = ((float)493.54) * (PixDouble ? 0.5f : 1.0f);
		    v[0].y	    = ((float)324.52) * (PixDouble ? 0.5f : 1.0f);
            v[0].z      = (float)0.61294;
		    v[0].rhw    = (float)0.38911;
		    v[0].u	    = (float)1.0 + uadjust;
		    v[0].v	    = (float)1.0 + vadjust;
		    v[0].argb   = 0xFFFFFFFF;

		    v[1].x      = ((float)367.43) * (PixDouble ? 0.5f : 1.0f);
		    v[1].y	    = ((float)433.59) * (PixDouble ? 0.5f : 1.0f);
            v[1].z      = (float)0.46117;
		    v[1].rhw    = (float)0.54037;
		    v[1].u	    = (float)0.0 + uadjust;
		    v[1].v	    = (float)1.0 + vadjust;
		    v[1].argb   = 0xFFFFFFFF;

		    v[2].x      = ((float)377.85) * (PixDouble ? 0.5f : 1.0f);
		    v[2].y      = ((float)131.72) * (PixDouble ? 0.5f : 1.0f);
            v[2].z      = (float)0.34199;
		    v[2].rhw    = (float)0.65915;
		    v[2].u	    = (float)0.0 + uadjust;
		    v[2].v	    = (float)0.0 + vadjust;
		    v[2].argb   = 0xFFFFFFFF;

            v[0].frgb   = 0xff000000;
            v[1].frgb   = 0xff000000;
            v[2].frgb   = 0xff000000;

    	    gos_DrawTriangles( v, 3 );
        }

		{
    		gos_VERTEX v[16*3];

			v[0].x      = ((float)493.54) * (PixDouble ? 0.5f : 1.0f);
			v[0].y	    = ((float)324.52) * (PixDouble ? 0.5f : 1.0f);
            v[0].z      = (float)0.61294;
			v[0].rhw    = (float)0.38911;
			v[0].u	    = (float)1.0 + uadjust;
			v[0].v	    = (float)1.0 + vadjust;
			v[0].argb   = 0xFFFFFFFF;

			v[1].x      = ((float)377.85) * (PixDouble ? 0.5f : 1.0f);
			v[1].y      = ((float)131.72) * (PixDouble ? 0.5f : 1.0f);
            v[1].z      = (float)0.34199;
			v[1].rhw    = (float)0.65915;
			v[1].u	    = (float)0.0 + uadjust;
			v[1].v	    = (float)0.0 + vadjust;
			v[1].argb   = 0xFFFFFFFF;

			v[2].x      = ((float)519.42) * (PixDouble ? 0.5f : 1.0f);
			v[2].y	    = ((float)103.49) * (PixDouble ? 0.5f : 1.0f);
            v[2].z      = (float)0.55472;
			v[2].rhw    = (float)0.44713;
			v[2].u	    = (float)1.0 + uadjust;
			v[2].v	    = (float)0.0 + vadjust;
			v[2].argb   = 0xFFFFFFFF;

            v[0].frgb   = 0xff000000;
            v[1].frgb   = 0xff000000;
            v[2].frgb   = 0xff000000;
    		gos_DrawTriangles( v, 3 );
        }

		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "Perspective texture mapping" );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);

		gos_SetRenderState( gos_State_AlphaTest, 0 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
		gos_SetRenderState( gos_State_AlphaTest, true);
		gos_SetRenderState( gos_State_Texture, TestTexture );
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
        gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulateAlpha );
		gos_SetRenderState( gos_State_ZCompare, 0 );
		gos_SetRenderState( gos_State_ZWrite, 0 );

        float xshift = (float)-50.0;
        float yshift = (float)-50.0;

        {
    	    gos_VERTEX v[16*3];

		    v[0].x      = ((float)493.54 + xshift) * (PixDouble ? 0.5f : 1.0f);
		    v[0].y	    = ((float)324.52 + yshift) * (PixDouble ? 0.5f : 1.0f);
            v[0].z      = (float)0.61294;
		    v[0].rhw    = (float)0.38911;
		    v[0].u	    = (float)1.0 + uadjust;
		    v[0].v	    = (float)1.0 + vadjust;
		    v[0].argb   = 0x7FFFFFFF;

		    v[1].x      = ((float)367.43 + xshift) * (PixDouble ? 0.5f : 1.0f);
		    v[1].y	    = ((float)433.59 + yshift) * (PixDouble ? 0.5f : 1.0f);
            v[1].z      = (float)0.46117;
		    v[1].rhw    = (float)0.54037;
		    v[1].u	    = (float)0.0 + uadjust;
		    v[1].v	    = (float)1.0 + vadjust;
		    v[1].argb   = 0x7FFFFFFF;

		    v[2].x      = ((float)377.85 + xshift) * (PixDouble ? 0.5f : 1.0f);
		    v[2].y      = ((float)131.72 + yshift) * (PixDouble ? 0.5f : 1.0f);
            v[2].z      = (float)0.34199;
		    v[2].rhw    = (float)0.65915;
		    v[2].u	    = (float)0.0 + uadjust;
		    v[2].v	    = (float)0.0 + vadjust;
		    v[2].argb   = 0x7FFFFFFF;

            v[0].frgb   = 0xff000000;
            v[1].frgb   = 0xff000000;
            v[2].frgb   = 0xff000000;
    	    gos_DrawTriangles( v, 3 );
        }

		{
    		gos_VERTEX v[16*3];

			v[0].x      = ((float)493.54 + xshift) * (PixDouble ? 0.5f : 1.0f);
			v[0].y	    = ((float)324.52 + yshift) * (PixDouble ? 0.5f : 1.0f);
            v[0].z      = (float)0.61294;
			v[0].rhw    = (float)0.38911;
			v[0].u	    = (float)1.0 + uadjust;
			v[0].v	    = (float)1.0 + vadjust;
			v[0].argb   = 0x7FFFFFFF;

			v[1].x      = ((float)377.85 + xshift) * (PixDouble ? 0.5f : 1.0f);
			v[1].y      = ((float)131.72 + yshift) * (PixDouble ? 0.5f : 1.0f);
            v[1].z      = (float)0.34199;
			v[1].rhw    = (float)0.65915;
			v[1].u	    = (float)0.0 + uadjust;
			v[1].v	    = (float)0.0 + vadjust;
			v[1].argb   = 0x7FFFFFFF;

			v[2].x      = ((float)519.42 + xshift) * (PixDouble ? 0.5f : 1.0f);
			v[2].y	    = ((float)103.49 + yshift) * (PixDouble ? 0.5f : 1.0f);
            v[2].z      = (float)0.55472;
			v[2].rhw    = (float)0.44713;
			v[2].u	    = (float)1.0 + uadjust;
			v[2].v	    = (float)0.0 + vadjust;
			v[2].argb   = 0x7FFFFFFF;

            v[0].frgb   = 0xff000000;
            v[1].frgb   = 0xff000000;
            v[2].frgb   = 0xff000000;
    		gos_DrawTriangles( v, 3 );
        }

        gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulateAlpha );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_AlphaTest, false);
		gos_SetRenderState( gos_State_ZCompare, 1 );
		gos_SetRenderState( gos_State_ZWrite, 1 );
        gos_SetRenderState( gos_State_Fog, 0 );
    }

	if( Test==15 )
	{
        static int lfirst = 1;

        if (lfirst)
        {
            zwrite = 1;
            zcompare = 1;
            lfirst = 0;
        }

        int lpolys = 3;
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=(float)lpolys;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d - 32768 Pixel %s Triangles w/Z",Polys/20,UseTexture ? "Textured" : "" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
        gos_SetRenderState( gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);

		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

        int argb = 0xF00000;
        float z = .75, z2 = .25, z3 = .5;

		for( int t0=0; t0<1; t0+=16 )
		{
			for( int t1=0; t1<9; t1+=3 )
			{
				float xo=0.1f+(t1/3*16834)*(0.4f/32768.0f);
				float yo=0.1f+(t1/3*16834)*(0.25f/32768.0f);
			
				v[t1].argb		= argb;
                argb >>= 8;

                if (argb == 0)
                    argb		= 0xF00000;

				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= 1.0;
				v[t1+1].rhw		= 1.0;
				v[t1+2].rhw		= 1.0;

                v[t1].frgb      = 0xff000000;
                v[t1+1].frgb    = 0xff000000;
                v[t1+2].frgb    = 0xff000000;

				v[t1+1].v		= 1.0;
				v[t1+2].u		= 1.0;
				v[t1+2].v		= 1.0;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1].z = z;

				v[t1+1].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1+1].z = z2;

				v[t1+2].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*256)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1+2].z = z3;

                if (z == .75)
                    z = .25;
                else
                    z = .75;

                z2 = (float)1.0 - z;
			}
			gos_DrawTriangles( v, 9 );
		}

		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
        gos_SetRenderState( gos_State_Fog, 0 );
	}

	if( Test==16 )
	{
        static int lfirst = 1;

        if (lfirst)
        {
            zwrite = 0;
            zcompare = 0;
            lfirst = 0;
        }

        int lpolys = 3;
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=(float)lpolys;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d - 32768 Pixel %s Triangles w/Z",Polys/20,UseTexture ? "Textured" : "" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? AlphaTexture : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
        gos_SetRenderState( gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);

		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

        int argb = 0xF00000;
        float z = .75, z2 = .25, z3 = .5;

		for( int t0=0; t0<1; t0+=16 )
		{
			for( int t1=0; t1<9; t1+=3 )
			{
				float xo=0.1f+(t1/3*16834)*(0.4f/32768.0f);
				float yo=0.1f+(t1/3*16834)*(0.25f/32768.0f);
			
				v[t1].argb		= argb;
                argb >>= 8;

                if (argb == 0)
                    argb		= 0xF00000;

				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= 1.0;
				v[t1+1].rhw		= 1.0;
				v[t1+2].rhw		= 1.0;

                v[t1].frgb      = 0xff000000;
                v[t1+1].frgb    = 0xff000000;
                v[t1+2].frgb    = 0xff000000;

				v[t1+1].v		= 1.0;
				v[t1+2].u		= 1.0;
				v[t1+2].v		= 1.0;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1].z = z;

				v[t1+1].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1+1].z = z2;

				v[t1+2].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*256)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1+2].z = z3;

                if (z == .75)
                    z = .25;
                else
                    z = .75;

                z2 = (float)1.0 - z;
			}
			gos_DrawTriangles( v, 9 );
		}

		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
        gos_SetRenderState( gos_State_Fog, 0 );
    }

	if( Test==17 )
	{
        int lpolys = 3;
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=(float)lpolys;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "cull test\n" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? AlphaTexture : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
        gos_SetRenderState( gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);

		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.1f, 0.1f, 0.13f, 0.1f, 0.115f, 0.13f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.15f, 0.1f, 0.18f, 0.1f, 0.165f, 0.13f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.2f, 0.1f, 0.23f, 0.1f, 0.215f, 0.13f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.1f, 0.15f, 0.115f, 0.18f, 0.13f, 0.15f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.15f, 0.15f, 0.165f, 0.18f, 0.18f, 0.15f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.2f, 0.15f, 0.215f, 0.18f, 0.23f, 0.15f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.13f, 0.2f, 0.115f, 0.23f, 0.1f, 0.2f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.18f, 0.2f, 0.165f, 0.23f, 0.15f, 0.2f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.23f, 0.2f, 0.215f, 0.23f, 0.2f, 0.2f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.115f, 0.28f, 0.13f, 0.25f, 0.1f, 0.25f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.165f, 0.28f, 0.18f, 0.25f, 0.15f, 0.25f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.215f, 0.28f, 0.23f, 0.25f, 0.2f, 0.25f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.115f, 0.33f, 0.1f, 0.3f, 0.13f, 0.3f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.165f, 0.33f, 0.15f, 0.3f, 0.18f, 0.3f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.215f, 0.33f, 0.2f, 0.3f, 0.23f, 0.3f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.13f, 0.35f, 0.1f, 0.35f, 0.115f, 0.38f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.18f, 0.35f, 0.15f, 0.35f, 0.165f, 0.38f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.23f, 0.35f, 0.2f, 0.35f, 0.215f, 0.38f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.1f, 0.4f, 0.13f, 0.415f, 0.115f, 0.43f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.15f, 0.4f, 0.18f, 0.415f, 0.165f, 0.43f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.2f, 0.4f, 0.23f, 0.415f, 0.215f, 0.43f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.1f, 0.45f, 0.115f, 0.48f, 0.13f, 0.465f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.15f, 0.45f, 0.165f, 0.48f, 0.18f, 0.465f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.2f, 0.45f, 0.215f, 0.48f, 0.23f, 0.465f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.13f, 0.515f, 0.115f, 0.53f, 0.1f, 0.5f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.18f, 0.515f, 0.165f, 0.53f, 0.15f, 0.5f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.23f, 0.515f, 0.215f, 0.53f, 0.2f, 0.5f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.115f, 0.58f, 0.13f, 0.565f, 0.1f, 0.55f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.165f, 0.58f, 0.18f, 0.565f, 0.15f, 0.55f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.215f, 0.58f, 0.23f, 0.565f, 0.2f, 0.55f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.115f, 0.63f, 0.1f, 0.6f, 0.13f, 0.615f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.165f, 0.63f, 0.15f, 0.6f, 0.18f, 0.615f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.215f, 0.63f, 0.2f, 0.6f, 0.23f, 0.615f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.13f, 0.665f, 0.1f, 0.65f, 0.115f, 0.68f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.18f, 0.665f, 0.15f, 0.65f, 0.165f, 0.68f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.23f, 0.665f, 0.2f, 0.65f, 0.215f, 0.68f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.4f, 0.1f, 0.43f, 0.1f, 0.415f, 0.07f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.45f, 0.1f, 0.48f, 0.1f, 0.465f, 0.07f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.5f, 0.1f, 0.53f, 0.1f, 0.515f, 0.07f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.4f, 0.15f, 0.415f, 0.12f, 0.43f, 0.15f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.45f, 0.15f, 0.465f, 0.12f, 0.48f, 0.15f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.5f, 0.15f, 0.515f, 0.12f, 0.53f, 0.15f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.43f, 0.2f, 0.415f, 0.17f, 0.4f, 0.2f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.48f, 0.2f, 0.465f, 0.17f, 0.45f, 0.2f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.53f, 0.2f, 0.515f, 0.17f, 0.5f, 0.2f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.415f, 0.22f, 0.43f, 0.25f, 0.4f, 0.25f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.465f, 0.22f, 0.48f, 0.25f, 0.45f, 0.25f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.515f, 0.22f, 0.53f, 0.25f, 0.5f, 0.25f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.415f, 0.27f, 0.4f, 0.3f, 0.43f, 0.3f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.465f, 0.27f, 0.45f, 0.3f, 0.48f, 0.3f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.515f, 0.27f, 0.5f, 0.3f, 0.53f, 0.3f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.43f, 0.35f, 0.4f, 0.35f, 0.415f, 0.32f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.48f, 0.35f, 0.45f, 0.35f, 0.465f, 0.32f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.53f, 0.35f, 0.5f, 0.35f, 0.515f, 0.32f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.4f, 0.4f, 0.43f, 0.415f, 0.415f, 0.37f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.45f, 0.4f, 0.48f, 0.415f, 0.465f, 0.37f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.5f, 0.4f, 0.53f, 0.415f, 0.515f, 0.37f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.4f, 0.45f, 0.415f, 0.42f, 0.43f, 0.465f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.45f, 0.45f, 0.465f, 0.42f, 0.48f, 0.465f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.5f, 0.45f, 0.515f, 0.42f, 0.53f, 0.465f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.43f, 0.515f, 0.415f, 0.47f, 0.4f, 0.5f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.48f, 0.515f, 0.465f, 0.47f, 0.45f, 0.5f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.53f, 0.515f, 0.515f, 0.47f, 0.5f, 0.5f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.415f, 0.52f, 0.43f, 0.565f, 0.4f, 0.55f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.465f, 0.52f, 0.48f, 0.565f, 0.45f, 0.55f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.515f, 0.52f, 0.53f, 0.565f, 0.5f, 0.55f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.415f, 0.57f, 0.4f, 0.6f, 0.43f, 0.615f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.465f, 0.57f, 0.45f, 0.6f, 0.48f, 0.615f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.515f, 0.57f, 0.5f, 0.6f, 0.53f, 0.615f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
        DrawTriangleAt(0.43f, 0.665f, 0.4f, 0.65f, 0.415f, 0.62f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CW);
        DrawTriangleAt(0.48f, 0.665f, 0.45f, 0.65f, 0.465f, 0.62f, 0, 0, 0);

        gos_SetRenderState(gos_State_Culling, gos_Cull_CCW);
        DrawTriangleAt(0.53f, 0.665f, 0.5f, 0.65f, 0.515f, 0.62f, 0, 0, 0);


        gos_SetRenderState(gos_State_Culling, gos_Cull_None);
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
        gos_SetRenderState( gos_State_Fog, 0 );
    }

	if( Test==18 )
	{
        int lpolys = 9;
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=(float)lpolys;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "cull test\n" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? AlphaTexture : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
        gos_SetRenderState( gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);

		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

		gos_SetRenderState( gos_State_Fog, (int)&fogColor );

        gos_SetRenderState(gos_State_MonoEnable, 1 | (PixDouble ? 0x100 : 0));

        DrawTriangleAt(0.1f, 0.1f, 0.3f, 0.1f, 0.2f, 0.3f, 0xFF000000, 0, 0xFF000000);
        DrawTriangleAt(0.4f, 0.1f, 0.6f, 0.1f, 0.5f, 0.3f, 0xDF000000, 0, 0xDF000000);
        DrawTriangleAt(0.7f, 0.1f, 0.9f, 0.1f, 0.8f, 0.3f, 0xBF000000, 0, 0xBF000000);
        DrawTriangleAt(0.1f, 0.3f, 0.3f, 0.3f, 0.2f, 0.5f, 0x9F000000, 0, 0x9F000000);
        DrawTriangleAt(0.4f, 0.3f, 0.6f, 0.3f, 0.5f, 0.5f, 0x7F000000, 0, 0x7F000000);
        DrawTriangleAt(0.7f, 0.3f, 0.9f, 0.3f, 0.8f, 0.5f, 0x5F000000, 0, 0x5F000000);
        DrawTriangleAt(0.1f, 0.5f, 0.3f, 0.5f, 0.2f, 0.7f, 0x3F000000, 0, 0x3F000000);
        DrawTriangleAt(0.4f, 0.5f, 0.6f, 0.5f, 0.5f, 0.7f, 0x1F000000, 0, 0x1F000000);
        DrawTriangleAt(0.7f, 0.5f, 0.9f, 0.5f, 0.8f, 0.7f, 0x00000000, 0xFF000000, 0x00000000);

        DrawTriangleAt(0.7f, 0.7f, 0.9f, 0.7f, 0.8f, 0.9f, 0xFF000000, 0xFF000000, 0xFF000000);
        DrawTriangleAt(0.4f, 0.7f, 0.6f, 0.7f, 0.5f, 0.9f, 0x7F000000, 0x7F000000, 0x7F000000);
        DrawTriangleAt(0.1f, 0.7f, 0.3f, 0.7f, 0.2f, 0.9f, 0x00000000, 0x00000000, 0x00000000);

        gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
    }

	if( Test==19 )
	{
        int lpolys = 9;
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=(float)lpolys;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "cull test\n" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? AlphaTexture : 0 );
        gos_SetRenderState( gos_State_AlphaMode, do11 ? gos_Alpha_OneOne : gos_Alpha_OneZero );
        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
        gos_SetRenderState( gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);

		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

        DWORD fc = 0x00FFFFFF;
		gos_SetRenderState( gos_State_Fog, (int)&fc);

        gos_SetRenderState(gos_State_MonoEnable, 1 | (PixDouble ? 0x100 : 0) /*| 0x400*/);

        DrawTriangleAt(0.1f, 0.1f, 0.3f, 0.1f, 0.2f, 0.3f, 0xFF000000, 0, 0xFF000000);
        DrawTriangleAt(0.4f, 0.1f, 0.6f, 0.1f, 0.5f, 0.3f, 0xDF000000, 0, 0xDF000000);
        DrawTriangleAt(0.7f, 0.1f, 0.9f, 0.1f, 0.8f, 0.3f, 0xBF000000, 0, 0xBF000000);
        DrawTriangleAt(0.1f, 0.3f, 0.3f, 0.3f, 0.2f, 0.5f, 0x9F000000, 0, 0x9F000000);
        DrawTriangleAt(0.4f, 0.3f, 0.6f, 0.3f, 0.5f, 0.5f, 0x7F000000, 0, 0x7F000000);
        DrawTriangleAt(0.7f, 0.3f, 0.9f, 0.3f, 0.8f, 0.5f, 0x5F000000, 0, 0x5F000000);
        DrawTriangleAt(0.1f, 0.5f, 0.3f, 0.5f, 0.2f, 0.7f, 0x3F000000, 0, 0x3F000000);
        DrawTriangleAt(0.4f, 0.5f, 0.6f, 0.5f, 0.5f, 0.7f, 0x1F000000, 0, 0x1F000000);
        DrawTriangleAt(0.7f, 0.5f, 0.9f, 0.5f, 0.8f, 0.7f, 0x00000000, 0xFF000000, 0x00000000);

        DrawTriangleAt(0.7f, 0.7f, 0.9f, 0.7f, 0.8f, 0.9f, 0xFF000000, 0xFF000000, 0xFF000000);
        DrawTriangleAt(0.4f, 0.7f, 0.6f, 0.7f, 0.5f, 0.9f, 0x7F000000, 0x7F000000, 0x7F000000);
        DrawTriangleAt(0.1f, 0.7f, 0.3f, 0.7f, 0.2f, 0.9f, 0x00000000, 0x00000000, 0x00000000);

        gos_SetRenderState( gos_State_Fog, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
    }

	if( Test==20 )
	{
        static int lfirst = 1;

        if (lfirst)
        {
            zwrite = 1;
            zcompare = 1;
            lfirst = 0;
        }

        int lpolys = 3;
		gos_GetViewport( &ViewportMulX, &ViewportMulY, &ViewportAddX, &ViewportAddY );
		gos_VERTEX v[16*3];

		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );

        gos_SetRenderState( gos_State_Perspective, dopersp ? 1 : 0);

		double framerate=gos_GetElapsedTime()-lastframe;
		lastframe=gos_GetElapsedTime();
		TotalPolys+=(float)lpolys;
		TotalFrameRate+=framerate;
		if( framerate && 0==(frame&31) )
		{
			Average=(DWORD)(TotalPolys/TotalFrameRate);
			TotalPolys=0;
			TotalFrameRate=0.0;
		}
		frame++;
		gos_TextSetPosition( 50,445 );
		gos_TextDraw( "%d triangles/second",Average );

        UseTexture = dotex;

		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "%d - 32768 Pixel %s Triangles w/Z",Polys/20,UseTexture ? "Textured" : "" );
		gos_TextSetPosition( 350,460 );
		gos_TextDraw( "1 - Advance test, 2 - More Tris, 3-Less, T-Texture" );

		gos_SetRenderState( gos_State_Clipping, 0 );
		gos_SetRenderState( gos_State_Fog, dofog ? (DWORD)&fogColor : 0);
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, UseTexture ? TestTexture : 0 );

        if (do11)
        {
            gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneOne );
        }
        else
        {
            gos_SetRenderState( gos_State_AlphaMode, doalphainvalpha ? gos_Alpha_AlphaInvAlpha : gos_Alpha_OneZero );
        }

        gos_SetRenderState( gos_State_TextureMapBlend, doalphamodulate ? gos_BlendModulateAlpha : gos_BlendDecal );
        gos_SetRenderState( gos_State_ShadeMode, dogouraud ? gos_ShadeGouraud : gos_ShadeFlat);

		gos_SetRenderState( gos_State_ZCompare, zcompare );
		gos_SetRenderState( gos_State_ZWrite, zwrite );

		memset( v, 0, 48*sizeof(gos_VERTEX) );

        int argb = 0xF00000;
        float z = .75, z2 = .25, z3 = .5;

		for( int t0=0; t0<1; t0+=16 )
		{
			for( int t1=0; t1<9; t1+=3 )
			{
				float xo=0.1f+(t1/3*16834)*(0.4f/32768.0f);
				float yo=0.1f+(t1/3*16834)*(0.25f/32768.0f);
			
				v[t1].argb		= argb;
                argb >>= 8;

                if (argb == 0)
                    argb		= 0xF00000;

				v[t1+1].argb	= 0xff00ff00;
				v[t1+2].argb	= 0xff0000ff;
				v[t1].rhw		= 1.0;
				v[t1+1].rhw		= 1.0;
				v[t1+2].rhw		= 1.0;

                v[t1].frgb      = 0x00000000;
                v[t1+1].frgb    = 0x00000000;
                v[t1+2].frgb    = 0x00000000;

				v[t1+1].v		= 1.0;
				v[t1+2].u		= 1.0;
				v[t1+2].v		= 1.0;

				v[t1].x		= ((xo)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1].y		= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1].z = z;

				v[t1+1].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+1].y	= ((yo)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1+1].z = z2;

				v[t1+2].x	= ((xo+Xpixels*256)*ViewportMulX + ViewportAddX) * (PixDouble ? 0.5f : 1.0f);
				v[t1+2].y	= ((yo+Ypixels*256)*ViewportMulY + ViewportAddY) * (PixDouble ? 0.5f : 1.0f);
                v[t1+2].z = z3;

                if (z == .75)
                    z = .25;
                else
                    z = .75;

                z2 = (float)1.0 - z;
			}
			gos_DrawTriangles( v, 9 );
		}

		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud);
        gos_SetRenderState( gos_State_Fog, 0 );
	}

//
// Test lines and points
//
	if( Test==5 )
	{
		gos_TextSetRegion( 0, 0, 640, 480 );
		gos_TextSetAttributes( Font_Arial8, 0xffffffff, -8, 0, 1, 0, 0 );
		gos_TextSetPosition( 50,460 );
		gos_TextDraw( "Lines and Points" );
		gos_TextSetPosition( 380,460 );
		gos_TextDraw( "1 - Advance test" );

		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_Texture, 0 );
		gos_SetRenderState( gos_State_MonoEnable, 0 );
		gos_SetRenderState( gos_State_ShadeMode, gos_ShadeGouraud );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_AlphaInvAlpha );
		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendModulate );

		if( Environment.screenWidth<640 )
			gos_SetRenderState( gos_State_Clipping, 1 );

		tDrawLine( 10,440, 630,440, 0x80ff0000 );
		tDrawLine( 10,442, 630,442, 0x8000ff00 );
		tDrawLine( 10,444, 630,444, 0x800000ff );
		tDrawLine( 10,446, 630,446, 0x80ffffff );
		tDrawLine( 10,448, 630,448, 0x80000000 );

		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );

		tDrawLine( 10,430, 630,430, 0xffff0000 );
		tDrawLine( 10,432, 630,432, 0xff00ff00 );
		tDrawLine( 10,434, 630,434, 0xff0000ff );
		tDrawLine( 10,436, 630,436, 0xffffffff );
		tDrawLine( 10,438, 630,438, 0xff000000 );

		for( int t0=0; t0<NumLines; t0+=2 )
		{
			if( Lines[t0][4]!=Lines[t0][5] )
			{
				DWORD X1=Lines[t0][0]+   ( (Lines[t0][1]-Lines[t0][0]) * Lines[t0][5] ) / Lines[t0][4];
				DWORD Y1=Lines[t0][2]+   ( (Lines[t0][3]-Lines[t0][2]) * Lines[t0][5] ) / Lines[t0][4];
				DWORD X2=Lines[t0+1][0]+ ( (Lines[t0+1][1]-Lines[t0+1][0]) * Lines[t0][5] ) / Lines[t0][4];
				DWORD Y2=Lines[t0+1][2]+ ( (Lines[t0+1][3]-Lines[t0+1][2]) * Lines[t0][5] ) / Lines[t0][4];
				
				tDrawLine( X1,Y1, X2,Y2, ColorTable[(t0>>1)&7] );

				tDrawPoint( X1,(Y1-(480-256)/2)/3+20, ColorTable[(t0>>1)&7] );
			}
		}
		gos_SetRenderState( gos_State_Clipping, 0 );
	}

//
// Test texture swapping
//
	if( Test==6 )
	{
		gos_SetRenderState( gos_State_Filter, gos_FilterNone );
		gos_SetRenderState( gos_State_AlphaMode, gos_Alpha_OneZero );
		gos_SetRenderState( gos_State_TextureMapBlend, gos_BlendDecal );

		gos_SetRenderState( gos_State_Texture, tex1 );
		DofQuad( 20+0*64,20, 20+0*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex2 );
		DofQuad( 20+1*64,20, 20+1*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex3 );
		DofQuad( 20+2*64,20, 20+2*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex4 );
		DofQuad( 20+3*64,20, 20+3*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex5 );
		DofQuad( 20+4*64,20, 20+4*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex6 );
		DofQuad( 20+5*64,20, 20+5*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex7 );
		DofQuad( 20+6*64,20, 20+6*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex8 );
		DofQuad( 20+7*64,20, 20+7*64+256,256, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex9 );
		DofQuad( 20+8*64,20, 20+8*64+256,256, 0.25 );


		gos_SetRenderState( gos_State_Texture, tex10 );
		DofQuad( 20+0*64,256, 20+0*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex11 );
		DofQuad( 20+1*64,256, 20+1*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex12 );
		DofQuad( 20+2*64,256, 20+2*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex13 );
		DofQuad( 20+3*64,256, 20+3*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex14 );
		DofQuad( 20+4*64,256, 20+4*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex15 );
		DofQuad( 20+5*64,256, 20+5*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex16 );
		DofQuad( 20+6*64,256, 20+6*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Texture, tex17 );
		DofQuad( 20+7*64,256, 20+7*64+256,512, 0.25 );

		gos_SetRenderState( gos_State_Clipping, 0 );

	}
}



#pragma warning( disable: 4514 )
#include <stdio.h>
#include <math.h>

void __stdcall InitializeGameEngine()
{
//
// Load up a single 256*256 24 bit test TGA
//
	TestTexture = gos_NewTextureFromFile( gos_Texture_Detect, "Assets\\Graphics\\texture.tga",gosHint_AGPMemory );
	AlphaTexture = gos_NewTextureFromFile( gos_Texture_Detect, "Assets\\Graphics\\font.tga" );
	TestMipMap = gos_NewTextureFromFile( gos_Texture_Detect, "Assets\\Graphics\\MipMap0.tga",gosHint_DisableMipmap|gosHint_DontShrink );

	Font_Arial8=gos_LoadFont("Assets\\Graphics\\arial8.tga",0);

	Test=9;

	Polys=500;
	BackGroundColor=0x004000;
	Size=1.0f;
	Step=0.004f;
	SafeSize=1.0f;

	for( int t0=0; t0<NumLines; t0++ )
	{
		Lines[t0][0]=(640-512)/2+(gos_rand()&511);		
		Lines[t0][1]=(640-512)/2+(gos_rand()&511);		
		Lines[t0][2]=(480-256)/2+(gos_rand()&255);		
		Lines[t0][3]=(480-256)/2+(gos_rand()&255);		
		Lines[t0][4]=(gos_rand()&63)+32;
		Lines[t0][5]=0;
	}

	tex1=0;
	UseTexture=0;

	tex1=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t1.tga",0 );
	tex2=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t2.tga",0 );
	tex3=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t3.tga",0 );
	tex4=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t4.tga",0 );
	tex5=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t5.tga",0 );
	tex6=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t6.tga",0 );
	tex7=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t7.tga",0 );
	tex8=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t8.tga",0 );
	tex9=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t9.tga",0 );
	tex10=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t10.tga",0 );
	tex11=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t11.tga",0 );
	tex12=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t12.tga",0 );
	tex13=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t13.tga",0 );
	tex14=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t14.tga",0 );
	tex15=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t15.tga",0 );
	tex16=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t16.tga",0 );
	tex17=gos_NewTextureFromFile(gos_Texture_Detect, "Assets\\Graphics\\t17.tga",0 );

    StatisticFormat( "Test Blade Variables" );
	StatisticFormat( "====================" );
	StatisticFormat( "" );
    AddStatistic( "Triangle Time", "cycles", gos_cycledata, &goscdTriangleTime, Stat_Graph );

}


//
// Gamelogic
//
void __stdcall DoGameLogic()
{
//
// Animate stuff in the tests
//
	Size-=Step;
	if( Size>1.0f || Size<Step )
	{
		Step=-Step;
		Size-=Step;
	}
//
// Mess with the background color
//
	BackGroundColor+=1;
//
// Read keyboard - increment test?
//
	DWORD KeyPress=gos_GetKey();
	if( KeyPress )
		SPEW(( "Keyboard", "Key=0x%x, '%c', Description='%s'",KeyPress, (KeyPress&255) ? KeyPress&255 : ' ', gos_DescribeKey(KeyPress) ));

	for( int t0=0; t0<256; t0++ )
	{
		if( KEY_PRESSED==gos_GetKeyStatus((gosEnum_KeyIndex)t0) )
		{
			SPEW(( "Keyboard", "Held Key = 0x%x",t0 ));
		}
	}

	KeyPress&=255;

	if( KeyPress=='1' )
	{
		Test++;
		if( Test==21 )
			Test=0;
	}
	if( KeyPress=='`' )
	{
		if( Test--==0 )
			Test=20;
	}

	if( KeyPress=='2' )
	{
		Polys+=200;
	}
	if( KeyPress=='3' )
	{
		Polys-=200;
		if( Polys<0 )
			Polys=0;
	}
	if( KeyPress=='T' || KeyPress=='t' )
	{
		UseTexture=!UseTexture;
	}
	if( KeyPress=='M' || KeyPress=='m' )
	{
		WhichTexture=!WhichTexture;
	}
	if( KeyPress=='0' )
	{
		TestStackWalk();
	}
	if( KeyPress=='D' || KeyPress=='d' )
	{
        PixDouble = !PixDouble;
    }
    if( KeyPress=='W' || KeyPress=='w' )
    {
        zwrite = !zwrite;
    }
    if( KeyPress=='C' || KeyPress=='c' )
    {
        zcompare = !zcompare;
    }
    if( KeyPress=='T' || KeyPress=='t' )
    {
        dotex = !dotex;
    }
    if( KeyPress=='G' || KeyPress=='g' )
    {
        dogouraud = !dogouraud;
    }
    if( KeyPress=='A' || KeyPress=='a' )
    {
        doalphamodulate = !doalphamodulate;
    }
    if( KeyPress=='B' || KeyPress=='b' )
    {
        do11 = !do11;
    }
    if( KeyPress=='P' || KeyPress=='p' )
    {
        dopersp = !dopersp;
    }
    if( KeyPress=='F' || KeyPress=='f' )
    {
        dofog = !dofog;
    }
    if( KeyPress=='L' || KeyPress=='l' )
    {
        doalphainvalpha = !doalphainvalpha;
    }
//
// Now do logic for some of the tests
//
	if( Test==5 )
	{
		for( int t0=0; t0<NumLines; t0+=2 )
		{
			if( Lines[t0][4]!=Lines[t0][5] )
			{
				Lines[t0][5]++;
			}
			else
			{
				Lines[t0][0]=Lines[t0][1];		
				Lines[t0][1]=(640-512)/2+(gos_rand()&511);		
				Lines[t0][2]=Lines[t0][3];		
				Lines[t0][3]=(480-256)/2+(gos_rand()&255);		
				Lines[t0+1][0]=Lines[t0+1][1];		
				Lines[t0+1][1]=(640-512)/2+(gos_rand()&511);		
				Lines[t0+1][2]=Lines[t0+1][3];		
				Lines[t0+1][3]=(480-256)/2+(gos_rand()&255);		
				Lines[t0][5]=0;
			}
		}
	}
//
// To test texture animation, enable this
//
	#if 1
	TEXTUREPTR Info;
	
	if( Test==2 && WhichTexture )
	{
		gos_LockTexture( TestMipMap,  0, 0, &Info );

		for( int ra=0; ra<200; ra++ )
		{
			DWORD xp=gos_rand()&(Info.Width-1);
			DWORD yp=gos_rand()&(Info.Height-1);

			DWORD* Pixel=Info.pTexture+xp+yp*Info.Pitch;

			*Pixel=*Pixel+0x404040;
		}

		gos_UnLockTexture( TestMipMap );
	}
	#endif
//
// Make sure all random numbers on UpdateRenders are the same
//
	Seed=gos_rand();
}



//
// Setup the GameOS structure
//
void __stdcall GetGameOSEnvironment( char* CommandLine )
{
	CommandLine=CommandLine;
	Environment.applicationName			= "Test Blade";
	Environment.screenWidth				= 640;
	Environment.screenHeight			= 480;
	Environment.fullScreen				= 0;
	Environment.bitDepth				= 16;

	Environment.debugLog				= "";			//"DebugLog.txt";
	Environment.spew					= "";			//"GameOS_Texture GameOS_DirectDraw GameOS_Direct3D";

	Environment.UpdateRenderers			= UpdateRenderers;
	Environment.InitializeGameEngine	= InitializeGameEngine;
	Environment.DoGameLogic				= DoGameLogic;
	
	Environment.FullScreenDevice		= 0;
	Environment.Renderer				= 3;			// Default to blade renderer
//
// Texture heap infomation
//
	Environment.Texture_S_256			= 0;			//96 - NT crash!
	Environment.Texture_S_128			= 0;
	Environment.Texture_S_64			= 0;
	Environment.Texture_S_32			= 0;
	Environment.Texture_S_16			= 0;

	Environment.Texture_K_256			= 0;
	Environment.Texture_K_128			= 0;
	Environment.Texture_K_64			= 0;
	Environment.Texture_K_32			= 0;
	Environment.Texture_K_16			= 0;

	Environment.Texture_A_256			= 0;
	Environment.Texture_A_128			= 0;
	Environment.Texture_A_64			= 0;
	Environment.Texture_A_32			= 0;
	Environment.Texture_A_16			= 0;

	Environment.RaidCustomFields		= "AssignedTo=andyg;Project=Blade;Client=GameOS;GOSRaid=Yes"; 
	Environment.RaidDataSource			= "GameTech :Raid4";
	Environment.RaidFilePath			= "\\\\aas1\\coretech\\Test\\Raid\\Exceptions";
}

