//------------------------------------------------------------------------------
// Includes                          
//------------------------------------------------------------------------------

    #include <windows.h>
    #include <stdio.h>
    #include <GL/glut.h>
    #include <jpeglib.h>
    #include <jerror.h>
    
//------------------------------------------------------------------------------
// Constantes & Variables
//------------------------------------------------------------------------------    
    
    #define EXIT        {fclose(fichier); return -1;}
    #define CTOI(C)     (*(int*)&C)
    #define MAX_TEX     3
    
    char *TabTex[MAX_TEX] = { "textures/tex.bmp",
                              "textures/tex.jpg",
                              "textures/tex.tga" };
    
    int     id_maxload=MAX_TEX;    
    GLuint  TexNum[MAX_TEX];
    int     WIDTH=640;
    int     HEIGHT=480;
    
//------------------------------------------------------------------------------
// Fichiers externes
//------------------------------------------------------------------------------

    #include "loadbmp.h"
    #include "loadjpg.h"
    #include "loadtga.h"

//------------------------------------------------------------------------------
// Redimensionnement
//------------------------------------------------------------------------------

void    ReshapeGL ( int Width, int Height )
 {
    if (Height==0) Height=1; 
    glViewport ( 0, 0, Width, Height );
    glMatrixMode ( GL_PROJECTION );
    glLoadIdentity ( );
    glOrtho ( 0, WIDTH, 0, HEIGHT, 0, 0.2 );               // Fait un écran Ortho    
 }    

//------------------------------------------------------------------------------
// Initialisation
//------------------------------------------------------------------------------

void    InitGL ( )
 {
    glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );        // Black Background
    glClearDepth(1.0f);                             // Configuration de la profondeur du buffer    
    
    glDepthFunc ( GL_LEQUAL );
    glEnable ( GL_DEPTH_TEST );

    glEnable ( GL_TEXTURE_2D );
    glEnable ( GL_COLOR );                          // Colors Enabled
 }

//------------------------------------------------------------------------------
// Dessin
//------------------------------------------------------------------------------

void    DrawGL ( )
 {
    glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Efface les buffers
    
    static float load=0.0f;
    static int   id_load=0;
    int pos;
    
    // si on a pas fini tous les chargements
    if (id_load < id_maxload)
     {    
        char *tmp;
        // BMP ?     
        tmp = strstr( TabTex[id_load], ".bmp" );
        pos = tmp - TabTex[id_load] + 1;
        if( tmp != NULL ) LoadBMP ( TabTex[id_load], id_load );
        // JPG ?
        tmp = strstr( TabTex[id_load], ".jpg" );
        pos = tmp - TabTex[id_load] + 1;
        if( tmp != NULL ) LoadJPG ( TabTex[id_load], id_load );    
        // TGA ?
        tmp = strstr( TabTex[id_load], ".tga" );
        pos = tmp - TabTex[id_load] + 1;
        if( tmp != NULL ) LoadTGA ( TabTex[id_load], id_load );

        // changement du titre et du numéro de texture à charger la prochaine fois
        id_load++;
        load = 100 * id_load / (float) ( id_maxload );
        char titre[255];
        sprintf ( titre, "loading ... %2.2f", load);
        glutSetWindowTitle( titre );
     }

    glDisable ( GL_TEXTURE_2D );

    // LA BARRE DE CHARGEMENT 
    glLineWidth(10);
    glColor3f ( 0.9, load/(float)(100), 0.3 );
    glBegin(GL_LINES);
        glVertex2f (                             50, HEIGHT/2);
        glVertex2f ( (WIDTH-100)*load/(float)(100)+50, HEIGHT/2);
    glEnd();
    glColor3d ( 1, 1, 1 );
    
    glEnable ( GL_TEXTURE_2D );

    glBindTexture(GL_TEXTURE_2D, TexNum[id_load-1]);
    glBegin(GL_QUADS);
        glTexCoord2d ( 0, 0 ); glVertex2f (   0,   0);
        glTexCoord2d ( 0, 1 ); glVertex2f (   0, 200);        
        glTexCoord2d ( 1, 1 ); glVertex2f ( 200, 200);
        glTexCoord2d ( 1, 0 ); glVertex2f ( 200,   0);
    glEnd();
    
    glutSwapBuffers ( );                           // Dessine la frame à l'écran
    glutPostRedisplay ( );                         // Redémarre DrawGL
 }

//------------------------------------------------------------------------------
// Main Function
//------------------------------------------------------------------------------

int    main( int argc, char *argv[ ], char *envp[ ] )
 {    
    // Création de la fenêtre
    glutInit ( &argc, argv );
    glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
    glutInitWindowSize ( WIDTH/2, HEIGHT/2 );
    glutInitWindowPosition ( 50, 50 );
    glutCreateWindow ( "Loader" );
    InitGL ( );
    
    glutReshapeFunc ( ReshapeGL );
    glutDisplayFunc ( DrawGL );
    
    glutMainLoop ( );
    
    return 0;
 }

//------------------------------------------------------------------------------
// THE END
//------------------------------------------------------------------------------