Question

I'm trying to create a game with LibGDX. My problem comes when I put the background in the screen that I have. I created a Base Screen (abstract) in order to make easier. In Desktop, the screen fits good, but in Android, trying different devices, the screen doesn't scale to full screen. I solved this situation using the next easy code:

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

    batch.begin(); 
    batch.draw(texture, 0, 0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight()); 
    batch.end();
}

That was enough to see my game in full screen on android devices. Now, I want to use Scene2D(which I don't know so much...) So I create a Stage, I'm dealing with the background like an actor. Here's my code.

public class MenuScreen extends BaseScreen {

private FitViewport viewport;
private Stage stage;
private Image imageFondo, imagePrueba;
//private float escala;

public MenuScreen(MissingWords missingwords) {
    super(missingwords);
}

@Override
public void show() {
    //viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    viewport = new FitViewport(800, 480);
    stage = new Stage(viewport, missingwords.getSB()); 
    //Gdx.input.setInputProcessor(stage);

    /* Crear fondo */

    imageFondo = new Image(missingwords.getAM().get("fondo.png", Texture.class));
    stage.addActor(imageFondo);

    imagePrueba = new Image(missingwords.getAM().get("prueba.png", Texture.class));
    imagePrueba.setPosition(50, 50);
    stage.addActor(imagePrueba);
}

@Override
public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act();
    stage.draw();
}

@Override
public void resize(int width, int height) {
    stage.getViewport().update(width, height, true);

}

However, it doesn't work and I don't know if I'm using the Viewports in the correct way. How can I write this line in order to be compatible with Scene 2D and support android devices?

batch.draw(texture, 0, 0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight());

Thanks in advance.

Was it helpful?

Solution

Finally I "solved" it using a ScalingViewport. Like this:

viewport = new ScalingViewport(Scaling.stretch, 800, 480);

Scaling.stretch as javadoc says: the world is scaled to take the whole screen. So, it doesn't keep the aspect ratio but for me is good. Maybe it looks wrong using large screens, but I'm still learning and I don't know if this is the best solution. So, I hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top