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

/* things to make and do :									 */
/* - desactivate the screensaver							 */


#define STARTING_ORDER 0 //1	//4
int ADDING_TIMING=0;//14000; //26000//26000 //28200 //1940	//5350
int current_hertz;

#include <stdio.h>
#include <math.h>
#include "main.h"
#include "cubes.h"
#include "writer.h"
#include "textures.h"
#include "3d.h"
#include "WaveletC.h"

#include "genox.inc"
#include "mxmplayer/mxmplay.h"
#include "mxmplayer/dsio.h"

#include "savehead.h"


#include "hand.inc"	
#include "feuille.inc"	//version non blurree
#include "bubble.inc"

#include "houd0.inc"
#include "houd1.inc"
#include "houd2.inc"
#include "houd3.inc"
#include "houd4.inc"
#include "houd5.inc"
#include "houd6.inc"
#include "houd7.inc"

#include "boulemsk.inc"
#include "boulekwi.inc"
#include "bmsk2.inc"

#include "txtnml1.inc"
#include "txtnml2.inc"
#include "txtnml3.inc"
#include "txtnml4.inc"



#define FULLSCREEN
#define ERRORMODE				// show all errors (through dialog boxes)
#define ACTIVATE_BOLD

char *genox_mxm;

GLuint			textures[TOTAL_TEXTURES];		//utiliser TOTAL_TEXTURES de main.h pour modifier le nb
unsigned char   *textures_buffer0, *textures_buffer0_temp;
unsigned char   *textures_buffer1;
unsigned char   *textures_buffer2;
unsigned char	*boule_decompressed_kwi, *masque_buffer, *boule_buffer2;
unsigned char   *bigtexture_buffer;

GLuint			texturebuffer; 
GLuint			temptexture;
GLuint			spritebubble;
GLuint			spriteleaf;
GLuint			textureboule,masque2, textureboule2;
KTEXT			arial_font_normal[97];		//de 32 à 128 = caractères utilisables 
KTEXT			arial_font_bold[97];		    //de 32 à 128 = caractères utilisables 
GLuint			houdini_frames[30]; 

static	HGLRC hRC;	static	HDC hDC;		
FILE *debugfile;
int keyparam=0;
GLfloat OGL_ZFOV=-2.431f;
void SYNC1_precalcscreen(int current_hertz);
void SYNC2_matrixscreen(int current_hertz);
void SYNC3_manonegra(int current_hertz);
void SYNC4_lotus(int current_hertz);
void SYNC5_submarine(int current_hertz);	//a simplifier
void SYNC_greets(int current_hertz);
void SYNC_tesla(int current_hertz);
void DECOMPRESS_samples();
void SPHERE3D_setup();	//le nouveau code
void SPHERE_precalc();	//l'ancien code de la sphere...a jarter
void SYNCRO_FINAL_PART(int current_hertz);

void AFFICHE_TEXTE();
//void *PROCESS_NML_FILE(unsigned char *filebuffer);
void PROCESS_NML_FILE(unsigned char *filebuffer, unsigned char *dest);
  void PUT_FUCKING_MASK(int current_hertz);
/************* gestion des textes - a jarter a la fin ***********/


float  KEY_POS_X;		
float  KEY_POS_Y;		
float  KEY_ZOOM;
int	KEY_MSG;	
float KEY_ECART;	 


/* OPENGL INITIALIZATION ***************************************************************************************/


void GENERATE_fonts()
{


	for (int i=0;i<96;i++)
	{
		memset(&arial_font_normal[i].text,0,255);
		arial_font_normal[i].text[0]=i+32;		//on commence par le caractère "espace"
		arial_font_normal[i].text[1]=0;
		arial_font_normal[i].pointsize=42;
		arial_font_normal[i].spacing=1;
		arial_font_normal[i].weight=400;
		drawfont(&arial_font_normal[i]);

		#ifdef ACTIVATE_BOLD
		memset(&arial_font_bold[i].text,0,255);
		arial_font_bold[i].text[0]=i+32;		//on commence par le caractère "espace"
		arial_font_bold[i].text[1]=0;
		arial_font_bold[i].pointsize=42;
		arial_font_bold[i].spacing=1;
		arial_font_bold[i].weight=700;
		drawfont(&arial_font_bold[i]);
		#endif
	}
}





void CreateTexture()
{

	textures_buffer0_temp=(unsigned char*) malloc(256*256);
	textures_buffer0=(unsigned char*) malloc(256*256*4);
	TEXTURE_scale_greyscale_128x128_to_256x256((unsigned char*) &spritemain, textures_buffer0_temp);
    TEXTURE_blur_greyscale_256x256(textures_buffer0_temp);
	TEXTURE_blur_greyscale_256x256(textures_buffer0_temp);
	TEXTURE_blur_greyscale_256x256(textures_buffer0_temp);
    TEXTURE_greyscale_clip_whiteblack(textures_buffer0_temp, textures_buffer0_temp, 128);
	TEXTURE_blur_greyscale_256x256(textures_buffer0_temp);
	TEXTURE_greyscale_invert(textures_buffer0_temp, 256*256);
	TEXTURE_greyscale_to_RGBA(textures_buffer0_temp, textures_buffer0, 256*256);

	bigtexture_buffer=(unsigned char*) malloc(1024*1024*4);
	memset(bigtexture_buffer, 0, 1024*1024*4);
	textures_buffer1=(unsigned char*) malloc(256*256*4);

}

	
void Compute_Texture1()			//le masque pour la matrice
{


	for (int j=0;j<256*256*4;j+=4)
	{	

	*((long*) & textures_buffer1[j])=0xFF000000;

	}

	for (int x=0;x<256;x++)
	{

		float height=2.0+(sin((float)x/82.0)*244.0);
		for (int y=0;y<height;y++)
		{
			int i=(x+(y<<8))<<2;

			*((long*) & textures_buffer1[i])=0x00000000;

		}
	}
}




unsigned char *fhoud0, *fhoud1,*fhoud2, *fhoud3, *fhoud4, *fhoud5, *fhoud6, *fhoud7;

GLvoid LoadGLTextures()

{

		CreateTexture();
		
        glGenTextures(1, &textures[0]);						//textures[0] = brush feuille
        glBindTexture(GL_TEXTURE_2D, textures[0]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, textures_buffer0);
		
		
		
		Compute_Texture1();									//calcule le masque
        glGenTextures(1, &textures[1]);						//textures[1] = masque pour matrice
        glBindTexture(GL_TEXTURE_2D, textures[1]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 4, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, textures_buffer1);


		
		
		textures_buffer2=(unsigned char*) malloc(256*256*4);
		TEXTURE_halo(textures_buffer2);
        glGenTextures(1, &textures[2]);						//textures[2] = halo
        glBindTexture(GL_TEXTURE_2D, textures[2]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, textures_buffer2);

		

		glGenTextures(1, &texturebuffer);					//texturebuffer = 1024x1024
        glBindTexture(GL_TEXTURE_2D, texturebuffer);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 1024, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, bigtexture_buffer);


        glGenTextures(1, &spritebubble);						//spritebubble = bubble :)
        glBindTexture(GL_TEXTURE_2D, spritebubble);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 1, 8, 8, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &bubble);

		  glGenTextures(1, &spriteleaf);						//spritebubble = bubble :)
        glBindTexture(GL_TEXTURE_2D, spriteleaf);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 1, 64, 64, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &feuille);


	
		unsigned char *d1, *d2, *d3, *d4;

		d1=(unsigned char*) malloc(256*256*4);
		d2=(unsigned char*) malloc(256*256*4);
		d3=(unsigned char*) malloc(256*256*4);
		d4=(unsigned char*) malloc(256*256*4);

		PROCESS_NML_FILE((unsigned char*) txtnml1, d1);
		PROCESS_NML_FILE((unsigned char*) txtnml2, d2);
		PROCESS_NML_FILE((unsigned char*) txtnml3, d3);
		PROCESS_NML_FILE((unsigned char*) txtnml4, d4);

		for (int i=0;i<256*256;i++)
		{
		unsigned char u=d3[i*3];
		d3[i*3]=d3[2+i*3];
		d3[2+i*3]=u;
		}

		glGenTextures(1, &temptexture);				
        glBindTexture(GL_TEXTURE_2D, temptexture);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, d1);

		glGenTextures(1, &textures[3]);						
        glBindTexture(GL_TEXTURE_2D, textures[3]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, d2);

		glGenTextures(1, &textures[4]);						
        glBindTexture(GL_TEXTURE_2D, textures[4]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, d3);

	
		glGenTextures(1, &textures[5]);						
        glBindTexture(GL_TEXTURE_2D, textures[5]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, d4);

		

	#define HOUDINI_SIZE 64

		fhoud0=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);
		fhoud1=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);
		fhoud2=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);
		fhoud3=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);
		fhoud4=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);
		fhoud5=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);
		fhoud6=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);
		fhoud7=(unsigned char*) malloc(HOUDINI_SIZE*HOUDINI_SIZE*4);


		load_kwi ((unsigned char*) &kwihoud0,fhoud0);
		load_kwi ((unsigned char*) &kwihoud1,fhoud1);
		load_kwi ((unsigned char*) &kwihoud2,fhoud2);	
		load_kwi ((unsigned char*) &kwihoud3,fhoud3);	
		load_kwi ((unsigned char*) &kwihoud4,fhoud4);
		load_kwi ((unsigned char*) &kwihoud5,fhoud5);
		load_kwi ((unsigned char*) &kwihoud6,fhoud6);		
		load_kwi ((unsigned char*) &kwihoud7,fhoud7);


		glGenTextures(1, &houdini_frames[0]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[0]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud0);

		glGenTextures(1, &houdini_frames[1]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[1]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud1);

				glGenTextures(1, &houdini_frames[2]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[2]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud2);

				glGenTextures(1, &houdini_frames[3]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[3]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud3);

				glGenTextures(1, &houdini_frames[4]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[4]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud4);

				glGenTextures(1, &houdini_frames[5]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[5]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud5);

				glGenTextures(1, &houdini_frames[6]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[6]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud6);

		glGenTextures(1, &houdini_frames[7]);						
        glBindTexture(GL_TEXTURE_2D, houdini_frames[7]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, fhoud7);

		boule_decompressed_kwi=(unsigned char*) malloc(128*128*4);
		boule_buffer2=(unsigned char*) malloc(128*128*4);
		load_kwi ((unsigned char*) &boule_kwi,boule_decompressed_kwi);
		masque_buffer=(unsigned char*) malloc(128*128*4);

		memcpy(boule_buffer2, boule_decompressed_kwi, 128*128*4);

		for ( i=0;i<128*128;i++)
		{
		boule_decompressed_kwi[(i<<2)+3]=0xff-boule_mask[i];
			if (bmsk2[i]==0xFF)
			{ boule_buffer2[(i<<2)+3]=0xff; }
			else
			{
			boule_buffer2[(i<<2)+3]=0x0;
			}

			if (bmsk2[i]==0)
			{
			*((long*) &masque_buffer[i<<2])=0xff44392C;
			}
			else
			{
			*((long*) &masque_buffer[i<<2])=0x0044392C;
			}

		}

		


		glGenTextures(1, &textureboule);						
        glBindTexture(GL_TEXTURE_2D, textureboule);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 4, 128,128, 0, GL_RGBA, GL_UNSIGNED_BYTE, boule_decompressed_kwi);

		glGenTextures(1, &textureboule2);						
        glBindTexture(GL_TEXTURE_2D, textureboule2);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 4, 128,128, 0, GL_RGBA, GL_UNSIGNED_BYTE, boule_buffer2);


		glGenTextures(1, &masque2);						
        glBindTexture(GL_TEXTURE_2D, masque2);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, 4, 128,128, 0, GL_RGBA, GL_UNSIGNED_BYTE, masque_buffer);

		GENERATE_fonts();
};



GLvoid InitGL(GLsizei Width, GLsizei Height)	
{	

	
	//debugfile=fopen("debugmode.txt", "wb");			//DBG FILE !!!

	LoadGLTextures();                               // Load The Texture(s) ( NEW )
    glEnable(GL_TEXTURE_2D);  

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	
	glClearDepth(1.0);		
	glDepthFunc(GL_LESS);	
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);	
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();	
	gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,13000.0f);	
	glMatrixMode(GL_MODELVIEW);	

}

GLvoid ReSizeGLScene(GLsizei Width, GLsizei Height)
{
	if (Height==0) Height=1;
	glViewport(0, 0, Width, Height);	
	glMatrixMode(GL_PROJECTION);	
	glLoadIdentity();		
	gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,13000.0f);	
	glMatrixMode(GL_MODELVIEW);	
}





/* WINDOWS INITIALIZATION **************************************************************************************/



void WinMainCRTStartup(void)
{
	WinMain(GetModuleHandle(NULL),NULL,NULL,SW_SHOW);
 	ExitProcess(0);
}


LRESULT CALLBACK WndProc(	HWND	hWnd,
				UINT	message,
				WPARAM	wParam,
				LPARAM	lParam)
{
	RECT	Screen;							// Used Later On To Get The Size Of The Window
	GLuint	PixelFormat;
	static	PIXELFORMATDESCRIPTOR pfd=
	{
		sizeof(PIXELFORMATDESCRIPTOR),		// Size Of This Pixel Format Descriptor
		1,									// Version Number (?)
		PFD_DRAW_TO_WINDOW |				// Format Must Support Window
		PFD_SUPPORT_OPENGL |				// Format Must Support OpenGL
		PFD_DOUBLEBUFFER,					// Must Support Double Buffering
		PFD_TYPE_RGBA,						// Request An RGBA Format
		32,							//  Color Depth
		0, 0, 0, 0, 0, 0,					// Color Bits Ignored (?)
		0,									// No Alpha Buffer
		0,									// Shift Bit Ignored (?)
		0,									// No Accumulation Buffer - it sucks anyway
		0, 0, 0, 0,							// Accumulation Bits Ignored (?)
		ZBITDEPTH,							// Z-Buffer (Depth Buffer)  
		1,							//Stencil Buffer
		4,									// Auxiliary Buffer (?)
		PFD_MAIN_PLANE,						// Main Drawing Layer
		0,									// Reserved (?)
		0, 0, 0								// Layer Masks Ignored (?)
	};

	switch (message)					
	{
		case WM_CREATE:
			hDC = GetDC(hWnd);				
			PixelFormat = ChoosePixelFormat(hDC, &pfd);	 

			if (!PixelFormat)
			{
				#ifdef ERRORMODE
					MessageBox(0,"","1",MB_OK|MB_ICONERROR);
				#endif

				PostQuitMessage(0);			
				break;					
			}

			if(!SetPixelFormat(hDC,PixelFormat,&pfd))
			{
				#ifdef ERRORMODE
					MessageBox(0,"","2",MB_OK|MB_ICONERROR);
				#endif

				PostQuitMessage(0);
				break;
			}

			hRC = wglCreateContext(hDC);
			if(!hRC)
			{
				#ifdef ERRORMODE
					MessageBox(0,"","3",MB_OK|MB_ICONERROR);
				#endif

				PostQuitMessage(0);
				break;
			}

			if(!wglMakeCurrent(hDC, hRC))
			{
				#ifdef ERRORMODE
					MessageBox(0,"","4",MB_OK|MB_ICONERROR);
				#endif

				PostQuitMessage(0);
				break;
			}

			GetClientRect(hWnd, &Screen);
			InitGL(Screen.right, Screen.bottom);
			break;

		case WM_DESTROY:
			ChangeDisplaySettings(NULL, 0);
			wglMakeCurrent(NULL,NULL);
			wglDeleteContext(hRC);
			ReleaseDC(hWnd,hDC);
			PostQuitMessage(0);
			exit(0);
			break;

		case WM_CLOSE:


			xmpStop();
			ds_close();


			ChangeDisplaySettings(NULL, 0);
			wglMakeCurrent(NULL,NULL);
			wglDeleteContext(hRC);
			ReleaseDC(hWnd,hDC);
			PostQuitMessage(0);
			exit(0);
			
			break;

		case WM_KEYDOWN:


			//	if (wParam==37) { ADDING_TIMING-=500; }
			//	if (wParam==39) { ADDING_TIMING+=500; }
			//	if (wParam==38) { ADDING_TIMING+=20; }
				//if (wParam==40) { ADDING_TIMING-=20; }

				if (wParam == 27)
				{
					SendMessage(hWnd,WM_CLOSE,0,0);
				}

			break;



		case WM_SIZE:
			ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));
			break;

		default:
			return (DefWindowProc(hWnd, message, wParam, lParam));
	}
return (0);
}


unsigned char STATE_INTRO=0;



int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int cmdshow)
{

	MSG			msg;		
	WNDCLASS	wc;		
	HWND		hWnd;	

	wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	wc.lpfnWndProc		= (WNDPROC) WndProc;
	wc.cbClsExtra		= 0;
	wc.cbWndExtra		= 0;
	wc.hInstance		= inst;
	wc.hIcon			= NULL;
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground	= NULL;
	wc.lpszMenuName		= NULL;
	wc.lpszClassName	= "IAM";



	if(!RegisterClass(&wc))
	{
		#ifdef ERRORMODE
			MessageBox(0,"","5",MB_OK|MB_ICONERROR);
		#endif ERRORMODE

		return FALSE;
	}

	hWnd = CreateWindow( "IAM", "", WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
					     0, 0, OGL_SZX, OGL_SZY, NULL,NULL,inst,NULL);

	
	if(!hWnd)
	{
		#ifdef ERRORMODE
			MessageBox(0,"","6",MB_OK|MB_ICONERROR);
		#endif

		return FALSE;
	}

		

	DEVMODE dmScreenSettings ;
	dmScreenSettings.dmSize				= sizeof(DEVMODE);
	dmScreenSettings.dmPelsWidth        = OGL_SZX;								// Width
	dmScreenSettings.dmPelsHeight       = OGL_SZY;								// Height
	dmScreenSettings.dmFields           = DM_PELSWIDTH | DM_PELSHEIGHT;		// Color Depth

	
		#ifdef FULLSCREEN
				ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);	
		#endif
				
	SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,FALSE,0,0);	/* desactivate screensaver */
				

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
	SetFocus(hWnd);
		
	ShowCursor(0);
	wglMakeCurrent(hDC,hRC);

	H3D_precalc();
	SPHERE3D_setup();

	genox_mxm=(char*) malloc(genox_length*2);
	memcpy(genox_mxm, (char*) &genox,  genox_length);

	DECOMPRESS_samples();
	COMPUTE_sublandscape();

	ds_init(GetForegroundWindow(),genox_mxm);
	xmpPlay(STARTING_ORDER);

	TIMER_init_timer();			//INITIALISATION



	while (1)
	{
		// Process All Messages
		while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if (GetMessage(&msg, NULL, 0, 0))
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
			else
			{
				return TRUE;
			}
		}

		
		current_hertz=TIMER_return_timer()+ADDING_TIMING;		//a remettre quand on a finit avec le texte
		//current_hertz=ADDING_TIMING;
	
		
		if (ADDING_TIMING<0) ADDING_TIMING=0;
		if (ADDING_TIMING>29000) ADDING_TIMING=29000;		// a kicker

		SYNC1_precalcscreen(current_hertz);
		SYNC2_matrixscreen(current_hertz);
		SYNC3_manonegra( current_hertz);
		SYNC4_lotus(current_hertz);
		SYNC5_submarine(current_hertz);
		SYNC_greets(current_hertz);
		SYNC_tesla(current_hertz);
		SYNCRO_FINAL_PART(current_hertz);
	
		AFFICHE_TEXTE();
		 PUT_FUCKING_MASK(current_hertz);

		if (current_hertz>=28000) { xmpSetVolume(0);}
		if (current_hertz>=30000) { SendMessage(hWnd,WM_CLOSE,0,0);}
		
	
		SwapBuffers(hDC);
	}


SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,TRUE,0,0);	/* enable screensaver */
 return(0);
}







