#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL/SDL.h>
#include "EzE.SDL.h"
#include "noid.h"

SDL_Surface * screen;
SDL_Surface * mouche;

struct {
	float angle;
	float dist;
	float x,y;
	float h,w;
	float x0,y0;
	float x1,y1;
	float x2,y2;
	float x3,y3;
	float x4,y4;
} player;
//================================================================================================
//================================================================================================
//================================================================================================
//================================================================================================

void dum_func(void)
{
	printf("yo\n");
}

int mice_stay, mice_pause, mice_opt;
void mice_func (void)
/* how to make a game that work only with one mouse button */
/* just a basic timing algo, if you release mouse button for */
/* more than 100ms , the game will pause , then its still possible */
/* to switch something by relasing mouse then reclick it before 100ms */
{
	if (EzE_mice.b == 0) {
		if (mice_stay == 1) {
			SDL_ShowCursor(SDL_ENABLE);
			mice_pause = 0;
		} else {
			mice_stay = 1;
		}
	} else {
		if (mice_stay = 1) {
			mice_stay = 0;
			SDL_ShowCursor(SDL_DISABLE);
			mice_pause = 1;
			if (mice_opt == 0)
			{	mice_opt = 1;	
			} else {
				mice_opt = 0;
			}
			
		}
	}
}

void menu_func (void)
{
		EzE_Clear(screen);

}

float X,Y,vX,vY;
void other_func (void)
{
	if (vX == 0)	{	vX = 0.01;	}
	if (vY == 0)	{	vY = 0.015;	}
	if (X >= 0.9)	{	X = 0.9;	vX = -0.015;	}
	if (X <= -0.9)	{	X = -0.9;	vX = 0.01;	}
	if (Y >= 0.9)	{	Y = 0.9;	vY = -0.01;	}
	if (Y <= -0.9)	{	Y = -0.9;	vY = 0.015;	}
	X += vX;
	Y += vY;
}

void video_func (void)
{
		EzE_Clear(screen);

		SDL_LockSurface(screen);
		EzE_bresline(screen,EzE_mice.x,  0.90,EzE_mice.y, 0,blue);
		EzE_bresline(screen,EzE_mice.x, -0.90,EzE_mice.y, 0,blue);
		EzE_bresline(screen,EzE_mice.x, 0,EzE_mice.y,  0.90,blue);
		EzE_bresline(screen,EzE_mice.x, 0,EzE_mice.y, -0.90,blue);
		SDL_UnlockSurface(screen);

		EzE_Stamp(screen,X,Y,mouche);
}

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

NOiD_memory * ram;
NOiD_node * nugget;

NOiD_node * src;
NOiD_node * une;
NOiD_node * deux;

NOiD_node * game;
NOiD_node * AAA;
NOiD_node * BBB;
NOiD_node * CCC;

void init_func(void)
{

	ram = NOiD_seg(malloc(1000),1000);

	src = NOiD_schedule(0,&dum_func,10,NOiD_alloc(ram,sizeof(NOiD_agenda)+sizeof(NOiD_node)));
	une = NOiD_schedule(src,&mice_func,100,NOiD_alloc(ram,sizeof(NOiD_agenda)+sizeof(NOiD_node)));
	deux = NOiD_schedule(src,&menu_func,100,NOiD_alloc(ram,sizeof(NOiD_agenda)+sizeof(NOiD_node)));

	game = NOiD_schedule(0,&dum_func,10,NOiD_alloc(ram,sizeof(NOiD_agenda)+sizeof(NOiD_node)));
	AAA = NOiD_schedule(game,&mice_func,100,NOiD_alloc(ram,sizeof(NOiD_agenda)+sizeof(NOiD_node)));
	BBB = NOiD_schedule(game,&video_func,33,NOiD_alloc(ram,sizeof(NOiD_agenda)+sizeof(NOiD_node)));
	CCC = NOiD_schedule(game,&other_func,10,NOiD_alloc(ram,sizeof(NOiD_agenda)+sizeof(NOiD_node)));

	mouche = EzE_Format( SDL_LoadBMP("mouche.bmp") );

}

void end_func(void)
{
	SDL_FreeSurface( mouche );

	NOiD_free(ram,NOiD_cancel(src,une));
	NOiD_free(ram,NOiD_cancel(src,deux));
	NOiD_free(ram,NOiD_cancel(src,src));

	NOiD_free(ram,NOiD_cancel(game,AAA));
	NOiD_free(ram,NOiD_cancel(game,BBB));
	NOiD_free(ram,NOiD_cancel(game,CCC));
	NOiD_free(ram,NOiD_cancel(game,game));

	free(ram);
}

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

int main( int argc, char* args[] ) 
{ 
	SDL_Init( SDL_INIT_EVERYTHING );
	screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE|SDL_ANYFORMAT );
	SDL_WM_SetCaption( "NOiD mush", NULL );

	EzE_Init(screen);
	init_func();

	SDL_WM_SetIcon(mouche, NULL);

	while (EzE_Loop(screen) == 0)
	{
		int idle;
		if (mice_pause != 0)
		{
			idle = NOiD_do(game,EzE_state.tick);
		}
		else
		{		
			idle = NOiD_do(src,EzE_state.tick);
		}
		EzE_Sleep( (((float)idle)/1000) );
	}

	end_func();

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