#include <cstdlib>

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

#include "LaunchCircle.h"

int main ( int argc, char** argv )
{
    // [1] Initialisation des SDL
    if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    // make sure SDL cleans up before exit
    atexit(SDL_Quit);

    // create a new window
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    // [2] création des composants
    LaunchCircle cercle(200, SDL_MapRGB(screen->format,255,128,0), 360, 150);
    int verrou(0);
    bool up(false);
    bool down(false);

    // [3] program main loop
    bool done = false;
    while (!done)
    {
        // message processing loop
        SDL_Event event;
        // check for messages
        while(SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = true;
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = true;
                    if(event.key.keysym.sym == SDLK_UP)
                        up=true;
                    if(event.key.keysym.sym == SDLK_DOWN)
                        down=true;

                    break;
                }
            case SDL_KEYUP:
                {
                    if(event.key.keysym.sym == SDLK_UP)
                        up=false;
                    if(event.key.keysym.sym == SDLK_DOWN)
                        down=false;

                    break;
                }
            } // end switch
        }//End message processing
        // [4] Calculs
        if (up)
            verrou++;
        if(down)
            verrou--;

        // [5] DRAWING STARTS HERE

        // clear screen
        SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 140, 128, 128));

        cercle.afficher(320,240,verrou,screen);

        // draw bitmap

        // DRAWING ENDS HERE

        // finally, update the screen :)
        SDL_Flip(screen);
    } // end main loop

    // all is well ;)
    printf("Exited cleanly\n");
    return 0;
}
