#include "jeu.h"
void tracerLigne(SDL_Surface *screen, int nbJoueurs)
{
    SDL_Rect pos;
    SDL_Surface* pixel;
    pixel = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, 1, 32, 0, 0, 0, 0);
    SDL_FillRect(pixel, 0, SDL_MapRGB(pixel->format, 255, 255, 255));
    for(int y = -RAYON_FENETRE; y<RAYON_FENETRE; y++)
    {
        for(int x = -RAYON_FENETRE; x<RAYON_FENETRE; x++)
        {
            pos.x = x+RAYON_FENETRE;
            pos.y = y+RAYON_FENETRE;
            if(nbJoueurs == 2)
            {
                if(x*x+y*y<RAYON_TERRAIN*RAYON_TERRAIN && y>-LARGEUR_LIGNE && y<LARGEUR_LIGNE)
                    SDL_BlitSurface(pixel, 0, screen, &pos);
            }
            else if(nbJoueurs == 3)
            {
                if(x*x+y*y<RAYON_TERRAIN*RAYON_TERRAIN && ((y<0 && x>-LARGEUR_LIGNE && x<LARGEUR_LIGNE) || (y>-COTE/400 && abs(1.75*y)-abs(x)>-LARGEUR_LIGNE*2 && abs(1.75*y)-abs(x)<LARGEUR_LIGNE*2)))
                    SDL_BlitSurface(pixel, 0, screen, &pos);
            }
            else if(nbJoueurs == 4)
            {
                if(x*x+y*y<RAYON_TERRAIN*RAYON_TERRAIN && abs(y)-abs(x)>-LARGEUR_LIGNE && abs(y)-abs(x)<LARGEUR_LIGNE)
                    SDL_BlitSurface(pixel, 0, screen, &pos);
            }
        }
    }
}
int jeu(SDL_Surface *screen,int const nbJoueurs)
{
    // [1] Pr�paration des composants
    //[1.1] Objets pratiques
    Uint32 tempsPrecedent(0);
    bool continuer(true);

    //[1.2] La surface point
    SDL_Surface* arene(0);
    arene = SDL_CreateRGBSurface(SDL_HWSURFACE, COTE, COTE, 32, 0, 0, 0, 0);
    Point point(COTE/2,SDL_MapRGB(screen->format,0,0,255));
    Point point2(COTE/2-COTE/36,SDL_MapRGB(screen->format,30,200,10));
    point.afficherPoint(arene, COTE/2, COTE/2);
    point2.afficherPoint(arene, COTE/2, COTE/2);
    tracerLigne(arene, nbJoueurs);

    //[1.2] Raquettes
    Raquette *tabRaquette[nbJoueurs];
    for(int i(0); i<nbJoueurs; i++)
        tabRaquette[i] = new Raquette(i, nbJoueurs, SDL_MapRGB(screen->format, 255, 0, 0));

    //[1.3] La balle
    Balle baballe(nbJoueurs);
    Balle baballe2(nbJoueurs);
    for (int i(0); i<nbJoueurs; i++)
        baballe.ajouterRaquette(i,tabRaquette[i]);
    if(nbJoueurs == 4)
        for (int i(0); i<nbJoueurs; i++)
            baballe2.ajouterRaquette(i,tabRaquette[i]);

    // [2] Program main loop
    Input input;
    bool pause = true, p = true;
    while (continuer && !input.getTouches(SDLK_ESCAPE))
    {
        //baballe.camenbertPerdant(screen);
        // [2.1] Gestion des evenement
        input.updateEvenement();
        continuer = input.continuer();
        if(nbJoueurs == 2)
        {
            if (input.getTouches(SDLK_v))
                tabRaquette[0]->deplacer(false);
            if (input.getTouches(SDLK_x))
                tabRaquette[0]->deplacer(true);
            if (input.getTouches(SDLK_RIGHT))
                tabRaquette[1]->deplacer(true);
            if (input.getTouches(SDLK_LEFT))
                tabRaquette[1]->deplacer(false);
        }
        if(nbJoueurs == 3)
        {
            if (input.getTouches(SDLK_v))
                tabRaquette[0]->deplacer(false);
            if (input.getTouches(SDLK_x))
                tabRaquette[0]->deplacer(true);
            if (input.getTouches(SDLK_q))
                tabRaquette[1]->deplacer(true);
            if (input.getTouches(SDLK_a))
                tabRaquette[1]->deplacer(false);
            if (input.getTouches(SDLK_KP3))
                tabRaquette[2]->deplacer(true);
            if (input.getTouches(SDLK_KP6))
                tabRaquette[2]->deplacer(false);
        }
        if(nbJoueurs == 4)
        {
            if (input.getTouches(SDLK_v))
                tabRaquette[0]->deplacer(false);
            if (input.getTouches(SDLK_x))
                tabRaquette[0]->deplacer(true);
            if (input.getTouches(SDLK_q))
                tabRaquette[1]->deplacer(true);
            if (input.getTouches(SDLK_a))
                tabRaquette[1]->deplacer(false);
            if (input.getTouches(SDLK_RIGHT))
                tabRaquette[2]->deplacer(true);
            if (input.getTouches(SDLK_LEFT))
                tabRaquette[2]->deplacer(false);
            if (input.getTouches(SDLK_KP3))
                tabRaquette[3]->deplacer(true);
            if (input.getTouches(SDLK_KP6))
                tabRaquette[3]->deplacer(false);
        }

        if(input.getTouches(SDLK_SPACE) && p)
        {
            pause = true;
            p = false;
        }
        else if(!input.getTouches(SDLK_SPACE))
            p = true;

        // [3] Calcul
        baballe.bouger(screen);
        //if(nbJoueurs == 4)
            //baballe2.bouger(screen);

        // [4] Dessin
        SDL_BlitSurface(arene, 0, screen, 0);
        for(int i(0); i<nbJoueurs; i++)
            tabRaquette[i]->afficher(screen);
        baballe.afficher(screen);
        if(nbJoueurs == 4)
            baballe2.afficher(screen);

        SDL_Flip(screen);

        //[5] Gestion du temps
        while (15>SDL_GetTicks()-tempsPrecedent || pause)
        {
            //�a bloque pendant 30 ms moins le temps de calcul
            if(pause)
            {
                input.updateEvenement();
                if(input.getTouches(SDLK_SPACE) && p)
                {
                    pause = false;
                    p = false;
                }
                else if(!input.getTouches(SDLK_SPACE))
                    p = true;
                if(!input.continuer() || input.getTouches(SDLK_ESCAPE))
                {
                    continuer = false;
                    break;
                }
            }
        }
        tempsPrecedent=SDL_GetTicks();

    } ///Boucle �venementielle

    // [6] Nettoyage
    for(int i(0); i<nbJoueurs; i++)
        delete tabRaquette[i];

    // [END] Renvoi
    return 0;
}