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

//SDL
#include <SDL/SDL.h>
#undef main
//#include <SDL/SDL_rotozoom.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("Sword_Finder", 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 << "Bug à l'initialisation: " << SDL_GetError() << std::endl;
        return 1;
    }

    // [2.2] Préparation variables
    int range(100) ;
    SDL_Rect mouse({0, 0, 2*range, 2*range});
    SDL_Rect pos({0,0,0,0});
    bool grip(false);

    // [2.3] Préparation surface
    SDL_Surface* text = 0x0;
    text = SDL_LoadBMP("pierres.bmp");
    if ( !text )
        std::cout << "Pb avec la texture." << std::endl;

    SDL_Surface* obj = 0x0;
    obj = SDL_LoadBMP("epee.bmp");
    SDL_SetColorKey(obj, SDL_SRCCOLORKEY, 0xff00ff);

    SDL_Surface* full = 0x0;
    full = SDL_CreateRGBSurface(SDL_HWSURFACE, 1280, 720, 32, 0, 0, 0, 0) ;

    SDL_Surface* calc = 0x0;
    calc = SDL_CreateRGBSurface(SDL_HWSURFACE, 1280, 720, 32, 0, 0, 0, 0) ;

    // [2.4] Remplissage
    for ( int x(0); x < 1280; x += text->w )
        for ( int y(0); y < 720; y += text->h )
        {
            pos.x = x ;
            pos.y = y ;
            SDL_BlitSurface(text, 0x0, full, &pos) ;
        }
    pos.x = 200;
    pos.y = 400;

    /// [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;
                if (event.key.keysym.sym == SDLK_UP)
                    range += 5;
                if (event.key.keysym.sym == SDLK_DOWN)
                    range -= 5;
                break;
            case SDL_MOUSEMOTION:
                mouse.x = event.motion.x - range ;
                mouse.y = event.motion.y - range ;
                mouse.w = 2*range ;
                mouse.h = 2*range ;
                if (grip) {
                    pos.x += event.motion.xrel;
                    pos.y += event.motion.yrel;
                }
                break;
            case SDL_MOUSEBUTTONDOWN:
                grip = true;
                break;
            case SDL_MOUSEBUTTONUP:
                grip = false;
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs

        // [3.3] Dessin des composants
        SDL_FillRect(screen, 0, 0x000000);

        SDL_BlitSurface(full, 0x0, calc, 0x0);
        SDL_BlitSurface(obj, 0x0, calc, &pos);
        SDL_BlitSurface(calc, &mouse, screen, &mouse);

        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(text);
    SDL_FreeSurface(full);

    return 0;
}
