#define MTH_LOW
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
#include "gli_st.h"
#include "GLI_Defn.h"
#include "light_st.h"
#include "PvObj_st.h"
#include "texture.h"
#include "TEX.h"
#include "load.h"
 
#include "TMP.h"
#include "VIG.h"
 
#include "FIL.h"
 
#include "acp_opfi.h"
#include "texture.h"
#include "DLLInter.h"
#include "DLLCaps.h"
#include "TexName.h"
#include "TexArray.h"
#include "TexCompress.h"
 
#include "sna.h"
 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*
extern void             SNA_fn_vRelocateTexturePointer(unsigned long **);
extern void             SNA_fn_xReadOpenGlobalTextureFile(char *szFileName);
extern void             SNA_fn_vCloseGlobalTextureFile();
*/
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
extern unsigned long    GLI_gsCurrentMemoryChannel;
extern long             gs_lNumberOfTextureToCreate;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
/**********************************************************************************************/
/* Name: TEX_GetNameOfBinaryFile() 1.0*/
/* Goal: save the globales variables for fast binarisation*/
/* Code: Philippe Vimont */
/* OPTIMMIZED : No*/
/**********************************************************************************************/
static char gs_ucFilenameForBinaryGlobals[256] = "BINARY_TEX_GLOBALS.BIN";
void TEX_SetNameOfBinaryFile(unsigned char *p_ucBinaryFile)
{
  strcpy( gs_ucFilenameForBinaryGlobals , p_ucBinaryFile ) ;
}
 
void TEX_vInitGlobalForLevel()
{
    /*long i;*/
    
    /* release all the texture's surfaces*/
    GLI_DRV_vUnLoadTextures();
    /* erase the texture's structures of the levels*/
    GLI_fn_vUnallocTextureInChannel( 2 );
}
 
/**********************************************************************************************/
/* Name: TEX_vSaveGlobales() 1.0*/
/* Goal: save the globales variables for fast binarisation*/
/* Code: Philippe Vimont */
/* OPTIMMIZED : No*/
/**********************************************************************************************/
void TEX_vSaveGlobales()
{
  FILE *p_stBinaryFile;
  unsigned long ulNbText, ulNbMaxText;
  long i;
  
  p_stBinaryFile = fopen(gs_ucFilenameForBinaryGlobals,"wb");
  i=0;
  while(gs_aDEFTableOfTextureMemoryChannels[i] == 1) i++;
  ulNbText = GLI_C_lNBMaxOfTextures-i;
  ulNbMaxText = GLI_C_lNBMaxOfTextures;
  fwrite ( &ulNbMaxText ,4 , 1, p_stBinaryFile );
  fwrite ( &ulNbText ,4 , 1, p_stBinaryFile );
  fwrite ( gs_aDEFTableOfTextureAlreadyRead+i , ulNbText , sizeof ( GLI_tdstTexture * ) , p_stBinaryFile );
  fwrite ( &gs_lNumberOfTextureToCreate , 1 , sizeof ( long ) , p_stBinaryFile );
  fwrite ( gs_aDEFTableOfTextureMemoryChannels , ulNbMaxText , sizeof ( long ) , p_stBinaryFile );
  fwrite ( &GLI_gsCurrentMemoryChannel , 1 , sizeof ( long ) , p_stBinaryFile );
  fclose(p_stBinaryFile);
}
 
/**********************************************************************************************/
/* Name: TEX_vLoadGlobales() 1.0*/
/* Goal: Load the globales variables for fast binarisation*/
/* Code: Philippe Vimont */
/* OPTIMMIZED : No*/
/**********************************************************************************************/
void TEX_vLoadGlobales()
{
  FILE *p_stBinaryFile;
  unsigned long ulNbText, ulNbMaxText;
  unsigned long ulDummy;
/*
  char szFile[255];
*/
  ACP_M_OPENFILE(fopen,p_stBinaryFile,NULL,gs_ucFilenameForBinaryGlobals,(gs_ucFilenameForBinaryGlobals,"rb"));
 
  fread ( &ulNbMaxText,4 ,1 ,p_stBinaryFile );
  fread ( &ulNbText,4 ,1 ,p_stBinaryFile );
  fread ( gs_aDEFTableOfTextureAlreadyRead+ulNbMaxText-ulNbText , ulNbText , sizeof ( GLI_tdstTexture * ) , p_stBinaryFile );
  fread ( &gs_lNumberOfTextureToCreate , 1 , sizeof ( long ) , p_stBinaryFile );
  fread ( gs_aDEFTableOfTextureMemoryChannels , ulNbMaxText , sizeof ( long ) , p_stBinaryFile );
  fread ( &GLI_gsCurrentMemoryChannel , 1 , sizeof ( long ) , p_stBinaryFile );
  fclose(p_stBinaryFile);
 
/*
  strcpy(szFile,gs_ucFilenameForBinaryGlobals);
  szFile[strlen(szFile)-3]=0;
  strcat(szFile,"rtt");
  SNA_fn_xReadOpenGlobalTextureFile(szFile);
*/

/* ANNECY OA - 12/07/99 { */
	/* Load dummy datas : for protections */
	SNA_M_LoadUnusedRelocationTable();	
/* END ANNECY OA } */

	SNA_fn_xReadOpenGlobalTextureFile();
  for(ulDummy=ulNbMaxText-ulNbText ; ulDummy<ulNbMaxText ; ulDummy++)
  {
	  SNA_fn_vRelocateTexturePointer(&((unsigned long *)gs_aDEFTableOfTextureAlreadyRead[ulDummy]));
  }
  SNA_fn_vCloseGlobalTextureFile();

/* ANNECY OA - 12/07/99 { */
	/* Load dummy datas : for protections */
	SNA_M_LoadUnusedRelocationTable();	
/* END ANNECY OA } */

}
 
/**********************************************************************************************/
/* Name: TEX_vSaveGlobales() 1.0*/
/* Goal: save the globales variables for fast binarisation*/
/* Code: Philippe Vimont */
/* OPTIMMIZED : No*/
/**********************************************************************************************/
void TEX_vSaveGlobalesFix()
{
  FILE *p_stBinaryFile;
  unsigned long ulNbText, ulNbMaxText;
  long i;
  
  p_stBinaryFile = fopen(gs_ucFilenameForBinaryGlobals,"wb");
  i=0;
  while(gs_aDEFTableOfTextureMemoryChannels [i] == 1) i++;
  ulNbText = i;
  ulNbMaxText = GLI_C_lNBMaxOfTextures;
  fwrite ( &ulNbMaxText ,4 , 1, p_stBinaryFile );
  fwrite ( &ulNbText ,4 , 1, p_stBinaryFile );
  fwrite ( gs_aDEFTableOfTextureAlreadyRead , ulNbText , sizeof ( GLI_tdstTexture * ) , p_stBinaryFile );
  fwrite ( &gs_lNumberOfTextureToCreate , 1 , sizeof ( long ) , p_stBinaryFile );
  fwrite ( gs_aDEFTableOfTextureMemoryChannels , ulNbText , sizeof ( long ) , p_stBinaryFile );
  fwrite ( &GLI_gsCurrentMemoryChannel , 1 , sizeof ( long ) , p_stBinaryFile );
  fclose(p_stBinaryFile);
}
 
/**********************************************************************************************/
/* Name: TEX_vLoadGlobales() 1.0*/
/* Goal: Load the globales variables for fast binarisation*/
/* Code: Philippe Vimont */
/* OPTIMMIZED : No*/
/**********************************************************************************************/
void TEX_vLoadGlobalesFix()
{
  FILE *p_stBinaryFile;
  unsigned long ulNbText, ulNbMaxText;
  unsigned long ulDummy;
//  char szFile[255];
  GLI_fn_vUnallocAllTextures();
 
  ACP_M_OPENFILE(fopen,p_stBinaryFile,NULL,gs_ucFilenameForBinaryGlobals,(gs_ucFilenameForBinaryGlobals,"rb"));
  fread ( &ulNbMaxText,4 ,1 ,p_stBinaryFile );
  fread ( &ulNbText,4 ,1 ,p_stBinaryFile );
  fread ( gs_aDEFTableOfTextureAlreadyRead , ulNbText , sizeof ( GLI_tdstTexture * ) , p_stBinaryFile );
  fread ( &gs_lNumberOfTextureToCreate , 1 , sizeof ( long ) , p_stBinaryFile );
  fread ( gs_aDEFTableOfTextureMemoryChannels , ulNbText , sizeof ( long ) , p_stBinaryFile );
  fread ( &GLI_gsCurrentMemoryChannel , 1 , sizeof ( long ) , p_stBinaryFile );
  fclose(p_stBinaryFile);
/*
  strcpy(szFile,gs_ucFilenameForBinaryGlobals);
  szFile[strlen(szFile)-3]=0;
  strcat(szFile,"rtt");
  SNA_fn_xReadOpenGlobalTextureFile(szFile);
*/

/* ANNECY OA - 12/07/99 { */
	/* Load dummy datas : for protections */
	SNA_M_LoadUnusedRelocationTable();	
/* END ANNECY OA } */

  SNA_fn_xReadOpenGlobalTextureFile();
  for(ulDummy=0 ; ulDummy<ulNbMaxText ; ulDummy++)
  {
	  SNA_fn_vRelocateTexturePointer(&((unsigned long *)gs_aDEFTableOfTextureAlreadyRead[ulDummy]));
  }
  SNA_fn_vCloseGlobalTextureFile();

/* ANNECY OA - 12/07/99 { */
	/* Load dummy datas : for protections */
	SNA_M_LoadUnusedRelocationTable();	
/* END ANNECY OA } */

}
