I'm trying to create a GLSurfaceView that displays a map of a game area. When the player moves, the game activity calls highlightSpot, which in turn should trigger a render request. The only time I want to re-draw the view is when the player moves.
However, with my current implementation, despite calling setRenderMode(RENDERMODE_WHEN_DIRTY)
on my GLSurfaceView, its render mode still seems to be continuous. To check, I threw a single println statement in my onDrawFrame method, and when I run my application, the output quickly fills up my logcat without the player moving even once-- it's clearly not behaving as I intended. Is there something else I need to do in order to make the view render only when asked?
(The bulk of this code is derived from the tutorials at http://insanitydesign.com/wp/projects/nehe-android-ports/. I omitted my onDrawFrame, OnSurfaceChanged, and onSurfaceCreated methods for the sake of conciseness, as I am not changing the render mode or requesting a render anywhere in those methods. If someone thinks it might be relevant, I can post those too.)
public class SurfaceViewClass extends GLSurfaceView implements Renderer {
public SurfaceViewClass(Context context) {
super(context);
...
this.setRenderer(this);
this.setRenderMode(RENDERMODE_WHEN_DIRTY);
}
public void highlightSpot(int x, int y) {
/* change some variables here */
...
this.requestRender();
}
}
OK, I think I got this sorted out. The place to set the render mode seems to be the class that contains your GLSurfaceView object, not in the GLSurfaceView constructor. Also (something I think I overlooked in the Android documentation for GLSurfaceView) you can't set the render mode of the GLSurfaceView before you set its renderer. This is perhaps why attempting to set the render mode in the constructor does not work.
This seems to force it to render only when I want it to, which is exactly what I wanted:
public class Game extends Activity {
private GLSurfaceView glSurface;
private SurfaceViewClass svc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
glSurface = (GLSurfaceView) findViewById(R.id.SurfaceView01);
svc = new SurfaceViewClass(this);
glSurface.setRenderer(svc);
glSurface.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public void movePlayer() {
svc.highlightSpot(location[PLAYER], 0);
glSurface.requestRender();
}
}