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

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

#include "game_manager.h"

// for Graphic Library
#include "renderer_manager.h"
#include "renderer.h"

// for Scene Management
#include "scene_manager.h"

// for Input system
#include "input.h"
#include "keyboard.h"
#include "mouse.h"
#include "gamepad.h"

// for

// for debug 

#include "d_manager.h"

#include "d_font.h"
#include "d_log.h"

#include "d_console.h"

#include "window.h"


#include "scenesample.h"

#include "virtual.h"

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


/*---------------------------------------------------------------
	Const Number Difinition
---------------------------------------------------------------*/


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

GameManager* GameManager::pManager = nullptr;



// GameManager Constructor
GameManager::GameManager( )
	: pRendererManager( nullptr )
	, pSceneManager( nullptr )
	, gameMaster( nullptr )
{

	// Menber Clear Process
	if( !pManager ){
		pManager = this;
	}

}

// GameManager Destructor
GameManager::~GameManager( )
{

	// Do Nothing!!

}

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

	// Create Class Instance
	GameManager* pObject;
	pObject = new GameManager;

	return pObject;

}

// GameManager Initialize
void GameManager::Init( HWND hWnd, bool bWindow )
{

	// Initialize Process
	
	{ // Renderer Initialize
		
		pRendererManager = RendererManager::Create( Renderer::RENDERER_DX9 );	// DirectX9 Platform
		pRendererManager->Init( );
		pRendererManager->GetRenderer( )->Init( hWnd, bWindow );
		platform = pRendererManager->ConfirmRendererType( );
	}

	{ // Input COM Initialize

		// get [ HINSTANCE ]
		HINSTANCE hInstance = (HINSTANCE)GetWindowLong( hWnd, GWL_HINSTANCE );
		CInputKeyboard::Create( "keyboard" )->Init( hInstance, hWnd ); 
		InputMouse::Create( )->Init( hInstance, hWnd ); 
	}

	{ // DebugManager Initialize

		CDebugManager::Create( ) ->Init( );
		//CConsole::OpenConsole( );
	}

	{ // Scene Manager Initialize
	
		pSceneManager = SceneManager::Create( );
		pSceneManager ->Init( new CVirtual );
	}



}

// GameManager Uninitialize
void GameManager::Uninit( void )
{

	// Uninitialize Process
	
	{ // Scene Manager Uninialize
	
		pSceneManager ->Uninit( );
	}

	{ // Release Input
		CInput::ReleaseAll( );
	}

	{ // サウンドデバイス解放処理
		//CModelManager::UnloadAll( );
		//CLight::ReleaseAll( );
	}

	{ // Release Debug Manager
		CConsole::CloseConsole( );
		//CDebugManager::ReleaseAll( );
	}

	{ // Renderer Uninitialize
		pRendererManager->GetRenderer( )->Uninit( );
		pRendererManager->Uninit( );
		SAFE_DELETE( pRendererManager )
	}

	// GameManager Destory
	SAFE_DELETE( pManager )

}

// GameManager Update
void GameManager::Update( void )
{

	// Update Process
	CInput::UpdateAll( );

	// Update Scene
	pSceneManager->GetScene( )->Update( );
	
	// Update Debug Manager
	#ifdef _DEBUG
	//CConsole::DebugLog( "WindowFPS : %d\n", Window::GetFPS( ) );
	//CDebugManager::UpdateAll( );
	#endif // _DEBUG
	
}

// GameManager Draw
void GameManager::Draw( void )
{

	static int renderCount = 0;
	#ifdef _DEBUG
	//CDebugFont::Access(0)->SetRenderData( L_RENDER ," renderCount : %d \n ", renderCount );
	#endif // _DEBUG


	// Draw Process
	if( pRendererManager->GetRenderer( )->RenderBegin( ) ){
	
		// Render Scene 
		pSceneManager->GetScene( )->Draw( );

		#ifdef _DEBUG
		//CDebugManager::IndexAccess(0) ->EndMeasureProcess( );
		Vector2 screen = InputMouse::Access()->GetPosWorld();
		CDebugFont::Access()->SetRenderData( L_RENDER, " [ %s ] \n ALL PROCESS FPS : %d \n MOaUSE X:%d Y:%d Z:%d \n",  APPLICATION_NAME, Window::GetFPS( ), screen.x, screen.y, InputMouse::Access()->GetAxisZ() );
		CDebugFont::Access()->Render( );
		//CDebugManager::DrawAll( );
		#endif // _DEBUGwd

		pRendererManager->GetRenderer( )->RenderEnd( );
	}
	pRendererManager->GetRenderer( )->Present( ); 

	renderCount++;

}

// GameManager Operate
GameManager* GameManager::Operate( void )
{

	// GameManager Operate
	return pManager;

}
 
// GameManager GetRendererManager
RendererManager* GameManager::GetRendererManager( void )
{

	return pRendererManager;

}

// GameManager Device Info
Renderer* GameManager::DeviceInfo( void )
{

	// GameManager Device Info 
	return  pManager->GetRendererManager( )->GetRenderer( );

}

// GameManager Platform 
Renderer::TYPE& GameManager::PlatformInfo( void )
{

	// GameManager Platform 
	return pManager->platform;

}



