1
votes

I have been tackling a severe performance issue with an AndEngine powered live wallpaper for a while now (I've posted another question about it a while back, before I knew what was causing it at all), and it seems that the only way to get this to work properly is to merge a few sprites together.

I have a half dozen or so fairly large sprites, all the same size, that get layered directly on top of each other. I do it this way so it all looks like one image, but each part of it can be set to a different color via code whenever I'd like. For some reason or other, layering sprites when using a blending mode that utilizes alpha at all (Even if there wasn't any alpha in the images) does some major damage to your frame rate, and I can't find any simple way around this.

The solution I've arrived at is that I will need to do what I need to with the sprites individually, and then merge them together into one. I've spent quite a few hours diving into various classes of AndEngine, trying not to destroy anything in the process, but I just cannot figure out how to do it.

I'm certain that it cannot be that difficult to slap one sprite onto another, or putting the re-worked sprites back into a temporary texture atlas somehow, I just can't quite grasp how to get it going.

EDIT ::

    public void onLoadResources(){
    this.mTestAtlas = new BitmapTextureAtlas(512, 1024, TextureOptions.NEAREST_PREMULTIPLYALPHA);

    Bitmap b = BitmapFactory.decodeStream(layer1);
    Bitmap a =  overlay(b);
    test = new BitmapTextureAtlasSource(a);

    this.mTesting = BitmapTextureAtlasTextureRegionFactory.createFromSource(this.mTestAtlas, test, 0, 0);

    this.getEngine().getTextureManager().loadTexture(this.mTestAtlas);      
}   

public Scene onLoadScene(){
    layer1Sprite = new Sprite(x, y, width, height, this.mTesting);
    scene.attachChild(layer1Sprite);
}

public static Bitmap overlay(Bitmap bmp1) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);

    Paint layer1Paint = new Paint();
    ColorFilter layer1Filter;
    layer1Filter = new LightingColorFilter(Color.rgb(175, 0, 175), 1);
    layer1Paint.setColorFilter(layer1Filter);
    canvas.drawBitmap(bmp4, 0, 0, layer1Paint);

   return bmOverlay;

}
1

1 Answers

1
votes

Andengine does not provide any tools or methods to do this, but fortunately, native Android does. Here is an article that shows how to make a function that inputs two bitmaps but outputs one, merged bitmap.

http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas There is a related question here: Android merge two images

Update for Andengine GLES2:

AS of December 2011, there is a new version of Andrengine called Andengine GLES2. It has a built in resource for drawing your andengine sprites into a textures. Here is teh release note:

RenderToTexture/RenderTexture

Usage:

Render something into a Texture and use that texture for Sprites. (i.e. useful when rendering some complex mesh once and then efficiently drawing it again as a sprite.) Post-Processing effects

And the link to the original forum post outlining additions to GLES2.

http://www.andengine.org/forums/announces/andengine-gles2-pre-release-on-december-23rd-t6097.html