2
votes

I've searched all around google and this website for infos about this problem, but cannot solve it..

I'm a newbie in game development and LibGDX, and cannot find a solution well explained on how to port my game to all the various screen sizes..

Would you kindly help me?

Thanx

2

2 Answers

4
votes

When using the newest libgdx version, you will find the Viewport class...

The viewport describes the transformation of the coordinate system of the screen (being the pixels from 0,0 in the lower left corner to e.g. 1280,768 in the upper right corner (depending on the device)) to the coordinate system of your game and scene.

The Viewport class has different possibilities on how to do that transformation. It can either stretch your scene coordinate system to exactly fit the screen coordinate system, which might change the aspect ratio and for example "stretch" your images or buttons.

It's also possible to fit the scene viewport with its aspect ratio into the viewport, which might produce a black border. E.g. when you have developed the game for 4:3 screens and now embed it into 16:10 displays.

The (in my opinion) best option is through fitting the scene viewport into the screen by matching either the longest or shortest edge.

This way, you can have a screen/window coordinate system from (0,0) to (1280,768) and create your game coordinate system maybe from (0,0) to (16,10) in landscape mode. When matching the longest edge, this means that the lower left corner of the screen will be (0,0), the lower right will be (16,0)... On devices that don't have the same aspect ratio, the y-values on the upper corners might differ a bit.

Or when matching the shortest edge, this means your scene coordinates will always be shown from (x,0) to (x,10) ... But the right edge might not exactly have and x value of 16, since device resolutions differ...

When using that method, you might have to reposition some buttons or UI elements, when they are supposed to be rendered on the top or the right edges...

Hope it helps...

1
votes

Once me too suffered from this problem but at end i got the working solution, for drawing anything using SpriteBatch or Stage in libgdx. Using OrthographicCamera we can do this.

first choose one constant resolution which is best for game. Here i have taken 1280*720 (landscape).

class ScreenTest implements Screen {

    final float appWidth = 1280, screenWidth = Gdx.graphics.getWidth();
    final float appHeight = 720, screenHeight = Gdx.graphics.getHeight();

    OrthographicCamera camera;

    SpriteBatch batch;
    Stage stage;

    Texture img1;
    Image img2;

    public ScreenTest() {
        camera = new OrthographicCamera();
        camera.setToOrtho(false, appWidth, appHeight);

        batch = new SpriteBatch();
        batch.setProjectionMatrix(camera.combined);

        img1 = new Texture("your_image1.png");
        img2 = new Image(new Texture("your_image2.png"));
        img2.setPosition(0, 0); // drawing from (0,0)

        stage = new Stage(new StretchViewport(appWidth, appHeight, camera));
        stage.addActor(img2);
    }

    @Override
    public void render(float delta) {
        batch.begin();
        batch.draw(img, 0, 0);
        batch.end();

        stage.act();
        stage.act(delta);
        stage.draw();

        // Also You can get touch input according to your Screen.
        if (Gdx.input.isTouched()) {
            System.out.println(" X " + Gdx.input.getX() * (appWidth / screenWidth));
            System.out.println(" Y " + Gdx.input.getY() * (appHeight / screenHeight));
        }
    }
    // ...
}

run this code in any type of resolution it will going to adjust in that resolution without any disturbance.