/*
 *
 * Copyright (C) 2008 Fontaine Clément
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * this program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "main.h"

int main(void)
{
        SDL_Event event;
        SDL_Surface *ecran = NULL;
        SDL_Color couleur = {255, 255, 255};
        bool continuer = TRUE;
        hauteur graphe, altitude;
        coordonnees depart = {10, 10}, arrivee = {500, 500};
        unsigned int ancien, courant, stop, ecart, debut, temporisation = 10;
        int x = 0, y = 0, z = 260;
        
        /* initialisations des tableaux dynamique */
        initialiserGraphe(&altitude, &graphe);
        
        /* recherche du chemin */
        chemin(altitude, &graphe, depart, arrivee);
        
        printf("\n--------- Visualisation 3D du resultat dans 5 secondes. ---------\n");
        
        printf("\nEchap pour quiter.\n");
        printf("couleur bleu = surface sans obstacle.\n");
        printf("couleur rouge = surface avec obstacle.\n");
        printf("couleur vert = chemin le plus rapide.\n");
        printf("couleur rose = relief.\n");
        printf("touche `t' = tourner.\n");
        printf("touche `haut' et `bas' = aller en haut ou en bas.\n");
        sleep(5);
        
        /* initialisations de la fenêtre et du temps */
        fenetrePleineEcran(&ecran);
        fenetrePerspective(ecran, 70, 1, 1000);
        tempsInitialiser(&ancien);
        
        while(continuer)
        {
                tempsInitialiser(&debut);
                
                while(SDL_PollEvent(&event) && continuer)
                {
                        switch(event.type)
                        {
                                case SDL_KEYDOWN: /* le clavier */
                                        switch(event.key.keysym.sym)
                                        {
                                                case SDLK_ESCAPE: /* on quitte le programme */
                                                        continuer = FALSE;
                                                        break;
                                        }
                                        break;
                                
                                case SDL_KEYUP:
                                        cameraClavier(altitude, event.key, &x, &y, &z);
                                        break;
                        }
                }
                tempsActualiser(&ecart, &courant, &ancien); /* on actualise le temps */
                cameraRegarder(altitude, x, y, z); /* gestion de la camere */
                afficherGraphe(altitude, graphe); /* affichage des données */
                fenetreAfficher(); /* on affiche le dessin */
                tempsPause(&ecart, debut, temporisation); /* on fait une pause si sa va trop vite */ 
        }
        printf("\n--------- Fin du programme ---------\n\n");
        printf("A-Star Copyright (C) 2008 Fontaine Clément.\n");
        printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
        printf("This is free software, and you are welcome to redistribute it\n");
        printf("under certain conditions.\n\n");
        printf("for details see the file LICENCE or see <http://www.gnu.org/licenses/>.\n\n");
        
        /* on quite tout */
        fenetreDetruire(ecran);
        
        return EXIT_SUCCESS;
}
