#include "eventLoop.h"

int eventLoop(SDL_Surface* ecran)
{
	int check(0);//Variable d'erreur; 0 = Ok, -1 = probleme
	// Charges des images
	SDL_Surface* mort = SDL_LoadBMP("Images/GameOver.bmp");
	SDL_SetColorKey(mort,SDL_SRCCOLORKEY,SDL_MapRGB(mort->format,0,255,255));

	Joueur joueur(ecran);
	joueur.setArme("Baroudeur");
	Bouton iconeOne(0,0,0,"Baroudeur", ecran, &joueur);//Pr�c�dent 0/404
	Bouton iconeTwo(0,76,1,"Usiane", ecran, &joueur);//Pr�c�dent 140/404
	Bouton iconeThree(0,152,2,"Triphaseur", ecran, &joueur);//Pr�c�dent 280/404
	ChampDeBataille arena(&joueur,ecran,10);

	if (!mort)//Recherche d'erreurs
	{
		printf("Unable to load bitmap: %s\n", SDL_GetError());
		check = -1;
		return check;
	}

	// Coordonn�es des surfaces
	SDL_Rect position;
	position.x = 0;
	position.y = 0;
	//Mise au point du tir sans r�p�tition due � la m�moire de l'evenement.
	bool latence(true);
	// program main loop
	bool done = false;
	SDL_ShowCursor(SDL_DISABLE);
	//G�rance du temps
	int tempsPrecedent(0);
	SDL_Event event;

	while (!done && optionsFire::continuer)
	{
		// activation du fps (fr�quence � 50Hz)
		tempsPrecedent = SDL_GetTicks();
		if (SDL_GetTicks() - tempsPrecedent < 20)
			SDL_Delay(15 + tempsPrecedent - SDL_GetTicks());

		while (SDL_GetTicks() - tempsPrecedent < 20); // Block until next frame

		// message processing loop
		while(SDL_PollEvent(&event))
		{
			// check for messages
			switch (event.type)
			{
				// exit if the window is closed
				case SDL_QUIT:
					optionsFire::continuer = false;
					done = true;
					break;
				case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						case SDLK_ESCAPE:
							done = true;
							break;
						case SDLK_b:
							joueur.setArme("Baroudeur");
							break;
						case SDLK_u:
							joueur.setArme("Usiane");
							break;
						case SDLK_t:
							joueur.setArme("Triphaseur");
							break;
					}
					break;
				case SDL_MOUSEMOTION:
					joueur.giveMousePosition(event.motion.x,event.motion.y);
					break;
				case SDL_MOUSEBUTTONDOWN:
					switch (event.button.button)
					{
						case SDL_BUTTON_LEFT:
							if(latence == true)
							{
								Cliquable::getFire(event.button.x,event.button.y,joueur.getTypeWeapon());
								joueur.tir();
							}
							latence = false;//Empeche de tirer en continu.
							break;
					}//SDL_MOUSEBUTTONDOWN
					break;
				case SDL_MOUSEBUTTONUP:
					switch (event.button.button)
					{
						case SDL_BUTTON_LEFT:
							latence = true;
							break;
					}


			}
		} //EVENT end switch event loop
		// DRAWING STARTS HERE

		// clear screen
		SDL_FillRect(ecran, 0, SDL_MapRGB(ecran->format, 0, 0, 0));
		// draw bitmap
		SDL_BlitSurface(optionsFire::level, 0, ecran, &position);

		if (joueur.estVivant())
		{
			iconeOne.afficher();
			iconeTwo.afficher();
			iconeThree.afficher();
			arena.afficher();
			joueur.afficher();
		}
		else
		{
			SDL_BlitSurface(mort, 0, ecran, &position);
			SDL_ShowCursor(SDL_ENABLE);
		}
		// DRAWING ENDS HERE
		Cliquable::upFire();

		// finally, update the screen :)
		SDL_Flip(ecran);
	} //MAIN end main loop


	// free loaded bitmap
	SDL_FreeSurface(optionsFire::level);//Fond
	optionsFire::level = 0;//On pointe le pointeur sur lui, pour la securit�

	return check;
}