Dan Tanaka
Dan Tanaka

Reputation: 1

Libgdx Why are textures being displayed different sizes and locations on android, than they are on desktop

When I load my game on the desktop launcher it the loading bar is a bit bigger and in a different location, to when I launch it on the android device.

Does anyone know how I can get it the android device to look like it looks like on the desktop launcher

The desktop size and positioning

The android size and positioning

This is the code I have at the top

 public class LoadingScreen implements Screen {
private GameMain mGameMain;
private float progress;
private int progressInt;
private BitmapFont loadingPercentageFont;
private Label loadingPercentageLabel;
private Viewport mViewport;
private OrthographicCamera mOrthographicCamera;
private final float lblX = 117f;
private final float lblY = 387f;


//textures
private Texture lbl, lbr, lbb, background;

public LoadingScreen(GameMain mGameMain) {
    this.mGameMain = mGameMain;


    progress = 0;
    mOrthographicCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    mViewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), mOrthographicCamera);

    getAssetsFromBeforeLoadingScreen();
    queueAssets();
}

and this is where i am drawing the assets inside the render method

  private void drawAssets(SpriteBatch batch) {
        batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.draw(lbb, 117f, 387f, 190, 26);
        batch.draw(lbl, 117f, 387f, 6, 26);
    }

Upvotes: 0

Views: 435

Answers (2)

SirTrashyton
SirTrashyton

Reputation: 173

You could use this sample GameCamera class that I've quickly created. The dimension's of the viewport are defined as VIRTUAL_HEIGHT and VIRTUAL_WIDTH, and the camera is centered when the constructor is called. Make sure to call gameCamera.updateSize(width, height) in your screen's resize(float width, float height) method. Set your renderer's projection matrix to gameCamera.camera.combined to render everything with a fixed location. Hope this helps :)

Upvotes: 0

Samuel T
Samuel T

Reputation: 402

As @MilanG said, the screen size (or window size on desktop) of your Desktop and Android device are different. Since you are positioning your image by exact pixels it will appear in different locations for both devices.

To get around this you can use viewports (api). You are already using a StretchViewport however you are passing the current screen height and width of the device (or size of the window) as the width and height of the viewport. This means the virtual world size will change on every device with a different screen size which defeats the purpose of the viewport.

Solution:

To fix the different positioning and sizing of your image, set the World height and width which you pass into your viewport constant. These could be any values you like.

Example:

mOrthographicCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
mViewport = new StretchViewport(1920, 1080, mOrthographicCamera); // The width and height do not need to be in pixels

Have a read of the viewports docs to learn more about viewports and the different viewports you can use.


Also please note that you will need the following code to make sure the batch draws to the screen according to your camera and viewport.

batch.setProjectionMatrix(mOrthographicCamera.combined);

Your background will also display strangely now as its height and width is being set to the actual screen size which may be alot bigger or smaller then the virtual word size. Fix this by setting the width and height of your background to the same as your virtual world width and height set for your viewport.

batch.draw(background, 0, 0, 1920, 1080); // Same as what was set for the viewport

I hope that helps :)

Upvotes: 2

Related Questions