Pregunta

I'm developing a 2D game in libGDX and I have a big problem with the resolution after resuming a screen.

After starting the app the screen looks like this: initial screen http://image-upload.de/image/uRVE3f/e8022a5f11.png Note that the graphics are scaled to fit the screen size.

After changing to another app and resuming the game the screen looks like this: resumed screen http://image-upload.de/image/syUI3u/7ff85232d1.png Note that the graphics were not scaled.

I set the LwjglApplicationConfiguration config.width and config.height to 800 and 480. I tested it with different devices and resolutions, and the effect is still the same. I'm not doing anything in the resume() or resize() methods and I'm using the OrthographicCamera (camera.setToOrtho(false, 800, 480));

What can I do to make this work correctly? It works perfectly for the desktop version by the way.

EDIT: Relevant code parts:

AbstractBaseScreen.java

public abstract class AbstractBaseScreen {

    // constants
    protected final int screenWidth = GlobalConstants.GAME_WIDTH; //800
    protected final int screenHeight = GlobalConstants.GAME_HEIGHT; //480

    // camera
    protected OrthographicCamera camera;

    // ui
    protected AbstractBaseInputHandler inputHandler;
    protected Skin skin;
    protected BitmapFont font;
    protected Background background;
    protected Texture backgroundTexture;
    protected SpriteBatch batch;
    protected Stage stage;

    public abstract void create();
    public abstract void resize(int width, int height);
    public abstract void update();
    public abstract void render();
    public abstract void pause();
    public abstract void resume();
    public abstract void dispose();
    public void onRender() {
        update();
        render();
    }
}

Screen example: MainMenuScreen.java Displays two buttons and a background.

public class MainMenuScreen extends AbstractBaseScreen {
    // @Override
    public void create() {
        // set spritebatch
        batch = SpriteBatchSingleton.getInstance();

        // create stage
        stage = new Stage(screenWidth, screenHeight, false);
        Gdx.input.setInputProcessor(stage);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, screenWidth, screenHeight);

        // set skin
        skin = new Skin(Gdx.files.internal("data/ui.json"));

        // background
        background = new Background("backgrounds/bg_base");
        background.loadSprite();

        // buttons
        skillButton = new SkillButton(MainMenuScreen.class, skin);
        startButton = new StartButton(MainMenuScreen.class, skin);

        // add buttons to stage
        stage.addActor(skillButton.getButton());
        stage.addActor(startButton.getButton());
    }

    // @Override
    public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        camera.update();
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        background.draw();
        batch.end();
        stage.draw();
    }

    // @Override
    public void resize(int width, int height) {
        stage.setViewport(width, height, false);
    }

    // @Override
    public void dispose() {
        stage.dispose();
        skin.dispose();
        skillButton.dispose();
        startButton.dispose();
    }

    @Override
    public void update() {}

    @Override
    public void pause() {}

    @Override
    public void resume() {
        create();
    }
}
¿Fue útil?

Solución

Don't do anything on resize() .. Hope it will help :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top