/*
 =======================================================================================
  Name        : TexName.c
  Author      : GLIGLOU corporation
  Description : Fonction for texture name analysis / storage
 =======================================================================================
 */
 
#define MTH_LOW
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
#include "PvObj_st.h"
#include "texture.h"
#include "TEX.h"
#include "dpt.h"

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  
#ifdef __cplusplus
extern "C" 
{
#endif
 
 
/*
 =======================================================================================
    Analyse name of texture 
 =======================================================================================
 */
 
/*
 ----------------------------------------------------------------------------------------
  Description   : find on the name of texture if it cycling on X,Y or XY
 ----------------------------------------------------------------------------------------
 */
long TEX_fnl_GetCyclingTextNameKey( char *d_ucFileName, char **_ppc_BeforeAnalysed )
{
    long            lReturnValue = 0;
 
    if ( *((*_ppc_BeforeAnalysed) - 2) == 't' )
    {
        if ( *((*_ppc_BeforeAnalysed) - 1) == 'x' ) 
        {
            lReturnValue = GLI_C_lCylingU;
            *_ppc_BeforeAnalysed -= 2;
        }
        else if ( *((*_ppc_BeforeAnalysed) - 1) == 'y' )
        {
            lReturnValue = GLI_C_lCylingV;
            *_ppc_BeforeAnalysed -= 2;
        }
    }
    else if ( *((*_ppc_BeforeAnalysed) - 3) == 't' )
    {
        if ( strncmp( ((*_ppc_BeforeAnalysed) - 2), "xy", 2) == 0)
        {
            lReturnValue = GLI_C_lCylingUV;
            *_ppc_BeforeAnalysed -= 3;
        }
    }

    if (lReturnValue == 0)
    {
        if (strstr( d_ucFileName, "_txy") != NULL) lReturnValue = GLI_C_lCylingUV;
        if (strstr( d_ucFileName, "_tx" ) != NULL) lReturnValue = GLI_C_lCylingU; 
        if (strstr( d_ucFileName, "_ty" ) != NULL) lReturnValue = GLI_C_lCylingV;
    }

	if (strstr( d_ucFileName, "_mxy" ) != NULL )  lReturnValue |= GLI_C_lMirrorUV;
	if (strstr( d_ucFileName, "_mx" )  != NULL )  lReturnValue |= GLI_C_lMirrorV;
	if (strstr( d_ucFileName, "_my" )  != NULL )  lReturnValue |= GLI_C_lMirrorU;	
    
    return lReturnValue;
}
 
/*
 ----------------------------------------------------------------------------------------
  Description   : find a key in texture name. return 1 if Yes 
 ----------------------------------------------------------------------------------------
 */
long TEX_fnl_GetTextureKey( char *_szFileName, char *_szKey )
{
	return (strstr(_szFileName, _szKey) != NULL) ? 1 : 0;
}
 
 
/*
 ----------------------------------------------------------------------------------------
  Description   : find "NZW" key in texture name. return 1 if Yes 
 ----------------------------------------------------------------------------------------
 */
long TEX_fnl_NoZWriteTextNameKey( char *d_ucFileName, char **_ppc_BeforeAnalysed )
{
    if (strncmp((*_ppc_BeforeAnalysed - 3),"_ad", 3) == 0) 
    {
        *_ppc_BeforeAnalysed -= 3;
    }
    
    if (strncmp( (*_ppc_BeforeAnalysed - 3), "nzw", 3) == 0) 
    {
        *_ppc_BeforeAnalysed -= 3;
        return 1;
    }

    if (strstr(d_ucFileName,"_nzw") != NULL)
	    return 1;
 
    return 0;
}
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Test if there's NZ code in texture name
 ----------------------------------------------------------------------------------------
 */
ACP_tdxBool	TEX_fnb_TestIfNonZero(char *d_ucFileName)
{
	char			*cRes;
	
	cRes = strstr(d_ucFileName, "_nz" );
	if (cRes == NULL)
		return FALSE;
	
	if ( cRes[3] != 'w' ) 
        return TRUE;

    if ( strstr( (cRes + 3), "_nz" ) == NULL)
        return  FALSE;
		
    return TRUE;
}	
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Main function : analyse name and set some data
 ----------------------------------------------------------------------------------------
 */
long GLI_fn_lAnalyseTextureName( char *_szFileName, long *_p_lTextureCaps, unsigned char *_p_ucCyclingMode )
{
    char *pc_BeforeAnalysed;
    char *pc_ShortFileName;

    /* swap dir names to analyse only texture name */
    pc_ShortFileName = strrchr( _szFileName, '\\' );
    if (pc_ShortFileName == NULL)
        pc_ShortFileName = _szFileName;
    else
        pc_ShortFileName++;

    /* search dos extension */
    pc_BeforeAnalysed = strrchr( pc_ShortFileName, '.' );
    if (pc_BeforeAnalysed == NULL)
        return 0;
 
    if (strcmp(pc_BeforeAnalysed + 1, "bmp" ) == 0)
        *_p_lTextureCaps |= GLI_C_lBMPTexture;
    else if (strcmp(pc_BeforeAnalysed + 1, "tga" ) == 0) 
        *_p_lTextureCaps |= GLI_C_lTGATexture;
    else
        return 0;
 
    /* search nz key */
    if ( strncmp(pc_BeforeAnalysed - 2, "nz", 2) == 0 )
    {
        pc_BeforeAnalysed -= 2;
        *_p_lTextureCaps |= GLI_C_lNZTexture + GLI_C_lNZFilteredTexture;
    }
    else if ( TEX_fnb_TestIfNonZero( _szFileName ) )
    {
        *_p_lTextureCaps |= GLI_C_lNZTexture + GLI_C_lNZFilteredTexture;
    }

    *_p_ucCyclingMode = (unsigned char ) TEX_fnl_GetCyclingTextNameKey( pc_ShortFileName, &pc_BeforeAnalysed );
    
    if ( TEX_fnl_GetTextureKey( pc_ShortFileName, "_ad" ) )
        *_p_lTextureCaps |= GLI_C_lAddTransparencyTexture;
	
    if ( TEX_fnl_GetTextureKey( pc_ShortFileName, "_aaa" ) )
		*_p_lTextureCaps |= GLI_C_lAAATexture;
    
    if ( TEX_fnl_GetTextureKey( pc_ShortFileName, "_maa" ) )
        *_p_lTextureCaps |= GLI_C_lMAATexture;
 
    if ( TEX_fnl_NoZWriteTextNameKey( pc_ShortFileName, &pc_BeforeAnalysed ) )
        *_p_lTextureCaps |= GLI_C_lNoZBufferWriteTexture;
 
    return 1;
}
 
/*
 =======================================================================================
    Manage list of textures
 =======================================================================================
 */
 
/*
 ----------------------------------------------------------------------------------------
  Description   : harmonize texture name
 ----------------------------------------------------------------------------------------
 */
void TEX_fnv_FixTextureName(char *_pszOut, char *_pszIn)
{
	/*remove the base path if it does not start from the game data's directory*/
	char *pszGameDataPath = fn_szGetGameDataPath();
	char *pszBase  = strstr(_pszIn, pszGameDataPath);
	short wOffset = 0;
	
    if ( pszBase != NULL )
		wOffset = strlen(pszGameDataPath) + (pszBase - _pszIn) + 1;
	
    /*copy the file name, not including the game data path*/
	strcpy(_pszOut, _pszIn + wOffset);
	/*now replace '/' by '\'*/
	do
	{
		if ( *_pszOut == '/' )
			*_pszOut = '\\';
		else
			*_pszOut = tolower(*_pszOut);
	} while ( *(++ _pszOut) );
}
 
#ifdef __cplusplus
}
#endif
 
 
