
/* mekka intro               */
/* c a l o d o x   2 0 0 1   */

#include "main.h"
#include <math.h>
#include "cubes.h"
#include "writer.h"
#include "prc_bonh.inc"



//taille matrice 163x52


//Cube_Matrix_Plot_LCD (163, 52, 1.0, 0.01215, 
//							 &ccolor, &ccolor1, 0.0, 1.0);


#define SyncroFunction INTERPOLATE_float;

void PRECALCSCR_part1(float fxinterp, float textinterp, float alphatextinterp);
void PRECALCSCR_part2(float fxinterp);
void PRECALCSCR_part3(float fxinterp, unsigned char style);


#define SYNC1_key2	3582		//debut du randomized logo -> logo final

void SYNC1_precalcscreen(int current_hertz)
{
	float a,b,c;

	if (current_hertz>3882) return;
	
	if (current_hertz>=0 && current_hertz<2913)
	{
	 a=0.0;													//no morphing = black screen
	 b=INTERPOLATE_float(0,2913,0.0,1.0,current_hertz);		//typed letters style
	 c=0.0;													//text totally visible
	}

	if (current_hertz>=2913 && current_hertz<SYNC1_key2)
	{
	 a=INTERPOLATE_float(2913,SYNC1_key2,0.0,1.0,current_hertz);	//morphing
	 b=1.0;													//whole text
	 c=INTERPOLATE_float(2913,SYNC1_key2,0.0,1.0,current_hertz);	//alpha blending for text.
	}

	if (current_hertz>=SYNC1_key2)
	{
	
		a=INTERPOLATE_float(SYNC1_key2,3882,0.0,1.0,current_hertz);
		PRECALCSCR_part2(a);
		return;
	}

	PRECALCSCR_part1(a, b, c);

}

#define SYNC2_key2	4064
#define SYNC2_key3	4433
#define SYNC2_key4	4822
#define SYNC2_key5	5183
#define SYNC2_key6	5510
#define SYNC2_key7	5900
#define SYNC2_keylight1 6015 
#define SYNC2_keylight2 6632
#define SYNC2_keylight3 6819






void SYNC2_matrixscreen(int current_hertz)
{

	float a;

	if (current_hertz>=3882 && current_hertz<SYNC2_key2)
	{
	a=INTERPOLATE_float(3882,SYNC2_key2,0.0,1.0,current_hertz);
	PRECALCSCR_part3(a, 1);
	}

	if (current_hertz>=SYNC2_key2 && current_hertz<SYNC2_key3)
	{
	a=INTERPOLATE_float(SYNC2_key2,SYNC2_key3,0.0,1.0,current_hertz);
	PRECALCSCR_part3(a, 4);
	}

	if (current_hertz>=SYNC2_key3 && current_hertz<SYNC2_key4)
	{
	a=INTERPOLATE_float(SYNC2_key3,SYNC2_key4,0.0,1.0,current_hertz);
		PRECALCSCR_part3(a, 0);
	}

	if (current_hertz>=SYNC2_key4 && current_hertz<SYNC2_key5)
	{
	a=INTERPOLATE_float(SYNC2_key4,SYNC2_key5,0.0,1.0,current_hertz);
	PRECALCSCR_part3(a, 3);
	}

	if (current_hertz>=SYNC2_key5 && current_hertz<SYNC2_key6)
	{
	a=INTERPOLATE_float(SYNC2_key5,SYNC2_key6,0.0,1.0,current_hertz);
	PRECALCSCR_part3(a, 2);
	}

	if (current_hertz>=SYNC2_key6 && current_hertz<SYNC2_key7)
	{
	a=INTERPOLATE_float(SYNC2_key6,SYNC2_key7,0.0,1.0,current_hertz);
		PRECALCSCR_part3(a, 5);
	}

	if (current_hertz>=SYNC2_key7 && current_hertz<SYNC2_keylight1)
	{
	a=INTERPOLATE_float(SYNC2_key7,SYNC2_keylight1,0.0,1.0,current_hertz);
		PRECALCSCR_part3(a, 3);
	}

	/* on fout la light sur le logo */
	if (current_hertz>=SYNC2_keylight1 && current_hertz<SYNC2_keylight2)
	{
	a=INTERPOLATE_float(SYNC2_keylight1,SYNC2_keylight2,0.0,1.0,current_hertz);
	PRECALCSCR_part3(a, 10);
	}

	/* on fout la light sur l'ensemble */
	if (current_hertz>=SYNC2_keylight2 && current_hertz<SYNC2_keylight3)
	{
	a=INTERPOLATE_float(SYNC2_keylight2,SYNC2_keylight3,0.0,1.0,current_hertz);
	PRECALCSCR_part3(a, 11);
	}


}




#define BNH_space 0.09
#define BNH_size  0.07


typedef struct {
	float block_x[219];
	float block_y[219];
	float block_sz[219];
	float block_nb;
}BNH_struct;


BNH_struct BNH_start;
BNH_struct BNH_end;

FLOATcolor BNH_block_color;

void BONHOMME_blocs(float x, float y, float size)
{

	float tx, ty;



	tx=0.5*size;
	ty=0.5*size;

	glLoadIdentity();										// reset the view
	glTranslatef(0.0f,0.0f,OGL_ZFOV);

	
	glColor3f(BNH_block_color.r,BNH_block_color.g,BNH_block_color.b);	

	glBegin(GL_QUADS);

				glVertex3f(x-tx, y -ty, -10.0);	
				glVertex3f(x+size-tx, y-ty, -10.0);
				glVertex3f(x+size-tx, y+size -ty, -10.0);
				glVertex3f(x-tx, y+size -ty, -10.0);

	glEnd();

}




void BONHOMME_draw()
{
	int i=0;
	int bidx=0;

	for (int y=0;y<9;y++)
	{
		for (int x=0;x<31;x++)
		{

			if (precalc_bonhomme[i]!=0)
			{			
				//BONHOMME_blocs(BNH_space*x, BNH_space*y, BNH_size);	
				BNH_start.block_x[bidx]=3.772990+BNH_space*x;
				BNH_start.block_y[bidx]=-0.966002+BNH_space*y;
				BNH_start.block_sz[bidx]=BNH_size;
				bidx++;
			}
		i++;
		}
	}
	BNH_start.block_nb=bidx;
}


void BONHOMME_fill_end()
{


	for (int i=0;i<BNH_start.block_nb;i++)
	{

		int x=random(160);
		int y=random(51);

		BNH_end.block_x[i]=(-1.292434+0.01636*x)*5.1;
		BNH_end.block_y[i]=(-0.425358+0.01636*y)*5.1;
		BNH_end.block_sz[i]=0.01215*5.1;
	}
	BNH_end.block_nb=BNH_start.block_nb;
}


void BONHOMME_interpolate(float normalized_step)
{
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_TEXTURE_2D);


	for (int i=0;i<BNH_start.block_nb;i++)
	{
	 float nx=INTERPOLATE_float(0, 10000, BNH_start.block_x[i], BNH_end.block_x[i], normalized_step*9999.0);
	 float ny=INTERPOLATE_float(0, 10000, BNH_start.block_y[i], BNH_end.block_y[i], normalized_step*9999.0);
	 float ns=INTERPOLATE_float(0, 10000, BNH_start.block_sz[i], BNH_end.block_sz[i], normalized_step*9999.0);
	
	 BONHOMME_blocs(nx, ny, ns);	
	
	}

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
}


unsigned char STATE_PRECALCSRC_done1=0;
unsigned char STATE_PRECALCSRC_done2=0;
unsigned char STATE_PRECALCSRC_done3=0;


FLOATcolor matrice_color={130.0*INV256*0.5, 166.0*INV256*0.5, 132*INV256*0.5};
FLOATcolor matrice_color1={191.0*INV256,244.0*INV256, 193.0*INV256};


unsigned char PRECALCSCR_matrix_buffer[163*52];

void PRECALCSCR_part1(float fxinterp, float textinterp, float alphatextinterp)
{

	if (STATE_PRECALCSRC_done1==0)
	{

		//1. creation de l'ecran de la matrice sans points si c'est pas encore fait

		glClearColor(67.0*INV256, 94.0*INV256, 68.0*INV256, 0.0f);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glLoadIdentity();
		glColor3f(1.0, 1.0, 1.0);

		memset(PRECALCSCR_matrix_buffer, 0, 163*52);
			

		Cube_Matrix_Plot_LCD (163, 52, 1.0, 0.01215, &matrice_color, &matrice_color1, 
							  0.0, 0.0, (unsigned char*) &PRECALCSCR_matrix_buffer);

		// 2. on place dans la texture si c'est pas encore fait

		glBindTexture(GL_TEXTURE_2D, texturebuffer);
        glCopyTexSubImage2D(GL_TEXTURE_2D, 0,0,0,0,0,640,480);	

		//3. on calcule les ptits bonhommes et les points aleatoires de destination
		BONHOMME_draw();		
		BONHOMME_fill_end();
	
	 STATE_PRECALCSRC_done1=1;	//done.
	}

	
	glClearColor(0.0*INV256, 0.0*INV256, 0.0*INV256, 0.0f);	//efface noir...
	glClear(GL_COLOR_BUFFER_BIT);


	float alphatxt=1.0-(alphatextinterp*2.0);	//pour fader le texte 2x plus vite
	if (alphatxt<0.0) alphatxt=0.0;

	BNH_block_color.r=1.0+(matrice_color1.r-1.0)*fxinterp;
	BNH_block_color.g=1.0+(matrice_color1.g-1.0)*fxinterp;
	BNH_block_color.b=1.0+(matrice_color1.b-1.0)*fxinterp;


	unsigned char str1[]="DEATH IN VEGAS         ";		//23 chars
	unsigned char str2[]="DAKE AND FRED ARE BACK ";
	unsigned char str3[]="DANCING WITH GENOX     ";

	int limitstrg;

	if (textinterp >= 0.0 && textinterp< 0.33)
	{
		limitstrg=22*textinterp*(1/0.33);
		str2[limitstrg]=0;
		str1[0]=0;
		str3[0]=0;
	}

	if (textinterp>=0.33 && textinterp<0.66)
	{
		limitstrg=22*(textinterp-0.33)*(1/0.33);
		str3[limitstrg]=0;
		str1[0]=0;
	}

	if (textinterp>=0.66 && textinterp<=1.0)
	{
		limitstrg=22*(textinterp-0.66)*(1/0.33);
		str1[limitstrg]=0;
	}


	FONT_print_string((unsigned char*) &str1, arial_font_bold, 0.35, -1.134, -0.405,0.0, alphatxt, 0);
	FONT_print_string((unsigned char*) &str2, arial_font_normal, 0.12, -2.589, -0.217, 0.0, alphatxt, 0);
	FONT_print_string((unsigned char*) &str3, arial_font_normal, 0.12, -2.365, -0.35, 0.0, alphatxt, 0);

	
	glBindTexture(GL_TEXTURE_2D, texturebuffer);
	LAYER_buffer(fxinterp, 1.0);

	BONHOMME_interpolate(fxinterp);

	glBindTexture(GL_TEXTURE_2D, textures[1]);
	MASK_draw(4.780015, 0.27, 0.0,0.89, 0.0);
	MASK_draw(4.780015, 0.27, -0.939,-0.129999, 90.0);
	MASK_draw(3.67, -1.777, -2.739998,0.040001, 90.0);
	MASK_draw(3.67, -1.777, 0.0,2.76, 0.0);
}


/**********************************************************************************************/

int PRECALCSRC_morphing_idx[163*52];

void PRECALCSCR_part2(float fxinterp)		//affichage de points aleatoires
{

	if (STATE_PRECALCSRC_done2==0)
	{

		//calcule une liste d'indexs 1 à 163x52 mais dans un ordre aleatoire
		
		for (int i=0;i<163*52;i++)
		{
		 PRECALCSRC_morphing_idx[i]=i;
		}

		//randomize tout ca en effectuant X permutations au sein de la liste 

		for (int j=0;j<10000;j++)
		{
		 int idx1=random(162*52);
		 int idx2=random(162*52);

		 int swp;

			if (idx1!=idx2)
			{
				swp=PRECALCSRC_morphing_idx[idx1];
				PRECALCSRC_morphing_idx[idx1]=PRECALCSRC_morphing_idx[idx2];
				PRECALCSRC_morphing_idx[idx2]=swp;
			}
		}
		
	STATE_PRECALCSRC_done2=1;
	}


	//crée la matrice conséquente et morphée..

	int maxidx=fxinterp*163.0*52.0;		//choppe un index maximum (maximum = on a toute la matrice d'origine)
	memset(PRECALCSCR_matrix_buffer, 0, 163*52);

	for (int u=0;u<maxidx;u++)
	{
	int pos=PRECALCSRC_morphing_idx[u];
	PRECALCSCR_matrix_buffer[pos]=lcdlogo[pos];
	}

	//balance quelques points aleatoires pour faire une transition 
	
	for (int i=0;i<(1.0-fxinterp)*84;i++)
	{
		if (i>5)
		{
		PRECALCSCR_matrix_buffer[random(162*53)]=0xFF;
		}
	}
	
	glClearColor(67.0*INV256, 94.0*INV256, 68.0*INV256, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	glColor3f(1.0, 1.0, 1.0);
		
	int z=0;


	Cube_Matrix_Plot_LCD (163, 52, 1.0, 0.01215, &matrice_color, &matrice_color1, 
						  0.0, 0.0, (unsigned char*) &PRECALCSCR_matrix_buffer);


	
	glBindTexture(GL_TEXTURE_2D, textures[1]);
	MASK_draw(4.780015, 0.27, 0.0,0.89, 0.0);
	MASK_draw(4.780015, 0.27, -0.939,-0.129999, 90.0);
	MASK_draw(3.67, -1.777, -2.739998,0.040001, 90.0);
	MASK_draw(3.67, -1.777, 0.0,2.76, 0.0);

}


/**********************************************************************************************/

float PRECALCSRC_sin[256];
float PRECALCSRC_cos[256];

void PRECALCSCR_part3(float fxinterp, unsigned char style)		
{



	if (STATE_PRECALCSRC_done3==0)
	{

		for (int w=0;w<256;w++)
		{
			PRECALCSRC_sin[w]=sin(w*3.14/128.0);
			PRECALCSRC_cos[w]=cos(w*3.14/128.0);
		}

	STATE_PRECALCSRC_done3=1;
	}




	glClearColor(67.0*INV256, 94.0*INV256, 68.0*INV256, 0.0f);

	if (style==11)
	{ glClearColor((67.0+fxinterp*(286.0-67.0))*INV256, (94.0+fxinterp*(286.0-94.0))*INV256, 
					(68.0+fxinterp*(286.0-69.0))*INV256, 0.0f);}


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();
	glColor3f(1.0, 1.0, 1.0);
		
	int z=0;


	int ttt=TIMER_return_timer();


	for (int y=0;y<52;y++)
	{
		for (int x=0;x<163;x++)
		{
		float z1, z2;
			

			if (style==0)
			{
				int ag=x+ttt*0.05;
				int ag1=y+ttt*0.03;
				ag<<=2; ag1<<=2;

				float h=PRECALCSRC_sin[ag&255]*0.33;
				h+=PRECALCSRC_cos[ag1&255]*0.33;
				h+=PRECALCSRC_cos[(ag1+ag)&255]*0.33;
				 //z1=0.6+fabs(h)*0.4;
				 z2=0.6+(1.0-fabs(h))*0.5;
				 z1=z2;
			}

			if (style==1)
			{
				int ag=x+ttt*0.08;
				int ag1=(y+x)+ttt*0.07;
				ag<<=2; ag1<<=2;

				float h=PRECALCSRC_sin[ag&255]*0.33;
				h+=PRECALCSRC_sin[ag1&255]*0.33;
				h+=PRECALCSRC_cos[(ag1+40)&255]*0.33;
				 z1=0.6;
				 z2=0.6+(1.0-fabs(h))*0.4;
			}

			
			if (style==2)
			{
				int ag=x+ttt*0.08;
				int ag1=(y+x)+ttt*0.07;
				ag<<=4; ag1<<=4;

				float h=PRECALCSRC_sin[ag&255]*0.33;
				h+=PRECALCSRC_sin[(ag1+ag)&255]*0.33;
				h+=PRECALCSRC_cos[(ag1+40)&255]*0.33;
				 z1=0.7;
				 z2=0.9+(h*0.1);
			}

			if (style==3)
			{
				int ag=x+ttt*0.08;
				int ag1=(y+x)+ttt*0.07;
				ag<<=4; ag1<<=4;

				float h=PRECALCSRC_cos[ag&255]*0.33;
				h+=PRECALCSRC_sin[(ag1)&255]*0.33;
				h+=PRECALCSRC_cos[(ag+40)&255]*0.33;
				z1=0.8+(h*0.1);
				z2=1.0;
			}

			if (style==4)
			{
			
				int ag=x+ttt*0.18;
				float bg=x+ttt*0.05;
				float bg1=y+ttt*0.03;

				int hz=sin(bg*3.14/81.5)*100.0;
				hz+=cos(bg1*3.14/26)*100.0;
				hz+=cos((bg1+bg)*3.14/26)*100.0;
				hz+=sin((bg1+bg*0.1)*3.14/26*0.5)*100.0;

				hz>>=7;

				z1=0.7;
				z2=0.9+(1.0-fabs(hz))*fabs(PRECALCSRC_cos[ag&255]*0.1);	
			}


			if (style==5)
			{
			
				int ag=x+ttt*0.18;
				float bg=x+ttt*0.05;
				float bg1=y+ttt*0.03;
		
				int hz=sin(bg*3.14/81.5)*100.0;
				hz+=cos(bg1*3.14/26)*100.0;
				hz+=sin((bg1+bg)*3.14/26*0.5)*100.0;
				hz+=cos((bg1)*3.14/26*0.5)*100.0;
				hz>>=7;
				z1=0.7;
				z2=0.9+(1.0-fabs(hz))*0.1;	
			}
		

			if (style==10)
			{
				int ag=x+ttt*0.08;
				int ag1=(y+x)+ttt*0.07;
				ag<<=4; ag1<<=4;


				float h=PRECALCSRC_cos[ag&255]*0.33;
				h+=PRECALCSRC_sin[(ag1)&255]*0.33;
				h+=PRECALCSRC_cos[(ag+40)&255]*0.33;
				z1=0.8+(h*0.1);
				z2=1.0;

				if (lcdlogo[z]!=0)	
				{
				Cube_Matrix_Height_Light[z]=1.0-fxinterp*0.2;
				Cube_Matrix_Height_Light_Alpha[z]=fxinterp*0.3;
				}
				else
				{
				Cube_Matrix_Height_Light[z]=1.0;
				Cube_Matrix_Height_Light_Alpha[z]=0.0;
				}
			}

			if (style==11)
			{
				z1=0.8;
				z2=1.0; 

				if (lcdlogo[z]!=0)	
				{
				Cube_Matrix_Height_Light[z]=0.8;
				Cube_Matrix_Height_Light_Alpha[z]=0.3;
				}
				else
				{
				Cube_Matrix_Height_Light[z]=1.0-fxinterp*0.2;
				Cube_Matrix_Height_Light_Alpha[z]=fxinterp;
				}
			}


/*
			short mkey[]={3882, 3952, 4019, 4112, 4136, 4160, 4180, 4203, 4227, 4250, 4320, 4387, 4501,
						  4524, 4548, 4571, 4594};

			char hmkey[]={80, 80, 50, 60, 80, 60, 80, 60, 80, 50,   80, 80,  60, 80, 60, 80, 60};

*/
			
			int fredkey[]={50,3882,80,3952,50,3975,80,4019,50,4112,80,4136,50,4160,80,4180,50,4203,80,4227,50,4250,80,4320,50,4340,80,4387,50,4501,80,4524,50,4548,80,4571,50,4594,
80,4618,50,4685,80,4708,50,4731,80,4755,50,4822,80,4846,50,4869,80,4892,50,4915,80,4939,50,4962,80,4982,50,5052,80,5076,50,5100,80,5120,50,5190,80,5213,50,5237,80,5260,50,5280,80,5304,50,5327,
80,5350,50,5421,80,5441,50,5488,80,5581,50,5601,80,5625,50,5648,80,5671,50,5695,80,5719,50,5786,80,5809,50,5856,80,5969,50,5992,80,6016,50,6040,80,6063,
50,6083,80,6153,50,6177,80,6223,50,6267,80,6290,50,6314,80,6338,50,6361,80,6384,50,6405,80,6428,50,6451,80,6521,50,6544,80,6600,50,6635,80,6659,50,6682,80,6705,50,6726,80,6749,50,6772,80,6796};
	
		
			float aginhib;

			for (int keyidx=0;keyidx<86*2;keyidx+=2)
			{
				float k;
				//if (fredkey[keyidx]==50){k=80.0;} else {k=50.0;}
				k=fredkey[keyidx];
				if (current_hertz>=fredkey[keyidx+1] && current_hertz<=(fredkey[keyidx+1]+10)){ z1*=k*0.01; }						
			}


			 if (lcdlogo[z]!=0)
			{
				Cube_Matrix_Height[z]=z1;

			 }
			
			 else
			 {
				Cube_Matrix_Height[z]=z2;		//le background
			 }
			

			z++;
		}
	}


	if (style==10 || style==11)
	{
 

	Cube_Matrix_Plot_LCD_V3(163, 52, 0.01215, 
							 &matrice_color, &matrice_color1, fxinterp*0.1,
							 (unsigned char*) &lcdlogo);

	}
	else
	{
    Cube_Matrix_Plot_LCD_V2(163, 52, 1.0, 0.01215, &matrice_color, &matrice_color1, 
				            0.0, 0.0, (unsigned char*) &lcdlogo);
	}
	
	glBindTexture(GL_TEXTURE_2D, textures[1]);

	float rideau;

	if (style==11)
	{
	 rideau=fxinterp*2.5;
	}
	else 
	{
	 rideau=0.0;
	}

	MASK_draw(4.780015, 0.27, 0.0,       0.89+rideau,        0.0);
	MASK_draw(4.780015, 0.27, -0.939-rideau,    -0.129999,   90.0);
	MASK_draw(3.67, -1.777,   -2.739998-rideau, 0.040001,    90.0);
	MASK_draw(3.67, -1.777,   0.0,       2.76+rideau,        0.0);
	

}

