Using XML to load resources

I have a XML parser (JDOM) and a pretty simple game engine. I was wondering if there was a way to load XML tags in a type of “resource”. Resource being game assets, like levels, entities, and textures. I need to figure this out for a game engine I’m creating with a ‘Game Design Environment’. (As pretentious as that sounds ;))

How I need to use it is that there is a game resource XML file like this:
(Keep in mind this is an example, I don’t think XML files are laid out like this…)

EDIT: Used java comments because the syntax highlighting was weird…


<project>

//  <!-- The game's configuration -->
  <config>
     <displaymode width=800 height=600></displaymode>
  </config>

//  <!-- The game's resources -->
  <resources>
      <resource type="texture" path="./assets/textures/tex.png">Name-of-texture</resource>
      <resource type="level" path="./assets/levels/level.xml">Name-of-level</resource>
  </resources>
</project>

And the class to use in code:


//... Initialization
ResourceLoader loader = new ResourceLoader(new File("./assets/project.xml"), new SomeLogger("./assets/project.log"));
loader.parseXML();
//...

//... Use inside of game's class
Texture myTex = loader.getTexture("Name-of-texture");
Level myLevel = loader.getLevel("Name-of-level");
//...

But my problem is, how do I setup the resource loader class? Should I make HashMaps for different resources like textures, and such? Or to I make a “Resource” object that Textures, and Levels extend off of. Then have one hashmap for String resourceName, and Resource resource?

I may be doing this completely wrong, if there’s a better way to have applications like a ‘Game design environment’ interface with game resources, let me know!

There is really no one right way to do resource loading, but I think the main thing you want is resources to be very easy to find for yourself. When you are debugging for you, or the people who are playing your game, you have to figure out where your resources are.

The way I organize assets is by folder/ file name. In the classes, I make sure that each class has a slot for images. When I load the assets, I know exactly where the images are by giving each one a tag representing the file name. Using this system with an array of objects usually keeps things organized for me.

This is my own personal way I’ve done it for JSON, XML, Lua, JS… etc. But, there is no one right way. As long as you know where the data is being placed, it should be fine on any game.

Thanks for the reply

I should have constucted my question better :clue:. I mean how do I handle the resource loader class? Like having seperate hashmaps for each resource type. For example; one for images, one for shaders, and one for levels, each having a string as their key, representing their resource name. Or would I have one complete hashmap for key being string, and value being the resource. Then when you’re getting a resource, you have to cast it to whatever resource your loading it as.

Okay, I’ve made some progress, here is how the XML file looks right now. The configuration element is used for specifying engine configurations, such as what to do during the initialization, and the post game-loop. And the resources element is used to specify game resources, such as textures, levels, and entities.

<project>

//    <!-- Configuration -->
    <configuration>
        <initialize title="Some game" width=800 height=600></initialize>
    </configuration>
//    <!-- End configuration -->
	
    <resources>
//        <!-- Textures -->
        <resource type="texture" path="./resources/game-icon.png">test-icon</resource>
        
        <group name="level-0-resources">
        	<resource type="texture" path="./resources/texture1.png">test-texture1</resource>
        	<resource type="texture" path="./resources/texture2.png">test-texture2</resource>
        </group>
//        <!-- End textures -->
    </resources>

</project>

My question is how to handle this in the resource loader. I was thinking of having a resource class for holding a resource name, path, and type. Then during a state’s initialization, it registeres the texture, and can be “gotten” any time in the game loop. Then, in the state’s destroy method, it “removes” that texture, basically un-loading it. Making each state have its own resources. Good for actual loading times, rather than loading all resources at once.