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

#include <SDL/SDL_ttf.h>

int main ( int argc, char** argv )
{
    /// [1] Démarrage
    // [1.1] Démarrages SDL et TTF
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 || TTF_Init() < 0 )
    {
        std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
        std::cout << "Ou problème de TTf: " << TTF_GetError() << std::endl;
        return 1;
    }

    // [1.2] Préparation de fermeture
    atexit(SDL_Quit);
    atexit(TTF_Quit);

    // [1.3] Para-fenêtre
    SDL_WM_SetCaption("Gestion du texte avec SDL_ttf", 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 )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    // [2.2] Préparation d'image(s)
    SDL_Surface *texte(0);
    SDL_Rect position;

    // [2.3] Préparation du texte
    TTF_Font *police(0);
    police = TTF_OpenFont("Polices/droid.ttf",65);
    SDL_Color couleurNoire = {0,0,0};
    std::string chaine(" ");

    /// [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;
            case SDL_KEYUP:
                switch (event.key.keysym.sym)
                {
                    case SDLK_BACKSPACE:
                        if (chaine.size()!=1)
                            chaine.erase(chaine.size()-1,1);
                        break;
                    case SDLK_a:
                        chaine+='a';
                        break;
                    case SDLK_z:
                        chaine+='z';
                        break;
                    case SDLK_e:
                        chaine+='e';
                        break;
                    case SDLK_r:
                        chaine+='r';
                        break;
                    case SDLK_t:
                        chaine+='t';
                        break;
                    case SDLK_y:
                        chaine+='y';
                        break;
                    case SDLK_u:
                        chaine+='u';
                        break;
                    case SDLK_i:
                        chaine+='i';
                        break;
                    case SDLK_o:
                        chaine+='o';
                        break;
                    case SDLK_p:
                        chaine+='p';
                        break;
                    case SDLK_q:
                        chaine+='q';
                        break;
                    case SDLK_s:
                        chaine+='s';
                        break;
                    case SDLK_d:
                        chaine+='d';
                        break;
                    case SDLK_f:
                        chaine+='f';
                        break;
                    case SDLK_g:
                        chaine+='g';
                        break;
                    case SDLK_h:
                        chaine+='h';
                        break;
                    case SDLK_j:
                        chaine+='j';
                        break;
                    case SDLK_k:
                        chaine+='k';
                        break;
                    case SDLK_l:
                        chaine+='l';
                        break;
                    case SDLK_m:
                        chaine+='m';
                        break;
                    case SDLK_w:
                        chaine+='w';
                        break;
                    case SDLK_x:
                        chaine+='x';
                        break;
                    case SDLK_c:
                        chaine+='c';
                        break;
                    case SDLK_v:
                        chaine+='v';
                        break;
                    case SDLK_b:
                        chaine+='b';
                        break;
                    case SDLK_n:
                        chaine+='n';
                        break;
                    case SDLK_SPACE:
                        chaine+=' ';
                        break;
                }
                break;
            } // end switch event type
        } // end of message processing

        // [3.2] Calculs
        texte = TTF_RenderText_Blended(police, chaine.c_str(),couleurNoire);

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

        position.x = screen->w/2 - texte->w/2;
        position.y = screen->h/2 - texte->h/2;
        SDL_BlitSurface(texte, NULL, screen, &position);

        SDL_Flip(screen);
    } //fin bcl principale

    ///[4] Destruction des composants
    TTF_CloseFont(police);
    SDL_FreeSurface(texte);

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