// HamsterMatic.vtx
// Author - MrC
// Based on HamsterMatic.r4 by Gordon Williams

Texture	tex;
float btime;
float[] rot(8);
float[] rots(8);
int x;
float dxx,dxy,dyx,dyy;
// colours
float ar,ag,ab,br,bg,bb;
float colamt,coltime;

void Init()
{
	int id = Rand() * 3;
	if (id == 0)
		tex.LoadTexture("Alien.png");
	else if (id == 1)
		tex.LoadTexture("Smiley.png");
	else
		tex.LoadTexture("skull.png");
	
	btime = 0;
	for (x=0;x<8;x=x+1)
	{
		rot[x]=Rand();
		rots[x]=Rand()+1.0f;
	}
	colamt = 1;
	br = 0.0;
	bg = 0.0;
	bb = 0.5;
	coltime = 1+(Rand()*5);
}

void quad(float rt, float sz)
{
   dxx = Cos(rt)*sz;
   dxy = Sin(rt)*sz;
   dyx = Sin(rt)*sz;
   dyy = 0.0f-Cos(rt)*sz;

   gfxColour(1.0f,1.0f,1.0f,1.0f);
   gfxTexCoord((0.0f-dxx)+(0.0f+dyx),(0.0f-dxy)+(0.0f+dyy));
   gfxVertex(1.0,-1,0);
   gfxTexCoord((0.0f+dxx)+(0.0f+dyx),(0.0f+dxy)+(0.0f+dyy));
   gfxVertex(-1.0,-1,0);
   gfxTexCoord((0.0f+dxx)+(0.0f-dyx),(0.0f+dxy)+(0.0f-dyy));
   gfxVertex(-1.0,1,0);
   gfxTexCoord((0.0f-dxx)+(0.0f-dyx),(0.0f-dxy)+(0.0f-dyy));
   gfxVertex(1.0,1,0);
}

void Render()
{
	gfxSetBlendMode(BLEND_MOD);

	// keep moving
	btime = btime + TIMEPASS;
	while (btime>1)
	{
		btime = btime - 1;
		for (x=6;x>=0;x=x-1)
		{
			rot[x+1]=rot[x];
			rots[x+1]=rots[x];
		}
		rot[0] = 0;
		rots[0] = Rand()+1.0f;
	}

	coltime = coltime - TIMEPASS;
	if (coltime<0)
	{
		coltime = 1+(Rand()*5);
		ar = br; ag = bg; ab = bb;
		colamt =  0;
		x = (Rand()*6.0)+1.0;
		br = x%2;
		bg = ((x%4)-br)/2;
		bb = ((x%8)-(bg*2)-br)/4;
		br = br/2.0;
		bg = bg/2.0;
		bb = bb/2.0;
	}
	colamt = colamt + TIMEPASS;
	if (colamt>1) colamt=1;
	gfxClear( ((1.0f-colamt)*ar)+(colamt*br), ((1.0f-colamt)*ag)+(colamt*bg), ((1.0f-colamt)*ab)+(colamt*bb) );

	// draw
	gfxSetTexture(tex);
	gfxSetAspect(0);
	gfxTranslate(0.0f, 0.0f, 2.414f);
	gfxBegin(PRIM_QUADLIST);
	for (x=7;x>=0;x=x-1)
	{
		rot[x] = rot[x] + (TIMEPASS*rots[x]*(0.5+BASS)*1.5f);
		gfxColour(1.0f, 1.0f, 1.0f, 0.0f-((x+btime)/8.0f));
		quad(rot[x],0.2f+((x+btime)/2.0));      
	}
	gfxEnd();
	
}