/*=========================================================================
*
* Textures.c	- Textures functions 
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "Textures.h"

#include "conventi.h"
#include "system.h"
#include "print.h"

#include "Material.h"

#include "SaveModifLst.h"


//--------------------------------------------------------------------



/****************************************************************************
*		Description:	copy all the textures from source directory into destination directory
*
*		Parameters:		sRawData	: raw directory
*						sGameData	: game directory
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vComputeAllTextures (char *sRawData, char *sGameData)
{
  xString			sDirectory;
  HANDLE			hHandle;
  WIN32_FIND_DATA	stFindFileData;

  // save the current directory
  GetCurrentDirectory(256, sDirectory);
  SetCurrentDirectory(sRawData);

  // search all the anim's directories
  memset(&stFindFileData,0,sizeof(stFindFileData));
  hHandle = FindFirstFile("World/Graphics/Textures/*.*", &stFindFileData);
  if( hHandle == INVALID_HANDLE_VALUE)
  {
    SetCurrentDirectory(sDirectory);
    printf("\nNo textures in %s\n", sRawData);
    return;
  }
  do if(stFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  {
    if(strlen(stFindFileData.cFileName) == 0) break;
    if(strcmp(stFindFileData.cFileName, ".") == 0) continue;
    if(strcmp(stFindFileData.cFileName, "..") == 0) continue;

    MLT_vComputeOneTexture(stFindFileData.cFileName, sRawData, sGameData);
  }
  while(FindNextFile(hHandle, &stFindFileData));
  FindClose(hHandle);

  SetCurrentDirectory(sDirectory);
}

/****************************************************************************
*		Description:	copy all the textures of the given sub-directory
*
*		Parameters:		TextureName	: name of sub-directory
*						sRawData	: raw directory
*						sGameData	: game directory
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vComputeOneTexture (char *TextureName, char *sRawData, char *sGameData)
{
  xString sRawTexture, sGameTexture;

  // save the name of the texture
  sprintf(g_sFileIn, "%s", TextureName);
  // save the name of the texture's directory
  sprintf(g_sDirectorySave, "");

  MLT_vOutput( C_ComRes_cTitleLine, "\nTexture %s", g_sFileIn);

  sprintf(sRawTexture, "%s\\World\\Graphics\\Textures\\%s", sRawData, g_sFileIn);
  sprintf(sGameTexture, "%s\\World\\Graphics\\Textures\\%s", sGameData, g_sFileIn);
  CreateDirectory(sGameTexture, NULL);

  MLT_vOutput( C_ComRes_cNormalLine, "\n\tCopy...");

  if (g_bStandardConfig)
	  MLT_vCopyFiles("*.tga", sRawTexture, sGameTexture);
  else
  {
	  xString sTexture;
	  sprintf(sTexture, "%s\\%s", g_szTextures, g_sFileIn);
	  MLT_vCopyFiles("*.tga", sTexture, sGameTexture);
  }

  MLT_vUpdatePathInModifFile(sGameTexture);

  MLT_vOutput( C_ComRes_cResultLine, "\nTexture %s converted\n", g_sFileIn);
}

/****************************************************************************
*		Description:	serach the texture file in the given directory
*
*		Parameters:		szParams	: name of texture file
*						sDir		: directory to search
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSearchGameData (char *szParams, xString sDir)
{
	xString sCurrentDir, sDir1;

	// build complete file name for material
	switch (g_iData)
	{	
		case 0:
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameCommon, g_sFileMaterial);
				break;
		case 1:
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
				break;
		case 2:
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
				break;
	}

    // change directory
	GetCurrentDirectory(256, sCurrentDir);
    SetCurrentDirectory(sDir);
    
	// search file in given directory
	if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
    {	
		// if the texture is not found, search in texture directory
		switch (g_iData)
		{	case 0:
				sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameCommon);
				break;
			case 1:
				sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific1);
				break;
			case 2:
				sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific2);
				break;
		}
        SetCurrentDirectory(sDir);
        
		if (!MLT_bFindFile(sDir, szParams)) 
		{
		  // if the texture is not found, search in other game directories
		  switch (g_iData)
		  {	
			// initial directory was GameData
		    case 0:
				// search GameSpecific1
				if (strcmp(g_sGameSpecific1, "")) 
				{	
					sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
					SetCurrentDirectory(sDir);
					// search given directory
					if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
					{
						sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific1);
						SetCurrentDirectory(sDir);
						// search texture directory
						if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
						{
							// search GameSpecific2
							if (strcmp(g_sGameSpecific2, ""))
							{	
								// search given directory
								sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
								SetCurrentDirectory(sDir);
								if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
								{
									sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific2);
									SetCurrentDirectory(sDir);
									// search texture directory
									if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
										MLT_vSetTextureFile(g_sFileMaterial, szParams, sDir1, sDir);
								}
							}	
							else
								MLT_vSetTextureFile(g_sFileMaterial, szParams, sDir1, sDir);
						}
					}	
				}
				else
					MLT_vSetTextureFile(g_sFileMaterial, szParams, sDir1, sDir);
				break;

			// initial directory was GameSpecific1
			case 1:
				// search GameData
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameCommon, g_sFileMaterial);
				SetCurrentDirectory(sDir);
				// search given directory
				if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
				{
					sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameCommon);
					SetCurrentDirectory(sDir);
					// search texture directory
					if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
					{
						// search GameSpecific2
						if (strcmp(g_sGameSpecific2, ""))
						{	
							sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
							SetCurrentDirectory(sDir);
							// search given directory
							if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
							{
								sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific2);
								SetCurrentDirectory(sDir);
								// search texture directory
								if (!MLT_bFindFile(sDir, szParams)) 
									MLT_vSetTextureFile(g_sFileMaterial, szParams, sDir1, sDir);
							}
						}	
						else
							MLT_vSetTextureFile(g_sFileMaterial, szParams, sDir1, sDir);
					}
				}
				break;

			// initial directory was GameSpecific1
			case 2: 
				// search GameData
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameCommon, g_sFileMaterial);
				SetCurrentDirectory(sDir);
				// search given directory
				if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
				{
					sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameCommon);
					SetCurrentDirectory(sDir);
					// search texture directory
					if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
					{
						// search GameSpecific1
						if (strcmp(g_sGameSpecific1, ""))
						{	
							sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
							SetCurrentDirectory(sDir);
							// search given directory
							if (!MLT_bFindFile(sDir, MLT_p_cGetFileNameWithoutPath(szParams))) 
							{
								sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific1);
								SetCurrentDirectory(sDir);
								// search texture directory
								if (!MLT_bFindFile(sDir, szParams)) 
									MLT_vSetTextureFile(g_sFileMaterial, szParams, sDir1, sDir);
							}
						}	
						else
							MLT_vSetTextureFile(g_sFileMaterial, szParams, sDir1, sDir);
					}
				}
				break;
		  }
		 }
	  }
      // restore directory
	  SetCurrentDirectory(sCurrentDir);
}

/****************************************************************************
*		Description:	copy texture form RawData to GameData
*
*		Parameters:		szParams	: name of texture file
*						sDir		: directory to search
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vCopyTexturesFromRaw (char *szParams, xString sDir)
{
    xString  sCurrentDir;

	if (g_bCopyTextures)
	{
	  if (g_bStandardConfig)
	  {
		// get complete file name
		switch (g_iRaw)
		{	case 0:
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sRawCommon, g_sFileMaterial);
				break;
			case 1:
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sRawSpecific1, g_sFileMaterial);
				break;
			case 2:
				sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sRawSpecific2, g_sFileMaterial);
				break;
		}
	  }
	  else
		sprintf(sDir, "%s\\%s", g_szTextures, g_sFileMaterial);
		
		// change directory
		GetCurrentDirectory(256, sCurrentDir);
		SetCurrentDirectory(sDir);

		// search given directory
		if (!MLT_bFindFile(sDir, szParams)) 
		{
		  if (g_bStandardConfig)
		  {
			// build path for texture directory
			switch (g_iRaw)
			{	
				case 0:
					sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sRawCommon);
					break;
				case 1:
					sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sRawSpecific1);
					break;
				case 2:
					sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sRawSpecific2);
					break;
			}
		  }
		  else
			sprintf(sDir, "%s", g_szTextures);

			SetCurrentDirectory(sDir);
			
			// search texture directory
			if (MLT_bFindFile(sDir, szParams)) 
			{	
				// copy texture in appropriate directory
				switch (g_iData)
				{	
					case 0:
						MLT_vCopyOneTexture(szParams, sDir, g_sGameCommon);
						break;
					case 1: 
						MLT_vCopyOneTexture(szParams, sDir, g_sGameSpecific1);
						break;
					case 2: 
						MLT_vCopyOneTexture(szParams, sDir, g_sGameSpecific2);
						break;
				}
			}
			// if texture was not found, write a line in LOG file
			else
			{
			    xString sComment;
				// build exact name of the texture
				switch (g_iData)
				{	
					case 0:
						sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameCommon, g_sFileMaterial, szParams);
						sprintf(sDir, "%s\\%s", g_sGameTextureDirectory, g_sFileMaterial);
						break;
					case 1: 
						sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameSpecific1, g_sFileMaterial, szParams);
						sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
						break;
					case 2: 
						sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameSpecific2, g_sFileMaterial, szParams);
						sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
						break;
				}
				// write a line in LOG file and display a warning
				MLT_vWriteInTextureFile(sDir, sComment);
//				MLT_vOutput( C_ComRes_cWarningLine, "\nWarning : Can't find a texture file %s", szParams);
			}
		}
		// if texture was found, copy it in appropriate directory
		else
		{	
			switch (g_iData)
			{	
				case 0:
					MLT_vCopyOneTexture(szParams, sDir, g_sGameCommon);
					break;
				case 1: 
					MLT_vCopyOneTexture(szParams, sDir, g_sGameSpecific1);
					break;
				case 2: 
					MLT_vCopyOneTexture(szParams, sDir, g_sGameSpecific2);
					break;
			}
		}
        // restore directory
		SetCurrentDirectory(sCurrentDir);
	}
}

/****************************************************************************
*		Description:	search copy texture form RawData to GameData
*
*		Parameters:		szParams	: name of texture file
*						sDir		: directory to search
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vCopyTextureIfFound (char * szParams, xString sDir)
{
  xString	sCurrentDir;

  // build file name
  switch (g_iData)
  {	
	case 0:
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameCommon, g_sFileMaterial);
			break;
	case 1:
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
			break;
	case 2:
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
			break;
  }
  // change directory
  GetCurrentDirectory(256, sCurrentDir);
  SetCurrentDirectory(sDir);
  
  // check if the texture already exist in the appropriate destination directory
  if (MLT_bFindFile(sDir, szParams)) 
  {	
	  SetCurrentDirectory(sCurrentDir);
	  return;
  }

  // if the texture is not found, continue with searching
  switch (g_iData)
  {	
	case 0:
		sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameCommon);
		break;
	case 1:
		sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific1);
		break;
	case 2:
		sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific2);
		break;
  }
  SetCurrentDirectory(sDir);
  // check if the texture already exist in the texture destination directory
  if (MLT_bFindFile(sDir, szParams)) 
  {	
	  SetCurrentDirectory(sCurrentDir);
	  return;
  }

  // if the texture is not found, continue with searching
  switch (g_iData)	
  {	
	case 0:
		// search in GameSpecific1
		if (strcmp(g_sGameSpecific1, "")) 
		{	
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
			SetCurrentDirectory(sDir);
			if (MLT_bFindFile(sDir, szParams)) 
			{	
				SetCurrentDirectory(sCurrentDir);
				return;
			}
			else
			{
				sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific1);
				SetCurrentDirectory(sDir);
				if (MLT_bFindFile(sDir, szParams)) 
				{	
					SetCurrentDirectory(sCurrentDir);
					return;
				}
			}
		}
		// search in GameSpecific2
		if (strcmp(g_sGameSpecific2, ""))
		{	
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
			SetCurrentDirectory(sDir);
			if (MLT_bFindFile(sDir, szParams)) 
			{	
				SetCurrentDirectory(sCurrentDir);
				return;
			}
			else
			{
				sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific2);
				SetCurrentDirectory(sDir);
				if (MLT_bFindFile(sDir, szParams)) 
				{	
					SetCurrentDirectory(sCurrentDir);
					return;
				}
			}
		}
		// texture was not found
		MLT_vWriteTextureIfFound(szParams);
		MLT_vOutput( C_ComRes_cWarningLine, "\nWarning : Can't find a texture file %s", szParams);
		break;

	case 1:
		// search in GameData
		sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameCommon, g_sFileMaterial);
		SetCurrentDirectory(sDir);
		if (MLT_bFindFile(sDir, szParams)) 
		{	
			SetCurrentDirectory(sCurrentDir);
			return;
		}
		else
		{
			sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameCommon);
			SetCurrentDirectory(sDir);
			if (MLT_bFindFile(sDir, szParams)) 
			{	
				SetCurrentDirectory(sCurrentDir);
				return;
			}
		}
		// search in GameSpecific2
		if (strcmp(g_sGameSpecific2, ""))
		{	
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
			SetCurrentDirectory(sDir);
			if (MLT_bFindFile(sDir, szParams)) // texture dans le bon repertoire de GameSpecific2
			{	
				SetCurrentDirectory(sCurrentDir);
				return;
			}
			else
			{
				sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific2);
				SetCurrentDirectory(sDir);
				if (MLT_bFindFile(sDir, szParams)) // texture dans un autre repertoire de GameDataN64
				{	
					SetCurrentDirectory(sCurrentDir);
					return;
				}
			}
		}
		// texture was not found
		MLT_vWriteTextureIfFound(szParams);
		MLT_vOutput( C_ComRes_cWarningLine, "\nWarning : Can't find a texture file %s", szParams);
		break;

	case 2: 
		// search in GameData
		sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameCommon, g_sFileMaterial);
		SetCurrentDirectory(sDir);
		if (MLT_bFindFile(sDir, szParams)) 
		{	
			SetCurrentDirectory(sCurrentDir);
			return;
		}
		else
		{
			sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameCommon);
			SetCurrentDirectory(sDir);
			if (MLT_bFindFile(sDir, szParams)) 
			{	
				SetCurrentDirectory(sCurrentDir);
				return;
			}
		}
		// search in GameSpecific1
		if (strcmp(g_sGameSpecific1, "")) 
		{	
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
			SetCurrentDirectory(sDir);
			if (MLT_bFindFile(sDir, szParams)) 
			{	
				SetCurrentDirectory(sCurrentDir);
				return;
			}
			else
			{
				sprintf(sDir, "%s\\World\\Graphics\\Textures", g_sGameSpecific1);
				SetCurrentDirectory(sDir);
				if (MLT_bFindFile(sDir, szParams)) 
				{	
					SetCurrentDirectory(sCurrentDir);
					return;
				}
			}
		}
		// texture was not found
		MLT_vWriteTextureIfFound(szParams);
		MLT_vOutput( C_ComRes_cWarningLine, "\nWarning : Can't find a texture file %s", szParams);
		break;
	}
}


/****************************************************************************
*		Description:	write texture name in file
*
*		Parameters:		szParams	: name of texture 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vWriteTextureIfFound (char *szParams)
{
  xString sComment, sDir;

  switch (g_iData)
  {	
	case 0:
			sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameCommon, g_sFileMaterial, szParams);
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameCommon, g_sFileMaterial);
			MLT_vWriteInTextureFile(sDir, sComment);
			break;
	case 1:
			sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameSpecific1, g_sFileMaterial, szParams);
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific1, g_sFileMaterial);
			MLT_vWriteInTextureFile(sDir, sComment);
			break;
	case 2:
			sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameSpecific2, g_sFileMaterial, szParams);
			sprintf(sDir, "%s\\World\\Graphics\\Textures\\%s", g_sGameSpecific2, g_sFileMaterial);
			MLT_vWriteInTextureFile(sDir, sComment);
			break;
  }
}									

/****************************************************************************
*		Description:	set texture name in material file
*
*		Parameters:		sFileMaterial	: name of file material 
*						szParams		: name of texture 
*						sDir1			: name of specific1 directory 
*						sDir			: name of specific2 directory 
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSetTextureFile(xString sFileMaterial, char* szParams, xString sDir1, xString sDir)
{
    xString sComment;

	switch (g_iData)
	{	
		case 0:
				sprintf(sComment, "%s\\%s\\%s\n", g_sGameTextureDirectory, sFileMaterial, szParams);
				MLT_vWriteInTextureFile(g_sGameTextureDirectory, sComment);
				break;
		case 1:
				sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameSpecific1, sFileMaterial, szParams);
				sprintf(sDir1,"%s\\World\\Graphics\\Textures",g_sGameSpecific1  );
				MLT_vWriteInTextureFile(sDir1, sComment);
				break;
		case 2:
				sprintf(sComment, "%s\\World\\Graphics\\Textures\\%s\\%s\n", g_sGameSpecific2, sFileMaterial, szParams);
				MLT_vWriteInTextureFile(sDir, sComment);
				break;
	}

}



/****************************************************************************
*		Description:	load texture section in TEX file
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadTextureSection(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstMaterial *p_hMaterial;

  int i;

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      break;

    case SCR_EA_Anl_Entry:
      SCR_M_RdL0_GetContextLong(-1,0,MLT_tdstMaterial*,p_hMaterial);

// multitextures
	if (strstr(p_hMaterial->sName, "Material")) 
	{
       SCR_M_RdL0_SetSectionLong(0,0,p_hMaterial->sTexture[p_hMaterial->lTexture]);

      // load Texture
      if (strcmp(szAction,M_TEXActionLoadTexture)==0)
      {
        xString	sDir;
        char	*sTemp;


		MLT_vSearchGameData(szParams[0], sDir);

        sTemp = strstr(sDir, "Textures\\");
        if(sTemp == NULL)
        {
          sprintf(sDir, "");
        }
        else
        {
          sprintf(sDir, "%s\\", sTemp+9);
        }

        sprintf(p_hMaterial->sTexture[p_hMaterial->lTexture], "%s%s", sDir, szParams[0]);
      }

      if (strcmp(szAction,M_TEXActionQuality)==0)
      {
        sprintf(p_hMaterial->sQuality[p_hMaterial->lTexture], "%s", szParams[0]);
	  }
      if (strcmp(szAction,M_TEXActionChromakey)==0)
	  {
        sprintf(p_hMaterial->sEnable[p_hMaterial->lTexture], "%s", szParams[0]);
        sprintf(p_hMaterial->sOn[p_hMaterial->lTexture], "%s", szParams[1]);
		for (i=0; i<4; i++)
			p_hMaterial->iVal[i][p_hMaterial->lTexture] = atoi(szParams[2+i]);
	  }
      if (strcmp(szAction,M_TEXActionMipMapping)==0)
      {
		// check validity
		if (stricmp(szParams[0], "ON") && stricmp(szParams[0], "OFF") && stricmp(szParams[0], "BILINEAR")  && stricmp(szParams[0], "TRILINEAR"))
			sprintf(p_hMaterial->sMipMapping, "ON");
		else
			sprintf(p_hMaterial->sMipMapping, "%s", szParams[0]);
      }
      if (strcmp(szAction,M_TEXActionTiling)==0)
      {
        sprintf(p_hMaterial->sTilingFirst, "%s", szParams[0]);
        sprintf(p_hMaterial->sTilingSecond, "%s", szParams[1]);
      }

// multitextures
      if (strcmp(szAction,M_TEXActionLinarMipMapping)==0)
      {
        sprintf(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sLinarMipMapping, "%s", szParams[0]);
	  }

      if (strcmp(szAction,M_TEXActionCorrection)==0)
      {
        sprintf(p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].sPerspectiveCorrection, "%s", szParams[0]);
	  }

      if (strcmp(szAction,M_TEXActionBlendToColor)==0)
      {
		for (i=0; i<3; i++)
			p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].fBlendToColor[i] = (float)atof(szParams[i]);
	  }

      if (strcmp(szAction,M_TEXActionBlendToAlpha)==0)
      {
		p_hMaterial->stMultiTexture[p_hMaterial->lMultiTexture].fBlendToAlpha = (float)atof(szParams[0]);
	  }
	}
      break;

    case SCR_EA_Anl_EndSection:
      break;
        }
  return  SCR_ERV_Anl_NormalReturn;
}

