This is a problem I've been having for a while and I'm hoping someone here might be able to shed some light on it.
I have an Android game which loads a GLSurfaceView who's renderer is set up like this:
public class GameRenderer implements GLSurfaceView.Renderer
{
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
BaseLib.init(); // Initializes the native code
}
public void onSurfaceChanged(GL10 gl, int width, int height)
{}
public void onDrawFrame(GL10 gl)
{
BaseLib.render(); // Call to native method to render/update the current frame
}
}
The view is like this:
public class GameView extends GLSurfaceView implements SurfaceHolder.Callback
{
private GameRenderer _renderer;
private GameListener _listener;
public GameView(Context context)
{
super(context);
this._renderer = new GameRenderer();
setRenderer(this._renderer);
this._listener = new GameListener();
BaseLib.setListener(this._listener);
}
public boolean onTouchEvent(final MotionEvent event)
{
int action = -1;
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: action = 0; break;
case MotionEvent.ACTION_UP: action = 1; break;
case MotionEvent.ACTION_MOVE: action = 2; break;
}
if(action >= 0)
BaseLib.touch(action, event.getX(), event.getY());
return true;
}
}
I've been tracing through the native code and I'm noticing a problem where midway through the touch
event it appears another render()
call is being made. Because the touch event hasn't finished it ends up producing some errors due to it trying to render objects that haven't completed loading yet.
This might be a problem in the native code itself but as I haven't been able to find the problem there so far I wanted to ask if it's possible the Java code might be interrupting the call to touch
and calling another onDrawFrame()
before it completes.
Touch events and rendering happen on two different threads. You have to properly synchronize your data read/write.