#include <windows.h>
#include "dllcom.h"
#include "GliBench.h"
#include "TMR.h"
 
/*
 =======================================================================================
    Globals
 =======================================================================================
 */
 
tdstGliDriverIdentity   g_stGliDrvId;
tdstGliCaps             *g_p_stGliCaps = NULL;
 
/*
 * imported object Have to be initialized
 */
GLI_tdst_DllInfos						*GLI_gpst_DllInfo; //chbani
GLI_tdstInternalGlobalValuesFor3dEngine *GLI_BIG_GLOBALS;
 
long                                    *gs_aDEFTableOfTextureMemoryChannels;
GLI_tdstTexture                         **gs_aDEFTableOfTextureAlreadyRead;
void                                    *GLI_gs_p_ConvertBufferMipMapping;
 
long                                    *GLD_p_lRenderingCounter;
 
void                                    *g_p_vDrawSemaphore;
 
#ifdef GLI_BENCH
tdstGliBench                            *GLI_MDRV_g_p_stBench;
#endif
 
/*
 * imported function
 */
/*
 * From GLI
 */
void            (* GLI_MDRV_vSetGlobalAlpha)( float );
void            (* GLI_MDRV_vSerialProjection)( GLI_tdstCamera *, long , GLI_tdstAligned3DVector *, GLI_tdstAligned2DVector *);
 
long            (* GLI_MDRV_xIsGliInit)();
void *          (* GLI_MDRV_p_vTmpMalloc)( unsigned long );
void            (* GLI_MDRV_vTmpFree)( void * );
void            (* GLI_MDRV_vErmPrintMsg)( char *, char *);
 
long            (* GLI_MDRV_lComputeTexturesSize)( void );
void            (* GLI_MDRV_vCompressTex565) ( GLI_tdstTexture *, void *);
void            (* GLI_MDRV_vCompressTex4444 )( GLI_tdstTexture *, void *);
void            (* GLI_MDRV_vCompressTex1555) ( GLI_tdstTexture *, void *);
void            (* GLI_MDRV_vDivideBitmapSurfaceBy2) (unsigned long *,unsigned long *,long , long );
void            (* GLI_MDRV_vDivideBitmapSurfaceBy2NonZero) (unsigned long *,unsigned long *,long , long , unsigned long ); /* G.CLEMENT 29/07/1999 : added chromakey */
void            (* GLI_MDRV_vDivideBitmapSurfaceBy2Palette) (unsigned long *,unsigned long *,long , long );
unsigned long   (* GLI_MDRV_vVerifyTextureIsRealyAlpha) ( GLI_tdstTexture * );
void            (* GLI_MDRV_xLoadTextureInTexelField) (GLI_tdstTexture *, long *, BOOL);
void            (* GLI_MDRV_vReloadTextures) (void);
 
long            (* GLI_MDRV_lAddDisplayMode)( long , long , long, long );
//long            (* GLI_MDRV_lDoDialogBoxLB)( char *, DLGPROC, long );
void			(* GLI_MDRV_vUsePreCalculatedMipMaps) ( GLI_tdstTexture * , void ** , unsigned char *);
 
 
/*
 * From GAM
 */
void            (* GAM_MDRV_vReadInIniFile)( char *, char *, long, char *);
void            (* GAM_MDRV_vWriteInIniFile) (char *, char *, char *);
 
/* 
 * From GLD
 */
void            (* GLD_MDRV_vBlitStretched16b)( long , long , unsigned short *, long , long , long , unsigned short * );
GLD_tdstPixelFormat * (* GLD_MDRV_fn_p_stGetDefinedPixelFormat) ( long );
long            (* GLD_MDRV_fn_lAddDefinedPixelFormat)( GLD_tdstPixelFormat * );
long            (* GLD_MDRV_fn_lComputeWaitFrameForSmoothSynchro) ();
 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#ifndef RETAIL
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void            (* GAM_MDRV_vShowRasters)();
 
void            (* MMG_MDRV_vAddMemoryInfo)(long , long , void * );    /* Must be called before making a memory allocation*/
 
/*
 * From PRF
 */
long            (* PRF_MDRV_lIncIndependantVariable)(unsigned long , long );
void            (* PRF_MDRV_vSetIndependantVariable)(unsigned long , long );
long            (* PRF_MDRV_lGetIndependantVariable)(unsigned long );
void            (* PRF_MDRV_vIncreaseVariable) (unsigned long , void *, unsigned long );
void            (* PRF_MDRV_vResetVariable)( unsigned long );
void            (* PRF_MDRV_vStopChrono )(unsigned long , void *);
void            (* PRF_MDRV_vStartChrono ) (unsigned long , void *);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#endif
 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*
 * BENCH_TEXTURE is defined or not in file TexBench.h
 */
#ifdef BENCH_TEXTURE
 
void			(*GLI_MDRV_TEXBENCH_vSetTextureSize)( GLI_tdstTexture *, char, long);
 
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void            (* TMR_MDRV_vTimerGetCounter)( short, stTimerCount* );
unsigned long   (* TMR_MDRV_ulTimerTickPerSecond)( short );
 
 
 
/*
 =======================================================================================
    DLL entry point
 =======================================================================================
 */
/* specific function define for each display driver DLL*/
void fn_vInitDllIdentity();
 
BOOL __stdcall DllMain(HANDLE hModule, DWORD  ulReasonForCall, LPVOID lpReserved)
{
    fn_vInitDllIdentity();
    return TRUE;
}
 
/*
 =======================================================================================
    Set Gli Common function 
 =======================================================================================
 */
 
long GLI_DRV_lGetDllInfo( char *_szName, void *_p_vData )
{
    if ( strcmp(_szName, "Name") == 0)              strcpy( (char *) _p_vData, g_stGliDrvId.szName );
    else if( strcmp(_szName, "Description") == 0)   strcpy( (char *) _p_vData, g_stGliDrvId.szDesc );
    else if( strcmp(_szName, "Version") == 0)       strcpy( (char *) _p_vData, g_stGliDrvId.szVersion );                 
    else if( strcmp(_szName, "Code") == 0)          *(unsigned long *)_p_vData = g_stGliDrvId.ulCode;
    else
        return 0;
    return 1;
}
 
long GLI_DRV_lSetCommonData( char *_szName, void *_p_vData )
{
	if ( strcmp(_szName, "GliDllInfo") == 0)                   GLI_gpst_DllInfo = (GLI_tdst_DllInfos *) _p_vData;//chbani
    else if ( strcmp(_szName, "GliBigGlobals") == 0)           GLI_BIG_GLOBALS = (GLI_tdstInternalGlobalValuesFor3dEngine *) _p_vData;
    else if( strcmp(_szName, "TextureMemoryChannels") == 0)     gs_aDEFTableOfTextureMemoryChannels = (long *) _p_vData;
    else if( strcmp(_szName, "TextureAlreadyRead") == 0)        gs_aDEFTableOfTextureAlreadyRead = (GLI_tdstTexture **) _p_vData;
    else if( strcmp(_szName, "ConvertBufferMipMapping") == 0)   GLI_gs_p_ConvertBufferMipMapping = _p_vData;
    else if( strcmp(_szName, "RenderingCounterPointer") == 0)   GLD_p_lRenderingCounter = (long *) _p_vData;
    else if( strcmp(_szName, "DrawSem") == 0)                   g_p_vDrawSemaphore = _p_vData;
    else if( strcmp(_szName, "GliCaps") == 0)                   g_p_stGliCaps = (tdstGliCaps *) _p_vData;
#ifdef GLI_BENCH
    else if( strcmp(_szName, "GliBench") == 0)                  GLI_MDRV_g_p_stBench = (tdstGliBench *) _p_vData;
#endif
    else 
        return 0;
 
    return 1;
}
 
long GLI_DRV_lSetCommonFct( char *_szName, FARPROC _xFct )
{
    if ( strcmp( _szName, "SetGlobalAlpha") == 0)                       GLI_MDRV_vSetGlobalAlpha                = (void (__cdecl *) (float )) _xFct;
    else if ( strcmp( _szName, "SerialProjection") == 0)                GLI_MDRV_vSerialProjection              = (void (__cdecl *) ( GLI_tdstCamera *, long , GLI_tdstAligned3DVector *, GLI_tdstAligned2DVector *)) _xFct;
    else if ( strcmp( _szName, "IsGliInit") == 0)                       GLI_MDRV_xIsGliInit                     = (long (__cdecl *) (void)) _xFct;
    else if ( strcmp( _szName, "TmpMalloc") == 0)                       GLI_MDRV_p_vTmpMalloc                   = (void *(__cdecl *) (unsigned long)) _xFct;
	else if ( strcmp( _szName, "TmpFree") == 0)							GLI_MDRV_vTmpFree						= (void (__cdecl *) (void *)) _xFct;
    else if ( strcmp( _szName, "ErmPrintMessage") == 0)                 GLI_MDRV_vErmPrintMsg                   = (void (__cdecl *) (char *, char *)) _xFct;
    else if ( strcmp( _szName, "ComputeTextureSize") == 0)              GLI_MDRV_lComputeTexturesSize           = (long (__cdecl *) (void )) _xFct;
    else if ( strcmp( _szName, "CompressTex565") == 0)                  GLI_MDRV_vCompressTex565                = (void (__cdecl *) (GLI_tdstTexture *, void *)) _xFct;
    else if ( strcmp( _szName, "CompressTex4444") == 0)                 GLI_MDRV_vCompressTex4444               = (void (__cdecl *) (GLI_tdstTexture *, void *)) _xFct;
    else if ( strcmp( _szName, "CompressTex1555") == 0)                 GLI_MDRV_vCompressTex1555               = (void (__cdecl *) (GLI_tdstTexture *, void *)) _xFct;
    else if ( strcmp( _szName, "DivideBitmapSurfaceBy2") == 0)          GLI_MDRV_vDivideBitmapSurfaceBy2        = (void (__cdecl *) (unsigned long *,unsigned long *,long , long )) _xFct;
    else if ( strcmp( _szName, "DivideBitmapSurfaceBy2NonZero") == 0)   GLI_MDRV_vDivideBitmapSurfaceBy2NonZero = (void (__cdecl *) (unsigned long *,unsigned long *,long , long , unsigned long )) _xFct;
    else if ( strcmp( _szName, "DivideBitmapSurfaceBy2Palette") == 0)   GLI_MDRV_vDivideBitmapSurfaceBy2Palette = (void (__cdecl *) (unsigned long *,unsigned long *,long , long )) _xFct;
    else if ( strcmp( _szName, "VerifyTextureIsReallyAlpha") == 0)      GLI_MDRV_vVerifyTextureIsRealyAlpha     = (unsigned long (__cdecl *) (GLI_tdstTexture * )) _xFct;
    else if ( strcmp( _szName, "LoadTextureInTexelField") == 0)         GLI_MDRV_xLoadTextureInTexelField       = (void (__cdecl *) (GLI_tdstTexture *, long *, BOOL)) _xFct;
    else if ( strcmp( _szName, "ReloadTextures") == 0)                  GLI_MDRV_vReloadTextures                = (void (__cdecl *) (void)) _xFct;
    else if ( strcmp( _szName, "AddDisplayMode") == 0)                  GLI_MDRV_lAddDisplayMode                = (long (__cdecl *)(long, long, long, long)) _xFct;
    //else if ( strcmp( _szName, "DoDialogLB") == 0)                      GLI_MDRV_lDoDialogBoxLB                 = (long (__cdecl *)(char *, DLGPROC, long)) _xFct;
    else if ( strcmp( _szName, "BlitStretched16b") == 0)                GLD_MDRV_vBlitStretched16b              = (void (__cdecl *) (long , long , unsigned short *, long , long , long , unsigned short *)) _xFct;
    else if ( strcmp( _szName, "GetDefinedPixelFormat") == 0)           GLD_MDRV_fn_p_stGetDefinedPixelFormat   = (GLD_tdstPixelFormat *(__cdecl *) ( long )) _xFct;
    else if ( strcmp( _szName, "AddDefinedPixelFormat") == 0)           GLD_MDRV_fn_lAddDefinedPixelFormat      = (long (__cdecl *) ( GLD_tdstPixelFormat * )) _xFct;
    else if ( strcmp( _szName, "ComputeWaitFrameForSmoothSynchro") == 0)           GLD_MDRV_fn_lComputeWaitFrameForSmoothSynchro  = (long (__cdecl *) ( void )) _xFct;
    else if ( strcmp( _szName, "ReadIniString") == 0)                   GAM_MDRV_vReadInIniFile                 = (void (__cdecl *) (char *, char *, long, char *)) _xFct;
    else if ( strcmp( _szName, "WriteIniString") == 0)                  GAM_MDRV_vWriteInIniFile                = (void (__cdecl *) (char *, char *, char *)) _xFct;
	else if ( strcmp( _szName, "UseMipMaps") == 0)						GLI_MDRV_vUsePreCalculatedMipMaps		= (void (__cdecl *) ( GLI_tdstTexture * , void *, unsigned char * )) _xFct;
 
#ifndef RETAIL
    else if ( strcmp( _szName, "ShowRasters") == 0)                     GAM_MDRV_vShowRasters                   = (void (__cdecl *) (void)) _xFct;
    else if ( strcmp( _szName, "AddMemoryInfo") == 0)                   MMG_MDRV_vAddMemoryInfo                 = (void (__cdecl *) (long, long, void *)) _xFct;
    else if ( strcmp( _szName, "IncIndependantVariable") == 0)          PRF_MDRV_lIncIndependantVariable        = (long (__cdecl *) (unsigned long, long)) _xFct;
    else if ( strcmp( _szName, "SetIndependantVariable") == 0)          PRF_MDRV_vSetIndependantVariable        = (void (__cdecl *) (unsigned long, long)) _xFct;
    else if ( strcmp( _szName, "GetIndependantVariable") == 0)          PRF_MDRV_lGetIndependantVariable        = (long (__cdecl *) (unsigned long)) _xFct;
    else if ( strcmp( _szName, "IncreaseVariable") == 0)                PRF_MDRV_vIncreaseVariable              = (void (__cdecl *) (unsigned long , void *, unsigned long)) _xFct;
    else if ( strcmp( _szName, "ResetVariable") == 0)                   PRF_MDRV_vResetVariable                 = (void (__cdecl *) (unsigned long)) _xFct;
    else if ( strcmp( _szName, "StopChrono") == 0)                      PRF_MDRV_vStopChrono                    = (void (__cdecl *) (unsigned long , void *)) _xFct;
    else if ( strcmp( _szName, "StartChrono") == 0)                     PRF_MDRV_vStartChrono                   = (void (__cdecl *) (unsigned long , void *)) _xFct;
#endif
 
#ifdef BENCH_TEXTURE
	else if (strcmp( _szName, "TexBenchSetTextureSize") == 0 )          GLI_MDRV_TEXBENCH_vSetTextureSize       = (void (__cdecl *) (GLI_tdstTexture *, char, long) ) _xFct;
#endif

    else if(strcmp( _szName, "TimerGetCounter") == 0 )                  TMR_MDRV_vTimerGetCounter               = (void (__cdecl *) ( short, stTimerCount* ) ) _xFct;
    else if(strcmp( _szName, "TimerTickPerSecond") == 0 )               TMR_MDRV_ulTimerTickPerSecond           = (unsigned long (__cdecl *) ( short ) ) _xFct;
    else
        return 0;
 
    return 1;
}
