I’ve never really been a fan of using Strings and hash maps to store and use my game’s images. It leads to ugly code, and a small performance impact (I know… premature optimization blah blah). There is also no compile-time checking or content assist, which I’ve fallen in love with since the introduction of enums and generics.
So here is a crazy idea… Let’s say you’re already pre-processing your game’s assets into texture atlases with a tool like Texturepacker-GUI or TexturePacker2. During this step, we could also generate a Java source file for an Enum class which contains all of our resources. For example, take the following input images that will be packed into a TextureAtlas:
grass.png
font_small.png
pbar1.png
blur_box.png
Our pre-processing step generates the following Enum source code:
public enum Images {
grass,
font_small,
pbar1,
blur_box,
}
Then, we could render a particular sprite like so:
//convenience method...
public AtlasRegion getRegion(Images image) {
return atlas.getRegions().get(image.ordinal());
}
public void render() {
batch.draw( getRegion(Images.font_small), 0, 0 );
}
And with a little tweaking could generate nested public static enums for sub-directories in our input, so that we could do things like this:
[icode]Images.hud.text.font_small;[/icode]
It would also simplify things like animations. For example, you could generate a final array after your enum declarations:
public enum Images {
//generated from image file names
grass,
playerWalk1,
playerWalk2,
playerWalk3;
//generated arrays, for images with associates indices
public static final Images[] playerWalk = { playerWalk1, playerWalk2, playerWalk3 };
}
It would be ideal if you could re-generate the atlas and enum source automatically whenever the image folder is changed.
Anyways… Just a crazy thought. Probably not a great idea in practice, but it sounds interesting on paper.