// **********************************************************************************
// * "l_global_v6.c"																*
// * Written by :	Sébastien Rubens												*
// * Tabulations :	4 char															*
// **********************************************************************************
#define L_GLOBAL_V6_C



// **********************************************************************************
#define	Details



// **********************************************************************************
//																	Included files

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "SCR.h"

#include "a3x_glob.h"

#include "makeanim.h"
#include "l_global_v6.h"


//Mihaela Tancu - Animations
#include "Print.h"
//Mihaela Tancu - Animations

// **********************************************************************************
//																	Globals datas
#define	MaxInStringsTab					10000
#define	MaxInVertexesTab			   100000
#define	MaxInQuaternionsTab			   100000
#define	MaxInA3dGENERALTab				 2000
#define	MaxInChannelsTab				 5000
#define	MaxInFramesTab				   100000
#define	MaxInFramesNTTOTab			   100000
#define	MaxInOnlyFramesTab				20000
#define	MaxInHierarchiesTab				10000
#define	MaxInChannelNamesTab			 2000
#define	MaxInSavedVertexesTab			50000
#define	MaxInSavedQuaternionsTab		50000
#define	MaxInSavedNTTOTab				 5000
#define MaxInMorphTab					80000

#define	MaxInFileA3dAnimationGENERALTab	  500
#define	MaxInFileVertexesTab			10000
#define	MaxInFileQuaternionsTab			10000
#define	MaxInFileHierarchyTab			  500
#define	MaxInFileNTTOTab				 5000
#define	MaxInFileEventTab				 1000
#define	MaxInFileOnlyFrameTab			 1000
#define	MaxInFileChannelTab				  500
#define	MaxInFileFrameTab				10000
#define	MaxInFileKeyFrameTab			 5000
#define	MaxInSndEventIdTab				 5000
#define	MaxInEventTab					10000
#define	MaxInFileEventTab				 1000
#define MaxInFileMorphTab				 5000

typedef char	tdFileName[256];



tdstBinStackInfos	tdstStacks[eStackBinNbMax]=
{	{	0,	0,	MaxInStringsTab			},			// Strings
	{	0,	0,	MaxInVertexesTab		},			// Vertexes
	{	0,	0,	MaxInQuaternionsTab		},			// Quaternions
	{	0,	0,	MaxInA3dGENERALTab		},			// A3dGENERAL
	{	0,	0,	MaxInChannelsTab		},			// Channels
	{	0,	0,	MaxInFramesTab			},			// Frames
	{	0,	0,	MaxInOnlyFramesTab		},			// OnlyFrames
	{	0,	0,	MaxInHierarchiesTab		},			// Hierarchies
	{	0,	0,	MaxInChannelNamesTab	},			// ChannelNames

	{	0,	0,	MaxInSavedVertexesTab	},			// SavedVertexes
	{	0,	0,	MaxInSavedQuaternionsTab},			// SavedQuaternions
	{	0,	0,	MaxInSavedNTTOTab		},			// Saved FrameNumber and TypeOfObject

	{	0,	0,	MaxInFileA3dAnimationGENERALTab	},
	{	0,	0,	MaxInFileVertexesTab			},
	{	0,	0,	MaxInFileQuaternionsTab			},
	{	0,	0,	MaxInFileHierarchyTab			},
	{	0,	0,	MaxInFileNTTOTab				},
	{	0,	0,	MaxInFileOnlyFrameTab			},
	{	0,	0,	MaxInFileKeyFrameTab			},
	{	0,	0,	MaxInFileFrameTab				},
	{	0,	0,	MaxInFileChannelTab				},
	{	0,	0,	MaxInFileEventTab				},
	{	0,	0,	MaxInMorphTab },

	{	0,	0,	MaxInSndEventIdTab				},
	{	0,	0,	MaxInEventTab					}
};



tdFileName					ax_xStrings				[MaxInStringsTab];
tdxVector3					axa3_xVertexes			[MaxInVertexesTab];
tdxQuater4					axa4_xBinQuaternions	[MaxInQuaternionsTab];
tdstBinA3dAnimationGENERAL	ax_tdstA3dGENERAL		[MaxInA3dGENERALTab];
tdstBinChannel				ax_tdstChannels			[MaxInChannelsTab];
tdstBinFrame				ax_tdstFrames			[MaxInFramesTab];
unsigned long				ax_ulFramesNTTO			[MaxInFramesNTTOTab];
tdstBinOnlyFrame			ax_tdstOnlyFrames		[MaxInOnlyFramesTab];
tdstBinHierarchy			ax_tdstHierarchies		[MaxInHierarchiesTab];
tdFileName					ax_xChannelNames		[MaxInChannelNamesTab];

tdxVector3					ax_tdxSavedVertexes		[MaxInSavedVertexesTab];
tdxQuater4					ax_tdxSavedQuaternions	[MaxInSavedQuaternionsTab];
tdstNTTO					ax_tdstSavedNTTO		[MaxInSavedNTTOTab];

tdstFileA3dAnimationGENERAL	ax_tdstFileA3dGENERAL	[MaxInFileA3dAnimationGENERALTab];
tdxULongVector3				ax_tdxFileVertexes		[MaxInFileVertexesTab];
tdxSShortQuater4			ax_tdxFileQuaternions	[MaxInFileQuaternionsTab];
tdstFileHierarchy			ax_tdstFileHierarchy	[MaxInFileHierarchyTab];
tdstNTTO					ax_tdstFileNTTO			[MaxInFileNTTOTab];
tdstFileOnlyFrame			ax_tdstFileOnlyFrame	[MaxInFileOnlyFrameTab];
tdstFileKeyFrame			ax_tdstFileKeyFrame		[MaxInFileKeyFrameTab];
tdstFileFrame				ax_tdstFileFrame		[MaxInFileFrameTab];
unsigned short				ax_uwFramesNTTO			[];
tdstFileChannel				ax_tdstFileChannel		[MaxInFileChannelTab];
tdstFileEvent				ax_tdstFileEvent		[MaxInFileEventTab];
tdstFileMorphData			ax_tdstFileMorphInfos	[MaxInFileMorphTab];

tdstSndEventId				ax_tdstSndEventId		[MaxInSndEventIdTab];
tdstBinEvent				ax_tdstEvents			[MaxInEventTab];
tdstBinMorphData			ax_tdstBinMorphInfos	[MaxInMorphTab];

tdxVector3	a3_xVector0=	{ 0, 0, 0 };
tdxVector3	a3_xVector1=	{ 1, 1, 1 };
tdxQuater4	a4_xQuater0=	{ 0, 0, 0, 0 };
tdxQuater4	a4_xQuater1=	{ 0, 0, 0, 1 };
FILE		*ErrorFile, *SizeOut, *EventFile;



// **********************************************************************************
void	fn_v_AllocateOnStack(	unsigned short	uwNumStack,
								signed long		slWantedSize )
{
	tdstStacks[uwNumStack].slStackPos+=	slWantedSize;

	if	(tdstStacks[uwNumStack].slStackPos > tdstStacks[uwNumStack].slMaxPos)
	{	fprintf( ErrorFile, "%s :  Stack #%d is full (monty)...\n", AnimationName, uwNumStack );
		tdstStacks[uwNumStack].slStackPos=	tdstStacks[uwNumStack].slMaxPos - slWantedSize;
	}

	tdstStacks[uwNumStack].slPosAlloc=	tdstStacks[uwNumStack].slStackPos - slWantedSize;
}



// **********************************************************************************
void	fn_v_DeAllocateOnStack(	unsigned short	uwNumStack,
								signed long		slSize )
{
	tdstStacks[uwNumStack].slStackPos-=	slSize;
	if	(tdstStacks[uwNumStack].slStackPos < 0)
	{	fprintf( ErrorFile, "%s :  Stack #%d is over (2 be) free...\n", AnimationName, uwNumStack );
		tdstStacks[uwNumStack].slStackPos=	0;
	}
}



// **********************************************************************************
void	fn_v_ResetStacks( void )
{	unsigned short	uwCnt;

	for	( uwCnt=0 ; uwCnt<eStackBinNbMax ; uwCnt++ )
	{	tdstStacks[uwCnt].slPosAlloc=	0;
		tdstStacks[uwCnt].slStackPos=	0;
	}
}



// **********************************************************************************
static int gs_iPrintPos = 0;

void	fn_v_LogSectionBegin(	int		_bFlagNew,
								char	*_szType,
								char	*_szString )
{	int		iCnt;
	char	szTxt[1024];

	for	( iCnt=0 ; iCnt<gs_iPrintPos ; iCnt++ )
		putchar(' ');

	gs_iPrintPos++;

	if	(_bFlagNew)
			sprintf( szTxt, "New %s:%s", _szType, _szString );
	else	sprintf( szTxt, "Old %s:%s", _szType, _szString );
//Mihaela Tancu - Animations
//	printf( "%s\n", szTxt );
	MLT_vOutput( C_ComRes_cOkLine, "	%s\n", szTxt);
//Mihaela Tancu - Animations
}

void	fn_v_LogSectionEnd(void)
{		gs_iPrintPos--;
}



// **********************************************************************************
void	fn_v_InitMemV6i( void )
{
	Erm_M_InitErrMsg(Erm);
	Erm_M_InitErrMsg(Mmg);
	Mmg_fn_vFirstInitMmgModule(10);

	// For morphing datas -> reset !
	memset(ax_tdstBinMorphInfos,0,MaxInMorphTab*sizeof(tdstBinMorphData));

	SCR_fn_v_RdL0_Init();
}



// **********************************************************************************
void	fn_v_KillMem( void )
{//#ifndef SCR_DM_LightVersion
	//fn_v_Anl_CloseModule();		// Destroy CallBack
	fn_v_Cxt_CloseModule();
	fn_v_Err_CloseModule();
//#endif /* SCR_DM_LightVersion */

	fn_v_Link_CloseModule();
	fn_v_File_CloseModule();

//#ifndef SCR_DM_LightVersion
	fn_v_Ntfy_CloseModule();
	fn_v_Sect_CloseModule();
	fn_v_Vars_CloseModule();
//#endif /* SCR_DM_LightVersion */
}



// **********************************************************************************
#undef	L_GLOBAL_V6_C
