Home > Mobile >  libGDX problem with getWidth() and getHeight() after resizing
libGDX problem with getWidth() and getHeight() after resizing

Time:09-07

Problem recorded and uploaded to Youtube

libGDX resizing problem

I am starting out with libGDX and wanted to make a game.
However... when I was just experimenting around... this happened. I don't know why it does that or what I should do to eliminate this. I don't know what to google either. It's almost as if libGDX's default width and height methods doesn't update correctly when the screen is being resized.

Am I doing something wrong here?

Code :

public class Metroidvania extends ApplicationAdapter {
ShapeRenderer s;

@Override
public void create () {
    s = new ShapeRenderer();
}

@Override
public void render () {
    ScreenUtils.clear(1, 1, 1, 1);
    s.begin(ShapeRenderer.ShapeType.Filled);
    s.setColor(0, 0, 0, 1);
    s.rect(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    s.end();
}

@Override
public void dispose () {
    s.dispose();
}

}

CodePudding user response:

The video is currently stuck at 97% uploaded... Even Youtube is against me now
Basically, The code is supposed to cover the entire screen in black... even when resized. But here, when resised Gdx.graphics.getWidth() and Height() returns wrong values, causing white spaces to show. The rect()'s shape depends on which direction you resize in.
Resize the screen smaller and the rect() become's smaller at R times the rate of resizing (R is unknown). Vice versa for reisizing it larger.

CodePudding user response:

LibGDX usually uses a virtual screen and viewports i.e. you render to a different width and height than are the real ones in pixel terms. Maybe have a look here Libgdx Window Resizing: Keeping Aspect Ratio as there is a resize method you can hook into to dynamically update your own coordinate system.

  • Related