Last few days I played with
AndEngine, an open source, free, games framework for Android. I'm not sure yet if AndEngine will save time building new games.
Anyhow, one weird part in AndEngine is the way you load all the resources into a single canvas. You have to define the location of each resource on the canvas, and if you change the resource size you have to recalculate everything.
Here is a simple extension to BitmapTextureAtlas that calculate everything automatically and saves you the trouble of positioning the resources on the canvas.
public class BitmapTextureAtlasEx extends BitmapTextureAtlas {
int mUsedWidth;
public BitmapTextureAtlasEx(int width, int height, TextureOptions options) {
super(width, height, options);
mUsedWidth = 0;
}
public TextureRegion appendTextureAsset(Context c, String assetPath) {
TextureRegion region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
this, c, assetPath, mUsedWidth, 0);
mUsedWidth += region.getWidth();
return region;
}
public TiledTextureRegion appendTiledAsset(Context c, String assetPath, int tiledColumns, int tiledRows) {
TiledTextureRegion region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(
this, c, assetPath, mUsedWidth, 0, tiledColumns, tiledRows);
mUsedWidth += region.getWidth();
return region;
}
}
And this is how to use it (in the onLoadResources funcation):
mTexture = new BitmapTextureAtlasEx(1024, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
mTextureRegion = this.mTexture.appendTextureAsset(this, "texture.png");
mTiledRegion = this.mTexture.appendTiledAsset(this, "tile.png", 1, 1);
No comments:
Post a Comment