Ogre Helper Class ConfigFile

 1                         // load resource paths from config file
 2             Ogre::ConfigFile cf;
 3             cf.load(mFSLayer->getConfigFilePath("resources.cfg"));
 4             Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
 5             Ogre::String sec, type, arch;
 6 
 7             // go through all specified resource groups
 8             while (seci.hasMoreElements())
 9             {
10                 sec = seci.peekNextKey();
11                 Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();
12                 Ogre::ConfigFile::SettingsMultiMap::iterator i;
13 
14                 // go through all resource paths
15                 for (i = settings->begin(); i != settings->end(); i++)
16                 {
17                     type = i->first;
18                     arch = i->second;
19                                         Ogre::ResourceGroupManager::getSingleton().addResourceLocation(arch, type, sec);
20                 }
21             }

一、学习目标:ConfigFile的使用

二、OGRE配置文件(resources.cfg)描述:

  • 每个配置文件的后缀是.cfg
  • #后面的描述为注释
  • 每个配置文件由许多Section(段)组成,比如:resources.cfg由4个段组成(分别是:[Essential];[Popular];[General];[Tests])
  • 每个Section(段)由许多配置项组成,比如:[Essential]段由3个配置项组成(分别是:Zip=http://www.cnblogs.com/media/packs/SdkTrays.zip;Zip=http://www.cnblogs.com/media/packs/profiler.zip;FileSystem=http://www.cnblogs.com/media/thumbnails)
  • 每个配置项是一个Key-Value,比如:Zip是Key,http://www.cnblogs.com/media/packs/SdkTrays.zip是Value

 

三、示例代码

 1         virtual void locateResources()
 2         {
 3             // load resource paths from config file
 4             Ogre::ConfigFile cf;
 5             cf.load(mFSLayer->getConfigFilePath("resources.cfg"));
 6             //获取Section迭代器
 7             Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
 8             Ogre::String sec, type, arch;
 9 
10             // go through all specified resource groups
11             while (seci.hasMoreElements())//Section是否迭代完成
12             {
13                 //获取Section的名字,比如:Essential,或Popular
14                 //但是Section迭代器没有往前移动
15                 sec = seci.peekNextKey();
16                 //获取Section的配置项列表
17                 //并且Section迭代器向前移动
18                 Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();
19                 //申明配置项列表迭代器
20                 Ogre::ConfigFile::SettingsMultiMap::iterator i;
21 
22                 // go through all resource paths
23                 for (i = settings->begin(); i != settings->end(); i++)
24                 {
25                     //获取配置项信息,first为key,second为value
26                     type = i->first;
27                     arch = i->second;
28                     Ogre::ResourceGroupManager::getSingleton().addResourceLocation(arch, type, sec);
29                 }
30             }
31         }

 

 

posted @ 2012-12-09 12:39  a-ir  阅读(204)  评论(0)    收藏  举报