Java 如何更改 libgdx 不同的窗口大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21079122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:26:04  来源:igfitidea点击:

How to change libgdx different window size?

javalibgdx

提问by user2570888

How to change size in libgdx-android-desktop? I am confuse on window sizeing and not sure how to solve this problem.

如何更改 libgdx-android-desktop 中的大小?我对窗口大小感到困惑,不知道如何解决这个问题。

So for desktop window i want 500x500 but with android i want full screen so i cant hard code it.
For some reason ANDROID_WIDTH is always equal to WINDOW_WIDTH.

所以对于桌面窗口,我想要 500x500,但对于 android,我想要全屏,所以我无法对其进行硬编码。
出于某种原因,ANDROID_WIDTH 总是等于 WINDOW_WIDTH。

 int WINDOW_WIDTH = 500;
 int WINDOW_WIDTH = 500;

public void create() {
    if (Gdx.app.getType() == ApplicationType.Android) {
        int ANDROID_WIDTH = Gdx.graphics.getWidth();
        int ANDROID_HEIGHT = Gdx.graphics.getHeight();
        camera = new OrthographicCamera(ANDROID_WIDTH, ANDROID_HEIGHT);
        camera.translate(ANDROID_WIDTH/2, ANDROID_HEIGHT/2);
        camera.update();
    } else {
        camera = new OrthographicCamera(WINDOW_WIDTH, WINDOW_HEIGHT);
        camera.translate(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
        camera.update();
    }

    Gdx.input.setInputProcessor(new GameInputProcessor());
}

回答by Arash

You cannot change the size of the windows by changing the camera. They are two separate concepts.

您无法通过更改相机来更改窗口的大小。它们是两个不同的概念。

You set the size on desktop application in your main method through config lwjpg . Android application is full screen anyway.

您可以通过 config lwjpg 在主方法中设置桌面应用程序的大小。无论如何,Android 应用程序都是全屏的。

LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Title";
cfg.useGL20 = true;
cfg.height = 640;
cfg.width = 360;
new LwjglApplication(new MyGame(), cfg);

回答by sandeep kundliya

do this in your desktop project

在您的桌面项目中执行此操作

cfg.width=500;
cfg.height=500;

and in your main class

在你的主班

int ANDROID_WIDTH = Gdx.graphics.getWidth();
int ANDROID_HEIGHT = Gdx.graphics.getHeight();

camera = new OrthographicCamera();
camera.setToOrtho(false, ANDROID_WIDTH, ANDROID_HEIGHT);
camera.update();

回答by Springrbua

You can give the Window size on Startup:

您可以在启动时提供窗口大小:

new LwjglApplication(yourApplicationListener(), "Title", 500, 500, true/false (useGL2));

You can also Change it in your game using:

您还可以使用以下方法在游戏中更改它:

Gdx.graphics.setDisplayMode(500, 500, true/false (fullscreen));

You can surround this by an if Statement like stuntmania said:

你可以用像 stuntmania 说的 if 语句来包围它:

if (Gdx.app.getType().equals(ApplicationType.Android)) {
    Gdx.graphics.setDisplayMode(500, 500, false);
} else {
    Gdx.graphics.setDisplayMode(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), true);
}

EDIT:In LibGDX 1.8 the Method Gdx.graphics.setDisplayModehas been renamed to Gdx.graphics.setWindowedMode:

编辑:在 LibGDX 1.8 中,方法Gdx.graphics.setDisplayMode已重命名为Gdx.graphics.setWindowedMode

API Change: Graphics#setDisplayMode(int, int, boolean)has been renamed to Graphics#setWindowedMode(int, int). This will NOT allow you to switch to fullscreen anymore, use Graphics#setFullscreenMode()instead. If the window is in fullscreen mode, it will be switched to windowed mode on the monitor the window was in fullscreen mode on.

API 更改:Graphics#setDisplayMode(int, int, boolean)已重命名为 Graphics#setWindowedMode(int, int). 这将不再允许您切换到全屏,Graphics#setFullscreenMode()而是使用。如果窗口处于全屏模式,它将在窗口处于全屏模式的监视器上切换到窗口模式。

(Source)

来源