Taras
Taras

Reputation: 2576

Clearing screen in LibGDX

Why do we need to clear screen every frame with using

@Override
public void render() {

    Gdx.graphics.getGL20().glClearColor( 1, 0, 0, 1 );
    Gdx.graphics.getGL20().glClear( GL20.GL_COLOR_BUFFER_BIT |  GL20.GL_DEPTH_BUFFER_BIT );

    // scene render code...

}

Isn't enough just to redraw a background again and then other elements on top?

Upvotes: 2

Views: 1166

Answers (1)

Tenfour04
Tenfour04

Reputation: 93581

Telling OpenGL to clear the screen lets it know that it does not need to preserve the image from the last frame. OpenGL ES is designed to redraw the screen from scratch for each frame, so if you don't clear the screen, it assumes you want to keep all the data from the last frame and has to waste time copying the old image to the new frame.

You don't have to call glClearColor every frame. It just changes the setting for the color that will be used when clearing the color buffer.

Upvotes: 4

Related Questions