#include "stdafx.h"
#include "Application.h"

#include "DXVersion.h"
#include "AppWindow.h"
#include "AppTimer.h"

#include "FilePackSystem.h"
#include "RenderSystem.h"
#include "SoundSystem.h"
#include "InputSystem.h"
#include "DevSystem.h"

#include "GameResourceManager.h"
#include "ResourceManager.h"
#include "CameraManager.h"
#include "SceneManager.h"
#include "SkillManager.h"
#include "DramaturgyManager.h"

cApplication* cApplication::mSingleton = 0;

cApplication::cApplication()
: mFileSystem( 0 )
, mRenderSystem( 0 )
#ifdef MAP_EDITOR
, mSoundSystem( 0 )
#endif
, mInputSystem( 0 )
, mDevSystem( 0 )
, mResourceManager( 0 )
, mGameResourceManager( 0 )
, mCameraManager( 0 )
, mSceneManager( 0 )
, mSkillManager(0)
, mDramaManager(0)
{
	assert( mSingleton == 0 && "bad singleton!" );
	mSingleton = this;

	/// 전역 타이머 생성
	mTimer = new cAppTimer;
	mTimer->Reset();
	mAccumTime = 0;
	mDeltaTime = 0;

	mActive = true;
}

cApplication::~cApplication()
{
	delete mDramaManager;
	delete mSkillManager;
	delete mSceneManager;
	delete mCameraManager;
	delete mGameResourceManager;
	delete mResourceManager;
	delete mDevSystem;
	delete mInputSystem;
	delete mRenderSystem;
	delete mFileSystem;
	delete mSoundSystem;
	delete mWindow;
	delete mTimer;

	mSingleton = 0;
}

void cApplication::Clear()
{
	if( mSceneManager )
		mSceneManager->Clear();

	if( mResourceManager )
		mResourceManager->Clear();
}

bool cApplication::Init( HWND wnd )
{
	/// 전역 윈도우를 생성
	mWindow = new cAppWindow( wnd );

	/// 파일 시스템을 생성
	mFileSystem = new cFilePackSystem;
	if( !mFileSystem || !mFileSystem->Init() )
	{
		return false;
	}

	/// 렌더링 시스템을 생성
	mRenderSystem = new cRenderSystem( mWindow );
	if( mRenderSystem->Init() == false )
	{
		assert( 0 && "failed to init render system" );
		return false;
	}

	/// 사운드 시스템을 생성
	mSoundSystem = new cSoundSystem;
	if( mSoundSystem->Init( wnd ) == false )
	{
		assert( 0 && "failed to init sound system" );
		return false;
	}

#ifdef MAP_EDITOR
	/// 데브 시스템을 생성
	mDevSystem = new cDevSystem( NiRenderer::GetRenderer() );
	if( mDevSystem->Init() == false )
	{
		assert( 0 && "failed to init dev system" );
		return false;
	}
#endif

	/// 입력 시스템을 생성
	mInputSystem = new cInputSystem;

	/// 자원 관리자를 생성
	mResourceManager = new cResourceManager;
	//mpResourceManager->SetBaseDir( baseDir );

	mDramaManager = new cDramaturgyManager;
	if( mDramaManager == 0 )
		return false;
//	if( mDramaManager->Init() == false )
//	{
//		assert( 0 && "failed to init Drama manager" );
//		return false;
//	}

	mGameResourceManager = new cGameResourceManager;
	if( mGameResourceManager->Init() == false )
		return false;

	/// 카메라 관리자를 생성
	mCameraManager = new cCameraManager;
	mCameraManager->CreateFree( true );

	/// 장면 관리자를 생성
	mSceneManager = new cSceneManager;
	if( !mSceneManager || !mSceneManager->Init( NiPoint3::ZERO, 100.0f, 100000.0f ) )
	{
		return false;
	}

	mSkillManager = new cSkillManager;
	if( mSkillManager->Init() == false )
	{
		assert( 0 && "failed to init skill manager" );
		return false;
	}

	/// 타이머 시작
	mTimer->Start();
	return true;
}

void cApplication::Exit()
{

	/// 타이머 중지
	mTimer->Stop();
}

void cApplication::Process()
{
//	NiSleep(1);

	/// 경과 시간 얻음
	mDeltaTime = mTimer->GetDeltaTime() * 0.001f;
	mAccumTime += mDeltaTime;

	/// 입력 관리자를 처리
	mInputSystem->Process();

	/// 카메라 관리자를 처리
	mCameraManager->Process( mDeltaTime );

	mSkillManager->Process( mDeltaTime, mAccumTime );

	mSoundSystem->Process( mDeltaTime, mAccumTime );

	/// 장면 관리자를 처리
	mSceneManager->Process( mDeltaTime, mAccumTime );

	if( mDevSystem )
		mDevSystem->Process( mAccumTime );
}

void cApplication::Render()
{
	mRenderSystem->Render();
}
