#include "cSerpent.hpp"
#include "cJeu.hpp"
#include "cPomme.hpp"
#include "constantes.hpp"
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>


#include <cstdlib>


#include "cPoint.hpp"



cJeu::cJeu() {
	this->enPause = false;
	this->score = 0;
}



bool cJeu::get_enPause() {
	return this->enPause;
}

void cJeu::set_enPause(bool enPause) {
	this->enPause = enPause;
}



void cJeu::effaceEcran() {

	// Coloration des surfaces
   SDL_FillRect(this->pScreen, NULL, SDL_MapRGB((this->pScreen)->format, 0, 0, 0));

   // Blit de la surface à l'écran et affichage
   SDL_BlitSurface(this->pScreen, NULL, this->pScreen, NULL);
}



int cJeu::gameInit() {

		// Initialisation de SDL
	if (SDL_Init (SDL_INIT_VIDEO) < 0) {
		fprintf (stderr, "Erreur d'initialisation de SDL: %s\n", SDL_GetError ());
		return 1;
	}



	atexit(SDL_Quit); // Pour quitter proprement



	// Initialisation de SDL_Video
	this->pScreen = SDL_SetVideoMode ((XMAX*BLOC), (YMAX*BLOC), 16, SDL_SWSURFACE);
	if (this->pScreen == NULL) {
		fprintf (stderr, "Erreur d'init du mode video: %s\n", SDL_GetError ());
		return 2;
	}


	// Titre de fenêtre
	SDL_WM_SetCaption ("Pad'Snake alpha", NULL);



	// Initialisation de SDL TTF
	if (TTF_Init() < 0) {
		fprintf (stderr, "Erreur d'init de SDL_TTF\n");
		return 3;
	}


	// Affiche le serpent à son emplacement initial
	(this->leSerpent).affiche(this->pScreen);

	
	SDL_ShowCursor(0);
}

	
	


int cJeu::gameRun() {

	bool continuer = true;
	SDL_Event event;
	Uint32 nextStep = SDL_GetTicks() + INTERVALLE;


	while (continuer == true) {

		while (SDL_PollEvent(&event)) {

			switch(event.type) {

				case SDL_QUIT 	:
					return 1; break;

				case SDL_KEYDOWN:
					switch(event.key.keysym.sym) {
						case SDLK_UP	: if (this->enPause == false) { (this->leSerpent).set_direction(NORD);} ; break;
						case SDLK_RIGHT	: if (this->enPause == false) { (this->leSerpent).set_direction(EST);} ; break;
						case SDLK_DOWN	: if (this->enPause == false) { (this->leSerpent).set_direction(SUD);} ; break;
						case SDLK_LEFT 	: if (this->enPause == false) { (this->leSerpent).set_direction(OUEST);} ; break;
						case SDLK_SPACE: this->enPause = !this->enPause; break;
						case SDLK_ESCAPE: exit(0); break;
					}
					break;

			}

		}	// while (sdl_pollevents)



		if (this->enPause == false && SDL_GetTicks() >= nextStep) {

			continuer = (this->leSerpent).deplace();
		
			if ((this->leSerpent).mangePomme() == true) {
				this->score += 10;
			}

			this->effaceEcran();
			(this->leSerpent).affiche(this->pScreen);			

			nextStep = SDL_GetTicks() + INTERVALLE;
		}


	}	// while (continue)



	this->gameOver();

}




void cJeu::gameOver() {


	this->effaceEcran();

	// Récupère le score dans un char *
	char* chaine;
	sprintf(chaine, "Score : %i", this->score);


	TTF_Font* police = TTF_OpenFont("arial.ttf", 16);
	SDL_Color couleur = { 10, 255, 255 };
	SDL_Surface* pPolice = TTF_RenderText_Solid(police, chaine, couleur );

	// Définition de la zone d'affichage du texte
	SDL_Rect posiPolice;
	posiPolice.x = (this->pScreen)->h / 2 - BLOC;
	posiPolice.y = (this->pScreen)->h / 2 - BLOC;
	posiPolice.w = BLOC;
	posiPolice.h = BLOC;

	// Affichage de la police sur le fond
	SDL_BlitSurface(pPolice, NULL, this->pScreen, &posiPolice);
	SDL_Flip(pScreen);

	// Libère la surface et la fonte utilisées
	SDL_FreeSurface(pPolice);
	TTF_CloseFont(police);


	SDL_Delay(1000);

}





cJeu::~cJeu() {

	SDL_FreeSurface(this->pScreen);
	TTF_Quit();

}
