#include <iostream>
#include <SDL/SDL.h>
#include "retourner.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
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                           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* originale = SDL_LoadBMP("cb.bmp");
    SDL_Surface* copie = retournement(originale,78);//Appel de la fonction
    SDL_Rect posOrig={20,20,0,0};
    //SDL_Rect posCop={620-copie->w,20,0,0};
    SDL_Surface* poubelle(0);

    /// [3] Boucle principale
    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;
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs
        poubelle = originale;
        originale = retournement(originale,1);
        SDL_FreeSurface(poubelle);
        posOrig.x=320-originale->w/2;
        posOrig.y=240-originale->h/2;

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

        SDL_BlitSurface(originale,0,screen,&posOrig);
        //SDL_BlitSurface(copie,0,screen,&posCop);

        SDL_Flip(screen);

        // [3.4] 5 fois par seconde
        SDL_Delay(200);

    } //fin bcl principale

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

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