检测OpenGl ES 2.0是否可用

7
我正在为Android创建一个应用程序,支持API级别>=7。其中一个屏幕使用GLSurfaceView和通过ndk使用OpenGL ES 2.0。如何检测是否支持OpenGL 2.0?我不能在AndroidManifest.xml中使用android:glEsVersion="0x00020000",因为我需要支持所有API级别>=7的手机。如果不支持2.0,我将显示静态屏幕。
我正在使用来自ndk的hello-gl2示例应用程序的类似代码。在GL2JNIView中,当它设置Opengl上下文时,如果没有找到适当的opengl配置(在我的情况下是需要opengl es 2.0的配置),它会抛出一个IllegalArgumentException("No configs match configSpec")异常并导致应用程序崩溃。我找不到拦截该异常并在该屏幕上执行其他操作的方法。有什么想法吗?
3个回答

9

这是我在互联网上找到的:

private boolean checkGL20Support( Context context )
{
    EGL10 egl = (EGL10) EGLContext.getEGL();       
    EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    int[] version = new int[2];
    egl.eglInitialize(display, version);

    int EGL_OPENGL_ES2_BIT = 4;
    int[] configAttribs =
    {
        EGL10.EGL_RED_SIZE, 4,
        EGL10.EGL_GREEN_SIZE, 4,
        EGL10.EGL_BLUE_SIZE, 4,
        EGL10.EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
        EGL10.EGL_NONE
    };

    EGLConfig[] configs = new EGLConfig[10];
    int[] num_config = new int[1];
    egl.eglChooseConfig(display, configAttribs, configs, 10, num_config);     
    egl.eglTerminate(display);
    return num_config[0] > 0;
} 

来源:http://www.badlogicgames.com/wordpress/?p=343

这篇文章介绍了如何使用LibGDX游戏引擎来创建一个简单的游戏。首先,你需要安装Java开发工具包(JDK)和Eclipse集成开发环境(IDE)。然后,你可以下载并导入LibGDX项目模板。在项目中,你将学习如何创建场景、角色和动画,并了解游戏循环的基本知识。最后,你将学习如何将游戏打包为可执行文件并在移动设备上运行。希望这篇文章对于想要学习如何使用LibGDX创建游戏的人有所帮助。

7
也许这可以帮助您。
@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);

  // Check if the system supports OpenGL ES 2.0.
  final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
  final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
  final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

  if (supportsEs2)
  {
    // Request an OpenGL ES 2.0 compatible context.

  }
  else
  {
    // This is where you could create an OpenGL ES 1.x compatible
    // renderer if you wanted to support both ES 1 and ES 2.

  }
}

1

来自 Android CTS(兼容性测试套件)的OpenGlEsVersionTest.java

private static int getVersionFromPackageManager(Context context) {
    PackageManager packageManager = context.getPackageManager();
    FeatureInfo[] featureInfos = packageManager.getSystemAvailableFeatures();
    if (featureInfos != null && featureInfos.length > 0) {
        for (FeatureInfo featureInfo : featureInfos) {
            // Null feature name means this feature is the open gl es version feature.
            if (featureInfo.name == null) {
                if (featureInfo.reqGlEsVersion != FeatureInfo.GL_ES_VERSION_UNDEFINED) {
                    return getMajorVersion(featureInfo.reqGlEsVersion);
                } else {
                    return 1; // Lack of property means OpenGL ES version 1
                }
            }
        }
    }
    return 1;
}

/** @see FeatureInfo#getGlEsVersion() */
private static int getMajorVersion(int glEsVersion) {
    return ((glEsVersion & 0xffff0000) >> 16);
}

它实际上还提供了其他几种方式,测试验证它们都返回相同的结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接