#include "main.h"




int main ( int argc, char *argv[] )
{

    if (!initialisationSDLetTTF())
        fprintf(stderr, "Echec lors de l'initialisation.\n");
    /* make sure SDL cleans up before exit */
    atexit(SDL_Quit);
    atexit(TTF_Quit);

    if(!initialisationFenetre())
        fprintf(stderr, "Echec lors de l'initialisation.\n");

    lecteurCD();

    /* free loaded bitmap */
    SDL_FreeSurface(fond);
    SDL_FreeSurface(boutons);

    return 0;
}
int initialisationSDLetTTF(void)
{
    /* initialise SDL video et le CDROM*/
    if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_CDROM ) < 0 )
    {
        printf( "Impossible d'initialiser la SDL: %s\n", SDL_GetError() );
        return EXIT_FAILURE;
    }

    if(TTF_Init() == -1)
    {
        fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
        return EXIT_FAILURE;
    }
}

int initialisationFenetre(void)
{

    /* crée une nouvelle fenêtre */

    ecran = SDL_SetVideoMode(540, 300, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !ecran )
    {
        printf("Impossible d'afficher en 540x300 video: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    /* charge le fond */

    fond = SDL_LoadBMP("data/fond.bmp");
    if (!fond)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }
    /* clear screen */
    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));




    position.x = 0;
    position.y = 0;

    /* draw bitmap */
    SDL_BlitSurface(fond, NULL, ecran, &position);



    /* boutons piste précédente */
    boutons = IMG_Load("data/avant.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_LECTURE_X;
    position.y = POSITION_1ER_BOUTONS_LECTURE_Y;
    SDL_BlitSurface(boutons, NULL, ecran, &position);


    /* bouton lecture */
    boutons = IMG_Load("data/play.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_LECTURE_X+boutons->w;
    position.y = POSITION_1ER_BOUTONS_LECTURE_Y;
    SDL_BlitSurface(boutons, NULL, ecran, &position);


    /* bouton éjection */
    boutons = IMG_Load("data/ejection.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_LECTURE_X+boutons->w;
    position.y = POSITION_1ER_BOUTONS_LECTURE_Y-boutons->h;
    SDL_BlitSurface(boutons, NULL, ecran, &position);


    /* bouton piste suivante */
    boutons = IMG_Load("data/apres.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_LECTURE_X+(boutons->w * 2);
    position.y = POSITION_1ER_BOUTONS_LECTURE_Y;
    SDL_BlitSurface(boutons, NULL, ecran, &position);



    /* bouton Shuffle */
    boutons = IMG_Load("data/shuffle.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_MODE_X;
    position.y = POSITION_1ER_BOUTONS_MODE_Y;
    SDL_BlitSurface(boutons, NULL, ecran, &position);


    /* bouton boucle pour shuffle */
    boutons = IMG_Load("data/boucle.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_MODE_X + boutons->w;
    position.y = POSITION_1ER_BOUTONS_MODE_Y;
    SDL_BlitSurface(boutons, NULL, ecran, &position);

    /* bouton lecture normale */
    boutons = IMG_Load("data/normal.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_MODE_X;
    position.y = POSITION_1ER_BOUTONS_MODE_Y - boutons->h;
    SDL_BlitSurface(boutons, NULL, ecran, &position);

    /* bouton boucle lecture normal */
    boutons = IMG_Load("data/boucle.gif");
    if (!boutons)
    {
        printf("Impossible de charger l'image: %s\n", SDL_GetError());
        return EXIT_FAILURE;
    }

    position.x = POSITION_1ER_BOUTONS_MODE_X + boutons->w;
    position.y = POSITION_1ER_BOUTONS_MODE_Y - boutons->h;
    SDL_BlitSurface(boutons, NULL, ecran, &position);


    /* finally, update the screen :) */
    SDL_Flip(ecran);
}
