#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "noidZ.h"
#include "feng.shui.h"
#include "NOiD.iDLE.h"


//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================
//==========================================================================

SDL_Surface *screen = NULL;
struct noidZ_mice mouse_info;
int end;



void init(void)
{

	int f,g;
	for (f=50;f!=150;f+=1)
	{for (g=150;g!=250;g+=1)
	{
		creer();
		tail->back->col = f;
		tail->back->row = g;
		tail->back->une = 4;
		tail->back->clk = 0;
		tail->back->pix = 0x00000000;
		join_the_matrix(tail->back);
	}
	}

	//napolitain mode
	int m,n;
	for (m=1;m!=40;m+=1)
	{for (n=1;n!=100;n+=1)
	{	creer();
		tail->back->row = n;
		tail->back->col = m;
		tail->back->une = 3;
		tail->back->clk = 0;
		tail->back->pix = 0x0000ff00;
		join_the_matrix(tail->back);
	}}
	for (m=40;m!=80;m+=1)
	{for (n=1;n!=100;n+=1)
	{	creer();
		tail->back->row = n;
		tail->back->col = m;
		tail->back->une = 1;
		tail->back->clk = 0;
		tail->back->pix = 0x00ffcc00;
		join_the_matrix(tail->back);
	}}
	for (m=80;m!=120;m+=1)
	{for (n=1;n!=100;n+=1)
	{	creer();
		tail->back->row = n;
		tail->back->col = m;
		tail->back->une = 2;
		tail->back->clk = 0;
		tail->back->pix = 0x000000ff;
		join_the_matrix(tail->back);
	}}
	for (m=120;m!=160;m+=1)
	{for (n=1;n!=100;n+=1)
	{	creer();
		tail->back->row = n;
		tail->back->col = m;
		tail->back->une = 4;
		tail->back->clk = 0;
		tail->back->pix = 0x00cccccc;
		join_the_matrix(tail->back);
	}}
	for (m=160;m!=200;m+=1)
	{for (n=1;n!=100;n+=1)
	{	creer();
		tail->back->row = n;
		tail->back->col = m;
		tail->back->une = 5;
		tail->back->clk = 0;
		tail->back->pix = 0x00ff0000;
		join_the_matrix(tail->back);
	}}
	//napolitain mode
}

//==========================================================================
//==========================================================================

void loop(void) 
{
	ici = head;
	while (ici->next != NULL)
	{
		SDL_LockSurface(screen);
		noidZ_Giv_Pixel(screen,((((float)(ici->col))/101)-1),((((float)(ici->row))/151)-1),(Uint32)ici->pix);
		SDL_UnlockSurface(screen);
			
			element(ici);


		ici = ici->next;
	}

	if(mouse_info.b == 1)
	{
		int mousx =((int)(99*(mouse_info.x+1)));
		int mousy =((int)(149*(mouse_info.y+1)));

		int m,n;
		for (m=1;m!=30;m=m+1)
		{
			for (n=1;n!=30;n=n+1)
			{
				int killx = (mousx - 15) + n;
				int killy = (mousy - 15) + m;
				if ((matrix[killx][killy] != NULL)&&(killx<195)&&(killx>5)&&(killy<295)&&(killy>5))
				{
					detruire(matrix[killx][killy]);
					matrix[killx][killy] = NULL;
				}
			}
		}
	}
noidZ_Refresh(screen);
}

void loop2(void)
{
}

//==========================================================================
//==========================================================================

int main( int argc, char* args[] ) 
{ 
	SDL_Init( SDL_INIT_EVERYTHING );
	screen = SDL_SetVideoMode( 200, 300, 16, SDL_SWSURFACE|SDL_ANYFORMAT );
	SDL_WM_SetCaption( "NOiD Zero", NULL );

	init();

		//NOiD_iDLE_LiST * calendrier = NOiD_iDLE_createLiST();
		//NOiD_iDLE * mainloop = NOiD_iDLE_create(0.004,&loop,calendrier);
		//NOiD_iDLE * dumbloop = NOiD_iDLE_create(0.001,&loop2,calendrier);
			//NOiD_iDLE_loop(noidZ_Got_Time(),calendrier);

	noidZ_Init(screen);
		while (!end)
		{
              		SDL_Delay(40);
			end = noidZ_Loop();
			mouse_info = noidZ_Got_Mouse(screen);
			bresline(screen,mouse_info.x+0.1,mouse_info.x-0.1,mouse_info.y+0.1,mouse_info.y-0.1,white);
			bresline(screen,mouse_info.x+0.1,mouse_info.x-0.1,mouse_info.y-0.1,mouse_info.y+0.1,white);
			loop();
				 //NOiD_iDLE_loop(noidZ_Got_Time(),calendrier);
			//SDL_Delay( (int)(1000 * NOiD_iDLE_loop(noidZ_Got_Time(),calendrier)) );			
		}

	SDL_FreeSurface( screen );
	SDL_Quit(); 
	return 0; 
}
