1
我想在我的Android遊戲中釋放紋理,緩衝區和着色器,如果用戶點擊返回按鈕,則調用完成()活動方法,導入onDestroy ),我重寫了清理遊戲數據,關閉服務器連接等。
我在我的清單中設置了android:launchMode =「singleTask」,因此調用finish()活動總是會導致立即銷燬。但是要使用glDeleteBuffers(...),例如,它必須從具有EGL上下文(Renderer線程)的線程調用,但即使設置並從Renderer類調用此類方法,我也會得到 - 無OpenGL ES上下文錯誤。Android OpenGL ES 2.0 - 在onDestroy中使用glDelete *()
我使用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());
}
在活動
@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
}