/*
 =======================================================================================
  Name        : DLLInter.c
 
  Author      : vincent lhullier            Date :14/09/98
 
  Description : initialize Specific driver DLL
 =======================================================================================
 */
 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "windows.h"
#include "GLI_util.h"
#include "DLLInter.h"
#include "DLLCaps.h"
 
#include "vertex.h"
#include "PvObj_st.h"
#include "Camera.h"
#include "GliBench.h"
#include "TexBench.h"
 
#include "GLD.h"
#include "PRF.h"
 
#include "Init_Gli.h"
#include "DispMode.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
/*
 =======================================================================================
    Structure 
 =======================================================================================
 */
GLI_tdst_DllInfos       GLI_gst_DllInfo;

 
/*
 =======================================================================================
    Constants
 =======================================================================================
 */
#ifdef _DEBUG
    #define C_szVersionSuffix   "vd"
#elif RETAIL
    #define C_szVersionSuffix   "vf"
#elif NDEBUG
    #define C_szVersionSuffix   "vr"
#else
#error "Bad version"
#endif

 
/*
 =======================================================================================
    External
 =======================================================================================
 */
 
extern GLI_tdstInternalGlobalValuesFor3dEngine  *GLI_BIG_GLOBALS;
extern long                                     gs_aDEFTableOfTextureMemoryChannels[];
extern GLI_tdstTexture                          *gs_aDEFTableOfTextureAlreadyRead [];
extern void                                     *GLI_gs_p_ConvertBufferMipMapping;
extern long                                     GLD_RenderingCounter;
 
extern void                 GLI_vSetGlobalAlpha(float );
extern void                 GLI_xSerialProjection ( GLI_tdstCamera *,long ,GLI_tdstAligned3DVector *,GLI_tdstAligned2DVector *);
extern long                 GLI_xIsGliInit(void);
extern void *               GLI_fn_p_vTmpMalloc( unsigned long );
extern void                 GLI_fn_vTmpFree( void *);
extern long                 GLI_lComputeTexturesSize();
extern void                 GLI_vCompressTex565 ( GLI_tdstTexture *, void *);
extern void                 GLI_vCompressTex4444( GLI_tdstTexture *, void *);
extern void                 GLI_vCompressTex1555( GLI_tdstTexture *, void *);
extern void                 GLI_xLoadTextureInTexelField(GLI_tdstTexture *, long *, BOOL);
extern void                 GLI_vReloadTextures(void);
extern void                 GLI_vDivideBitmapSurfaceBy2NonZero (unsigned long *,unsigned long *,long , long );
extern void                 GLI_vDivideBitmapSurfaceBy2Palette (unsigned long *,unsigned long *,long , long );
extern void                 GLI_vDivideBitmapSurfaceBy2 (unsigned long *,unsigned long *,long , long );
extern unsigned long        GLI_vVerifyTextureIsRealyAlpha ( GLI_tdstTexture *);
extern long                 GLI_fn_lAddDisplayMode( long , long , long , long );
 
extern void                 GLD_fn_vBlitStretched16b ( long , long , unsigned short *, long , long , long , unsigned short *);
extern long                 GLD_fn_lAddDefinedPixelFormat( GLD_tdstPixelFormat * );
extern GLD_tdstPixelFormat  *GLD_fn_p_stGetDefinedPixelFormat( long );
extern long                 GLD_fn_lComputeWaitFrameForSmoothSynchro();
 
extern void                 Erm_fn_v_PrintErrMsg(char *, char *);
 
extern void                 GAM_fn_vReadStringInUbiIniFile( char *, char *, long , char *);
extern void                 GAM_fn_vWriteStringInUbiIniFile( char *, char *, char *);
 
extern void *               fn_hGetDrawSemaphore( void );
 
extern void				    GLI_vUsePreCalculatedMipMaps ( GLI_tdstTexture *, void	**, unsigned char *);
 
#if !defined(RETAIL)
extern void                 MMG_fn_vAddMemoryInfo (long , long , void *);
#endif
#if !defined(RETAIL) && !defined(PRESS_DEMO)
extern void                 GAM_fn_vShowRasters(void);
extern long                 PRF_fn_lGetIndependantVariable(unsigned long );
extern void                 PRF_fn_vSetIndependantVariable(unsigned long , long );
extern long                 PRF_fn_lIncIndependantVariable(unsigned long , long );
extern void                 GLI_fn_vPRFIncreaseVariable (unsigned long , void *, unsigned long );
extern void                 GLI_fn_vPRFStartChrono (unsigned long , void *);
extern void                 GLI_fn_vPRFStopChrono (unsigned long , void *);
#endif
 
extern HINSTANCE            fn_hGetApplicationInstance(void);

extern u_long fn_ulTimerTickPerSecond(short wTimerType);
extern void fn_vTimerGetCounter(short wTimerType, stTimerCount* p_stValue);
 
/*
 =======================================================================================
    Globals 
 =======================================================================================
 */
 
/* General */
long    (*GLI_DRV_lSendDataToDll)(char *, void *);
 
/* DOMATxxx.C */
void    (* GLI_DRV_vDoOpaqueTextureSelection)( GLI_tdstInternalGlobalValuesFor3dEngine * );
void    (* GLI_DRV_vWrite16bBitmapToBackBuffer) ( void *,long ,long ,long ,long, long, long );
void    (* GLI_DRV_vAddBlackPolygon)( long , long , long , long );
void    (* GLI_DRV_vNoBlackPolygon)( void );
void    (* GLI_DRV_vComputeFogEffect)(GLI_tdstInternalGlobalValuesFor3dEngine *);
BOOL    (* GLI_DRV_bBeginScene)(void);
BOOL    (* GLI_DRV_bEndScene)(void);
    
/* ACCES_XXXX.C */
void    (* GLI_DRV_vSendSpriteToClip)(GLI_tdstAligned2DVector *,MTH_tdxReal ,GLI_tdstInternalGlobalValuesFor3dEngine *);
void    (* GLI_DRV_vSendSpriteToClipWithUV)( GLI_tdstAligned2DVector *, MTH_tdxReal *, MTH_tdxReal , GLI_tdstInternalGlobalValuesFor3dEngine *);
 
/* ACCES_ALL.C */
void    (* GLI_DRV_vSendSingleLineToClip)( GLD_tdstViewportAttributes *, GLI_tdstAligned3DVector *, GLI_tdstAligned2DVector *, GLI_tdstAligned3DVector *, GLI_tdstAligned2DVector *, GLI_tdstInternalGlobalValuesFor3dEngine *, long , GEO_tdstColor *);
 
/* HDWTex_xxx.C */
void    (* GLI_DRV_vDownLoadTextures)(long, long, BOOL);
void    (* GLI_DRV_vUnLoadTextures)(void);
long    (* GLI_DRV_lGetTextureSize)( GLI_tdstTexture *);
 
/* init_XXX.c */
void    (* GLI_DRV_vFlipDevice)(long );
void    (* GLI_DRV_vFlipDeviceWithSyncro)(void);
void    (* GLI_DRV_vClearDevice)(BOOL, BOOL , unsigned long );
void    (* GLI_DRV_xInitDriver) (HWND, BOOL, long, long, long );
void    (* GLI_DRV_vCloseDriver) (void);
void    (* GLI_DRV_vClearZBufferRegion)(long ,long ,long ,long );
//long    (* GLI_DRV_fn_lEnumerateDisplayMode)(void);
long    (* GLI_DRV_fnl_EnumModes)(char *, char *);
HRESULT (* GLI_DRV_hChangeMode)( BOOL, long, long, long );
BOOL    (* GLI_DRV_bLockDevice)( void **, long *);
BOOL    (* GLI_DRV_bUnlockDevice)( void );
BOOL    (* GLI_DRV_bPrepareForGliWindowed)(HWND);
void    (* GLI_DRV_vPrepareForGliFullScreen)(HWND);
void    (* GLI_DRV_vActivateGli)(HWND, BOOL);
void    (* GLI_DRV_vReadaptDisplay)(void);
 
BOOL    (* GLI_DRV_bWindowedModeIsOptimized)( void );
void    (* GLI_DRV_vOptimizedWindowedMode)( void );
void    (* GLI_DRV_vNonOptimizedWindowedMode)( void );
void    (* GLI_DRV_vSetMainWindowForRender)(HWND);
void    (* GLI_DRV_vRecomputeViewportToMaximizeDisplay)(int);
 
/* ClipTriangles */
void    (* GLI_DRV_xClearViewingList)( void );
void    (* GLI_DRV_xSendListToViewport)( GLD_tdstViewportAttributes * );
void    (* GLI_DRV_vSetZClip)(float ,GLI_tdstInternalGlobalValuesFor3dEngine *);
void    (* GLI_DRV_vSetClipWindow)(float ,float ,float ,float ,GLI_tdstInternalGlobalValuesFor3dEngine *);
void    (* GLI_DRV_xSendElementTIToClip_TRIANGLES)( GEO_tdstElementIndexedTriangles *, GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT);
void    (* GLI_DRV_xSendSingleTriangleToClip_TRIANGLES)( GLI_tdstAligned2DVector *a3_st2DVertex, ACP_tdst2DUVValues *a3_stUV, GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT);
 
/* misc request (see request type n dllcaps.h file) */
long    (* GLI_DRV_lRequest)( long, long, long );
 
#ifndef RETAIL
void    (*GLI_DRV_vGetStats)(unsigned long *, unsigned long *);
#endif
void    (*GLI_DRV_vWaitRetrace)(void);
 
 
 
/*
 =======================================================================================
    Public function 
 =======================================================================================
 */
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Load Driver DLL
 ----------------------------------------------------------------------------------------
 */
long GLI_fnl_InitDll( void )
{
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    long    (*SendFunctionToDll)(char *, FARPROC );
    long    (*pfnGetDllInfo)(char *, void *);
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
    GLI_gst_DllInfo.h_Module = LoadLibrary( GLI_gst_DllInfo.sz_FileName );
    if (GLI_gst_DllInfo.h_Module == NULL)
        return 0;

    /*
     * Get Info
     */
    pfnGetDllInfo = (long (__cdecl *)(char *, void *) ) GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_lGetDllInfo" );
    if (pfnGetDllInfo == NULL)
        return 0;
    
    pfnGetDllInfo( "Name", (void *)  GLI_gst_DllInfo.sz_Desc );
    if (strcmp( GLI_gst_DllInfo.sz_Desc, GLI_gst_DllInfo.sz_Name ) != 0)
        return 0;

    pfnGetDllInfo( "Description",(void *)  GLI_gst_DllInfo.sz_Desc );
    pfnGetDllInfo( "Version",    (void *)  GLI_gst_DllInfo.sz_Version );
    pfnGetDllInfo( "Code",       (void *) &GLI_gst_DllInfo.ul_Code );
 
    /*
     * Send data pointer to DLL (all data cannot be send now, some initialized later are send later)
     */
    GLI_DRV_lSendDataToDll = (long (__cdecl *)(char *, void *) ) GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_lSetCommonData" );
   
	
	GLI_DRV_lSendDataToDll("GliDllInfo",  &GLI_gst_DllInfo);//chbani
    GLI_DRV_lSendDataToDll("GliBigGlobals",  GLI_BIG_GLOBALS);
    GLI_DRV_lSendDataToDll("TextureMemoryChannels", gs_aDEFTableOfTextureMemoryChannels);
    GLI_DRV_lSendDataToDll("TextureAlreadyRead", gs_aDEFTableOfTextureAlreadyRead );
    GLI_DRV_lSendDataToDll("ConvertBufferMipMapping", GLI_gs_p_ConvertBufferMipMapping);
    GLI_DRV_lSendDataToDll("RenderingCounterPointer", &GLD_RenderingCounter );
    GLI_DRV_lSendDataToDll("DrawSem", fn_hGetDrawSemaphore() );
    GLI_DRV_lSendDataToDll("GliCaps", &GLI_g_stCaps );
#ifdef GLI_BENCH
    GLI_DRV_lSendDataToDll("GliBench", &GLI_g_stBench );
#endif
 
    /*
     * send function pointer to DLL (all needed are sended here)
     */
    SendFunctionToDll = (long (__cdecl *)(char *, FARPROC) ) GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_lSetCommonFct" );
    SendFunctionToDll("SetGlobalAlpha",                 (FARPROC) GLI_vSetGlobalAlpha);
    SendFunctionToDll("SerialProjection",               (FARPROC) GLI_xSerialProjection);
    SendFunctionToDll("IsGliInit",                      (FARPROC) GLI_xIsGliInit);
    SendFunctionToDll("TmpMalloc",                      (FARPROC) GLI_fn_p_vTmpMalloc);
    SendFunctionToDll("TmpFree",						(FARPROC) GLI_fn_vTmpFree);
    SendFunctionToDll("ErmPrintMessage",                (FARPROC) Erm_fn_v_PrintErrMsg );
    SendFunctionToDll("ComputeTextureSize",             (FARPROC) GLI_lComputeTexturesSize);
    SendFunctionToDll("CompressTex565",                 (FARPROC) GLI_vCompressTex565);
    SendFunctionToDll("CompressTex4444",                (FARPROC) GLI_vCompressTex4444);
    SendFunctionToDll("CompressTex1555",                (FARPROC) GLI_vCompressTex1555);
    SendFunctionToDll("DivideBitmapSurfaceBy2",         (FARPROC) GLI_vDivideBitmapSurfaceBy2);
    SendFunctionToDll("DivideBitmapSurfaceBy2NonZero",  (FARPROC) GLI_vDivideBitmapSurfaceBy2NonZero);
    SendFunctionToDll("DivideBitmapSurfaceBy2Palette",  (FARPROC) GLI_vDivideBitmapSurfaceBy2Palette);
    SendFunctionToDll("VerifyTextureIsReallyAlpha",     (FARPROC) GLI_vVerifyTextureIsRealyAlpha );
    SendFunctionToDll("LoadTextureInTexelField",        (FARPROC) GLI_xLoadTextureInTexelField);
    SendFunctionToDll("ReloadTextures",                 (FARPROC) GLI_vReloadTextures);
    SendFunctionToDll("AddDisplayMode",                 (FARPROC) GLI_fn_lAddDisplayMode );
    SendFunctionToDll("BlitStretched16b",               (FARPROC) GLD_fn_vBlitStretched16b );
    SendFunctionToDll("GetDefinedPixelFormat",          (FARPROC) GLD_fn_p_stGetDefinedPixelFormat );
    SendFunctionToDll("AddDefinedPixelFormat",          (FARPROC) GLD_fn_lAddDefinedPixelFormat );
    SendFunctionToDll("ComputeWaitFrameForSmoothSynchro",(FARPROC) GLD_fn_lComputeWaitFrameForSmoothSynchro );

    SendFunctionToDll("ReadIniString",                  (FARPROC) GAM_fn_vReadStringInUbiIniFile );
    SendFunctionToDll("WriteIniString",                 (FARPROC) GAM_fn_vWriteStringInUbiIniFile );
 
#if !defined(RETAIL)
    SendFunctionToDll("AddMemoryInfo",                  (FARPROC) MMG_fn_vAddMemoryInfo );  
#endif
#if !defined(RETAIL) && !defined(PRESS_DEMO)
    SendFunctionToDll("ShowRasters",                    (FARPROC) GAM_fn_vShowRasters);
    SendFunctionToDll("IncIndependantVariable",         (FARPROC) PRF_fn_lIncIndependantVariable);
    SendFunctionToDll("SetIndependantVariable",         (FARPROC) PRF_fn_vSetIndependantVariable);
    SendFunctionToDll("GetIndependantVariable",         (FARPROC) PRF_fn_lGetIndependantVariable);
    SendFunctionToDll("IncreaseVariable",               (FARPROC) GLI_fn_vPRFIncreaseVariable );
    SendFunctionToDll("ResetVariable",                  (FARPROC) PRF_fn_vResetVariable );
    SendFunctionToDll("StopChrono",                     (FARPROC) GLI_fn_vPRFStopChrono );
    SendFunctionToDll("StartChrono",                    (FARPROC) GLI_fn_vPRFStartChrono );
#endif
 
#ifdef BENCH_TEXTURE
	SendFunctionToDll("TexBenchSetTextureSize",			(FARPROC) TEXBENCH_fn_vSetTextureSize );
#endif

    SendFunctionToDll("TimerGetCounter",                (FARPROC) fn_vTimerGetCounter );
    SendFunctionToDll("TimerTickPerSecond",             (FARPROC) fn_ulTimerTickPerSecond );
    
    /* DOMATxxx.C */
    GLI_DRV_vDoOpaqueTextureSelection           = ( void    (__cdecl *) (GLI_tdstInternalGlobalValuesFor3dEngine *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vDoOpaqueTextureSelection" );
    GLI_DRV_vWrite16bBitmapToBackBuffer         = ( void    (__cdecl *) (void *,long ,long ,long ,long, long, long) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vWrite16bBitmapToBackBuffer" );
    GLI_DRV_vAddBlackPolygon                    = ( void    (__cdecl *) (long , long , long , long) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vAddBlackPolygon" );
    GLI_DRV_vNoBlackPolygon                     = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vNoBlackPolygon" );
    GLI_DRV_vComputeFogEffect                   = ( void    (__cdecl *) (GLI_tdstInternalGlobalValuesFor3dEngine *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vComputeFogEffect" );
    GLI_DRV_bBeginScene                         = ( BOOL    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_bBeginScene" );
    GLI_DRV_bEndScene                           = ( BOOL    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_bEndScene" );
   
    /* ACCES_XXXX.C */
    GLI_DRV_vSendSpriteToClip                   = ( void    (__cdecl *) (GLI_tdstAligned2DVector *,MTH_tdxReal ,GLI_tdstInternalGlobalValuesFor3dEngine *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vSendSpriteToClip" );
    GLI_DRV_vSendSpriteToClipWithUV             = ( void    (__cdecl *) (GLI_tdstAligned2DVector *, MTH_tdxReal *, MTH_tdxReal , GLI_tdstInternalGlobalValuesFor3dEngine *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vSendSpriteToClipWithUV" );
 
    /* ACCES_ALL.C */
    GLI_DRV_vSendSingleLineToClip               = ( void    (__cdecl *) (GLD_tdstViewportAttributes *, GLI_tdstAligned3DVector *, GLI_tdstAligned2DVector *, GLI_tdstAligned3DVector *, GLI_tdstAligned2DVector *, GLI_tdstInternalGlobalValuesFor3dEngine *, long , GEO_tdstColor *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vSendSingleLineToClip" );
 
    /* HDWTex_xxx.C */
    GLI_DRV_vDownLoadTextures                   = ( void    (__cdecl *) (long, long, BOOL) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vDownLoadTextures" );
    GLI_DRV_vUnLoadTextures                     = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vUnLoadTextures" );
    GLI_DRV_lGetTextureSize                     = ( long    (__cdecl *) ( GLI_tdstTexture *) ) GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_lGetSizeOfTexture" );
    
    /* init_XXX.c */
    GLI_DRV_vFlipDevice                         = ( void    (__cdecl *) (long) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vFlipDevice" );
    GLI_DRV_vFlipDeviceWithSyncro               = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vFlipDeviceWithSyncro" );
    GLI_DRV_vClearDevice                        = ( void    (__cdecl *) (BOOL, BOOL , unsigned long) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vClearDevice" );
    GLI_DRV_xInitDriver                         = ( void    (__cdecl *) (HWND, BOOL, long, long, long ) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_xInitDriver" );
    GLI_DRV_vCloseDriver                        = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vCloseDriver" );
    GLI_DRV_vClearZBufferRegion                 = ( void    (__cdecl *) (long, long, long, long) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vClearZBufferRegion" );
//    GLI_DRV_fn_lEnumerateDisplayMode            = ( long    (__cdecl *) (void))GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_fn_lEnumerateDisplayMode" );
    GLI_DRV_fnl_EnumModes                       = ( long    (__cdecl *) (char*, char* ) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_fnl_EnumModes" );
    GLI_DRV_hChangeMode                         = ( HRESULT (__cdecl *) (BOOL, long, long, long ) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_hChangeMode" );
    GLI_DRV_bLockDevice                         = ( BOOL    (__cdecl *) (void **, long *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_bLockDevice" );
    GLI_DRV_bUnlockDevice                       = ( BOOL    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_bUnlockDevice" );

    GLI_DRV_bPrepareForGliWindowed              = ( BOOL    (__cdecl *) (HWND) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_bPrepareForGliWindowed" );
    GLI_DRV_vPrepareForGliFullScreen            = ( void    (__cdecl *) (HWND) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vPrepareForGliFullScreen" );
    GLI_DRV_vActivateGli                        = ( void    (__cdecl *) (HWND, BOOL) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vActivateGli" );
    GLI_DRV_vReadaptDisplay                     = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vReadaptDisplay" );

 
    GLI_DRV_bWindowedModeIsOptimized            = ( BOOL    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_bWindowedModeIsOptimized" );
    GLI_DRV_vOptimizedWindowedMode              = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vOptimizedWindowedMode" );
    GLI_DRV_vNonOptimizedWindowedMode           = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vNonOptimizedWindowedMode" );
	GLI_DRV_vSetMainWindowForRender             = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vSetMainWindowForRender" );//chbani
	GLI_DRV_vRecomputeViewportToMaximizeDisplay = ( void    (__cdecl *) (int) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vRecomputeViewportToMaximizeDisplay" );//chbani
 
    /* ClipTriangles */
    GLI_DRV_xClearViewingList                   = ( void    (__cdecl *) (void) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_xClearViewingList" );
    GLI_DRV_xSendListToViewport                 = ( void    (__cdecl *) (GLD_tdstViewportAttributes *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_xSendListToViewport" );
    GLI_DRV_vSetZClip                           = ( void    (__cdecl *) (float ,GLI_tdstInternalGlobalValuesFor3dEngine *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vSetZClip" );
    GLI_DRV_vSetClipWindow                      = ( void    (__cdecl *) (float ,float ,float ,float ,GLI_tdstInternalGlobalValuesFor3dEngine *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vSetClipWindow" );
    GLI_DRV_xSendElementTIToClip_TRIANGLES      = ( void    (__cdecl *) (GEO_tdstElementIndexedTriangles *, GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_xSendElementTIToClip_TRIANGLES" );
    GLI_DRV_xSendSingleTriangleToClip_TRIANGLES = ( void    (__cdecl *) (GLI_tdstAligned2DVector *a3_st2DVertex, ACP_tdst2DUVValues *a3_stUV, GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_xSendSingleTriangleToClip_TRIANGLES" );
 
    /* misc request (see request type n dllcaps.h file) */
    GLI_DRV_lRequest                            = ( long    (__cdecl *) ( long, long, long ) ) GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_lRequest" );
 
 
 #ifndef RETAIL
    GLI_DRV_vGetStats                           = ( void    (__cdecl *) (unsigned long *, unsigned long *) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vGetStats" );
 #endif
    GLI_DRV_vWaitRetrace                        = ( void    (__cdecl *) (void ) )GetProcAddress( GLI_gst_DllInfo.h_Module, "GLI_DRV_vWaitRetrace" );
 
    return 1;
}
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Close driver
 ----------------------------------------------------------------------------------------
 */
long GLI_fn_lCloseGliDriver( void )
{
    FreeLibrary( GLI_gst_DllInfo.h_Module );
    return 0;
}
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Set Dll filename
 ----------------------------------------------------------------------------------------
 */
void GLI_fnv_SetDllFileName( char *_sz_Name )
{
    sprintf( GLI_gst_DllInfo.sz_FileName, "dll/%s%s.dll", _sz_Name, C_szVersionSuffix ); 
}

/*
 ----------------------------------------------------------------------------------------
  Description   : set dll name
 ----------------------------------------------------------------------------------------
 */
void GLI_fnv_SetDllName( char *_sz_Name )
{
    strcpy( GLI_gst_DllInfo.sz_Name, _sz_Name );
}

/*
 ----------------------------------------------------------------------------------------
  Description   : set driver name
 ----------------------------------------------------------------------------------------
 */
void GLI_fnv_SetDriverName( char *_sz_Name )
{
    strcpy( GLI_gst_DllInfo.sz_DriverName, _sz_Name );
}

/*
 ----------------------------------------------------------------------------------------
  Description   : set device name
 ----------------------------------------------------------------------------------------
 */
void GLI_fnv_SetDeviceName( char *_sz_Name )
{
    strcpy( GLI_gst_DllInfo.sz_DeviceName, _sz_Name );
}

/*
 ----------------------------------------------------------------------------------------
  Description   : set display mode
 ----------------------------------------------------------------------------------------
 */
void GLI_fnv_SetMode( long _l_FullScreen, long _l_Width, long _l_Height, long _l_Bpp )
{
    GLI_gst_DllInfo.l_InitWidth       = _l_Width;
    GLI_gst_DllInfo.l_InitHeight      = _l_Height;
    GLI_gst_DllInfo.l_InitFullScreen  = _l_FullScreen;
    GLI_gst_DllInfo.l_InitBpp         = _l_Bpp;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : set display mode (parameters are in a string )
 ----------------------------------------------------------------------------------------
 */
void GLI_fnv_SetModeByString( char *_sz_Line )
{
    GLI_gst_DllInfo.l_InitFullScreen = (*_sz_Line = '1') ? 1 : 0;
    if ( ( _sz_Line = strchr( _sz_Line, '-' ) ) == NULL) 
        return;
    GLI_gst_DllInfo.l_InitWidth = atoi( _sz_Line + 1 );
    if ( ( _sz_Line = strchr( _sz_Line + 1, 'x' ) ) == NULL) 
        return;
    GLI_gst_DllInfo.l_InitHeight = atoi( _sz_Line + 1 );
    if ( ( _sz_Line = strchr( _sz_Line + 1, 'x' ) ) == NULL) 
        return;
    GLI_gst_DllInfo.l_InitBpp = atoi( _sz_Line + 1 );
}

/*
 =======================================================================================
    Functions to make gateway between other modules (GAM, AI...) and GLI specific
 =======================================================================================
 */
void GLI_xClearViewingList ( void )
{
    GLI_DRV_xClearViewingList();
}
 
void GLI_xSendListToViewport( GLD_tdstViewportAttributes *p_stVpt )
{
    GLI_DRV_xSendListToViewport( p_stVpt );
}
 
void GLI_ClearZBufferRegion(long lXStart,long lXEnd,long lYStart,long lYEnd)
{
    GLI_DRV_vClearZBufferRegion(lXStart, lXEnd, lYStart, lYEnd);
}
 
void GLI_vWrite16bBitmapToBackBuffer( void *_p_vSourceBuffer, long _lWidth, long _lHeight, long _lDestLeft, long _lDestTop, long _lDestRight, long _lDestBottom )
{
    GLI_DRV_vWrite16bBitmapToBackBuffer( _p_vSourceBuffer, _lWidth, _lHeight, _lDestLeft, _lDestTop, _lDestRight, _lDestBottom );
}
 
void GLI_fn_vGetStats( unsigned long *_p_ulPixelsSent, unsigned long *_p_ulPixelsDisplayed )
{
    GLI_DRV_vGetStats( _p_ulPixelsSent, _p_ulPixelsDisplayed );
}
 
void GLI_fn_vWaitRetrace(void )
{
	GLI_DRV_vWaitRetrace( );
}

 
void GLI_fn_vDrawMirror( BOOL _bStart )
{
	GLI_BIG_GLOBALS->lAMirrorIsDetected = _bStart ? 3 : 0;
}
 
char GLI_fn_cWindowedModeIsOptimized(void)
{
	return (GLI_DRV_bWindowedModeIsOptimized())?1:0;
}
 
void GLI_fn_vOptimizedWindowedMode(void)
{
	GLI_DRV_vOptimizedWindowedMode();
}
 
void GLI_fn_vNonOptimizedWindowedMode(void)
{
	GLI_DRV_vNonOptimizedWindowedMode();
}


void GLI_fn_vRecomputeViewportToMaximizeDisplay(int _arg)
{
	GLI_DRV_vRecomputeViewportToMaximizeDisplay(_arg);
}
 

void GLI_fn_vSetMainWindowForRender(void * _hWnd)
{
	GLI_DRV_vSetMainWindowForRender((HWND)_hWnd);
}


 
/*
 =======================================================================================
    Access to DLL data
 =======================================================================================
 */
long GLI_lWhatIsGLI(void)
{
    //return  GLI_C_VersionGlide;
    return (long) GLI_gst_DllInfo.ul_Code;
}
 
/*
 =======================================================================================
    Functions to make gateway between Gli specific and other module (inline functions)
 =======================================================================================
 */
void GLI_fn_vPRFIncreaseVariable (unsigned long ulVar, void *p_vUser, unsigned long ulInc)
{
    PRF_fn_vIncreaseVariable(ulVar, p_vUser, ulInc );
}
 
void GLI_fn_vPRFStartChrono (unsigned long _ulVar, void *_p_vUser)
{
    PRF_fn_vStartChrono ( _ulVar, _p_vUser);
}
 
void GLI_fn_vPRFStopChrono (unsigned long _ulVar, void *_p_vUser)
{
    PRF_fn_vStopChrono( _ulVar, _p_vUser );
}
 
/*
 =======================================================================================
    Functions for memory
 =======================================================================================
 */
 
void *GLI_fn_p_vTmpMalloc( unsigned long _ulSize )
{
    void			*p_vPointer;
    p_vPointer = TMP_M_p_Malloc( _ulSize );
    return p_vPointer;
}
 
void GLI_fn_vTmpFree( void *_p_vPointer )
{
    TMP_M_Free(_p_vPointer);
}
