Search code examples
androidopengl-esscreen-orientationandroid-contextunlock

OpenGL ES context squashed on unlock - landscape to portrait


Working on a game that only runs in Portrait mode, I tried forcing sensorPortrait via the manifest:

    android:configChanges="locale|orientation|keyboardHidden|screenSize|screenLayout"
    android:screenOrientation="sensorPortrait"

But when I lock the device in portrait, rotate it to landscape and then unlock it this happens:

landscape

And this is How it looks in portrait:

portrait

I think this happens because onSurfaceChanged is called twice but AFAIK there's nothing I can really do about that.

In the renderer I use:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    if (b_GameRunning) {
        // mark the fact that textures need reloading
        b_SurfaceWasChanged = true; 
    }
}

public void onSurfaceChanged(GL10 gl, int w, int h) {
    gl.glMatrixMode( GL_PROJECTION );
    gl.glLoadIdentity();
    n_width = w;
    n_height = h;
    gl.glOrthof(0.f, (float)m_width, (float)m_height, 0.f, -1.f, 1.0f);
}

public void onDrawFrame(GL10 gl) {
    if (m_bGameRunning) {
        if (b_SurfaceWasChanged) {
            b_SurfaceWasChanged = false;
            ReloadTextures();
        }
        GameRender();
    }
}

Solution

  • Many Thanks to harism.

    I changed onSurfaceChanged to:

    public void onSurfaceChanged(GL10 gl, int w, int h) {
        n_width = w;
        n_height = h;
        gl.glViewport(0, 0, n_width, n_height);
        gl.glMatrixMode( GL_PROJECTION );
        gl.glLoadIdentity();
        gl.glOrthof(0.f, (float)m_width, (float)m_height, 0.f, -1.f, 1.0f);
    }