Problem setting up GLSurfaceView.Renderer

Started by
2 comments, last by Fl00Fy 10 years, 7 months ago

I'm trying to create an android app using OpenGL ES, but having some trouble with the GLSurfaceView.Renderer.

Here's some of my code:


public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature (Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                     WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(new MainView(this));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

public class MainView extends GLSurfaceView {

	private GLRenderer renderer;
	
	public MainView(Context context) {
		super(context);
		setEGLContextClientVersion(2);
		renderer = new GLRenderer (context, this);
		setRenderer (renderer);
		setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
	}
}

public class GLRenderer implements Renderer {

	private MainView view;
	private MainGame game; //This is where the game logic begins.
	private GLRes gl20; //This is a class to initialise and store resources such as matrices and shaders
	
	public GLRenderer (Context context, MainView view)
	{
		this.view = view;
		gl20 = new GLRes (context);
	}
	
	@Override
	public void onDrawFrame(GL10 gl) {
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
		game.draw();
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) 
	{
		gl20.onSurfaceChanged(width, height);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) 
	{
		gl20.onSurfaceCreated();

		game = new MainGame (view, gl20);
		game.setRunning(true);
		game.start();
	}
	
	public void onSurfaceDestroyed ()
	{
		game.setRunning(false);
	}
	
	public void onTouchEvent (MotionEvent event)
	{
		game.handleTouchEvent(event);
	}
}

The constructor of the Renderer is called as expected, but its onSurfaceCreated() is not, and therefore none of the other methods either, including onDrawFrame(). My GLSurfaceView's onSurfaceCreated is, however, called. This leads to me assume that I haven't set something up properly somewhere, regarding OpenGL and the Renderer, but I can't think what or where that could be.

Advertisement

I can't see anything wrong with your OpenGL setup.

I even tried running your sample code on my phone, and it works okay.

Do you get any runtime errors?

Does the device support OpenGl ES 2? Maybe try ES 1 and see if it works.

Are you using any other threads? It's possible the flow of execution is stuck some where else in the code.

No runtime errors at all.

I've tested it on two devices, both of which are supposedly 2.0 compatible, but I tried switching it to 1.0 anyway and it didn't make any difference.

I haven't implemented any threads, no. I was under the impression OpenGL ES isn't compatible with threads?

I can't think of what else might be causing this. I am also currently using the Kindle Development Kit, as one of my devices is the Kindle Fire. I don't know if maybe there's something about that kit which I don't know? I haven't found anything concerning OpenGL problems on the Kindle though.

I'll create another project today and setup and test OpenGL again, and see if I can get that working.

I created another project and it worked perfectly fine, and also helped me realise where I made a mistake, and a very stupid mistake it was. In my GLSurfaceView implementation, I still had overridden, empty stubs for its onSurfaceCreated() and onSurfaceChanged() methods.

Thank you for the help Angex.

This topic is closed to new replies.

Advertisement