Mogre学习笔记(六)

Section 6: load configurations manually

有时候由于某些原因,会需要去掉OGRE项目的配置文件,这时我们就需要在代码中hardcode,以定义这些资源。

我们在新建Root对象的时候,如果不设置参数,那么它会默认读取resource.cfg和plugins.cfg中的配置信息。因此,我们新建Root对象的时候应该使用如下代码:

new Root("", "");

这样,我们就可以在之后的代码中手动定义了,首先是定义resource.cfg:

ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);

archName是需要加载的路径;typeName指定种类,多用FileSystem和Zip(大小写敏感);secName指定ResourceGroup。我们可以hardcode这些配置,当然,我们也可以从文件中读入,给出示例代码如下:

ConfigFile cf = new ConfigFile();
cf.Load("resources.cfg", "\t:=", true);
ConfigFile.SectionIterator seci = cf.GetSectionIterator();
String secName, typeName, archName;

while (seci.MoveNext())
{
secName = seci.CurrentKey;
ConfigFile.SettingsMultiMap settings = seci.Current;
foreach (KeyValuePair<string, string> pair in settings)
{
typeName = pair.Key;
archName = pair.Value;
ResourceGroupManager.Singleton.AddResourceLocation(archName, typeName, secName);
}
}

然后是加载plugins.cfg,Root有一个LoadPlugin方法,例如加载Direct3D9:

Root.Singleton.LoadPlugin("RenderSystem_Direct3D9");

这样,我们就把所需的配置文件定义好和插件手动加载完了。之后调用ResourceGroupManager的InitialiseAllResourceGroups方法加载:

ResourceGroupManager.Singleton.InitialiseAllResourceGroups();

但是,有些时候,我们可能会想在运行时加载一个Zip包,比如加载一个场景或模型,那么我们需要在加载以前添加代码:

ResourceGroupManager.Singleton.AddResourceLocation("model/model.zip", "Zip", "General");

但是,在实践中,却发现无法装载*.material文件,需要手动解析,因此我们将*.material文件解压,解析的代码如下:

FileStream fs = new FileStream(material, FileMode.Open);
DataStream ds = new ManagedDataStream(fs);
MaterialManager.Singleton.ParseScript(new DataStreamPtr(ds), "General");
ds.Close();
fs.Close();

这样,我们就完成了手动动态加载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值