#include "main.h"

#define kInitWidth 640
#define kInitHeight 480
// a global variable whee
cGame game_; 

int main(int argc, char **argv)
{
    // initGLialize glut 
    glutInit(&argc, argv);
    
    // set window size
    glutInitWindowSize(kInitWidth, kInitHeight);
    
    // establish glut display parameters
    glutInitDisplayMode(GLUT_DOUBLE   | GLUT_RGB | GLUT_DEPTH);
    
    // create window
    glutCreateWindow("Triangle World");
    
    // register callback functions
    glutDisplayFunc(display);
    glutReshapeFunc(reshapeWindow); 
    glutIdleFunc(idle);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouseClick);
    glutMotionFunc(mouseMove);
    
    // initGLalize opengl parameters
    initGL();
    
    // initialized the game and have it build the world
    game_.init(); 
    game_.screenWidth_ = kInitWidth;
    game_.screenHeight_ =  kInitHeight;
    
    //glutTimerFunc(20, timer, 20);  // will call the timer 50 times per second, or every 20ms
    
    game_.timer_.start();
    // loop until something happens
    glutMainLoop();  

}

void initGL() 
{ 
    glEnable(GL_TEXTURE_2D);				// Enable Texture Mapping 
    glClearColor(0, 0, 0, 0.0f);			// This Will Clear The Background Color To Black
    glClearDepth(1.0);					// Enables Clearing Of The Depth Buffer
    glDepthFunc(GL_LESS);				// The Type Of Depth Test To Do
    glEnable(GL_DEPTH_TEST);				// Enables Depth Testing 
    glShadeModel(GL_SMOOTH);				// Enables Smooth Color Shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);	// Really Nice Perspective Calculations
    glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);		// Really Nice Point Smoothing
	
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();					// Reset The Projection Matrix
    
    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)kInitWidth/(GLfloat)kInitHeight, .5f ,10000.0f);
                                                                                  
    glMatrixMode(GL_MODELVIEW);

    // initialize lighting
    GLfloat ambience[4] = {0.3f, 0.3f, 0.3f, 1.0};	// The color of the light in the world
    GLfloat diffuse[4] = {0.5f, 0.5f, 0.5f, 1.0};	// The color of the positioned light
    GLfloat light_position[]={ 5.0,-10.0,0.0,1.0};

    // set the ambience of our world: (The non lit color)
    glLightfv( GL_LIGHT0, GL_AMBIENT,  ambience );
    // set the diffuse of our light:  (The light color)
    glLightfv( GL_LIGHT0, GL_DIFFUSE,  diffuse);
    // set the light's position:
    glLightfv( GL_LIGHT0, GL_POSITION, light_position);
    // once we initialized the light, we need to turn it on:
    glEnable(  GL_LIGHT0   ); 
    
    glEnable( GL_COLOR_MATERIAL );
    glEnable( GL_LIGHTING);
}

void reshapeWindow(int width, int height)
{
    if (height==0)height=1;			// Prevent a divide by zero error
                                                                                                    
    glViewport(0,0,width,height);		// Make our viewport the whole window

    glMatrixMode(GL_PROJECTION);		// Select The Projection Matrix
    glLoadIdentity();				// Reset The Projection Matrix
    
    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height, .5f ,10000.0f);

    glMatrixMode(GL_MODELVIEW);			// Select The Modelview Matrix
    glLoadIdentity();				// Reset The Modelview Matrix
    
    game_.screenWidth_ = width;
    game_.screenHeight_ =  height;
}

void display() { game_.display(); }

void idle() { game_.idle(); }

void keyboard(unsigned char key, int x, int y) { game_.keyboard(key, x, y); }

void mouseClick(int button, int state, int x, int y) { game_.mouseClick(button, state, x, y); }

void mouseMove(int x, int y) { game_.mouseMove(x, y); }

