Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split-Screen in LibGDX

This question is short and simple. How do I create a split screen effect in LibGDX. If I create two cameras all it will do is draw one located somewhere and then draw the next, overwriting the previous camera. I then thought to use multiple screens but that doesn't look like it will work as it only supports resizing and not relocating within the window. I'm also using Box2DDebugRenderer as well as a ShapeRenderer so it would also need to cut those off at the split-screen limit. There doesn't seem to be any documentation on the LibGDX site.

like image 253
Jonathan Pearl Avatar asked Jul 27 '13 20:07

Jonathan Pearl


1 Answers

After asking around a bit on the #libgdx IRC, the function Gdx.gl.glViewport( int x, int y, int width, int height ) was pointed out to me. So you only need one camera. Just set the viewport for the left side of the screen then perform your drawing commands, then set up the viewport for the right side of the screen and draw again. like so:

@Override
public void render( float delta )
{
    /*Wipe Screen to black*/
    Gdx.gl.glClearColor( Color.BLACK );
    Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );

    /*Left Half*/
    Gdx.gl.glViewport( 0,0,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight() );
    //Set up camera with viewport in mind
    draw( delta );

    /*Right Half*/
    Gdx.gl.glViewport( Gdx.graphics.getWidth()/2,0,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight() );
    //Set up camera again with other viewport in mind
    draw( delta );
}

You just need to set up the camera so that it is being positioned and transformed to the limited screen the way you want instead of the whole screen. You could potentially also use a 2nd camera.

like image 179
Jonathan Pearl Avatar answered Dec 10 '22 00:12

Jonathan Pearl