/***************************************************************/
//
//
//		DirectX	[ mesh3dobjectdx9.cpp ]
//
//											Author	kazuki tanaka
//											Date	2017 5/25
/*---------------------------------------------------------------
■Update : 2017/5/25
			mesh3dobjectdx9.cppの作成

/*---------------------------------------------------------------
	Include File
---------------------------------------------------------------*/


#include "mesh3dobjectdx9.h"

#include "texturedx9_interface.h"

/*---------------------------------------------------------------
	Macro Difinition
---------------------------------------------------------------*/

typedef TextureInterface* LPTextureInterface ;


/*----------------------------------------------------------------
	static member variable Initialize 
----------------------------------------------------------------*/





// Mesh3DObjectDX9 Constructor
Mesh3DObjectDX9::Mesh3DObjectDX9( )
	: pMesh(nullptr)
	, pBufMat(nullptr)
	, matNum(0)
	, ppTexture(nullptr)
{

	// Menber Clear Process

}

// Mesh3DObjectDX9 Destructor
Mesh3DObjectDX9::~Mesh3DObjectDX9( )
{

	// Do Nothing!!

}

// Mesh3DObjectDX9 Create Instance 
Mesh3DObjectDX9* Mesh3DObjectDX9::Create( void )
{

	// Create Class Instance
	return new Mesh3DObjectDX9;

}
// Mesh3DObjectDX9 Uninitialize
void Mesh3DObjectDX9::Release( void )
{
	
	// 処理なし
	D3DXMATERIAL* pMat =( D3DXMATERIAL*)pBufMat -> GetBufferPointer( );
	for( int index = 0; index < (int)matNum; index++ ){
		ppTexture[ index ]->Release( );
	}
	SAFE_DELETE_ARRAY( ppTexture )
	SAFE_RELEASE( pBufMat )
	SAFE_RELEASE( pMesh )
	
}

// Mesh3DObjectDX9 Load
bool Mesh3DObjectDX9::Load( const char* FILE_NAME )
{
	
	// XFileLoad
	HRESULT hr = D3DXLoadMeshFromX( 
		FILE_NAME,
		D3DXMESH_MANAGED,
		GetDevice( ),
		NULL,
		&pBufMat,
		NULL,
		&matNum,
		&pMesh );
	if( FAILED( hr )){
		#ifdef _DEBUG
			#ifdef _DEBUGMANAGER_H_
			CDebugManager::DebugMsg( " [ %s ] MODEL LOAD ERROR!! \n index : [ %d ] \n ERROR_CODE : 0x%x ", MODEL_NAME, index, hr );
			#endif // _DEBUGMANAGER_H_
		#endif	// _DEBUG
		return false;
	}else
	{ // TEXTURE LOAD
	
		// Get MaterialPointer
		D3DXMATERIAL* pMat =( D3DXMATERIAL*)pBufMat -> GetBufferPointer( );

		// Create <TextureBuffer>
		ppTexture = new LPTextureInterface[(int)matNum];

		for( int index = 0; index < (int)matNum; index++ ){

			// Load Texture
			ppTexture[ index ] = TextureDX9Interface::Create( );
			if( pMat[ index ].pTextureFilename ){
				if( ppTexture[index]->Load( pMat[ index ].pTextureFilename ) ) {

				}
			}else{
			
				// NULL値代入テクスチャ無しマテリアル
				dynamic_cast< TextureDX9Interface* >(ppTexture[index])->data = nullptr;
			}
		}
	}	// -> END TEXTURE LOAD PROCESS

	return true;

	
}

// Mesh3DObjectDX9 GetMesh
LPD3DXMESH Mesh3DObjectDX9::GetMesh( void )
{
	return pMesh;
}

// Mesh3DObjectDX9 GetMaterialBuffer
LPD3DXBUFFER Mesh3DObjectDX9::GetMatBuffer( void )
{
	return pBufMat;
}

// Mesh3DObjectDX9 GetMatNumber
DWORD Mesh3DObjectDX9::GetMatNumber( void )
{
	return matNum;
}

// Mesh3DObjectDX9 GetTexture
TextureInterface* Mesh3DObjectDX9::GetTexture( const int& index )
{
	return ppTexture[ index ];
}
