我想在我的 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/