4

I want to rotate BitmapFont in libgdx. There are topics on this matter. Draw a BitmapFont rotated in libgdx However, the first solution I try cuts my text, the whole text doesn't appear (diagonal cut). And it also rotates it ba 90 degrees, when it says it should be 180. I just don't get it.

Code:

public void show() {
    sr = new ShapeRenderer();
    w = Gdx.graphics.getWidth();
    h = Gdx.graphics.getHeight();
    textRotation = new Matrix4();
    textRotation.setToRotation( new Vector3(200,200, 0), 180);

    rectThickness = h / 120;
    c1 = Color.WHITE;
    c2 = Color.WHITE;
    f = new BitmapFont(Gdx.files.internal("fonts/MyFont.fnt"),
             Gdx.files.internal("data/MyFont.png"), true);
    f.setScale(h/500);
    sb = new SpriteBatch();
}
private void renderPlayerScores() { //callend in render
    sb.setTransformMatrix(textRotation);
    f.setColor(players[0].getColor());
    sb.begin();
    f.draw(sb, "Player 1: "+ Integer.toString(scores[0]), 100, 110);
    sb.end();
}
Community
  • 1
  • 1
gx10001
  • 135
  • 2
  • 11

1 Answers1

3

One way to do this would be to use scene2d labels. They are very easy to work with and you should be able to do what you want if you read up on Scene2D UI and Labels. Some people, however, prefer not to use Scene2D. The following code accomplishes what you want, but it's a bit of extra work just to avoid using Scene2D.

public SpriteBatch spriteBatch;
private int posX = 100;
private int posY = 100;
private float angle = 45;
private String text = "Hello, World!";
private BitmapFont font;
private Matrix4 oldTransformMatrix;
Matrix4 mx4Font = new Matrix4();

@Override
public void show() {
    font = new BitmapFont(Gdx.files.internal("someFont.ttf"));
    oldTransformMatrix = spriteBatch.getTransformMatrix().cpy();
    mx4Font.rotate(new Vector3(0, 0, 1), angle);
    mx4Font.trn(posX, posY, 0);
}

@Override
public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    spriteBatch.setTransformMatrix(mx4Font);
    spriteBatch.begin();
    font.draw(spriteBatch, text, 0, 0);
    spriteBatch.end();
    spriteBatch.setTransformMatrix(oldTransformMatrix);
}

I hope some of this can be of use to you.

tobloef
  • 1,821
  • 3
  • 21
  • 38