//*******************************************************************
//
// File: Asura_Platform_Carpet_MeshManager.cpp
//
// Created: 13 Jan 2006
//
// Description: 
//
//*******************************************************************

// headers
#include "Core/Asura.h"

#if defined( ASURA_USE_CARPET)

#include "Core/Asura_Timers.h"

#include "Carpet/Asura_Carpet_MeshManager.h"
#include "Carpet/Asura_Carpet_StyleManager.h"
#include "Carpet/Asura_Direct3D_Carpet_Game_Render.h"
#include "Carpet/Asura_Direct3D_Carpet_Render.h"

#include "Rendering/Asura_Direct3D.h"

#ifdef ASURA_TOOLS
#include "Carpet/Tools_Carpet_Mesh.h"
#include "Carpet/Tools_Carpet_Statistics.h"
#include "Interface/Asura_CommandConsole.h"

#include "Core/Asura_UserInput.h"

#include "Tools_System.h"
#endif

#ifdef ASURA_USE_DEBUG_TEXT
#include "Debugging/Asura_DebugText.h"
#endif
#include "Debugging/Asura_Profiling_System.h"

// other libraries

// internal dependencies

//******************************************** ***********************
// Constants
//*******************************************************************

//*******************************************************************
// Local Classes
//*******************************************************************


//*******************************************************************
// Function Definitions
//*******************************************************************

void Asura_Carpet_MeshManager::Platform_Initialise()
{
#if defined( ASURA_TOOLS) && defined(ASURA_SUPPORT_COMMAND_CONSOLE) 
	{
	static bool bInitialised = false;
		if( false == bInitialised)
		{
			Asura_CommandConsole::AddVar( L"Render.Carpet.StaticsShowCulling", &(Tools_Carpet_Statistics::s_bShowCulling), Asura_CommandConsole::REMOVE_AT_SHUTDOWN );
			bInitialised = true;
		}
	}
#endif
}

void Asura_Carpet_MeshManager::Platform_Shutdown()
{
	Asura_Direct3D_Carpet_Game_Render::ClearDataSet();
}

void Asura_Carpet_MeshManager::Platform_Reset()
{
	Asura_Direct3D_Carpet_Game_Render::ClearDataSet();
}

#ifdef ASURA_TOOLS
// Convert the loaded in data to a platform specific format
// Temporary function until we write out platform specific chunks
void Asura_Carpet_MeshManager::Platform_MakeDataSet( Asura_LinkList<Asura_Carpet_Triangle>& xTriangleList )
{
	// DLH - 19 January 2006 15:24:17 - Create Direct3D optimised data for rendering
//	Asura_Direct3D_Carpet_Game_Render::MakeDataSet( xTriangleList );
}
#endif // #ifdef ASURA_TOOLS


void Asura_Carpet_MeshManager::Platform_Render( const float fGlobalFade )
{
#if !defined( ASURA_TOOLS)
	if( Asura_Direct3D_Carpet_Game_Render::HasDataSet())
	{
		Asura_Direct3D_Carpet_Game_Render::Render();
	}
#else
	TOOLS_CARPET_STATISTICS_RESET();
	
	if( NULL == s_xTriangleList.Front())
	{
		return;
	}

	int eToolsMode = Tools_System::GetCurrentMode();
	switch( eToolsMode)
	{
		case TOOLS_MODE_CARPET:
		{
			break;
		}
		default:
		{
			if( false == Asura_Direct3D_Carpet_Game_Render::HasDataSet())
			{
				Asura_LinkList< Asura_Carpet_Triangle>& xListCarpetTrainalges = Asura_Carpet_MeshManager::GetTriangleList();
				
				if( NULL != xListCarpetTrainalges.Front())
				{
					Asura_Direct3D_Carpet_Game_Render::MakeDataSet( xListCarpetTrainalges);
				}
			}
			break;
		}
	}

#if defined( ASURA_CARPET_MESHMANAGER_PROFILE)	
	const char* pszProfileName = "";
	int iOverallTimer	= Asura_Profiling_CreateTimer();

	Asura_Profiling_StartTiming( iOverallTimer );
#endif
	
	if( Asura_Direct3D_Carpet_Game_Render::HasDataSet())
	{
		Asura_Direct3D_Carpet_Game_Render::Render();
#if defined( ASURA_CARPET_MESHMANAGER_PROFILE)	
		pszProfileName = " CGR";
#endif
	}
	else
	{
		Asura_Carpet_Mesh_Data_Frame xFrameData( Asura_Timers::GetLocalUnscaledTime());

		Asura_Direct3D_Carpet_Render::BeginRender();
		
		for (Asura_ListElt<Asura_Carpet_Triangle> *pElt=s_xTriangleList.Front();pElt;pElt=pElt->Next())
		{		
			Tools_Carpet_Mesh::RenderCarpetTriangle( xFrameData, &pElt->Data());
		}
		
		Asura_Direct3D_Carpet_Render::EndRender();
#if defined( ASURA_CARPET_MESHMANAGER_PROFILE)	
		pszProfileName = " TCM";
#endif
	}
	
#if defined( ASURA_CARPET_MESHMANAGER_PROFILE)	
	{
		const float fOverallTime = Asura_Timers::TicksToSeconds( Asura_Profiling_StopTiming( iOverallTimer ) );

		TOOLS_CARPET_STATISTICS_SET( s_fTime, fOverallTime);
		
		Asura_Profiling_DestroyTimer(iOverallTimer);
	}
#endif

#endif
}

#endif //defined( ASURA_USE_CARPET)


//*******************************************************************
//
// (c) 2006 Rebellion (Derby)
//
// Warning: This computer program is protected by copyright law and 
// international treaties. Unauthorized reproduction or distribution
// of this program, or any portion of it, may result in severe civil and
// criminal penalties, and will be prosecuted to the maximum extent 
// possible under the law.
//
//*******************************************************************

