Passing GLSurfaceView context to a thread

441 Views Asked by At

I want my GL Renderer to be set to "DIRTY" and use a thread to control the FPS but for some reason the requestRender call seems to be skipped over all together. I have a feeling this could be something to do with the thread not getting the proper context. Does anybody know good practice for passing GLSurfaceView context to other threads?

Here is my thread:

public class GameLoop extends Thread {


    private GLSurfaceView myGLSurfaceView;
    private final static int maxFPS = 30;
    private final static int maxFrameSkips = 5;
    private final static int framePeriod = 1000 / maxFPS;

    public static boolean running;
    public final static String TAG = "input";

    public GameLoop(){
        myGLSurfaceView = MyLaunchActivity.getMyGLSurfaceView();
    }

@Override
public void run() {

    running = MyLaunchActivity.getRunning();
    long beginTime;
    long timeDiff;
    int sleepTime;
    int framesSkipped;


    sleepTime = 0;
    while (running) {
        running = MyLaunchActivity.getRunning();
        //Log.d(TAG, "gameRunning");
        beginTime = System.currentTimeMillis();
        framesSkipped = 0;
        GameLogic.update();
        myGLSurfaceView.requestRender();
        //Log.d(TAG, "rendered");
        timeDiff = System.currentTimeMillis() - beginTime;
        sleepTime = (int)(framePeriod - timeDiff);

        if (sleepTime > 0) {
            try{
                Thread.sleep(sleepTime);
                //Log.d(TAG, "sleeping" + String.valueOf(sleepTime));
            }catch(InterruptedException e){}
        }

        while(sleepTime < 0 && framesSkipped < maxFrameSkips){
            GameLogic.update();
            sleepTime += framePeriod;
            framesSkipped++;
            Log.d(TAG, "Frames Skipped");
        }




}
}


}

And here is the constructor for my surface view ...

 public MyGLSurfaceView(Context context, AttributeSet attrs){
    super(context, attrs);
            myGLSurfaceRenderer = new MyGLSurfaceRenderer();
    setRenderer(myGLSurfaceRenderer);
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
            Log.d(TAG, "GLSurfaceViewed");

}
1

There are 1 best solutions below

4
On
myGLSurfaceView = MyLaunchActivity.getMyGLSurfaceView();

Some questions about your code :

  • How do you set the context ?
  • Did you check that you create the GameLoop object after initializing the MyGLSurfaceView returned by MyLaunchActivity.getMyGLSurfaceView() ?

The first thing you can do is adding a catch clause for Exception in your run() method to see if there is any problem that explain why the method isn't called.