Skip to content Skip to sidebar Skip to footer

Libgdx Text Not Rendering Properly On Larger Screens

On my 854 x 480 LG Leon it renders fine, but on a larger screen resolution (2560 x 1440) this happens: http://imgur.com/a/rcbJK. The code for processing the text is: http://imgur.c

Solution 1:

The different mobile phone has the different aspect ratio in their screen size . that is the reason it shows different different screen. to avoid this problem you must use the viewPort. So you will be able to handle the different screen size. for more details, you must read the libgdx documentation. here I'm posting a sample code which can help you to handle the different screen size. there are three kinds of viewports mainly using 1) fillviewport 2) fitviewPort 3)stretchviewport here is the documentation in detail.

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/viewport/FillViewport.html

publicclassmyGameextendsApplicationAdapter {

publicOrthographicCamera camera;
publicViewport viewPort;
privateSpriteBatch batch;

publicmyGame() {

}

@Overridepublicvoidcreate() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = newOrthographicCamera();
    camera.position.set(0, 0, 0);
    camera.update();
    camera.setToOrtho(false, 1280, 800);
    viewPort = newFillViewport(1280, 800, camera);


}

@Overridepublicvoiddispose() {
    batch.dispose();
}





@Overridepublicvoidrender() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    float deltaTime = Gdx.graphics.getDeltaTime();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(sprie,0,0)
    batch.end();

}

@Overridepublicvoidresize(int width, int height) {
    viewPort.update(width, height);
}

@Overridepublicvoidpause() {
}

@Overridepublicvoidresume() {
}
}
// don't copy paste it . just try to understand and implement it.

Solution 2:

You should use Scene2D and ViewPort.

Also , be careful to use coordinates (i.e : x: 64 , y:32 changes in every different screen size)

Solution 3:

This is the common issue when your running your game in multiple devices. Because your game is running in different aspect ratios in different screens . it's generally called the multi screen issue. To solve this problem the libgdx is providing it's on class called the viewPort. Mainly you can see three viewpors. 1)Fill viewPort. 2)Fit viewPort. 3)stretch viewport. For more details you can go through the libgdx documentation. Here im posting some example code which can solve your multi screen issues.

publicclassmyGameextendsApplicationAdapter {

public OrthographicCamera camera;
public Viewport viewPort;
private SpriteBatch batch;
private BitmapFont myScoreFont;

//General screen resolution public int APP_WIDTH=1280; public int APP_HEIGHT=800; public int fontPositionIn_X=600; public int fontPositionIn_Y=400; public myGame() {

}

@Override
publicvoidcreate() {
        myScoreFont = new    BitmapFont(Gdx.files.internal(Constants.PATH_TO_MY_SCORE_FONT), true);
    batch = new SpriteBatch();
        float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera();
    camera.position.set(0, 0, 0);
    camera.update();
    camera.setToOrtho(false, APP_WIDTH, APP_HEIGHT);
    viewPort = new fillViewPort(1280, 800, camera);


}

@Override
publicvoiddispose() {
    batch.dispose();
}





@Override
publicvoidrender() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    float deltaTime = Gdx.graphics.getDeltaTime();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    myScoreFont.draw(batch,"any texts", fontPositionIn_X, fontPositionIn_Y)
    batch.end();

}

@Override
publicvoidresize(int width, int height) {
    viewPort.update(width, height);
}

@Override
publicvoidpause() {
}

@Override
publicvoidresume() {
}

}

so by using the viewPort you can be able to play your game in all the different screens.

Post a Comment for "Libgdx Text Not Rendering Properly On Larger Screens"