Reputation: 2534
I am creating a game and I do not want my screen to be stretched when I resize my window (of course only if it can fit in the window). I am using a Stage
object with a FitViewport
but when I resize window, elements on it are being resized too. What am I doing wrong?
This is my Screen class:
public class SplashScreen implements Screen{
private final PuzzleGame app;
private Stage stage;
private Image splashImage;
public SplashScreen(final PuzzleGame app) {
this.app = app;
this.stage = new Stage(new FitViewport(PuzzleGame.V_WIDTH, PuzzleGame.V_HEIGHT, app.camera));
Gdx.input.setInputProcessor(stage);
Texture splashTex = new Texture(Gdx.files.internal("img/splash.png"));
splashImage = new Image(splashTex);
splashImage.setPosition(stage.getWidth() / 2, stage.getHeight() / 2);
stage.addActor(splashImage);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(.25f, .25f, .25f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
update(delta);
stage.draw();
}
private void update(float delta) {
stage.act(delta);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, false);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
stage.dispose();
}
}
Upvotes: 2
Views: 1993
Reputation: 19776
As Tenfour04 already pointed out, you are looking for ScreenViewport
.
You probably want to check out this question and then re-read the Viewports wiki article.
Basically, there are two different kind of viewports, the Camera and the OpenGL viewport.
FitViewport
keeps the Camera viewport the same, but scales the OpenGL viewport up or down. The result will be that all sizes will appear to be bigger or smaller, depending on how much bigger or smaller the OpenGL viewport is, compared to the camera viewport.
ScreenViewport
on the other hand keeps the camera viewport and the OpenGL viewport at the same size, which will result in elements always having the same size.
Upvotes: 4