俺とプログラミング

某IT企業でエンジニアをしてます。このブログではプログラミングに関わることを幅広く発信します。

【libGDX入門】画像に関する基本

libGDXにおける画像の取り扱いについて基礎的な部分を解説します。SpriteBatch, Texture, TextureRegion, Spriteについて順番にみていきます。

SpriteBatch

SpriteBatchは画像をGPUで効率的に描画するためのクラスです。 画像を扱う際には必ず現れます。でもScene2d使いはじめるとあまり見かけなくなるのがこのSpriteBatch。 
実際利用する際には、SpriteBatchのbeginとendの間に画像を描画するということだけ分かっていればOK。

public class MyGdxGame extends ApplicationAdapter {
    private SpriteBatch batch;

    public void create () {
        batch = new SpriteBatch();
    }

    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        /* ここに画像を描画する */
        batch.end();
    }
}

Texture

Textureは画像の最小単位であり、画像に関する情報のみ保持します。 読み込み可能な画像形式はpng, jpg, bmpです。
以下のプログラムではrender内でpositionXを更新してtextureを横に移動します。

public class MyGdxGame extends ApplicationAdapter {
    private SpriteBatch batch;
    Texture texture;
    float positionX;
    float positionY;

    public void create () {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("badlogic.jpg"));
        positionX = 10;
        positionY = 10;
    }

    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw( texture, positionX, positionY );
        batch.end(); 
        
        positionX += 0.1f;
    }
}

f:id:ttlg:20151027150005p:plain

batch.drawメソッドで画像を描画できます。batchを引数にして、その引き渡し先でdrawメソッドを呼ぶという使い方もできます。後述するSpriteがそれです。

Gdx.files.internalはWorking Directoryからの相対パスでファイルの位置を指定できます。 通常、 Working Directoryandroid/assets フォルダです。 ただし、intenalで指定するパスは読み込み専用なので、書き込みなどはできません。

TextureRegion

TextureRegionはその名の通り、Textureの一部分のことです。 複数のフレームを一つの画像に入れてアニメーションにする時などに活躍します。コンストラクタの引数でTextureの(x1, y1)から(x2, y2)の短形で切り取ることを指定します。

public class MyGdxGame extends ApplicationAdapter {
    private SpriteBatch batch;
    Texture texture;
    TextureRegion textureRegion;
    float positionX;
    float positionY;

    public void create () {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("badlogic.jpg"));
        textureRegion = new TextureRegion(texture, 50, 50, 100, 100);
        positionX = 10;
        positionY = 10;
    }

    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw( textureRegion, positionX, positionY );
        batch.end(); 
  
        positionX += 0.1f;
    }
}

f:id:ttlg:20151027150024p:plain


Sprite

SpriteはTextureに位置や回転などの情報を加えたクラスです。 Textureは位置情報などを持ってないので、上述のように冗長な表現になっていましたが、SpriteにはsetPositionで位置情報を保持できます。生成方法はTextureもしくはTextureRegionをコンストラクタに渡すだけです。

public class MyGdxGame extends ApplicationAdapter {
    private SpriteBatch batch;
    Texture texture;
    Sprite sprite;

    public void create () {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("badlogic.jpg"));
        sprite = new Sprite(texture);
        sprite.setPosition( 10, 10 );
        sprite.setRotation( 45 );
    }

    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        sprite.draw(batch);
        batch.end(); 
        
        sprite.translate(0.1f, 0);
    }
}

f:id:ttlg:20151027150031p:plain

translateは移動量を指定できます。rotate、scaleなどもあります。
他の便利メソッドについては、公式APIを参照ください。


Androidゲームプログラミング A to Z

Copyright © 2016 ttlg All Rights Reserved.