www.pudn.com > zfxcengine-0.1.0.zip > ceMainApp.cpp


// $Id: ceMainApp.cpp,v 1.3 2005/09/13 00:59:46 andreaskohn Exp $ 
///////////////////////////////////////////////////////////////// 
// 
//  Module:     Core 
//  File:       ceMainApp.cpp 
//  Created:    12.11.2003 22:22:37 
//  Author:     Kim Kulling aka kimmi 
// 
///////////////////////////////////////////////////////////////// 
#include  
#include "MainApp/ceMainApp.h" 
 
#include "Core/ceCoreTools.h" 
#include "Core/ceDynlib.h" 
#include "Core/ceDebug.h" 
#include "Core/ceResourceManager.h" 
#include "Core/ceConsole.h" 
#include "Core/ceTimer.h" 
#include "Core/ceEventTriggerer.h" 
#include "Core/ceDefaultController.h" 
#include "Core/ceMemManager.h" 
#include "Geometry/ceMesh.h" 
#include "Geometry/ceMeshFactory.h" 
#include "GUI/ceGUISystem.h" 
#include "Input/ceInputSystem.h" 
#include "SceneGraph/ceSceneGraph.h" 
#include "VFSystem/ceVFSystem.h" 
 
namespace ZFXCE { 
	//------------------------------------------------------------------------------ 
	using namespace std; 
 
	ceMainApp *ceMainApp::m_spClass = NULL; 
 
	//------------------------------------------------------------------------------ 
	ceMainApp* ceMainApp::CreateInstance(int argc, char *argv[]) 
	{ 
		PUSH_FUNCTION; 
 
		ce_assert(NULL == m_spClass); 
		m_spClass = new ceMainApp(); 
		ce_assert (NULL != m_spClass); 
		return m_spClass; 
	} 
	//------------------------------------------------------------------------------ 
	ceMainApp* ceMainApp::CreateInstance() 
	{ 
		PUSH_FUNCTION; 
 
		ce_assert(NULL == m_spClass);		 
		m_spClass = new ceMainApp(); 
		ce_assert (NULL != m_spClass); 
 
		return m_spClass; 
	} 
	//------------------------------------------------------------------------------ 
	ceMainApp *ceMainApp::GetInstance()  
	{ 
		PUSH_FUNCTION; 
 
		ce_assert (NULL != m_spClass);		 
		return m_spClass; 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::KillInstance() 
	{ 
		PUSH_FUNCTION; 
 
		ce_assert (NULL != m_spClass); 
		delete m_spClass; 
	} 
	//------------------------------------------------------------------------------ 
	ceMainApp::ceMainApp(int argc, char *argv[]) 
	{ 
		PUSH_FUNCTION; 
		 
		// Store incoming parameter from command line 
		Init(); 
		for (int i=0; iGetArchivManager(); 
			pAM->RegisterDataDir("./"); 
			pAM->RegisterFileExtension("zip", ARCHIVE_ZIP); 
			pAM->RegisterFileExtension("pk3", ARCHIVE_ZIP); 
			pAM->RegisterFileExtension("pk4", ARCHIVE_ZIP); 
			pAM->SearchForArchives(); 
		} 
		return m_pVFS; 
	} 
	//------------------------------------------------------------------------------ 
	ceResourceManager *ceMainApp::GetResourceServer() 
	{ 
		PUSH_FUNCTION; 
 
		// Enshure valid resource manager instance 
		if (NULL == m_pResourceManager) {		 
			m_pResourceManager = new ceResourceManager(); 
			ce_assert (NULL != m_pResourceManager); 
		} 
		return m_pResourceManager; 
	} 
	//------------------------------------------------------------------------------ 
	ceTimer *ceMainApp::GetTimer() 
	{ 
		PUSH_FUNCTION; 
 
		if (NULL == m_pTimer) { 
			m_pTimer = new ceTimer; 
			ce_assert (NULL != m_pTimer); 
		} 
 
		return m_pTimer; 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::Exec() 
	{ 
		PUSH_FUNCTION; 
		 
		GetController(); 
		const DOUBLE Tick = m_pTimer->GetFrameInterval(); 
		m_pMainController->Update(Tick); 
		for (m_It = m_vController.begin(); m_It != m_vController.end(); ++m_It) { 
			(*m_It)->Update(Tick); 
		} 
	} 
	//------------------------------------------------------------------------------ 
	Input::ceInputSystem *ceMainApp::CreateInputModule(const DynLib::ceModule Mod) 
	{ 
		PUSH_FUNCTION; 
		m_pInputSystem = (Input::ceInputSystem*) CreateSystemFromDynLib(Mod); 
		return m_pInputSystem; 
	} 
	//------------------------------------------------------------------------------ 
	Input::ceInputDevice *ceMainApp::GetInputDevice() 
	{ 
		PUSH_FUNCTION; 
 
		return m_pInputSystem->GetDevice(); 
	} 
	//------------------------------------------------------------------------------ 
	Render::ceRenderSystem *ceMainApp::CreateRenderSystem(const DynLib::ceModule Mod) 
	{ 
		PUSH_FUNCTION; 
 
		if (NULL == m_pRenderSystem) 
			m_pRenderSystem =(Render::ceRenderSystem*) CreateSystemFromDynLib(Mod); 
 
		return  m_pRenderSystem; 
	} 
	//------------------------------------------------------------------------------ 
	SceneGraph::ceSceneGraph *ceMainApp::CreateSceneGraph() 
	{ 
		PUSH_FUNCTION; 
		 
		SceneGraph::ceSceneGraph *pSystem = (SceneGraph::ceSceneGraph*)  
			CreateSystemFromDynLib(ZFXCE::DynLib::CE_SCENEGRAPH); 
		pSystem->SetParent(this); 
		pSystem->SetVFSystem(GetVFS()); 
 
		return pSystem; 
	} 
	//------------------------------------------------------------------------------ 
	SceneGraph::ceSceneGraph *ceMainApp::GetSceneGraph() const 
	{ 
		PUSH_FUNCTION; 
 
		ceIDefaultSystem *pSystem=NULL; 
		if (!GetLoadedSystem(ZFXCE::DynLib::CE_SCENEGRAPH, &pSystem)) 
			return NULL; 
 
		return (SceneGraph::ceSceneGraph*) pSystem; 
	} 
	//------------------------------------------------------------------------------		 
	GUI::ceGUISystem *ceMainApp::CreateGUISystem() 
	{ 
#if 0 
		if (NULL == m_pGUISystem) { 
			m_pGUISystem = new GUI::ceGUISystem(); 
			ce_assert (NULL != m_pGUISystem); 
		} 
		return m_pGUISystem; 
#else 
		return 0; 
#endif 
	} 
	//------------------------------------------------------------------------------ 
	GUI::ceGUISystem *ceMainApp::GetGUISystem() const 
	{ 
		return m_pGUISystem; 
	} 
	//------------------------------------------------------------------------------ 
	ceIDefaultSystem *ceMainApp::CreateSystemFromDynLib(const DynLib::ceModule Mod) 
	{ 
		PUSH_FUNCTION; 
 
		ceIDefaultSystem *pModule = NULL; 
		DynLib::RegisterDynLinkedLib(Mod); 
		ZFXCE::DynLib::GetFromDLL(Mod, (ceIDefaultSystem**) &pModule); 
		ce_assert(NULL != pModule); 
 
		return pModule; 
	} 
	//------------------------------------------------------------------------------ 
	ceMeshFactory *ceMainApp::GetMeshFactory() 
	{ 
		PUSH_FUNCTION; 
 
		if (NULL == m_pMeshFactory) { 
			m_pMeshFactory = new ceMeshFactory(GetVFS(), GetResourceServer()); 
			ce_assert (NULL != m_pMeshFactory); 
		} 
 
		return m_pMeshFactory; 
	} 
	//------------------------------------------------------------------------------ 
	ceMesh *ceMainApp::LoadMesh(const char *pFileName) 
	{ 
		PUSH_FUNCTION; 
 
		ceVFSystem *pVFS = GetVFS(); 
		string name(pFileName); 
		const INT FileID = pVFS->OpenFile(name, CE_VFS_BIN); 
		ce_check(-1 != FileID, "Cannot open file!" + name); 
		const size_t sFileSize = m_pVFS->GetFileSize(FileID); 
		BYTE *pBuffer = new BYTE[sFileSize]; 
		ce_assert (NULL != pBuffer); 
		m_pVFS->GetFileData(FileID, pBuffer, sFileSize); 
		ceMesh *pMesh = GetMeshFactory()->Load(pBuffer, sFileSize, string("bsp")); 
		delete [] pBuffer; 
		 
		return pMesh; 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::SetController(ceIController *pController) 
	{ 
		PUSH_FUNCTION; 
 
		ce_assert (NULL != pController); 
		m_pMainController = pController; 
	} 
	//------------------------------------------------------------------------------ 
	ceIController *ceMainApp::GetController() 
	{ 
		PUSH_FUNCTION; 
 
		if (NULL == m_pMainController) { 
			m_pMainController = new ceDefaultController(); 
			ce_assert (NULL != m_pMainController); 
		} 
 
		return m_pMainController; 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::RegisterController(ceIController *pController) 
	{ 
		PUSH_FUNCTION; 
		ce_assert (NULL != pController); 
		m_vController.push_back(pController); 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::RemoveController(const std::string &strName) 
	{ 
		PUSH_FUNCTION; 
		for (m_It= m_vController.begin(); m_It != m_vController.end(); ++m_It) { 
			if ((*m_It)->GetName() == strName) { 
				m_vController.erase(m_It); 
				break; 
			} 
		} 
 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::RemoveAllController() 
	{ 
		PUSH_FUNCTION; 
		if (m_vController.size()==0) 
			return; 
		m_vController.clear(); 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::InitSystem() 
	{ 
	}	 
	//------------------------------------------------------------------------------ 
	void ceMainApp::ShutdownSystem() 
	{ 
	} 
	//------------------------------------------------------------------------------ 
	void ceMainApp::SetParent(ceIDefaultSystem *pParentSystem) 
	{ 
	} 
	//------------------------------------------------------------------------------ 
 
} // Namespace ZFXCE