LibGDX how to scale images for every device

Andrzej1991

My code is running like this:

public class MainMenuOptions extends AbstractScreen {

private Texture newGameButton;
private Texture exitGameButton;
private Texture highScoresButton;
private Texture background;


public MainMenuOptions(FallingPeopleMain game) {
    super(game);
    init();
}

 private void init() {
    newGameButton = new Texture("newGameButton.jpg");
     exitGameButton = new Texture("exitGameButton.jpg");
     highScoresButton = new Texture("highScoresGameButton.jpg");
     background = new Texture("BG.png");

}

@Override
public void render(float delta) {
    spriteBatch.begin();
    spriteBatch.draw(background, 0, 0);
    spriteBatch.draw(newGameButton, 180, 380);
    spriteBatch.draw(highScoresButton, 180, 340);
    spriteBatch.draw(exitGameButton, 180, 300);

    spriteBatch.end();

}

}

But when i play it on my device Huawei p8 Lite its looking very crap on 40% screen device. How to scale everything for being good?

I have Width and Height declaring on main class

WonderfulWorld
@Override
public void render(float delta) {
    float widthScale = Gdx.graphics.getWidth() / originalWidth;
    float heightScale = Gdx.graphics.getHeight() / originalHeight;
    spriteBatch.begin();
    spriteBatch.draw(background, 0, 0, background.getWidth() * widthScale, background.getHeight() * heightScale);
    spriteBatch.draw(newGameButton, 180, 380, newGameButton.getWidth() * widthScale, newGameButton.getHeight() * heightScale);
    spriteBatch.draw(highScoresButton, 180, 340, highScoresButton.getWidth() * widthScale, highScoresButton.getHeight() * heightScale);
    spriteBatch.draw(exitGameButton, 180, 300, exitGameButton.getWidth() * widthScale, exitGameButton.getHeight() * heightScale);

    spriteBatch.end();

}

originalWidth and originalHeight are your desktop (development) version's width and height (even if they are the same as the actual width and height, declare them separately anyway - it will only mean the textures show in original size). The ratio between originalWidth and originalHeight must be 9:16 (720x1280 / 360x640...) or 16:9.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive