まずはメインループ
2013-11-02


package sys; import android.app.Activity; import android.content.Context; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.util.Log; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import javax.microedition.khronos.egl.EGL10; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.egl.EGLDisplay; import javax.microedition.khronos.opengles.GL10; /******************** アクティビティ ********************/ public class BaseActivity extends Activity { static { System.loadLibrary("native"); } private BaseView surface_view; // ビュー private BaseRenderer renderer; // レンダラー private ScheduledExecutorService executor; // 定期実行管理 private ScheduledFuture<?> future; public native void initNative(int _w, int _h); // native部初期化 public native boolean updateNative(); // native部稼働 /********** 開始 **********/ @Override protected void onCreate(Bundle _savedInstanceState) { super.onCreate(_savedInstanceState); renderer = new BaseRenderer(); // レンダラー作成 surface_view = new BaseView(getApplication(), renderer); // ビュー作成 setContentView(surface_view); executor = Executors.newSingleThreadScheduledExecutor(); // 定期実行管理 } /********** 終了 **********/ @Override protected void onDestroy() { super.onDestroy(); executor.shutdown(); } /************** 一時停止 **************/ @Override protected void onPause() { super.onPause(); if ( future != null ) { // 定期実行停止 future.cancel(false); future = null; } surface_view.onPause(); } /********** 再開 **********/ @Override protected void onResume() { super.onResume(); surface_view.onResume(); } /************ ビュー ************/ class BaseView extends GLSurfaceView { public BaseView(Context _context, BaseRenderer _renderer) { super(_context); setEGLContextClientVersion(2); // OpenGL ES 2.0使用 setEGLConfigChooser(new ConfigChooser()); setRenderer(_renderer); setRenderMode(RENDERMODE_WHEN_DIRTY); } class ConfigChooser implements GLSurfaceView.EGLConfigChooser { @Override public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { final int[] configAttribs = { EGL10.EGL_RED_SIZE, 8, EGL10.EGL_GREEN_SIZE, 8, EGL10.EGL_BLUE_SIZE, 8, EGL10.EGL_ALPHA_SIZE, 8, EGL10.EGL_NONE }; int[] num_config = new int[1]; egl.eglChooseConfig(display, configAttribs, null, 0, num_config); EGLConfig[] configs = new EGLConfig[num_config[0]]; egl.eglChooseConfig(display, configAttribs, configs, num_config[0], num_config); return configs[0]; } } } /**************** レンダラー ****************/ class BaseRenderer implements GLSurfaceView.Renderer { public void onSurfaceCreated(GL10 _gl, EGLConfig _config) {} public void onSurfaceChanged(GL10 gl, int width, int height) { initNative(width, height); // native部初期化 if ( future == null ) { // 定期実行開始 future = executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { surface_view.requestRender(); // 描画リクエスト } }, 0, 1000/30, TimeUnit.MILLISECONDS); } } public void onDrawFrame(GL10 gl) { if ( !updateNative() ) { // native部稼働 finish(); } } } } /***************** End of File ***************************************************/

続きを読む
戻る
[Android プログラミング]

コメント(全0件)
コメントをする


記事を書く
powered by ASAHIネット