#include <iostream>

#include "MetaDeg.h"

int main ( int argc, char** argv )
{
    /// [1] Démarrage
    // [1.1] Démarrages SDL
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
    {
        std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
        return 1;
    }

    // [1.2] Préparation de fermeture
    atexit(SDL_Quit);

    // [1.3] Para-fenêtre
    SDL_WM_SetCaption("Application SDL", 0);

    /// [2] Préparation des composants
    // [2.1] Préparation de la fenêtre
    int sc_w(640) ;
    int sc_h(480) ;
    SDL_Surface* screen = SDL_SetVideoMode(sc_w, sc_h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
        return 1;
    }

    // [2.2] Préparation de l'espace
    MetaDeg mySpace;
    if ( !mySpace.init(sc_w, sc_h) )
    {
        std::cout << "Problème initialisation du Metaspace." << std::endl ;
        return 1;
    }
    SDL_Surface* meta_img(mySpace.getSurf());

    // [2.3] Préparation des calculs
    Uint32 mx(sc_w / 2) ;
    Uint32 my(sc_w / 2) ;

    /// [3] Boucle principale
    SDL_EnableKeyRepeat(10,10);
    bool done = false;
    while (!done)
    {
        // [3.1] Gestion évènements
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                done = true;
                break;
            case SDL_MOUSEMOTION:
                mx = event.motion.x ;
                my = event.motion.y ;
                break;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    done = true;
                    break;
                case SDLK_UP:
                    /*if (seuil < 30000)
                        seuil += 1 ;
                    mySpace.setSeuil(seuil);*/
                    break;
                case SDLK_DOWN:
                    /*if (seuil > 10)
                        seuil -= 1 ;
                    mySpace.setSeuil(seuil);*/
                    break;
                default:
                    break;
                }
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs
        mySpace.clean();
        mySpace.deform(my, mx);
        mySpace.deform(180, 250);
        mySpace.draw();

        // [3.3] Dessin des composants
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));

        SDL_BlitSurface(meta_img, 0, screen, 0);

        SDL_Flip(screen);
    } //fin bcl principale

    ///[4] Destruction des composants
    SDL_FreeSurface(screen);


    std::cout << "Aucune erreur détectée." << std::endl;
    return 0;
}