I was using for sometime now OpenGL initialization code from Api demos
and it was quite hard to exchange events between View and Renderer.
So, I read pieces of documentation from GLES, EGL from jsr239 and
finally made the single threaded GL initialization code which is much
simpler than in ApiDemos. Here it is:

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;


public class MainActivitity extends Activity {
    @Override
    protected void onCreate(Bundle saved) {
      super.onResume();
      final EGL10 egl = (EGL10) EGLContext.getEGL();
      final EGLDisplay eglDisplay =
egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
      int[] version = new int[2];
      egl.eglInitialize(eglDisplay, version);

      int[] configSpec = {
          // EGL10.EGL_DEPTH_SIZE, 8,
          EGL10.EGL_NONE
      };

      final EGLConfig[] config = new EGLConfig[1];
      int num_configs[] = new int[1];
      egl.eglChooseConfig(eglDisplay, configSpec, config, 1, num_configs);
      final EGLContext eglContext =
        egl.eglCreateContext(eglDisplay, config[0], EGL10.EGL_NO_CONTEXT, null);

      // Setting up layouts and views
      SurfaceView view = new SurfaceView(this);
      setContentView(view);

      SurfaceHolder holder = view.getHolder();
      holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
      final GL10 gl = (GL10) eglContext.getGL();
      final Handler handler = new Handler();

      holder.addCallback(new Callback() {
        private EGLSurface surface;
        private Runnable painter;

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
          // initialize GL projection and other stuff
          // gl.glMatrixMode(GL10.GL_PROJECTION);
          // gl.glFrustumf(left, right, bottom, top, zNear, zFar);
          // ...
          painter = new Runnable() {
            @Override
            public void run() {
              drawFrame(gl);
              egl.eglSwapBuffers(eglDisplay, surface);
              handler.post(this);
            }
          };
          handler.post(painter);
        }
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
          surface =
            egl.eglCreateWindowSurface(eglDisplay, config[0], holder, null);
          egl.eglMakeCurrent(eglDisplay, surface, surface, eglContext);
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
          handler.removeCallbacks(painter);
        }
      });
    }

    private void drawFrame(GL10 gl) {
      // Frame drawing...
      long t = System.currentTimeMillis() % 10000;
      gl.glClearColor(t / (float) 10000, t / (float) 10000 ,1 , 1);
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to