Question

I'm using AndEngine to create a live wallpaper. However, in my onLoadResources() method I am currently loading 3 different textures:

@Override
public void onLoadResources() {
    prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
    prefs.registerOnSharedPreferenceChangeListener(this);
    this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);
    this.mAutoParallaxImage1Texture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);
    this.mAutoParallaxImage2Texture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    this.mParallaxLayerBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "background.jpg", 0, 0);
    this.mParallaxLayerImage1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxImage1Texture, this, "image1.jpg", 0, 0);
    this.mParallaxLayerImage2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxImage2Texture, this, "image2.png", 0, 800);

    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxBackgroundTexture);  
    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxImage1Texture);               
    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxImag2Texture);               

}

I feel like this is not the most efficient way to do this. But if I have all my BitmapTextureAtlasTextureRegionFactorys 'images' pointing to just a single BitmapTextureAtlas, and those 'images' are at the same coordinates, the images will load together on top of each other even if I only call 1 of them.

An example of the problem is here:

    @Override
 public void onLoadResources() {
    prefs = PhysicsWallpaperActivity.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
    prefs.registerOnSharedPreferenceChangeListener(this);
    this.mAutoParallaxBackgroundTexture = new BitmapTextureAtlas(2048, 2048, TextureOptions.DEFAULT);

    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    this.mParallaxLayerBackground = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "background.jpg", 0, 0);
    this.mParallaxLayerImage1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image1.jpg", 0, 0;
    this.mParallaxLayerImage2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mAutoParallaxBackgroundTexture, this, "image2.png", 0, 800);

    this.mEngine.getTextureManager().loadTexture(this.mAutoParallaxBackgroundTexture);               

}

^ I would have liked to use the code like this because it seems more efficient, but 'image1' and 'image2' automatically load on top of each other even if I only call 1 of them.

Am I doing this the right way? Or is there a more efficient way?

Was it helpful?

Solution

Your textures can contain more than one resource. Think of it like a sprite sheet. or like gluing photos to a big white sheet of paper. you do not need a new texture for each texture region. For example: Say you have a star image, that is 250x250 pixels and a spaceship image that is 100x100 pixels. You could put both of these on a texture that measure 512x256. You create a texture region for the star(250x250) at 0, 0. Then create a second texture region for the spaceship at 251, 0.

In that scenario there is still some leftover area for other images blow the spaceship, if they are small enough to fit. The Image below shows how your texture would look if you could see it in memory.

texture with two resources added

Hope this helps.

OTHER TIPS

Your Textures are huge! Are you very sure you need them that big?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top