#include "main.h"
#include <math.h>
#include "3d.h"



/*

	greets

3state
aardbei
amable
benoit

blasphemy
bomb
cocoon
exceed

 fake that
fuzzion
HAUJOBB
houdini

  hnzmssv
KOLOR
kooma
kwak

  nomad
nästa
numb
oj

  orion
PARK
popsy team
replay

  satori
slengpung
sunflower
tbl

  tpolm
vantage
xibalba

  */

#define S_RUBAN_1	20815
#define L_RUBAN_1	  1000
#define H_RUBAN_1	  900

#define S_RUBAN_2	21551
#define L_RUBAN_2	  1000
#define H_RUBAN_2	  900

#define S_RUBAN_3	22292
#define L_RUBAN_3	  1000
#define H_RUBAN_3	  900


#define S_RUBAN_4	23028
#define L_RUBAN_4	  1000
#define H_RUBAN_4	  900



void SPLINES_Draw_Text(float maxt, int nb_pts, int intrasteps, float zoom, float alpha, Vect2D move, 
					   unsigned char *strg, int style_x, int style_y, float speed, float factor1, float factor2);



void SYNC_tesla(int current_hertz)
{


	if (current_hertz>=23928 && current_hertz<25219)
	{

		float alpha=1.0;
		if (current_hertz<=24028)
		{
		alpha=INTERPOLATE_float(23928, 24028, 0.0, 1.0, current_hertz);
		}
		else
		{
			glClearColor(28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f);	//
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
			glBindTexture(GL_TEXTURE_2D, textures[4]);
			CIEL_LAYER(1.0);
		}

		TESLA_do(alpha,1.0,1.0);
	}

}



void SYNC_greets(int current_hertz)
{

	float a,b;
	Vect2D mov={0.0};

	if (current_hertz<S_RUBAN_1 || current_hertz>=S_RUBAN_4+L_RUBAN_4) return;

			glClearColor(28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f);	//
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
			glBindTexture(GL_TEXTURE_2D, textures[4]);
			CIEL_LAYER(1.0);
	

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

	if (current_hertz>=S_RUBAN_1 && current_hertz<S_RUBAN_1+L_RUBAN_1)
	{
		float a=INTERPOLATE_float(S_RUBAN_1, S_RUBAN_1+L_RUBAN_1, 0.0, 5.28, current_hertz);


		float alpha=1.0, b=0.0;
		
			if (current_hertz>S_RUBAN_1+H_RUBAN_1)
			{
				alpha=INTERPOLATE_float(S_RUBAN_1+H_RUBAN_1, S_RUBAN_1+L_RUBAN_1, 1.0, 0.0, current_hertz);
				b=INTERPOLATE_float(S_RUBAN_1+H_RUBAN_1, S_RUBAN_1+L_RUBAN_1, 0.0, 2.00, current_hertz);
			}

		SPLINES_Draw_Text(a, 600, 4, 1.0+b, alpha, mov, (unsigned char*)" 3state aardbei amable benoit bypass blasphemy bomb cocoon dilemma exceed ", 
							0,1, ACCURATE_TIMER*0.00016, 3.0, 4.0);
	}

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


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

	if (current_hertz>=S_RUBAN_2 && current_hertz<S_RUBAN_2+L_RUBAN_2)
	{
		float a=INTERPOLATE_float(S_RUBAN_2, S_RUBAN_2+L_RUBAN_2, 0.0, 7.28, current_hertz);


		float alpha=1.0, b=0.0;
		
			if (current_hertz>S_RUBAN_2+H_RUBAN_2)
			{
				alpha=INTERPOLATE_float(S_RUBAN_2+H_RUBAN_2, S_RUBAN_2+L_RUBAN_2, 1.0, 0.0, current_hertz);
				b=INTERPOLATE_float(S_RUBAN_2+H_RUBAN_2, S_RUBAN_2+L_RUBAN_2, 0.0, 2.00, current_hertz);
			}

		SPLINES_Draw_Text(a, 600, 4, 1.0+b, alpha, mov, (unsigned char*)" fakethat haujobb kolor fuzzion haujobb  hnzmssv kolor kooma", 
							1,0, ACCURATE_TIMER*0.00016, 3.4, 3.5);
	}

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

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

	if (current_hertz>=S_RUBAN_3 && current_hertz<S_RUBAN_3+L_RUBAN_3)
	{
		float a=INTERPOLATE_float(S_RUBAN_3, S_RUBAN_3+L_RUBAN_3, 0.0, 5.28, current_hertz);

		mov.x=0.0;
		mov.y=0.0;

		float alpha=1.0, b=0.0;
		
			if (current_hertz>S_RUBAN_3+H_RUBAN_3)
			{
				alpha=INTERPOLATE_float(S_RUBAN_3+H_RUBAN_3, S_RUBAN_3+L_RUBAN_3, 1.0, 0.0, current_hertz);
				b=INTERPOLATE_float(S_RUBAN_3+H_RUBAN_3, S_RUBAN_3+L_RUBAN_3, 0.0, 2.00, current_hertz);
			}

		SPLINES_Draw_Text(a, 600, 4, 1.0+b, alpha, mov, (unsigned char*)"nomad nasta numb oj orion  park  popsy team replay ", 
							0,1, ACCURATE_TIMER*0.00016, 4.4, 4.5);


	}

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

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

	if (current_hertz>=S_RUBAN_4 && current_hertz<=S_RUBAN_4+L_RUBAN_4)
	{
		float a=INTERPOLATE_float(S_RUBAN_4, S_RUBAN_4+L_RUBAN_4, 0.0, 5.28, current_hertz);

		mov.x=sin(ACCURATE_TIMER*0.0001)*0.3;
		mov.y=cos(ACCURATE_TIMER*0.0002)*0.4;

		float alpha=1.0, b=0.0;
		
			if (current_hertz>S_RUBAN_4+H_RUBAN_4)
			{
				alpha=INTERPOLATE_float(S_RUBAN_4+H_RUBAN_4, S_RUBAN_4+L_RUBAN_4, 1.0, 0.0, current_hertz);
				b=INTERPOLATE_float(S_RUBAN_4+H_RUBAN_4, S_RUBAN_4+L_RUBAN_4, 0.0, 2.00, current_hertz);
			}

		SPLINES_Draw_Text(a, 600, 4, 1.0+b, alpha, mov, (unsigned char*)"satori   slengpung   sunflower tbl  tpolm unik vantage xibalba ", 
							0,0, ACCURATE_TIMER*0.00016, 3.4, 2.5);


	}



	//glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
//	glClear(GL_COLOR_BUFFER_BIT);
//		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

			if (current_hertz>20815 && current_hertz<=21000)
		{
		Vect3D c2={28*0.5, 27*0.5, 87*0.5};
					float alpha=INTERPOLATE_float(20815, 21000, 1.0, 0.0, current_hertz);
		 LAYER_flat(c2, alpha);
		}
	
	

}




void MEDUSE_manage_everything(float step);

void SYNC5_submarine(int current_hertz)	//a simplifier
{


	if (current_hertz<12690 || current_hertz>20815) return;

		if (current_hertz>=12690 && current_hertz<14571)
		{
		
		
			glClearColor(28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f);	//
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glEnable(GL_TEXTURE_2D);

			float a=INTERPOLATE_float(12690, 14571, 0.0, 1.0, current_hertz);
		
			MEDUSE_manage_everything(a);
		}

		if (current_hertz>=14571 && current_hertz<15684)	
		{
		
		
			glClearColor(28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f);	//
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glEnable(GL_TEXTURE_2D);

			float a=INTERPOLATE_float(14571, 15684, 1.0, 3.0, current_hertz);
		
			MEDUSE_manage_everything(a);
		}

		if (current_hertz>=15684 && current_hertz<17140)	
		{
		
		
			glClearColor(28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f);	//
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glEnable(GL_TEXTURE_2D);

			float a=INTERPOLATE_float(15684, 17140, 3.0, 4.0, current_hertz);
		
			MEDUSE_manage_everything(a);
		}

		if (current_hertz>=17140 && current_hertz<20815)	
		{
		
		
			glClearColor(28*0.5*INV256, 27*0.5*INV256, 87*0.5*INV256, 1.0f);	//
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glEnable(GL_TEXTURE_2D);

			float a=INTERPOLATE_float(17140, 20815, 4.0, 7.0, current_hertz);
		
			MEDUSE_manage_everything(a);
		}

		
		if (current_hertz>=20500 && current_hertz<=20815)
		{
		Vect3D c2={28*0.5, 27*0.5, 87*0.5};
					float alpha=INTERPOLATE_float(20500, 20815, 0.0, 1.0, current_hertz);
		 LAYER_flat(c2, alpha);
		}
	
		
		
		/* fade bleu */
		
		if (current_hertz>=12690 && current_hertz<12800)
		{
		Vect3D c2={28*0.5, 27*0.5, 87*0.5};
					float alpha=INTERPOLATE_float(12690, 12800, 1.0, 0.0, current_hertz);
		 LAYER_flat(c2, alpha);
		}


	
	//greets jusqu'a :


}


void LAYER_flat(Vect3D color, float alpha)
{

			glLoadIdentity();  
			glTranslatef(0.0f,0.0f,-5.0);
			glDisable(GL_TEXTURE_2D);
			glDisable(GL_DEPTH_TEST);
			glEnable(GL_BLEND);

		
			glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

			glColor4f(color.x*INV256, color.y*INV256, color.z*INV256,alpha);		// Full Brightness, 50% Alpha ( NEW )

			glBegin(GL_QUADS);

		   glVertex3f(OGL_MINX*3.0, OGL_MAXY*3.0,  -0.0f);	// Bottom Left Of The Texture and Quad
		   glVertex3f(OGL_MAXX*3.0,  OGL_MAXY*3.0,  -0.0f);	// Bottom Right Of The Texture and Quad
		   glVertex3f(OGL_MAXX*3.0, OGL_MINY*3.0,  -0.0f);	// Top Right Of The Texture and Quad
		   glVertex3f(OGL_MINX*3.0, OGL_MINY*3.0,  -0.0f);	// Top Left Of The Texture and Quad
	
			glEnd();


			glDisable(GL_BLEND);
			glEnable(GL_DEPTH_TEST);
			glEnable(GL_TEXTURE_2D);
	}

void CIEL_LAYER(float zooma)	//zoom = 1 pour un rendu 1:1
{


	zooma*=2.0799999999;		

	glLoadIdentity();  
	glTranslatef(0.0f,0.0f,-5.0);
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);


	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);





	float tx=640.0/1024.0;
	float ty=480.0/1024.0;

		glBegin(GL_QUADS);

			glColor4f(1.0f,1.0f,1.0f,1.0);		// Full Brightness, 50% Alpha ( NEW )

		  glTexCoord2f(0.0, 1.0);  glVertex3f(OGL_MINX*zooma, OGL_MAXY*zooma*0.65,  -0.0f);	// Bottom Left Of The Texture and Quad
		  glTexCoord2f(tx, 1.0);  glVertex3f(OGL_MAXX*zooma, OGL_MAXY*zooma*0.65,  -0.0f);	// Bottom Right Of The Texture and Quad
		  
		  glColor4f(1.0f,1.0f,1.0f,0.0);		// Full Brightness, 50% Alpha ( NEW )
		  glTexCoord2f(tx, 0.0f);  glVertex3f(OGL_MAXX*zooma,0.0,  -0.0f);	// Top Right Of The Texture and Quad
		  glTexCoord2f(0.0f, 0.0f); glVertex3f(OGL_MINX*zooma, 0.0,  -0.0f);	// Top Left Of The Texture and Quad
	
        glEnd();

	glColor4f(1.0f,1.0f,1.0f,0.0);
	glDisable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);

	
}



#include "pal.inc"


void TESLA_do(float alpha, float size, float numberscale)
{


		float u=(((TIMER_return_timer()&1023))/1024.0);
		float glup=fabs(sin(u*3.14));
        
		float fact1=(1024-(TIMER_return_timer()&1023))/1024.0;
		float fact2=fabs(sin(fact1*3.14*2));
		float fact3=fabs(sin(fact1*3.14*3));
	
		glBindTexture(GL_TEXTURE_2D, spriteleaf);

		
	for (int i=0;i<300*numberscale;i+=1)
	{
		Vect2D r;
		Vect3D c;
		float param[3];
		unsigned char j=i;

		if (j==0) j=1;
		c.x=palette[(j<<2)]*INV256;
		c.y=palette[(j<<2)+1]*INV256;
		c.z=palette[(j<<2)+2]*INV256;


		param[0]=4*((TIMER_return_timer()&1023))/1024.0;
		param[1]=((TIMER_return_timer()&1023))/1024.0;

		r.x=-1.0+5.0*cos(param[0]+i)*sin(param[0]+i)*cos(param[0]+i);
		r.y=1.0+3.0*sin((param[1]+i)*6)*sin(param[1]+i);

		r.x*=size;
		r.y*=size;

		BRUSH_draw(0.3, r, fabs(sin((param[1]+i*4))*360), alpha*fabs(sin(param[1]+i))*0.8, c);
	}


	for ( i=0;i<300*numberscale;i+=1)
	{
		Vect2D r;
		Vect3D c;
		float param[3];
		unsigned char j=i;

		if (j==0) j=1;
		c.x=palette[(j<<2)]*INV256;
		c.y=palette[(j<<2)+1]*INV256;
		c.z=palette[(j<<2)+2]*INV256;


		param[0]=4*((TIMER_return_timer()&1023))/1024.0;
		param[1]=((TIMER_return_timer()&1023))/1024.0;

		r.x=8.0*cos(param[0]+i)*cos(param[1]+i)*cos(param[1]+i)*cos((param[0]+i)*3);
		r.y=1.0+4.0*sin((param[1]+i)*6)*cos(param[1]+i*2);

				r.x*=size;
		r.y*=size;

	//	z=(rand() % 1000)/1000.0;

		BRUSH_draw(0.3, r, fabs(sin((param[1]+i*4))*360), alpha*fabs(sin(param[0]+i))*0.8, c);
	}


	for ( i=0;i<300*numberscale;i+=1)
	{
		Vect2D r;
		Vect3D c;
		float param[3];
		unsigned char j=i;


		if (j==0) j=1;
		c.x=palette[(j<<2)]*INV256;
		c.y=palette[(j<<2)+1]*INV256;
		c.z=palette[(j<<2)+2]*INV256;


		param[0]=6*((TIMER_return_timer()&1023))/1024.0;
		param[1]=((TIMER_return_timer()&1023))/1024.0;

		r.x=1.0+8.0*cos(cos(param[0])+i)*cos(param[1]+i)*sin(param[0]+i)*cos((param[1]+i)*5);
		r.y=-1.0+4.0*sin((param[1]+i)*6)*cos(param[1]+i*2);

				r.x*=size;
		r.y*=size;


		BRUSH_draw(0.3, r, fabs(sin((param[1]+i*4))*360), alpha*fabs(sin(param[1]+i))*0.8, c);
	}


	for (  i=0;i<410*numberscale;i+=4)
	{
		Vect2D r;
		Vect3D c;
		float param[3];
		unsigned char j=i;


		if (j==0) j=1;
		c.x=1.0;
		c.y=1.0;
		c.z=1.0;


		param[0]=(2*((TIMER_return_timer()&2047))/2048.0);
		param[1]=((TIMER_return_timer()&1023))/1024.0;

		
		r.x=6.0*cos(param[0]+i*2)*cos(param[0]+i*4)*cos(param[0]+i*2)*cos((param[0]+i*2)*3);
		r.y=6.0*sin((param[0]+i*2)*6)*cos(param[0]+i*2);

		r.x+=4*cos(param[0]+i*2)*cos(param[1]+i*4)*cos(param[1]+i*4)*cos((param[0]+i*4)*3);
		r.y+=2*sin((param[0]+i*2)*6)*cos(param[1]+i*2);

		r.x*=size;
		r.y*=size;


		BRUSH_draw(1.2, r, fabs(i+sin((param[1]+i*33))*360), alpha*fabs(sin(param[1]+i*10))*fabs(sin(param[1]+i*120))*0.4, c);
	}
  }



  void PUT_FUCKING_MASK(int current_hertz)
  {
	  if (current_hertz>=10240 && current_hertz<=28200)
	  {
  		glBindTexture(GL_TEXTURE_2D, textures[1]);
		MASK_draw(4.780015, 0.27, 0.0,       0.89+0.5,        0.0);
		MASK_draw(4.780015, 0.27, -0.939-0.5,    -0.129999,   90.0);
		MASK_draw(3.67, -1.777,   -2.739998-0.5, 0.040001,    90.0);
		MASK_draw(3.67, -1.777,   0.0,       2.76+0.5,        0.0);
	  }
  }