I started to plunge into android and make an application that runs OpenGL ES 2. Everything is going pretty well but there is one thing I don't understand and it may sound extremely stupid but: How do run an init or start up method only once when creating a GLSurfaceView?
I have a method called StartUpInit that I want to call only once. Basically this method is where I would put any start up code, so things like initial OpenGL system creation, config loading, game save loads, etc.
My problem happens when the screen orientation changes. I see that my whole GLSurfaceView object / renderer get recreated from scratch breaking my start up code. How can I run my method only once even if the screen orientation changes?
The reason I need / have the start up method in the onSurfaceChanged is that some of my init code requires an OpenGL context for its calls and I need the width and height.
Any help would be awesome
Current code:
//Activity class
class App extends Activity
{
private MyRenderer renderer;
private MySurface surface;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
renderer = new MyRenderer();
surface = new MySurface(this.getBaseContext(), renderer);
setContentView(surface);
}
/*Other methods */
}
//Renderer Class
class MyRenderer implements GLSurfaceView.Renderer
{
public boolean startUp;
public float renderWidth;
public float renderHeight;
public MyRenderer()
{
startUp = true;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
/* init OpenGL calls EG: glClearColor */
}
public void onSurfaceChanged(GL10 gl, int width, int height)
{
renderWidth = width;
renderHeight = height;
if(startUp)
{
StartUpInit(renderWidth, renderHeight);
startUp = false;
}
}
public void onDrawFrame(GL10 gl)
{
/* Draw Stuff */
}
}
//Surface Class
class MySurface extends GLSurfaceView
{
public MySurface(Context context, MyRenderer renderer)
{
super(context);
setEGLContextClientVersion(2);
setRenderer(renderer);
}
/*Other methods */
}