xrash
xrash

Reputation: 961

Strange screen size on LibGDX DisplayMode

I am currently facing some problems with LibGDX and Box2d probably because of my camera setup.

While investigating, I found that:

    Gdx.graphics.getWidth() == 320
    Gdx.graphics.getHeight() == 526

Even though I set the Stage and the OrthographicCamera to 1080/1920 or 270/480 (I was playing with the values).

Then I followed the documentation and looks like this value has nothing to do with the Stage neither the Camera. After that, I checked that I only got one DisplayMode available:

    DisplayMode[] dms = Gdx.graphics.getDisplayModes();
    for (int i = 0; i < dms.length; i++) {
        Log.d("myapp", "dms > " + dms[i].toString());
    }

Results in:

    dms > 320x526, bpp: 0, hz: 0

Because of that, my call to setDisplayMode is being ignored:

    Gdx.graphics.setDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, true);

It changes nothing.

I am suspecting that this might be the problem, but how can I change it?

EDIT: I am testing in a Nexus 5.

Upvotes: 0

Views: 822

Answers (3)

xrash
xrash

Reputation: 961

Others have given useful answers, but I must tell why LibGDX was reporting the wrong screen size for my Nexus 5.

I found this link explaining that we need to set the target SDK in the AndroidManifest:

    <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="18" />

After setting it to my test version, LibGDX reports the right screen size for my device (1080x1776, probably because the menu takes some space).

Upvotes: 1

Robert P
Robert P

Reputation: 9793

Gdx.graphics.getWidth() and Gdx.graphics.getHeight() are the screen dimensions of your android device / the window size for desktop. There is a way to set the viewport, so the rest of the window is filled with black borders: Gdx.gl.glViewport((int)startX, (int)startY, (int)width, (int)height); (From this tutorial).

The cameras viewport is something else. It is like a conversation of the physic size and the game size: if you set the cameras viewport width to (for example) 16, your game window is "devided" into 16 parts. if you then draw an object at x = 16 it is on the right end of the window. If you draw it on x = 7.5 it is in the middle (if its size = 1, and the x pos is the left side). So you can calculate your things in your own unit (maybe meters?) and libgdx scales it up to fit the screen. So if you use a camera viewport of (16 / 9) on a 1600 * 900px screen (to keep it simple) 1 worldunit is 100 px. On a 800*450px screen it is 50px... I hope i could help

Upvotes: 1

dermetfan
dermetfan

Reputation: 339

Gdx.graphics.getWidth() and Gdx.graphics.getHeight() return the screen dimensions. On desktop they are the size of the window (which you can resize), but on Android you cannot change them. You should adapt the camera viewport size to the screen size instead.

Upvotes: 2

Related Questions