Discussion:
[Clanlib-devel] Custom resources question
Thomi Richards
2006-08-15 05:57:47 UTC
Permalink
Hi guys,

Just a quick question:

I want to create a custom resource type that encapsulates several
builtin types. For example, my builtin type will look like this in a
resources.xml file:


<GameSprite>
<sprite name="foo">
<image file="blah.png" />
</sprite>
<string name="someopt" value="somevalue" />
</GameSprite>

At the moment I'm stuck. In my ResourceData_GameSprite::on_load()
class, I can't seem to load the sprites into a CL_Sprite class - the
resource manager can't see the <sprite> tag because it's hidden inside
a <GameSprite> tag. Is there a way to work around this?


I sure hope so...


TIA,
Thomi Richards
2006-08-15 08:43:43 UTC
Permalink
Hi guys,

After talking to mrfun on IRC I wrote a small patch - to the
CL_ResourceManager class. Basically it exposes
CL_ResourceManager_Generic::parse_section so that users can parse
arbitrary CL_DomElements. There is a small problem that I have not
been able to resolve(see below) - I'm hoping someone here can apply my
patch and help me solve the last minor issue.

Patch details:

* Removed the for loop in CL_ResourceManager_Generic::load that
called sig_resource_added() for each resource in the resources map -
added a line in CL_ResourceManager_Generic::parse_section that calls
it (I can't see any reason NOT to call it here).
* Added appropriate lines to expose
CL_ResourceManager_Generic::parse_section to the user. Added
appropriate documentation.


Minor issue details:

The small issue is that when using CL_Resourcemanager::parse_section()
you cannot use the empty CL_ResourceManager constructor. If you do, it
looks for the data files (images or whatever) in the application
directory, instead of the directory where the resources are.

Let me give an example; this is the on_load() method for a custom
resource (as described in my original post):

void CL_ResourceData_GameSprite::on_load()
{
CL_Resource resource = get_resource();
// do loading stuff here:
// NEED to use this constructor, even though this file has already been loaded.
CL_ResourceManager resources("data/resources.xml");
CL_DomElement e = resource.get_element();
// tell the resource manager to parse the XML data for this custom resource
resources.parse_section(e);
// extract the CL_Sprite from the XML data:
CL_Sprite *spr = new CL_Sprite(resource.get_name() + "/testSpr", &resources);
delete spr;

}


Ideally I'd like not to have to create another ResourceManager - and
certainly not have to re-parse the resources.xml file, but I can't see
any other way around it, except perhaps creating a public method that
allows programmers to set a custom data directory.

Any ideas?

Thanks,
Post by Thomi Richards
Hi guys,
I want to create a custom resource type that encapsulates several
builtin types. For example, my builtin type will look like this in a
<GameSprite>
<sprite name="foo">
<image file="blah.png" />
</sprite>
<string name="someopt" value="somevalue" />
</GameSprite>
At the moment I'm stuck. In my ResourceData_GameSprite::on_load()
class, I can't seem to load the sprites into a CL_Sprite class - the
resource manager can't see the <sprite> tag because it's hidden inside
a <GameSprite> tag. Is there a way to work around this?
I sure hope so...
TIA,
Loading...