java - Libgdx 窗口调整大小 : Keeping Aspect Ratio

标签 java libgdx desktop-application

我目前正在使用 Libgdx 库制作游戏,目前遇到了一个小障碍。

我目前在桌面项目的 Main.java 类中禁用了调整大小的功能。

LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
    cfg.resizable = false;

我想知道是否有一种简单的方法可以在保持宽高比的同时调整窗口大小。 (就像你移动+调整大小一样)

我有哪些选择? :)

最佳答案

该功能无法让您保持宽高比,甚至无法保持您的想法。它只是禁用了在桌面应用程序中调整屏幕大小的可能性。

请查看 libGDX wiki,特别是 Scene2D页。 查看舞台中的Viewport 内容。它解释了如何保持当前 libGDX 的纵横比。有一些教程在虚拟分辨率和屏幕调整大小方法的帮助下以不同的方式进行了解释。它已经过时了!

来自维基

This example also uses a fixed stage size with "black bars" on either side, this time using glViewport. First the stage size of 800x480 is scaled to fit the screen size using the Scaling class. The result is used to configure glViewport, which changes which part of the screen OpenGL will use. Lastly, setViewport is passed the viewport position and size. The result is the same as the last example, the stage has "black bars" as necessary to keep the aspect ratio, but no drawing can occur outside the viewport.

public void resize (int width, int height) {
        Vector2 size = Scaling.fit.apply(800, 480, width, height);
        int viewportX = (int)(width - size.x) / 2;
        int viewportY = (int)(height - size.y) / 2;
        int viewportWidth = (int)size.x;
        int viewportHeight = (int)size.y;
        Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
        stage.setViewport(800, 480, true, viewportX, viewportY, viewportWidth, viewportHeight);
}

问候

关于java - Libgdx 窗口调整大小 : Keeping Aspect Ratio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18495975/

相关文章:

java - WSDL 中缺少信息(JAX-WS 运行时)

java - LibGDX 游戏突然卡顿了?

android - 关于Libgdx.How to show my picture from device.(我现在有图片路径)

c# - .net 桌面应用程序 - 如何存储/更改应用程序设置或连接字符串?

html - 在桌面图形化独立应用程序中嵌入 Dart VM

web-services - Azure 中的分布式计算应用程序作为 Web 服务提供给桌面 View 应用程序

java - Java中如何获取字符串中的特定单词

java - JButton 没有出现在 JDialog 上

java - 如何在Android模拟器上向WAMP服务器发出HTTP请求

java - 在一个 Github 存储库中拥有多个 eclipse 项目