//Basiques
#include <iostream>
#include <cmath>

//SDL
#include <SDL/SDL.h>
#undef main
#include <SDL/SDL_rotozoom.h>

//Projet
#include "Vecteur.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("Rotator", 0);

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

    // [2.2] Préparation surface
    SDL_Rect pos;
    SDL_Surface* epee = SDL_LoadBMP("Perso.bmp");
    SDL_SetColorKey(epee, SDL_SRCCOLORKEY, SDL_MapRGBA(epee->format, 0, 0, 0, 255));
    SDL_Surface* rotation = rotozoomSurface(epee, 0.0, 1.0, 1);
    double angle(0.0);
    Vec dir;
    Vec axeY( 0.0f, 1.0f );

    /// [3] Boucle principale
    bool done = false;
    SDL_Event event;
    while (!done)
    {
        // [3.1] Gestion évènements
        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;
            case SDL_MOUSEMOTION:
                dir.setX( event.motion.x - screen->w/2 );
                dir.setY( screen->h/2 - event.motion.y );
                angle = atan((double)(dir.getY()) / (dir.getX()+0.5f)); // Angle en radians
                angle = angle * 180.0 / M_PI; // Angle en ° + correction pointage
                if ( dir.getX() < 0 ) angle += 180;
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs
        SDL_FreeSurface(rotation);
        rotation = rotozoomSurface(epee, angle, 1.0, 0);

        pos.x = screen->w/2 - rotation->w/2;
        pos.y = screen->h/2 - rotation->h/2;

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

        SDL_BlitSurface(rotation, 0, screen, &pos);

        SDL_Flip(screen);

        // [3.4] Temps
        SDL_Delay(30);

    } //fin bcl principale

    ///Performance
    /*SDL_Surface* m_surface[1000];
    Uint32 temps(SDL_GetTicks());///Chrono START
    for (int i(0); i<1000; i++) m_surface[i] = rotozoomSurface(epee, 35.0, 1.0, 1);
    temps = SDL_GetTicks() - temps;///Chrono END
    for (int i(0); i<1000; i++) SDL_FreeSurface(m_surface[i]);
    std::cout << "Temps de calcul : " << temps << std::endl;*/

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

    return 0;
}
