5

I am trying to develop an Android live wallpaper using OpenGL wallpaper service , I am able to create live wallpaper as in this example by Mark F Guerra But I want to add some sprite animation to my wallpaper.

I have already created a OpenGL ES sprite animation in another project. I just want to recreate my animation in the live wallpaper project.

But in my live wallpaper project i am not able to get Context and load my images from assets or resources

Any suggestions or sample codes or link about loading resourses or asset files while using glwallpaper service will be very helpfull.

All suggestions and/or sample codes are welcome.

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
Renjith K N
  • 2,613
  • 2
  • 31
  • 53

2 Answers2

1

Pass the context from your engine to your renderer. Then, here's some sample code to load the asset. i.e. resourceID is your R.drawable.xxx bitmap. I have this inside a texture atlas class I made, so a few things might not be completely contained in the method. For example the options I might use to load the bitmap would include inscaled = false, but whatever works for you. I also modified this to remove my error handling for instance.

/**
 * Load the resource and push it to the gpu memory, setup default values
 * @param gl
 * @param context
 * @param resourceID
 * @return glTextureID
 * 
 */
public int loadFromContext(GL10 gl, Context context, int resourceID) {
    mResourceID = resourceID;
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resourceID, sBitmapOptions);
    sourceWidth = bmp.getWidth();
    sourceHeight = bmp.getHeight();
    gl.glGenTextures(1, mGLTextures, 0);
    mGLTextureID = mGLTextures[0];

    // bind and set min and mag scaling to bilinear
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mGLTextureID);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

    // repeat by default
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

    // upload bmp to video memory
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);

    // check error
    int error = gl.glGetError();
    if (error != GL10.GL_NO_ERROR) {
        // cleanup
        bmp.recycle();
        bmp = null;
        mLoaded = false;
            // error handling here
    } else {

        // unbind.
        gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
        bmp.recycle();
        bmp = null;
        mLoaded = true;
        mDirty = true;

      } 
      return mGLTextureID;
}
ratana
  • 175
  • 3
  • 12
1

We can use the context as shown below..

in wallpaper service class: 
------------------- 
renderer = new GlRenderer(this); 

in renderer class: 
---------------- 
private Context context; 

public GlRenderer(Context context) { 
this.context = context; 

Instead of this we can use getAssets() or getResources() as parameter to renderer .
On using getAssets() you can get the files saved in assets folder and by using getResources() you can get the files placed inside the resources folder in your project.

Lijo John
  • 578
  • 8
  • 21