#include "main.h"

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);
    
    // create window
    glutCreateWindow("Crystal Catstrophe");
    
    // register callback functions
    glutDisplayFunc(display);
    glutReshapeFunc(reshapeWindow); 
    glutIdleFunc(idle);
    glutKeyboardFunc(keyboard);
	glutKeyboardUpFunc(keyboardUp);
    glutSpecialFunc(specialKey);
    glutSpecialUpFunc(specialKeyUp);
    glutMouseFunc(mouseClick);
    glutMotionFunc(mouseMove);
    
    initGL();
    
    cGame::instance()->init();
    
    glutMainLoop(); 
}

void initGL() 
{ 
    glClearColor(0, 0, 0, 0.0f);			// This Will Clear The Background Color To Black
    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
    //glEnable(GL_CULL_FACE);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();					// Reset The Projection Matrix
    
    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,(GLfloat)kInitWidth/(GLfloat)kInitHeight, .5f , 1000.0f);
              
    // initialize lighting
    GLfloat ambience[4] = {.3, .3, .3, 1.0};	// The color of the light in the world
    GLfloat diffuse[4] = {.7, .7, .7, 1.0};	// The color of the positioned light
    GLfloat light_position[]={ 10, 10, 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);                                                                                                                                                                                 
    glMatrixMode(GL_MODELVIEW);

    // setup camera
    glLoadIdentity(); // setup camera angle on scene
    gluLookAt(	0, 0, 10,
                0, 0, 0,
                0, 1, 0);
                
}

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 , 1000.0f);

    glMatrixMode(GL_MODELVIEW);			// Select The Modelview Matrix
    glLoadIdentity(); // setup camera angle on scene
    gluLookAt(	0, 0, 10,
                0, 0, 0,
                0, 1, 0);
                
    cGame::instance()->screenWidth_ = width;
    cGame::instance()->screenHeight_ =  height;
}

void display() 
{
    cGame::instance()->display(); 
}

void idle() { cGame::instance()->idle(); }

void keyboard(unsigned char key, int x, int y) { cGame::instance()->keyboard(key, x, y); }

void keyboardUp(unsigned char key, int x, int y) { cGame::instance()->keyboardUp(key, x, y); }

void specialKey(int key, int x, int y) { cGame::instance()->specialKey(key, x, y); }

void specialKeyUp(int key, int x, int y) { cGame::instance()->specialKeyUp(key, x, y); }

void mouseClick(int button, int state, int x, int y) { cGame::instance()->mouseClick(button, state, x, y); }

void mouseMove(int x, int y) { cGame::instance()->mouseMove(x, y); }


