#include <kamui.h>
/* GET*/
#define GetX(a) a.fX
#define GetY(a) a.fY
#define GetZ(a) a.fZ
#define GetPackedColor(a) *(unsigned long *)&a.uBaseRGB
#define GetU(a) a.fU
#define GetV(a) a.fV
/* SET*/
#define SetX(a,b) a.fX = b
#define SetY(a,b) a.fY = b
#define SetZ(a,b) a.fZ = b
#define SetPackedColor(a,b) *(unsigned long *)&a.uBaseRGB = b
#define SetPackedSpecular(a,b)
#define SetU(a,b) a.fU = b
#define SetV(a,b) a.fV = b
#define SetU_Cpy(a,b) *(unsigned long *)&a.fU = *(unsigned long *)&b
#define SetV_Cpy(a,b) *(unsigned long *)&a.fV = *(unsigned long *)&b
 
/* GLOBALS*/
#define GLI_tdScreenVertx KMVERTEX_03 
extern  KMVERTEXBUFFDESC	VertexBufferDesc;
KMVERTEX_03 CurrentDestXYZ[4];
 
/* Special functions*/
#define GLI_M_RestoreCW()\
{\
    CurrentDestXYZ[0].ParamControlWord	= \
        CurrentDestXYZ[1].ParamControlWord	= \
        CurrentDestXYZ[3].ParamControlWord	= KM_VERTEXPARAM_NORMAL;\
    CurrentDestXYZ[2].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;\
}\
 
static void GLI_BeforeDraw()
{
    GLI_M_RestoreCW();
};
 
 
#define GLI_AfterDraw()
 
static void GLI_DrawQuad(void)
{
    kmSetVertex(&VertexBufferDesc,&CurrentDestXYZ[0],KM_VERTEXTYPE_03,sizeof(CurrentDestXYZ[1]));
    kmSetVertex(&VertexBufferDesc,&CurrentDestXYZ[1],KM_VERTEXTYPE_03,sizeof(CurrentDestXYZ[1]));
    kmSetVertex(&VertexBufferDesc,&CurrentDestXYZ[3],KM_VERTEXTYPE_03,sizeof(CurrentDestXYZ[1]));
    kmSetVertex(&VertexBufferDesc,&CurrentDestXYZ[2],KM_VERTEXTYPE_03,sizeof(CurrentDestXYZ[1]));
}
/* Draw Triangle*/
GLI_FuncNodeBegin(GLI_vDrawTriangle)
{
    kmSetVertex(&VertexBufferDesc,&CurrentDestXYZ[0],KM_VERTEXTYPE_03,3*sizeof(CurrentDestXYZ[0]));
}
 
void GLI_vDrawZSortedTriangle(void *p_3Point)
{
}
void GLI_vDrawZSortedQuad(void *p_3Point)
{
}
 
void GLI_vAddDrawFunc()
{
    GLI_M_AddFunc(GLI_vDrawTriangle);
}
 
 
/*    GLI_BIG_GLOBALS -> lClippingModeMask*/
void GLI_ReComputeClippingMask()
{
    *(float *)&fX_CMP_Optimize = 640.0f;
    *(float *)&fY_CMP_Optimize = 480.0f;
    *(float *)&fXMin_CLIP_Optimize = 100.0f;
    *(float *)&fYMin_CLIP_Optimize = 50.0f;
    *(float *)&fXMax_CLIP_Optimize = 540.0f;
    *(float *)&fYMax_CLIP_Optimize = 430.0f;
}
 
 
 
/**********************************************************************************************/
/* Name: GLI_DRV_vSendSpriteToClip*/
/* Goal: draw a sprite*/
/* Code: Philippe Vimont */
/* OPTIMMIZED : No*/
/**********************************************************************************************/
    
void GLI_DRV_vSendSpriteToClip
(
 GLI_tdstAligned2DVector *a4_st2DVertex ,
 MTH_tdxReal xZ ,
 GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT)
{
    GLI_BeforeDraw();
    GLI_M_InitSprite();
    SetU(CurrentDestXYZ[0],0.0f);
    SetV(CurrentDestXYZ[0],1.0f);
    SetU(CurrentDestXYZ[1],1.0f);
    SetV(CurrentDestXYZ[1],1.0f);
    SetU(CurrentDestXYZ[2],1.0f);
    SetV(CurrentDestXYZ[2],0.0f);
    SetU(CurrentDestXYZ[3],0.0f);
    SetV(CurrentDestXYZ[3],0.0f);
    GLI_DrawQuad();
    GLI_AfterDraw();
}
 
/**********************************************************************************************/
/* Name: GLI_DRV_vSendSpriteToClipWithUV 1.0*/
/* Goal: */
/* Code: Philippe Vimont / 1.0*/
/* REM: */
/* OPTIMMIZED : No*/
/**********************************************************************************************/
void GLI_DRV_vSendSpriteToClipWithUV
(
 GLI_tdstAligned2DVector *a4_st2DVertex ,
 MTH_tdxReal *a8_stUVVertex,
 MTH_tdxReal xZ ,
 GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT)
{
    GLI_BeforeDraw();
    GLI_M_InitSprite();
    SetU(CurrentDestXYZ[2],a8_stUVVertex[4]);
    SetV(CurrentDestXYZ[2],a8_stUVVertex[4+1]);
    SetU(CurrentDestXYZ[3],a8_stUVVertex[6]);
    SetV(CurrentDestXYZ[3],a8_stUVVertex[6+1]);
    SetU(CurrentDestXYZ[0],a8_stUVVertex[0]);
    SetV(CurrentDestXYZ[0],a8_stUVVertex[0+1]);
    SetU(CurrentDestXYZ[1],a8_stUVVertex[2]);
    SetV(CurrentDestXYZ[1],a8_stUVVertex[2+1]);
    GLI_DrawQuad();
    GLI_AfterDraw();
}
