Texture Manager for AndEngine Android App

This time I tell about how I manage Textures for all graphics in my Android Game. Actually I figure it out during refactoring of Remember Me.

Examples for AndEngine (Android game and apps engine) comes with no texture management. There is just statements for create new Texture, load texture region and finally load texture from Engine.

It is simple but after adding more textures becomes hard to read, add and remove textures.

For my Android Applications I use simple manager that handle textures in one Engine (Application Activity) scope.

Base of this manager is Map of Enums as a key and TextureHolder as a value.

TextureHolder has fields for Font, TextureRegion, TiledTextureRegion and Texture.
Object of this class contains Texture and one of Region or Font field filled, depends of type of handler.

To manage calling bad texture region type I use TextureType enum of fields REGION, TILED, FONT.

In TextureHolder all fields will be accessible from getters, inside each getter method type of TextureHolder is checked and for bad type call BadTextureException is thowed.

public static class TextureHolder {
		private Texture texture;
		private Font font;
		private TextureRegion region;
		private TiledTextureRegion tiledRegion;
		private boolean loaded;
		private TextureType type;
		
		public Texture getTexture() {
			return texture;
		}
	
		public Font getFont() {
			if (TextureType.FONT.equals(type)) {
				return font;
			}
			throw new BadTextureException(TextureType.FONT, type);
		}
		
		public TextureRegion getRegion() {
			if (TextureType.REGION.equals(type)) {
				return region;
			}
			throw new BadTextureException(TextureType.REGION, type);
		}
		
		public TiledTextureRegion getTiledRegion() {
			if (TextureType.TILED.equals(type)) {
				return tiledRegion;
			}
			throw new BadTextureException(TextureType.TILED, type);
		}
	}
	
	public static enum TextureType {
		TILED,
		REGION,
		FONT
	}
	
	public static class BadTextureException extends RuntimeException {
		private static final long serialVersionUID = -1;

		public BadTextureException(TextureType expected, TextureType found) {
			super("Bad texture type. Expected: " + expected.name() + ", found: " + found.name());
		}
	}

Now TextureManager will create Map entry for all types of textures, REGION, TILED and FONT.

	public void addFont(Enum<?> name, int size, int color, Typeface typeface) {
		TextureHolder holder = new TextureHolder();
		holder.type = TextureType.FONT;
		holder.texture = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
	    holder.font = FontFactory.create(holder.texture, typeface, size, true, color);
	    holders.put(name, holder);
	}

Because I don’t know when all textures will be loaded I prefer to create method for loading them. This method must be used by game after define all Textures.

	public void load() {
		for (TextureHolder holder : holders.values()) {
			if (!holder.loaded) {
				activity.getEngine().getTextureManager().loadTexture(holder.texture);
				if (holder.font != null) {
					activity.getEngine().getFontManager().loadFont(holder.font);
				}
			}
		}
	}

Finally usage of this TextureManager is simple.

I create enum and object of TextureManager in my BaseGameActivity.

private enum Textures {
  PLANE,
  ROCK
}

TextureManager textureManager = new TextureManager(this);

Then load all textures in onLoadResources method.

textureManager.addTiled(Textures.PLANE, 64*5, 64, "gfx/plane.png", 5, 1);

In sample class I just use resourceManager to get TiledTextureRegion.

textureManager.get(Textures.PLANE);

Why this is better solution than simple manage textures by fields
First of all my game code is smaller. Smaller code = less bugs.

Second and more more important thing is that solution gives me simple way to manage textures. If I want to remove one texture I will just remove it from enum, then I will see where this texture was used and clean all code.

Happy Codding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s