diff android-project/src/org/libsdl/app/SDLActivity.java @ 5227:c66b2a778b7e

Try to create an OpenGL ES 2.0 context on Android and successfully fall back to OpenGL ES 1.1 if that fails.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 07 Feb 2011 17:44:07 -0800
parents a514bfe6952a
children
line wrap: on
line diff
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Feb 07 16:45:40 2011 -0800
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Feb 07 17:44:07 2011 -0800
@@ -101,8 +101,8 @@
 
     // Java functions called from C
 
-    public static void createGLContext() {
-        mSurface.initEGL();
+    public static boolean createGLContext(int majorVersion, int minorVersion) {
+        return mSurface.initEGL(majorVersion, minorVersion);
     }
 
     public static void flipBuffers() {
@@ -351,11 +351,10 @@
 
 
     // EGL functions
-    public boolean initEGL() {
-        Log.v("SDL", "Starting up");
+    public boolean initEGL(int majorVersion, int minorVersion) {
+        Log.v("SDL", "Starting up OpenGL ES " + majorVersion + "." + minorVersion);
 
         try {
-
             EGL10 egl = (EGL10)EGLContext.getEGL();
 
             EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
@@ -363,20 +362,43 @@
             int[] version = new int[2];
             egl.eglInitialize(dpy, version);
 
+            int EGL_OPENGL_ES_BIT = 1;
+            int EGL_OPENGL_ES2_BIT = 4;
+            int renderableType = 0;
+            if (majorVersion == 2) {
+                renderableType = EGL_OPENGL_ES2_BIT;
+            } else if (majorVersion == 1) {
+                renderableType = EGL_OPENGL_ES_BIT;
+            }
             int[] configSpec = {
-                    //EGL10.EGL_DEPTH_SIZE,   16,
-                    EGL10.EGL_NONE
+                //EGL10.EGL_DEPTH_SIZE,   16,
+                EGL10.EGL_RENDERABLE_TYPE, renderableType,
+                EGL10.EGL_NONE
             };
             EGLConfig[] configs = new EGLConfig[1];
             int[] num_config = new int[1];
-            egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config);
+            if (!egl.eglChooseConfig(dpy, configSpec, configs, 1, num_config) || num_config[0] == 0) {
+                Log.e("SDL", "No EGL config available");
+                return false;
+            }
             EGLConfig config = configs[0];
 
             EGLContext ctx = egl.eglCreateContext(dpy, config, EGL10.EGL_NO_CONTEXT, null);
+            if (ctx == EGL10.EGL_NO_CONTEXT) {
+                Log.e("SDL", "Couldn't create context");
+                return false;
+            }
 
             EGLSurface surface = egl.eglCreateWindowSurface(dpy, config, this, null);
+            if (surface == EGL10.EGL_NO_SURFACE) {
+                Log.e("SDL", "Couldn't create surface");
+                return false;
+            }
 
-            egl.eglMakeCurrent(dpy, surface, surface, ctx);
+            if (!egl.eglMakeCurrent(dpy, surface, surface, ctx)) {
+                Log.e("SDL", "Couldn't make context current");
+                return false;
+            }
 
             mEGLContext = ctx;
             mEGLDisplay = dpy;