java - Android OpenGL ES 2.0 - 在 onDestroy() 中使用 glDelete*

标签 java android c android-ndk opengl-es-2.0

我想在我的 Android 游戏中释放纹理、缓冲区和着色器,如果用户点击“后退”按钮,则调用 Activity 的 finish() 方法,从而进入 onDestroy() ,我重写它来清理游戏数据,关闭服务器连接等等。
我在 list 中设置了 android:launchMode="singleTask" ,因此调用 Activity 的 finish() 总是会立即导致销毁。
但是,例如,要使用 glDeleteBuffers(...),它必须从具有 EGL 上下文的线程(渲染器线程)调用,但即使从渲染器类设置并调用此类方法,我也得到 - 否OpenGL ES 上下文错误。

我使用 NDK,因此 NativeLib.* 调用 C/C++ 函数,例如

JNIEXPORT void JNICALL /*class path*/_NativeLib_onDrawFrame(JNIEnv* env, jclass _class)
{
 glClear(GL_COLOR_BUFFER_BIT);
 ...
}

查看

public class OGLES2View extends GLSurfaceView
{

  private static class OGLES2Renderer implements GLSurfaceView.Renderer
  {
    public void onDrawFrame(GL10 unused)
    {
        NativeLib.onDrawFrame();
    }

    public void onSurfaceChanged(GL10 unused, int width, int height)
    {
        NativeLib.onSurfaceChanged(width, height);
    }

    public void onSurfaceCreated(GL10 unused, EGLConfig unusedConfig)
    {
        NativeLib.onSurfaceCreated();
    }

    public void onSurfaceDestroy()
    {
        Log.i(LOG_TAG, "Destroying Opengl objects");
        NativeLib.onGLDestroy();//Don't work - call to OpenGL API with current context
    }

}

public OGLES2Renderer renderer = null; 
public OGLES2View(Context context)
{
    super(context);
    setRenderer(renderer = new OGLES2Renderer());
}

在 Activity 中

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    ogles2View = new OGLES2View(this);
    setContentView(ogles2View);
}

@Override
protected void onDestroy()
{
    Log.i(LOG_TAG, "Got finish request for game");
    super.onDestroy();
    ogles2View.render.onSurfaceDestroy(); // Don't not work
}

最佳答案

如果有人确实需要在销毁EGLContext之前在GLThread上运行某些东西(或者在给定方法之外执行的任何点),您可以使用queueEvent() GLSurfaceView 的方法将 Runnable 发布到 GLThread。请注意,您必须在使用 queueEvent() 之前调用 setRenderer()

示例(OP的问题):

...

public void onSurfaceDestroy()
{
    queueEvent(new Runnable() {
        @Override
        public void run() {
            Log.i(LOG_TAG, "Destroying Opengl objects");
            NativeLib.onGLDestroy(); //Works since it's called on the GLThread
        }
    }
}

...

关于java - Android OpenGL ES 2.0 - 在 onDestroy() 中使用 glDelete*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987770/

相关文章:

java - Java中典型的层次继承

java - Java 7 中的新 Swing JLayer

android - DELETE 请求无效,返回状态码 200

c - 一小时后 PING 超时

c - PS/2 键盘不会发送按键中断,但会响应命令

java - 使用 Java 的简单 2D 图形的最佳 API

java - java.io.FileOutputStream.write(int, boolean) native 方法的源代码

android - 从 adb 向 BroadcastReceiver 发送 Intent

android - 如何在 Android 中为数据库编写可重用代码

从 char * 复制到 char 数组