简体   繁体   中英

How to limit window resize dimensions in libGDX?

I want to limit the minimum dimensions of my game to 170x267 on desktop. I'm currently using this code:

@Override
public void resize(int width, int height) {
    gamePort.update(width, height);
    if (Gdx.app.getType() == ApplicationType.Desktop) {
        if (width < 170)
            Gdx.graphics.setWindowedMode(170, height);
        if (height < 267)
            Gdx.graphics.setWindowedMode(width, 267);
    }
}

However, this sets the window's X and Y position to the center and sometimes causes the game window to go blank.

What can I do to set a minimum size for my game window?

You can switch from the LWJGL backend to the LWJGL3 backend, which natively supports this feature. To switch, change your build.gradle file in the root of your project. Change this line:

compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"

to

compile "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"

And change to Lwjgl3ApplicationConfiguration and Lwjgl3Application in your DesktopLauncher. Then you can set your minimum window size in the configuration for your game:

public class DesktopLauncher {
   public static void main (String[] arg) {
      Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
      config.setWindowSizeLimits(170, 267, 9999, 9999);
      new Lwjgl3Application(new MyGdxGame(), config);
   }
}

You can easily set the screen size for the desktop by setting the width and height . You have to open the Desktop folder and press on Main.java file and set the

cfg.width=170;
cfg.height=267;

Like this you can easily set the screen width and height for desktop. Here is an example code. Please go through it.

public class Main {
    public static void main(String[] args) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
        cfg.title = "myGame";
        cfg.useGL30 = false;
        cfg.width =1280;
        cfg.height = 800;

        new LwjglApplication(new copterAdventure(), cfg);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM