要获得档案文件的文件列表,只要获得Archive的指针,就可以调用list()函数获得文件列表
但是如何获得Archive的指针呢,先看看资源的载入过程:
先从resources.cfg文件获取资源的路径,资源组,资源文件类型,这3个数据:
ConfigFile cf;
cf.load("resources.cfg");
再把这3个数据传入资源定位的函数:
ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
资源定位到底做了什么,看看源码:
- void ResourceGroupManager::addResourceLocation(const String& name,
- const String& locType, const String& resGroup, bool recursive)
- {
- ResourceGroup* grp = getResourceGroup(resGroup);
- if (!grp)
- {
- createResourceGroup(resGroup);
- grp = getResourceGroup(resGroup);
- }
- OGRE_LOCK_MUTEX(grp->OGRE_AUTO_MUTEX_NAME) // lock group mutex
- // Get archive
- Archive* pArch = ArchiveManager::getSingleton().load( name, locType );
- // Add to location list
- ResourceLocation* loc = new ResourceLocation();
- loc->archive = pArch;
- loc->recursive = recursive;
- grp->locationList.push_back(loc);
- // Index resources
- StringVectorPtr vec = pArch->find("*", recursive);
- for( StringVector::iterator it = vec->begin(); it != vec->end(); ++it )
- {
- // Index under full name, case sensitive
- grp->resourceIndexCaseSensitive[(*it)] = pArch;
- if (!pArch->isCaseSensitive())
- {
- // Index under lower case name too for case insensitive match
- String indexName = (*it);
- StringUtil::toLowerCase(indexName);
- grp->resourceIndexCaseInsensitive[indexName] = pArch;
- }
- }
- StringUtil::StrStreamType msg;
- msg << "Added resource location '" << name << "' of type '" << locType
- << "' to resource group '" << resGroup << "'";
- if (recursive)
- msg << " with recursive option";
- LogManager::getSingleton().logMessage(msg.str());
- }
我们可以看到
// Get archive
Archive* pArch = ArchiveManager::getSingleton().load( name, locType );
这里,载入档案的时候就返回了档案的指针,这就是我们需要的,接下来他把这个指针传到了2个地方
// Add to location list
ResourceLocation* loc = new ResourceLocation();
loc->archive = pArch; // 这里传入一次
// Index under full name, case sensitive
grp->resourceIndexCaseSensitive[(*it)] = pArch; // 这里再传入一次
理论上我们可以从这2个数据结构中获取指针,但是不幸的是,这些都是protected成员,获取不了的
而这个类又没有提供相关的获取函数,怎么办呢?难道又要改OGRE源码么?
看看前面的这个函数
// Get archive
Archive* pArch = ArchiveManager::getSingleton().load( name, locType );
如果已经载入了一次,我们再载入一次,会发现什么情况
- Archive* ArchiveManager::load( const String& filename, const String& archiveType)
- {
- ArchiveMap::iterator i = mArchives.find(filename);
- Archive* pArch = 0;
- if (i == mArchives.end())
- {
- // Search factories
- ArchiveFactoryMap::iterator it = mArchFactories.find(archiveType);
- if (it == mArchFactories.end())
- // Factory not found
- OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Cannot find an archive factory "
- "to deal with archive of type " + archiveType, "ArchiveManager::load");
- pArch = it->second->createInstance(filename);
- pArch->load();
- mArchives[filename] = pArch;
- }
- else
- {
- pArch = i->second;
- }
- return pArch;
- }
看了源码,豁然开朗!!!
原来载入档案的时候,他会先查看这个档案资源已经载入了没有,如果已经载入了,直接返回这个档案资源的指针!!!
所以我们在外部再载入一次,就可以获得这个资源的指针:
- // 获得地图列表文件
- TLBBArchive* arch = (TLBBArchive*)ArchiveManager::getSingleton().load("../../Data/Scene.axp", "AxpPack");
- mMaplistPtr = arch->list();
到此,就已经获得了列表文件信息,
用MFC对话框打开看看:
- BOOL COpenMapDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 获得地图列表
- CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
- CTLBBView* view = (CTLBBView*)((CMainFrame*)AfxGetMainWnd())->GetActiveView(); // 获得视图类
- StringVectorPtr svPtr = view->getOgreApp()->getMaplistPtr();
- vector<String>::iterator iter = svPtr->begin();
- for (; iter != svPtr->end(); ++ iter)
- {
- String str = *iter;
- if (StringUtil::endsWith(str, ".Scene", false))
- {
- pListBox->AddString(str.c_str());
- }
- }
- return TRUE;
- }
最后效果: