/*
 =======================================================================================
  Name        : Init_Gli.c
  Author      : Philippe Vimont
  Description : Init/close Gli + misc functions
 =======================================================================================
  Modification -> Author :                  Date :
  Description :
 =======================================================================================
 */
 
#define MAIN_GLI

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 
#include "init_gli.h"
#include "gli_st.h"
#include "GLI_Defn.h"
#include "light_st.h"
#include "PvObj_st.h"
#include "material.h"
#include "linktab.h"
#include "Liste.h"
#include "watrplan.h"
#include "Priv_Obj.h"
#include "matstack.h"
#include "light.h"
#include "Object.h"
#include "texture.h"
#include "TMR.h"
#include "PRF.h"
#include "DLLInter.h"
#include "DispMode.h"
#include "GliText.h"
#include "DLLCaps.h"
#include "Tex.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
/*
 =======================================================================================
    Constants 
 =======================================================================================
 */
/* Defined in TMR\Timer.cxx */
extern u_long fn_ulTimerTickPerSecond(short wTimerType);

extern ACP_tdxHandleOfMaterial  gs_hDefaultMaterial;
static long                     GLI_gsIsInit = 0;
 
 
//float gs_xConstantFor3dfxBugFloat;
 
/*
 =======================================================================================
    External
 =======================================================================================
 */

extern HWND             fn_hGetApplicationWindow(void);
extern void             GLI_vInitLensFlare(void) ;

extern void             TEX_vSaveGlobales();
extern void             TEX_vLoadGlobales();
extern void             TEX_vSaveGlobalesFix();
extern void             TEX_vLoadGlobalesFix();

#ifdef EXGEOM_APPLI
#define fn_vInitFrameLenght()
#else 
void fn_vInitFrameLenght (void);
#endif
 
extern unsigned char                                g_ucMemorySnapshot;
extern char                                         g_bSaveLoadFix;
extern unsigned long                                GLI_g_ulFrameLength;
extern GLI_tdstInternalGlobalValuesFor3dEngine *    GLI_BIG_GLOBALS;

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 
#ifdef __cplusplus
extern "C" 
{
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    
    
/* End of turn engine.*/
/*
void GLI_vEndOfTurnEngine(void)
{
}
*/

/*
 =======================================================================================
    Gli initialisation function
 =======================================================================================
 */
 
/*
 ----------------------------------------------------------------------------------------
  Description   : initialize gli data
 ----------------------------------------------------------------------------------------
 */
void GLI_xInitGli (void)
{
    static long lFirst = 1;
     
    if (lFirst)
    {
        lFirst = 0;
 
/*
        GLI_g_ulFrameLength = (long) ( ((float) fn_ulTimerTickPerSecond( C_wTimerFrequencyHigh )) / 60.0f );
*/
        MMG_fn_vAddMemoryInfo (MMG_C_lTypeGLI , MMG_C_lSubTypeGlobal , NULL);
		GLI_BIG_GLOBALS = (GLI_tdstInternalGlobalValuesFor3dEngine *)TMP_M_p_Malloc(sizeof(GLI_tdstInternalGlobalValuesFor3dEngine));
        GLI_BIG_GLOBALS->GLD_RenderingCounter   = -1;
        GLI_BIG_GLOBALS->p_stActiveFog          = NULL;
        GLI_BIG_GLOBALS->p_stLastComputedFog    = NULL;
        GLI_BIG_GLOBALS->xFogIsOn               = 0;
        GLI_BIG_GLOBALS->lAMirrorIsDetected     = 0;
        GLI_BIG_GLOBALS->bDrawMirrorSymetric    = 0;
        GLI_BIG_GLOBALS->lDisplayMenuIsDetected = 0;
        GLI_BIG_GLOBALS->xGlobalAlpha           = 0.0f;
             
        GLI_DRV_lSendDataToDll("GliBigGlobals",  GLI_BIG_GLOBALS);
        
        GLI_xCreateMaterial( &gs_hDefaultMaterial); /* little bug.*/
        GLI_vIniTextRead();
        GLI_vCreateObjectsReference();/* create collision visual objects.*/
        //GLI_fn_vInitText();
        GLI_vInitWaterplaneModule();
        GLI_vSpecularOff();
        GLI_vInitLensFlare() ;
        GLI_xInitMatrixStack();
    }
 
    GLI_BIG_GLOBALS -> p_TheZListe = NULL;
    GLI_g_stCaps.fRefreshRate      = 0.0f;
        
    GLI_DRV_xInitDriver( fn_hGetApplicationWindow(), GLI_fn_lIsFullScreenMode(), GLI_fn_lDisplayModeWidth(), GLI_fn_lDisplayModeHeight(), GLI_fn_lDisplayModeBPP() );

    if ( (GLI_g_stCaps.fRefreshRate < 40.0f) || (GLI_g_stCaps.fRefreshRate >= 210.0f ) )
        GLI_g_stCaps.fRefreshRate = 60.0f;

    /* compute video frame length (for flip with synchro and meca) */
    GLI_g_ulFrameLength = (long) ( (float) fn_ulTimerTickPerSecond( C_wTimerFrequencyMedium ) / GLI_g_stCaps.fRefreshRate );
    /*GLI_g_ulFrameLength = (long) ( ((float) fn_ulTimerTickPerSecond( C_wTimerFrequencyMedium )) / 60.0f );*/

    fn_vInitFrameLenght();
    PRF_fn_vUpdateMonitorFrequency( (unsigned long)GLI_g_stCaps.fRefreshRate );

    GLI_gsIsInit = 1;
}
    
/*
 ----------------------------------------------------------------------------------------
  Description   : close driver
 ----------------------------------------------------------------------------------------
 */
void GLI_vCloseGli( void )
{
    GLI_gsIsInit = 0;
    GLI_DRV_vCloseDriver();
}
    
/*
 ----------------------------------------------------------------------------------------
  Description   : return non zero if gli is initialized
 ----------------------------------------------------------------------------------------
 */
long GLI_xIsGliInit()
{
    return GLI_gsIsInit;
}

/*
 =======================================================================================
    Other functions
 =======================================================================================
 */
    
/*
 ----------------------------------------------------------------------------------------
  Description   : called after all graphic data are loaded
 ----------------------------------------------------------------------------------------
 */
void GLI_vEndofGeometricLoad()
{
    if (g_ucMemorySnapshot == 2)
    {
        if(!g_bSaveLoadFix)
        {
            TEX_vSaveGlobales(); /* Save binary data's*/
        }
        else
        {
            TEX_vSaveGlobalesFix();
        }
    }
    if (g_ucMemorySnapshot == 1)
    {
        if(!g_bSaveLoadFix)
        {  
            TEX_vLoadGlobales(); /* Load binary data's*/
        }
        else
        {
            TEX_vLoadGlobalesFix();
        }
    }

    /*
     * load textures
     */
    GLI_vComputeTextures ();
}
    
/*
 ----------------------------------------------------------------------------------------
  Description   : memory signal (to change memory channel)
 ----------------------------------------------------------------------------------------
 */
void GLI_vSignalCurrentMemoryChannel(unsigned char ucCurrentChannel)
{
    GLI_TEX_vSignalCurrentMemoryChannel(ucCurrentChannel);
}
    
/*
 ----------------------------------------------------------------------------------------
  Description   : memory signal (to free memory channel)
 ----------------------------------------------------------------------------------------
 */
void GLI_vKillMemoryChannel(unsigned char ucCurrentChannel)
{
    GLI_TEX_vKillMemoryChannel(ucCurrentChannel);
}
    
/*
 ----------------------------------------------------------------------------------------
  Description   : draw a bar (not implemented : called by rasters display)
 ----------------------------------------------------------------------------------------
 */
void GLI_vDrawBar (long _lX0, long _lY0, long _lLength, long _lHeight)
{
}
 
#ifdef __cplusplus
} /*extern "C"*/
#endif
