//===============================================================================
// @ IvMain.cpp
// 
// Start up and main event loop
// ------------------------------------------------------------------------------
// Copyright (C) 2004 by Elsevier, Inc. All rights reserved.
//
//===============================================================================

//-------------------------------------------------------------------------------
//-- Dependencies ---------------------------------------------------------------
//-------------------------------------------------------------------------------

#include <stdlib.h>

#ifdef PLATFORM_OSX
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <IvGame.h>
#include <IvDebugger.h>
#include <IvEventHandler.h>
#include <IvDisplay.h>

extern "C" void displayCallback(void);
extern "C" void visibilityCallback(int vis);
extern "C" void reshapeCallback(int w, int h);
extern "C" void keyboardCallback(unsigned char key, int x, int y);
extern "C" void keyboardupCallback(unsigned char key, int x, int y);
extern "C" void mouseCallback(int button, int state, int x, int y);
extern "C" void idleCallback(void);

//-------------------------------------------------------------------------------
// @ main()
//-------------------------------------------------------------------------------
// Where it all begins
//-------------------------------------------------------------------------------
int main(int argc, char *argv[])
{   
    glutInit( &argc, argv );
    gDebugger.DumpToFile( "Debug.txt" );

    if (!IvGame::Create() || !IvGame::mGame->Initialize( argc, argv ))
    {
        IvGame::Destroy();
        return 1;
    }

    // set up display
    glutDisplayFunc(displayCallback);
    glutVisibilityFunc(visibilityCallback);
    glutReshapeFunc(reshapeCallback);

    // set up event handler
    glutIgnoreKeyRepeat(1);
    glutKeyboardFunc(keyboardCallback);
    glutKeyboardUpFunc(keyboardupCallback);
    glutMouseFunc(mouseCallback);

    // set up game update
    glutIdleFunc(idleCallback);

    // run it!
    glutMainLoop();
    
    IvGame::Destroy();

    return 0;
    
}   // End of main()


extern "C"
void
displayCallback(void)
{
    IvGame::mGame->Display();
}


extern "C"
void
visibilityCallback(int vis)
{
    IvGame::mGame->mDisplay->Activate( vis == GLUT_VISIBLE );
}


extern "C"
void
reshapeCallback(int w, int h)
{
    IvGame::mGame->mDisplay->Resize( w, h );
}


extern "C"
void
keyboardCallback(unsigned char key, int, int)
{
    IvGame::mGame->mEventHandler->KeyDown( key );   // Handle key down event
}


extern "C"
void
keyboardupCallback(unsigned char key, int, int)
{
    IvGame::mGame->mEventHandler->KeyUp( key ); // Handle key up event
}


extern "C"
void mouseCallback(int button, int state, int x, int y)
{
    if ( button == GLUT_LEFT_BUTTON )
    {
        if ( state == GLUT_DOWN )
            IvGame::mGame->mEventHandler->MouseDown( x, y );
        else
            IvGame::mGame->mEventHandler->MouseUp();
    }
}


extern "C"
void
idleCallback(void)
{
    IvGame::mGame->Update();
}


