Reputation: 8584
When I start my game using the correct aspect ratio my game is made for everything scales very well when I resize the window but when I start my app using different aspect ratio's things go wrong.
I have these globals set in the main game class:
public static final int VirtualWidth = 720;
public static final int VirtualHeight = 1280;
In the screen class i make use of fitviewport.
private Stage gameStage;
private OrthographicCamera camera;
private SpriteBatch batch;
private Viewport vp = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
public GameScreen()
{
batch = new SpriteBatch();
camera = new OrthographicCamera(ShufflePuzzle.VirtualWidth, ShufflePuzzle.VirtualHeight);
camera.setToOrtho(false, ShufflePuzzle.VirtualWidth, ShufflePuzzle.VirtualHeight);
FitViewport vp = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), camera);
vp.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
gameStage = new Stage(vp, batch);
Gdx.input.setInputProcessor(gameStage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(.2f, .2f, .3f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
gameStage.act();
gameStage.draw();
}
@Override
public void resize(int width, int height) {
vp.update(width,height);
gameStage.getViewport().update(width,height, false);
}
As far as I know i have to set the camera to the actual size of the screen and the viewport to my virtual values. I did try to switch this around with the exact same results. Here is my Desktop config:
//The size i made my game for, here everything works like a charm.
// If i resize my viewport manually to 1024x768 the screen gets fitted like it should.
config.width=720 / 2;
config.height=1280 / 2;
//When I use below config my stage will not fit the screen.
config.width = 1024 * 2;
config.height = 768 * 2;
Upvotes: 1
Views: 661
Reputation: 93581
You need to use your virtual sizes when you instantiate your viewport, not your camera. The camera values will be ignored and overwritten by the viewport anyway.
So do this:
camera = new OrthographicCamera();
FitViewport vp =
new FitViewport(ShufflePuzzle.VirtualWidth, ShufflePuzzle.VirtualHeight, camera);
Also, your constructor's vp
reference is hiding a member vp
, so it's not clear which is which. I can't think of a reason why you would need two separate viewports with the same parameters. But if you do, the one that is a member variable should also be instantiated with the virtual sizes, not the window's actual sizes (which will possibly be zero at instantiation time anyway if you instantiate it outside this class's constructor).
Upvotes: 1