Sunday, February 17, 2013

LiveWallpaper updates to improve performance

I have been reading over some sample code in andengine's posts. I decided to incorporate some ideas here so as to save on battery life. I would like to thank efung and his Glimmer Live Wallpaper (with source!). It can be found at https://github.com/efung/glimmer. I encourage you all to check it out. Also I have been reading "AndEngine for Android Game Development Cookbook" by Jayme Schroeder, Brian Broyles. This book contains thorough explanations and lots of sample code recipes. I recommend it highly.

First of all when writing wallpaper it is best to set its priority to 1. The value should be an integer with higher integers given higher priority. You want your wallpaper to have low priority. This is set in the AndroidManifest.xml file. Here is the snippet with the change.
<intent-filter android:priority="1">
   <action android:name="android.service.wallpaper.WallpaperService"/>
</intent-filter>
There are several other modifications that need to be made to keep a livewallpaper from draining the battery. Secondly I changed ParticleSystem to BatchedSpriteParticleSystem. From what I understand this will increase performance without compromising on the battery life. You need to change the declaration of course.
final BatchedSpriteParticleSystem particleSystem = 
      new BatchedSpriteParticleSystem(new PointParticleEmitter
      (mParticleX, mParticleY), mParticleMinRate, mParticleMaxRate,
       mParticleMax, this.mFlowerTextureRegion, 
       this.getVertexBufferObjectManager());
Then you need to replace every instance of <Sprite> with <UncoloredSprite> like this:
particleSystem.addParticleInitializer(new BlendFunctionParticleInitializer
<UncoloredSprite>(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE));
The third change I made was to disable the accelerometer when the game was paused. Then of course onResume you will need to enable it.

@Override
public void onResumeGame() {
       super.onResumeGame();
       this.enableAccelerationSensor(this);
       }
             
protected boolean disableAccelerationSensor() {
        return this.mEngine.disableAccelerationSensor(this);
        }
@Override
public void onPauseGame() {
        super.onPauseGame();
        this.disableAccelerationSensor();
        }
I have committed all changes to github at https://github.com/RealWorldApplications/LiveWallpaperExample.