Ziiiii
Ziiiii

Reputation: 413

Needs advice on Libgdx Scaling

This might be a simple question being addressed somewhere, but I can’t find it. I do hope someone can lead me to the right path. The design resolution for my application is 800x480. To maintain the correct aspect ratio on devices with higher resolution, I followed this post and managed to get the “black bar” (I used blue for testing) on both side on a bigger screen (nexus 7). However, it seems that the stage is not scaled to cover the screen. Please see screen shot bellow, blue colour are the Gdx.gl.glClearColor(0.5f, 1, 1, 1); black rectangular (800x480) is the actual Sprite.

Link to image

I am not sure where I went wrong. Any help is much appreciated. Code below:

private SpriteBatch batch;
private Texture splashTexture;
private Sprite splashSp;
TextureRegion splashTr;
Stage stage;

@Override
public void create() {

    stage = new Stage();

    batch = new SpriteBatch();

    splashTexture = new Texture(Gdx.files.internal("img/splashTexture.png"));
    splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    TextureRegion splashTr = new TextureRegion(splashTexture, 0, 0, 800, 480);

    splashSp = new Sprite(splashTr);

    Gdx.app.log("myapp", "Creating game");
}

@Override
public void render() {      
    Gdx.gl.glClearColor(0.5f, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.draw();

    batch.begin();
    batch.draw(splashSp, 0, 0);

    batch.end();
}

@Override
public void resize(int width, int height) {

    Gdx.app.log("myapp", "width:" + width + " height:" + height);
    Vector2 size = Scaling.fit.apply(800, 480, width, height);
    int viewportX = (int)(width - size.x) / 2;
    int viewportY = (int)(height - size.y) / 2;
    int viewportWidth = (int)size.x;
    int viewportHeight = (int)size.y;
    Gdx.app.log("myapp", "viewportWidth:" + viewportWidth + " viewportHeight:" + viewportHeight);
    Gdx.app.log("myapp", "viewportX:" + viewportX + " viewportY:" + viewportY);
    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
    stage.setViewport(800, 480, true);

    Gdx.app.log("myapp", "Resizing game");
}

Upvotes: 1

Views: 742

Answers (1)

sandeep kundliya
sandeep kundliya

Reputation: 961

You need to set the camera and stage.

first declare a variable of camera like this

OrthographicCamera camera;

then in create method do this

camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
camera.update();

mystage = new Stage(800, 480, false);

and in render method update the camera

camera.update();

worked fine for mee..

Upvotes: 1

Related Questions