Libgdx - placing the Bitmap font in the center of the screen

I am new to Libgdx but am knowledgeable in Android. I can place the texture in the center of the screen. But when it comes to a bitmapfont with a "Level 1" line, I cannot determine the width of the line and therefore cannot position it in the center of the screen.

Could you please help me find the width / height of the Bitmapfont that has the line "Level 1"

+3


source to share


3 answers


font.getBounds () no longer works, so the answer needs to be updated.

You can use the following:



GlyphLayout glyphLayout = new GlyphLayout();
String item = "Example";
glyphLayout.setText(font,item);
float w = glyphLayout.width;

      

and use w for center in relation to position

+5


source


You can get the width of a line drawn with Bitmapfont like this:

font.getBounds(<String>).width;

      



To draw it in the center of the screen (using the camera), follow these steps:

int level = 1;
String s = "Level "+level;
font.draw(batch, s, camera.viewportWidth/2F-font.getBounds(s).width/2F, camera.viewportHeight()/2F);

      

0


source


You should try using the FreeType Libgdx extension, it will fix all problems with your font.

If you want to draw text in your game, you usually use BitmapFont. However, there is a drawback: BitmapFonts rely on the image, so you need to scale them up if you want a different size that might look ugly.

You could just keep the BitmapFont as large as your game needs, and you never have to zoom in, just down, right? Well, that's true, but such a BitmapFont can easily take up twice as much space on your hard drive as the corresponding TrueType Font (.ttf). Now, imagine you have to send all your big bitmapphones with your game, and your game uses ten different fonts ... on an Android device.

The solution to your problem is the gdx-freetype extension:

  • send only lightweight .ttf files with your game.
  • generate BitmapFont of desired size on the fly
  • can put your own fonts into your game.

cited from: https://github.com/libgdx/libgdx/wiki/Gdx-freetype

-1


source







All Articles