LibGDX 中的分屏

2022-01-12 00:00:00 screen java libgdx

这个问题很简短.如何在 LibGDX 中创建分屏效果.如果我创建两个相机,它会做的就是在某处绘制一个,然后绘制下一个,覆盖前一个相机.然后我想使用多个屏幕,但这看起来并不可行,因为它只支持调整大小而不支持在窗口内重新定位.我还使用 Box2DDebugRenderer 和 ShapeRenderer,因此它还需要在分屏限制处切断它们.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.

推荐答案

在#libgdx IRC上询问了一下,函数Gdx.gl.glViewport( int x, int y, int width, int height) 向我指出.所以你只需要一台相机.只需设置屏幕左侧的视口,然后执行绘图命令,然后设置屏幕右侧的视口并再次绘制.像这样:

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.

相关文章