Issue
I am trying to improve openframeworks so that the GL context is preserved when pausing and resuming my game. So that I don't have to reload all textures after each pause. I don't expect you to know openframeworks code, I'll explain my problem independent of it.
I have java activity which loads a main_layout.xml file with a RelativeLayout inside another RelativeLayout. Then I instantiate a GLSurfaceView and add it to the inner RelativeLayout using glContainer.addView(glView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
. In onPause function, I call glView.onPause();
on GLSurfaceView and in onResume, I call glView.onResume();
on GLSurfaceView.
This code works but it destroys the gl surface in onPause (GLSurfaceView.surfaceDestroyed gets called on my GLSurfaceView, which is extended to OFGLSurfaceView, so that I can intercept this call).
To fix this, I added call to GLSurfaceView.setPreserveEGLContextOnPause with param 'true' to initialization of my GLSurfaceView. Now I see only black screen after pausing and resuming the game. I found out, that I can fix this by removing the GLSurfaceView from parent RelativeLayout and adding it back right away in onResume function: glContainer.removeView( glView ); glContainer.addView( glView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) );
. This however destroys the gl surface again and forces me to reload textures.
There are no reinstantiations of either one of the RelativeLayouts or the GLSurfaceView (I checked it by looking at the code and then checking identities of those instances with System.identityHashCode).
main_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:id="@+id/of_gl_surface_container" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<!-- add here other views' layouts -->
</RelativeLayout>
Code in OFAndroidLifeCycle.java which handles events received from Activity:
public static void glCreateSurface()
{
if(mGLView == null)
{
mGLView = new OFGLSurfaceView(m_activity);
OFGLSurfaceView glView = getGLView();
glView.setPreserveEGLContextOnPause( true );
ViewGroup parent = (ViewGroup)glView.getParent();
if( parent == null )
{
ViewGroup glContainer = getActivity().getSurfaceContainer();
glContainer.addView(glView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
}
public static void glPause()
{
OFGLSurfaceView glView = getGLView();
if( glView != null )
glView.onPause();
}
public static void glResume(ViewGroup glContainer)
{
OFGLSurfaceView glView = getGLView();
if( glView != null )
{
glView.onResume();
glContainer.removeView( glView );
glContainer.addView( glView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) );
}
}
Well, I am just confused. Can someone elaborate a bit about how this should be done, what are some pitfalls or what approaches should I try?
Thanks.
Solution
I later also created this question on gamedev.stackexchange.com and today I posted the solution there:
Answered By - Ivorne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.