#include <iostream>
#include <SDL/SDL.h>

#include "FeuFolet.h"

#define RAYON 160

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: Algorythme du Feu Follet", 0);

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

    // [2.2] Préparation
    SDL_Surface* fond = SDL_LoadBMP("soleil.bmp");
    SDL_Surface* follet = createFeuFolet(RAYON, SDL_MapRGB(screen->format, 0, 0, 255));
    SDL_Rect pos;
    pos.x=pos.y=0;

    short opacite (0);

    /// [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_KEYDOWN:
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = true;
                else if (event.key.keysym.sym == SDLK_UP)
                    opacite++;
                else if (event.key.keysym.sym == SDLK_DOWN)
                    opacite--;
                break;
            case SDL_MOUSEMOTION:
                pos.x = event.motion.x-RAYON;
                pos.y = event.motion.y-RAYON;
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs

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

        SDL_BlitSurface(fond, 0, screen, 0);
        SDL_BlitSurface(follet, 0, screen, &pos);

        //blitFeuFollet(screen, xFeu, yFeu, RAYON, SDL_MapRGB(screen->format,0,0,255),opacite);

        SDL_Flip(screen);
    } //fin bcl principale

    ///[4] Destruction des composants
    SDL_FreeSurface(fond);
    SDL_FreeSurface(follet);

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