JavaとJavaEEプログラマのブログ

JavaEEを中心にしたをソフトウェア開発についてのブログ

OpenGLで作るiPhone SDKゲームプログラミング 第2章ハエたたきゲームの移植に挑戦。

第1章のサンプルは、ほぼそのままAndroidでも動かすことができました。
引き続き、ハエたたきゲームも移植してみます。


EsGameプロジェクトを作成し、EsGameActivityクラスとEsGameRenderクラスを作成。
GraphicUtilsクラスとTextureLoaderクラスを適当なパッケージ下にコピー。
使用する画像や音声ファイルをリソース用のフォルダにコピー。

EsGameActivityクラスは、以前に作成したActivityと同じ。

public class EsGameActivity extends Activity {

	//OpenGLESビュー
	private GLSurfaceView gLSurfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //GLサーフェイスビューを作成して設定
        gLSurfaceView = new GLSurfaceView(this);

        gLSurfaceView.setRenderer(new EsGameRender(this));
        setContentView(gLSurfaceView);

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        gLSurfaceView.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        gLSurfaceView.onPause();
    }
}

EsGameRenderクラスで、バックグランド画像をテクスチャとして表示する。

package sample.game;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import sample.game.utils.GraphicUtils;
import sample.game.utils.TextureLoader;

import android.content.Context;
import android.opengl.GLSurfaceView;

public class EsGameRender implements GLSurfaceView.Renderer {

	private final int one = 0x10000;

	private Context context;

	public EsGameRender(Context context){
		this.context = context;
	}

	@Override
	public void onDrawFrame(GL10 gl10) {

		GraphicUtils.makeWorld(gl10);
		GraphicUtils.drawTextureRectangle(gl10, context, 0.0f, 0.0f, 2.0f, 3.0f, background, 0, 0, 0, one);
	}

	@Override
	public void onSurfaceChanged(GL10 gl10, int width, int height) {
		gl10.glViewport(0, 0, width, height);
	}

	@Override
	public void onSurfaceCreated(GL10 gl10, EGLConfig eglconfig) {
		background = TextureLoader.loadTexture(gl10, context, R.drawable.circuit);//リソース読み込み
	}

	private int background ;

}


iPhoneとほぼ同じです。