How to load images as background in LibGDx?

Tomeksss picture Tomeksss · Jan 10, 2013 · Viewed 12.2k times · Source

I make live wallpaper and I have one issue. I want to set image as background and Eclipse show error:

01-10 10:37:18.206: E/AndroidRuntime(1045): FATAL EXCEPTION: GLThread 99
01-10 10:37:18.206: E/AndroidRuntime(1045): com.badlogic.gdx.utils.GdxRuntimeException: Texture width and height must be powers of two: 480x800

My code looks like:

public class Tapeta implements ApplicationListener {


    private SpriteBatch batch;
    private Texture texture;
    private TextureRegion region;


    @Override
    public void create() {      
        texture = new Texture(Gdx.files.internal("data/cat.jpg"));
        Texture.setEnforcePotImages(false); 

    }

    @Override
    public void dispose() {
        texture.dispose();
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
        batch.begin();
        batch.draw(texture, 0, 0);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

I'm looking for solution of my issue and I didn't find.

Answer

P.T. picture P.T. · Jan 10, 2013

In this case the libGDX error message is actually helpful!

Texture width and height must be powers of two: 480x800

So the texture width and height must be one of: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 (neither 480 nor 800 are on that list ...)

You've got this magic line:

Texture.setEnforcePotImages(false);

but its after the texture is loaded. If you put it before the texture load, does the exception go away? (Including more of the exception backtrace in your question would be helpful). I believe this method is only available for the case where you know the underlying OpenGL ES 1.0 system supports the non-power-of-two texture extension. Setting this flag won't fix hardware that really does not support power-of-two textures. (I've no idea how much such hardware is left in the world though, so it may be harmless these days.)

The power-of-two texture requirement is a restriction of the hardware and OpenGL ES. This requirement was dropped from OpenGL ES 2.0.

Another way to get around this requirement is to build a texture atlas that is power-of-two sized, and it contains your not-a-power-of-two textures within it. Then you render subsections of the atlas image. See http://bitiotic.com/blog/2012/05/10/tutorial-for-texturepacker-and-libgdx/