Libgdx Window Resizing: Keeping Aspect Ratio
Asked Answered
Q

1

9

I'm currently making a game using the Libgdx library, and have currently hit a small hurdle.

I've disabled the ability to resize currently, in my Main.java class in the desktop project.

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

I'm wondering whether there is a simple way to make the window resize whilst keeping the aspect ratio. (Like when you shift+resize)

What are my options? :)

Qualitative answered 28/8, 2013 at 18:51 Comment(0)
D
3

that function does not give you the ability to keep the aspect ratio or even something of that what you think. It does just disables the possebilty to resize the screen at the desktop application.

Please take a look at the libGDX wiki especialy the Scene2D page. Take a look at the Viewport stuff from the Stage. It's explained how you do keep the aspect ratio with the current libGDX. There are tutorials out there who do explain a different way with the help of a virtual resolution and the screen resize method. It's outdate!

from the wiki

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);
}

regards

Diagonal answered 28/8, 2013 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.