Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

How to properly change resolution

$
0
0

I’m trying to understand how LibGDX handles screen size / resolution, etc.
Coming from XNA this seems very confusing to me. Basically I want to support 480×800 for my game. If the screen size is different it should scale the resolution and use letterboxing. To do this I use a FitViewport. And that’s the code:

public static final int VIRTUAL_WIDTH = 480;
public static final int VIRTUAL_HEIGHT = 800;
Viewport viewport;
SpriteBatch spriteBatch;
ShapeRenderer shapeBatch;
Texture img;

@Override
public void create()
{
    viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
    spriteBatch = new SpriteBatch();
    shapeBatch = new ShapeRenderer();
    img = new Texture("test.jpg");
}

@Override
public void resize(int width, int height)
{
    viewport.update(width, height);
}

@Override
public void render()
{
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    shapeBatch.begin(ShapeType.Filled);
    shapeBatch.setColor(Color.GRAY);
    shapeBatch.rect(0, 0, viewport.getWorldWidth(), viewport.getWorldHeight());
    shapeBatch.end();

    spriteBatch.begin();
    spriteBatch.draw(img, 0, 0);
    spriteBatch.end();
}

Now if I set the game resolution to be 480×800 right at the beginning, it basically works except that if I change the window size, the resolution keeps to be 480×800. So it stretches the image but keeps the aspect ratio:
screen1

And if I run the game with any other resolution like for example 700×500 I get this:
screen2

What is going on behind the scenes?
(And sorry if my english is bad)


Viewing all articles
Browse latest Browse all 434

Trending Articles