fengyue

OGRE资源管理分析

OGRE的文件系统比较复杂,不熟悉的人不知道怎么使用,经常出现找不到资源的问题,在这里简单介绍一下资源路径的设置:
资源路径可以通过程序设置也可以通过配置方式:
配置方式
resources.cfg就是用来配置资源的路径的,内容如下:
# Resource locations to be added to the 'boostrap' path
# This also contains the minimum you need to use the Ogre example framework
[Bootstrap]
Zip=../../Media/packs/OgreCore.zip

# Resource locations to be added to the default path
[General]
FileSystem=../../Media
FileSystem=../../Media/fonts
FileSystem=../../Media/materials/programs
FileSystem=../../Media/materials/scripts
FileSystem=../../Media/materials/textures
FileSystem=../../Media/models
FileSystem=../../Media/overlays
FileSystem=../../Media/particle
FileSystem=../../Media/gui
FileSystem=../../Media/DeferredShadingMedia
Zip=../../Media/packs/cubemap.zip
Zip=../../Media/packs/cubemapsJS.zip
Zip=../../Media/packs/dragon.zip
Zip=../../Media/packs/fresneldemo.zip
Zip=../../Media/packs/ogretestmap.zip
Zip=../../Media/packs/skybox.zip

Bootstrap、General就是资源组名称,FileSystem、Zip就是文件系统类型,表示压缩包还是文件系统。一般我们开发时都是使用文件系统,所以配置对应的路径就可以。OGRE是支持Zip压缩包的,如果要实现自己的压缩包需要实现对应的Archive的子类,实现对应接口就可以。可以参考ZipArchive、ZipArchiveFactory、ZipDataStream实现。

程序方式
其实配置方式最终也是调用程序来设置的,resources.cfg只是指定了ResourceGroupManager::getSingleton().addResourceLocation的参数。
看下面的函数就很清楚了,就是读取resources.cfg,将配置的路径都添加到资源管理,设置完资源组路径,调用ResourceGroupManager::getSingleton().initialiseAllResourceGroups()初始化就可以,后续就可以直接加载资源,很简单吧。

 1   virtual void setupResources(void)
 2    {
 3        // Load resource paths from config file
 4        ConfigFile cf;
 5        cf.load(mResourcePath + "resources.cfg");
 6
 7        // Go through all sections & settings in the file
 8        ConfigFile::SectionIterator seci = cf.getSectionIterator();
 9
10        String secName, typeName, archName;
11        while (seci.hasMoreElements())
12        {
13            secName = seci.peekNextKey();
14            ConfigFile::SettingsMultiMap *settings = seci.getNext();
15            ConfigFile::SettingsMultiMap::iterator i;
16            for (i = settings->begin(); i != settings->end(); ++i)
17            {
18                typeName = i->first;
19                archName = i->second;
20#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
21                // OS X does not set the working directory relative to the app,
22                // In order to make things portable on OS X we need to provide
23                // the loading with it's own bundle path location
24                ResourceGroupManager::getSingleton().addResourceLocation(
25                    String(macBundlePath() + "/" + archName), typeName, secName);
26#else
27                ResourceGroupManager::getSingleton().addResourceLocation(
28                    archName, typeName, secName);
29#endif
30            }

31        }

32    }


 

posted on 2011-11-23 11:07 风悦 阅读(2206) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理