我目前正在使用 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/