/*
Tested with LINT
*/
#include <KAMUI.H>
#include "init_gli.h"
#include "gli_st.h"
#include "GLI_Defn.h"
#include "light_st.h"
#include "PvObj_st.h"
#include "GLI_interface.h"
 
KMVERTEXBUFFDESC	VertexBufferDesc;
KMSURFACEDESC		PrimarySurfaceDesc;		/* Primary frame buffer*/
KMSURFACEDESC		BackSurfaceDesc;		/* Back-buffer frame buffer*/
KMSURFACEDESC       stSpecialTexture;
KMSURFACEDESC       stSpecialTexture20;
KMSURFACEDESC       stSpecialTexture21;
KMVERTEXCONTEXT		stRenderingContex;
 
extern short TEXTURETWIDDLED[];
 
extern volatile long gsbIsRender;
#define GLI_C_SiSeOFVertexBuffer (512288)
extern GLI_tdstInternalGlobalValuesFor3dEngine *GLI_BIG_GLOBALS;
 
 
#ifdef __cplusplus
extern "C" 
{
#endif
    
    void GLI_vEndOfRenderingCallback(void *pnull)
    {
        gsbIsRender &= 0xFFFFFFFF - 1;
    }
    
    void GLI_vEndOfVertexTransfertCallback(void *pnull)
    {
        gsbIsRender &= 0xFFFFFFFF - 2;
    }
    
    void GLI_vWaitEndOfVertexTransfert()
    {
        while (gsbIsRender & 2) {};
    }
    
    void GLI_vWaitEndOfRendering()
    {
        while (gsbIsRender & 1) {};
    }
    
    void GLI_vEndOfVSyncCallback(void *pnull)
    {
        gsbIsRender &= ~4;
    }
    
    void GLI_vWaitEndOfVSync()
    {
        gsbIsRender |= 4;
        while (gsbIsRender & 4) {};
    }
    
    
    
    /* Global initialisation.*/
    void GLI_DRV_xInitDriver( HWND _hWnd, BOOL _bFullScreen, long _lWidth, long _lHeight, long _lBpp )
    {
        kmInitDevice(KM_VGA);
        kmSetDisplayMode(	KM_DSPMODE_VGA,KM_DSPBPP_RGB565,FALSE,TRUE);
        kmCreateFrameBufferSurface(&PrimarySurfaceDesc,640,480,FALSE,FALSE);
        kmCreateFrameBufferSurface(&BackSurfaceDesc,640,480,FALSE,FALSE);
        kmCreateVertexBuffer(&VertexBufferDesc, GLI_C_SiSeOFVertexBuffer , 0 ,  GLI_C_SiSeOFVertexBuffer , 0);
        kmActivateFrameBuffer(	&PrimarySurfaceDesc,&BackSurfaceDesc,FALSE,TRUE);
        kmSetStrideWidth(512);
        kmCreateTextureSurface(&stSpecialTexture,512,512, KM_TEXTURE_STRIDE | KM_TEXTURE_RGB565);
#define WATERSIZE 256L
        kmCreateTextureSurface(&stSpecialTexture20,WATERSIZE ,WATERSIZE, KM_TEXTURE_TWIDDLED | KM_TEXTURE_ARGB4444);
        kmSetEORCallback(GLI_vEndOfRenderingCallback,NULL);
        kmSetEndOfVertexCallback(GLI_vEndOfVertexTransfertCallback,NULL);
        kmSetVSyncCallback(GLI_vEndOfVSyncCallback,NULL);
        memset( &stRenderingContex, 0, sizeof(stRenderingContex));
    }
    
    /* Temporary function.*/
    /*
    long GLI_INTERFACE_FUNC(GLI_DRV_lWhatIsGli)(void)
    {
        return GLI_C_VersionGlide;
    }
    */
    
    
    void GLI_DRV_vCloseDriver ()
    {
    }
    
    /* Return version number.*/
    /*
    long GLI_INTERFACE_FUNC(GLI_lGetNumOfVersion)(void)
    {
        return 0;//g_stCurLLInterface.iMode;
    }
    */
    
    /* Return a pointer on the text version of GLI engine.*/
    /*unsigned char *GLI_INTERFACE_FUNC(GLI_ucGetTextOfVersion)(void)
    {
        return NULL;//g_stCurLLInterface.ucTextMode;
    }
    */
    
    /* Erase a rectangular region of Z buffer.*/
    void GLI_INTERFACE_FUNC(GLI_DRV_vClearZBufferRegion)(long lXStart,long lXEnd,long lYStart,long lYEnd)
    {
        /*  g_stCurLLInterface.p_fnClearZBufferRegion(lXStart,lXEnd,lYStart,lYEnd);*/
        if (!bDrawMirrorSymetric)
            GLI_BIG_GLOBALS->lDisplayMenuIsDetected = 1;
    }
    void GLI_INTERFACE_FUNC(GLI_vBeginDrawingTexturedMirrors)(void)
    {
        GLI_BIG_GLOBALS->bDrawMirrorSymetric = 1;
    }
    void GLI_INTERFACE_FUNC(GLI_vBeginDrawingTexturedMirrorsSurfaces)()
    {
        /*kmSetFramebufferTexture(&TexSurfaceDesc2);*/
        GLI_BIG_GLOBALS->lAMirrorIsDetected = 3;
        /*    GLI_vWaitEndOfRendering();*/
        /*    GLI_vWaitEndOfVSync();*/
        /*    kmRenderTexture(&stSpecialTexture);*/
        /*    GLI_vWaitEndOfRendering();*/
        /*    GLI_vWaitEndOfVSync();*/
        /*    kmFlipFrameBuffer();//  */
        /*    GLI_vWaitEndOfRendering();*/
        /* Now Draw The reversed World . It will be displayed with Frame later.*/
        /*    GLI_vWaitEndOfRendering();*/
    }
    void 
        GLI_INTERFACE_FUNC(GLI_vEndDrawingTexturedMirrors)()
    {
        GLI_BIG_GLOBALS->lAMirrorIsDetected &= 1;
        GLI_BIG_GLOBALS->bDrawMirrorSymetric = 0;
    }
    
    /*extern long GLI_xIsGliInit();*/
    void GLI_INTERFACE_FUNC(GLI_DRV_vFlipDevice)(long lNbFrames)
    {
        if (!GLI_MDRV_xIsGliInit()) 
            return;
 
        GLI_BIG_GLOBALS->lDisplayMenuIsDetected = 0;
        GLI_BIG_GLOBALS->lAMirrorIsDetected = 0;
        gsbIsRender |= 3;
        kmFlipFrameBuffer();/*  */
        kmRender/*Direct*/();
    }
    
    void GLI_INTERFACE_FUNC(GLI_DRV_vClearDevice)(BOOL ZBuffer, BOOL ColorBuffer, unsigned long Color)
    {
    }
    
    
#ifdef __cplusplus
} /*extern "C"*/
#endif
