SheerSt
SheerSt

Reputation: 3337

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?

Upvotes: 0

Views: 461

Answers (1)

Fish
Fish

Reputation: 1697

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

Upvotes: 1

Related Questions