Question

i have a problem running my app using an external Thread for game loop in OpenGL ES 2.0.

here is my onCreate in my Starting class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    mGLView = new MyGLView(this);

    System.out.println(running);

    setContentView(mGLView);

    loop = new GameLoop();

    //loop.run();  <- this is supposed to run the loop but when its called,
    // nothing really appears on screen. just title of the app.


}

my GameLoop Class

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 = "test";

public GameLoop() {

}

@Override
public void run() {
    running = StartPoint.isRunning();
    long beginTime;
    long timeDiff;
    int sleepTime;
    int framesSkipped;
    sleepTime = 0;
    while (running) {

        running = StartPoint.isRunning();

        beginTime = System.currentTimeMillis();
        framesSkipped = 0;

        StartPoint.mGLView.requestRender(); // <- supposed to re-render?

        timeDiff = System.currentTimeMillis() - beginTime;
        sleepTime = (int) (framePeriod - timeDiff);

        if (sleepTime > 0) {
            try {
                Thread.sleep(sleepTime);

            } catch (InterruptedException e) {
            }
        }

        while (sleepTime < 0 && framesSkipped < maxFrameSkips) {

            sleepTime += framePeriod;
            framesSkipped++;
            Log.d(TAG, "Frames Skipped");
        }
    }

}

and finally my renderer class:

 private static final String TAG = "Renderer";

BoundsSquare square;

private final float[] mMVPMatrix = new float[16];
private final float[] mProjMatrix = new float[16];
private final float[] mVMatrix = new float[16];
private final float[] mRotationMatrix = new float[16];

private int camWidth,camHeight;

Camera2D cam;

Vector3 vec;
public long cycle = 0; // used this to determine how many cycles 
//went through, it is stuck on cycle 0, nothing else happens after first render

@Override
public void onDrawFrame(GL10 nope) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    //set camera position
            cam.setFrustum(mProjMatrix, mVMatrix, mMVPMatrix);

    square.draw(mMVPMatrix);
    //square.animate(tick);


    //square.setBounds(vec);
    System.out.println("Cycle " + cycle + " Ended");
    cycle++;

}

@Override
public void onSurfaceChanged(GL10 nope, int width, int height) {


    cam.setRatio(width, height);

    GLES20.glViewport(0, 0, width, height);

}

public static int loadShader(int type, String shaderCode) {

    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type); 

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
}

@Override
public void onSurfaceCreated(GL10 gl,
        javax.microedition.khronos.egl.EGLConfig config) {
    GLES20.glClearColor(0.0f, 0.0f, 1.0f, 0.5f);
    camWidth=480;camHeight=320;
    cam= new Camera2D(0, 0, camHeight, camWidth);
    // initialize a square
    vec = new Vector3 (10,10,40,90);
    square = new BoundsSquare(vec);
    System.out.println("Surface Created");

}

so basically what happens is that if i do not call loop.run(); i get a static picture, which is basically one cycle of onDrawFrame, after its done with this, nothing else pops up on the logCat.

the next thing that happens is if i do call loop.run(), basically the loop goes through forever and everything, but, nothing appears on the screen. i just see a title screen, not the glView.

What am i doing wrong? Is there another way to update the Screen?

Was it helpful?

Solution

Threads must be started with start() not run().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top