Manh Khôi Duong
Manh Khôi Duong

Reputation: 173

LibGDX Game Pause State: Animation flicker/bug

Im trying to implement a pause option for my following game. By pressing the back button my game should go to the updatePause() method. But instead it renders the latest 2-3 frames repeatedly. Making it look like it's "flickering".

By trying to prevent this I've added (in the comments) non-continously rendering which prevented the flickering. But then Im facing the problem that pressing the back-button more than once will render another frame. (one of those flickering frames). This is my code structure in short:

public class GameScreen implements Screen {
    private static final int GAME_RUNNING = 0;
    private static final int GAME_PAUSED = 1;
    private static final int GAME_OVER = 2;
    private static final int GAME_LEVEL_END = 3;
    private int gameState;

    private GdxGame game;

    //create()
    public GameScreen(final GdxGame game) {
        //create all the stuff
    }

    @Override
    public void render(float deltaTime) {
        switch (gameState) {
            case GAME_RUNNING:
                updateRunning(deltaTime);
                break;
            case GAME_PAUSED:
                updatePause(deltaTime);
                break;
            case GAME_OVER:
                updateGameOver();
                break;
        }
    }

    public void updateRunning(float deltaTime) {
        //draw section
        if (gameState == GAME_RUNNING) {
            game.batch.begin();
            //Draw some stuff
            game.batch.end();

            //move section
            handleInput();
        }
    }

    public void updatePause(float deltaTime) {
        //Gdx.graphics.setContinuousRendering(false);
        //resume
        if (Gdx.input.isTouched()) {
            gameState = GAME_RUNNING;
            //Gdx.graphics.setContinuousRendering(true);
            return;
        }
    }

    private void handleInput() {
        //catch back button
        Gdx.input.setCatchBackKey(true);
        if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)) {
            gameState = GAME_PAUSED;
            return;
        }
    }
}

Any tips to prevent the flickering?

Upvotes: 3

Views: 639

Answers (3)

Manh Khôi Duong
Manh Khôi Duong

Reputation: 173

As Thomas said: continue drawing is a way to fix it. So I drew the latest frame and disabled continous rendering after that. The result is having no bug at all and giving the CPU a rest.

Upvotes: 0

haxpor
haxpor

Reputation: 2601

As Thomas points out in his answer. It should be drawing every frame.

I haven't see you draw anything on screen yet, so backbuffer might just contain junk information. One way to solve this is to always clear the screen at the top of render(). This means when you switch immediately to another game state, it will make sure that screen is clear and no flickering result as you experienced.

public void render(float deltaTime) {
    // clear screen
    Gdx.gl.glClearColor(0f, 0f, 0f, 1.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    ...
}

Upvotes: 1

Thomas
Thomas

Reputation: 181785

The render() method is supposed to draw something to the screen. If it doesn't, then the contents of the back buffer will be undefined (so may contain a previous frame) and you get this flicker.

Disabling continuous rendering sounds like a good approach. Alternatively, continue drawing even if the game is paused.

Upvotes: 4

Related Questions