LibGDX 使视口大于屏幕

LibGDX making viewport larger than screen

一段时间以来,我一直在为如何在 LibGDX 中使用和设置视口而苦苦挣扎。我希望能够在 1920x1080 的显示器上渲染所有类似的内容,并使其缩放以适合其所在的显示器,我需要一些帮助才能像那样工作。
This is what I want it to look like (taken from a computer with a 1920x1080 monitor), but when I run the same code on my laptop which is 1440x800, it looks like this。对于屏幕的糟糕照片,我深表歉意,无论出于何种原因,我都无法截取游戏 运行,但它显示显示屏的顶部仍未使用,并且并非一切都在适合显示器。这是主要代码 运行 显示:

public class Main extends Game {
    ...
    public void create() {
        ...         
        Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());

        //last
        this.setScreen(new MainMenu(this));
    }

    public void render() {
        super.render(); //important!
    }
    ...
}

然后是主菜单class

public class MainMenu implements Screen{
    ...

    public MainMenu(final Main game) {
        this.game = game;
        cam = new OrthographicCamera();
        cam.setToOrtho(false, 1920, 1080);

        ...
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.025f, .025f, 0.025f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        cam.update();
        game.batch.setProjectionMatrix(cam.combined);

        ...

        if(Gdx.input.isKeyPressed(Keys.ESCAPE)) {
            Gdx.app.exit();
        }
    }

    ...
}

我如何实现视口或类似的东西以使其在较小的屏幕上看起来与在较大的屏幕上看起来一样?非常感谢任何帮助!如果您想查看我为简洁起见而省略的代码,全部在我的 GitHub 上。再次感谢!

这并没有花我很长时间,希望有人能向我学习,哈哈。

当你像这样使用固定高度和宽度的相机时,它确实填满了整个显示器,但相机宽度不等于 Gdx.graphics.getWidth() 返回的值。正因为如此,我所有的代码都像被压缩一样呈现,因为它引用了 Gdx.graphics 返回的宽度,而不是 camera.viewportWidth

经验教训:Gdx.graphics.getWidth() 可以并且将会根据设备而改变,cam.veiwportWidth 不会。糟糕!