Search code examples
libgdxtextures

LibGDX splicing up texture file


I have an image, which consists of blocks (32*32) and there is 1 row and 4 columns. How can I do, that when I load a texture, I only load one block and not all 4 of them?


Solution

  • Method 1)

        Texture texture=new Texture(Gdx.files.internal("ui/logo2.png"));
        Sprite sprite=new Sprite(texture);
        sprite.setRegion(0, 0, 1f/4f, 1);//this loads the first block
        sprite.setRegion(1f/4f, 0, 1f/4f+1f/4f, 1);//this loads the second block
        sprite.setRegion((1f/4f)*2, 0, 1f/4f+(1f/4f)*2, 1);//this loads the third block
        //on render
        sprite.draw(theSpriteBatch);   
    

    Method 2)

        Texture texture=new Texture(Gdx.files.internal("ui/logo2.png"));
        TextureRegion[][] tmp = TextureRegion.split(texture, 32, 32);
        Sprite sprite=new Sprite(tmp[0][0]);//first
        Sprite sprite=new Sprite(tmp[0][1]);//second
        Sprite sprite=new Sprite(tmp[0][2]);//third
        //on render
        sprite.draw(yourSpriteBatch);