Search code examples
libgdx

LibGdx TiledMapRenderer leaves a flickering image after each render() call


In LibGdx when I use the below code to render a tiled map the tiledMapRenderer.render() call leaves a flickering image in the black area after just one call (I tested this by rendering in the create method only) and I haven't been able to remove it. This flickering image becomes a problem when the window size changes.

TiledMap tiledMap;
OrthogonalTiledMapRenderer tMR;
ExtendViewport viewPort;

@Override
public void create () {
    OrthographicCamera camera = new OrthographicCamera();
    camera.setToOrtho(false, 512, 512);
    viewPort = new ExtendViewport(512, 512, 512, 512, camera );
    viewPort.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    tiledMap = new TmxMapLoader().load("LibGdxTutorial.tmx");
    tMR = new OrthogonalTiledMapRenderer(tiledMap);
}

@Override
public void render () {
    viewPort.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    tMR.setView((OrthographicCamera) viewPort.getCamera());
    tMR.render();
}

This is the default window you get after pressing run

This is the window after I dragged the right side out

The map image on the right of the second photo is the full and correct one. The map image on the left is the flickering after image (it appears full because it's one frame). Any help would be appreciated, thanks.


Solution

  • You need to clear the screen each frame. This happens in the render method and should be used before you start drawing.

        Gdx.gl.glClearColor(0f,0f,0f,1);  // set the clear color
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  // clear the screen with the given color.
    
        .. draw next frame ..