
ClanLib 中的资源管理接口:
•.资源管理器
•.资源对象
ClanLib 中的资源管理器允许妳为程序中所有的资源使用一個中央的来源。这個系统定义咯一個用来描述ClanLib 中使用的资源的简单XML 格式,而且,由于它是XML,所以妳也可以用它来管理妳的自定义的资源。
一個资源XML 文件大概是这样的:
<resources>
<section name="section1">
<texture name="texture1" file="image.png" />
</section>
<section name="section2">
<section name="section3">
<my-type name="my-resource1">
<text>Hello World!</text>
</my-type>
</section>
</section>
<sample name="my-sample" file="test.wav" />
</resources>
CL_ResourceManager类被用来载入、管理和保存资源文件。载入资源的语法非常直观:
CL_ResourceManager resources("resources.xml");
CL_Texture texture(gc, "section1/texture1", &resources);
CL_SoundBuffer sample("my-sample", &resources);
上面的语法是载入资源的常用方式,不过勒,事实上在一個资源XML 中定义的所有对象都是用CL_Resource 对象来表示的。为咯演示这個对象的用法,说一下上面那個用来载入材质的CL_Texture 的构造函数的可能的实现代码:
CL_ResourceManager resources("resources.xml");
CL_Resource resource = resources.get_resource("section1/texture1");
CL_String filename = resource.get_element().get_attribute("file");
CL_PixelBuffer image = CL_ImageProviderFactory::load(filename, resources.get_directory());
CL_Texture texture(gc, image);
我们可以同样的方式来创建我们自定义的资源的载入器:
CL_String load_my_type(const CL_String &res_id, CL_ResourceManager &resources)
{
CL_Resource resource = resources.get_resource(res_id);
if (resource.get_type() != "my-type")
throw CL_Exception("Resource not of expected type!");
CL_DomElement text_element = resource.get_element().named_item("text").to_element();
return text_element.get_text();
}
CL_ResourceManager resources("resources.xml");
CL_Console::write_line(load_my_type("section2/section3/my-resource1", resources);
HxLauncher: Launch Android applications by voice commands