libgdxメモ2

#2 - libgdx Tutorial: Game Screens で気になったところのメモ

Screenの遷移

setScreen()でScreenをセットできる

public SplashScreen getSplashScreen() {
    return new SplashScreen(this);
}

@Override
public void create() {
    Gdx.app.log(MyGdxGame.LOG, "Creating game");
    fpsLogger = new FPSLogger();
    setScreen(getSplashScreen());
}

Screenにはshow()とhide()メソッドがある。
show()はgame.setScreen()でScreenがセットされた時、 hide()は違うScreenになった時に呼び出される

作成したAbstractScreenを継承してScreenを作成すると良い

参考

TexturePacker

Texture Atlasが簡単に作成できる。詳しいことは別の記事で紹介する

libgdx-texturepacker-guiというものもある

画像の表示

TextureRegionを使って画像表示

については翻訳&理解しておきたい

package com.me.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class SplashScreen extends AbstractScreen {
    
    Texture splashTexture;
    TextureRegion splashTextureRegion;
    
    public SplashScreen(MyGdxGame game) {
        super(game);
    }
    
    @Override
    public void show() {
        super.show();
        
        // スプラッシュ用の画像をTextureとして読み込む
        splashTexture = new Texture("data/splash.png");
        
        // 縮小フィルタと拡大フィルタの方法を設定する
        splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        
        // TextureとTextureの位置を指定する
        splashTextureRegion = new TextureRegion(splashTexture, 0, 0, 512, 301);
    }
    
    @Override
    public void render(float delta) {
        super.render(delta);
        
        // 描画処理開始
        batch.begin();
        
        // TextureRegionを(0,0)から画面全体に表示する
        batch.draw(splashTextureRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        
        // 描画処理終了
        batch.end();
    }
    
    @Override
    public void dispose() {
        super.dispose();
        // Textureを破棄する
        splashTexture.dispose();
    }
    
}

TextureFilterには

  • Nearest(GL10.GL_NEAREST)
  • Linear(GL10.GL_LINEAR)
  • MipMap(GL10.GL_LINEAR_MIPMAP_LINEAR)
  • MipMapNearestNearest(GL10.GL_NEAREST_MIPMAP_NEAREST)
  • MipMapLinearNearest(GL10.GL_LINEAR_MIPMAP_NEAREST)
  • MipMapNearestLinear(GL10.GL_NEAREST_MIPMAP_LINEAR)
  • MipMapLinearLinear(GL10.GL_LINEAR_MIPMAP_LINEAR)

がある