简体   繁体   中英

libgdx - ui SpriteBatch needs to scale based on window size?

I am currently using libgdx 1.5.4. Normally to modify screen size, I have done this:

public class DesktopLauncher {
   public static void main (String[] arg) {
      LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();

      int temp = 3; //scale viewport
      config.width = temp *160;
      config.height = temp *144;

      new LwjglApplication(new MyGame(), config);
   }
}

The temp var above will scale the desktop application window size by some number (3 in this case). In my render() loop, I have a batch ( floatingBatch = new SpriteBatch(); ) that isn't modified by a camera, to draw my 'ui' elements:

      floatingBatch.begin();
                //bunch of floatingBatch.draw()'s...
      floatingBatch.end();

I have noticed, though, that the coordinates in floatingBatch don't scale with my screen size. For example, the point (200,200) in floating batch will be offscreen if temp above = 1, but will be onscreen if temp = 3. This isn't right, I want floatingBatch to scale with the window. If I start with temp = 1, and just manually drag the window out to about 3x it's starting size, everything is as I want obviously.

One thing that I have tried is have the temp = 1 above, and then do this in my create() function:

Gdx.graphics.setDisplayMode(160*3, 144*3, false);

The above is fine, and works, but it looks a little awkward visually (the screen first starts out as 144x160, then resizes to 3x that size.) Is there a better way to do what I am describing? Can I change floatingBatch somehow so that it scales with the window?

You can use

Gdx.graphics.getWidth();

and

Gdx.graphics.getHeight();

to obtain the pixel count of the device then adjust your scene2d

Refer to the api for more help

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