24

I'm rendering on screen the game fps using bitmap font but there are no methods for the size. This is a problem for me because my camera viewport size is very small so the text when rendered is huge and pixelated.

font.draw(batch, Float.toString(Gdx.graphics.getFramesPerSecond), x, y);
Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45

5 Answers5

48

Did you try setScale() method that what i use to resize my font

myFont.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
myFont.setScale(scale);

if you have trouble, leave a comment

Good Luck !!

Edit :

With the latest libgdx version, try to scale font like this :

myFont.getData().setScale();
Netero
  • 3,761
  • 2
  • 29
  • 29
7

I often use what minos23 suggested. But the downfall is that it can look pixelated especially when scaling upwards. A fancy large bitmap font can take up a lot of space and if you need many different fonts you might go over your budget.

By using Gdx.Freetype you are able to create bitmapfonts at runtime from small .ttf files. This means you only need to ship the .ttf files with your app and can generate a font based on user settings like resolution.

Other then scaling and the freetype solution is having multiple bitmaps of different font sizes. This way your fonts stay crisp all time but at the cost of runtime performance to generate the proper fonts.

Madmenyo
  • 8,389
  • 7
  • 52
  • 99
3

setScale is the function to use. Please note that with the newest LibGDX version (this changed earlier though) you need to do this isntead:

font.getData().setScale(2, 2);

before it was enough to do:

font.setScale(2, 2);

The first number in setScale is the X scale, and the second is the Y scale.

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

I use setScale() function too as others to reduce the font size, but here I want to offer another solution and I have a question. Why you don't use FPSRenderer instance or why you don't draw your fps label on another batch which projection matrix has the screen size?

0

Netero's answer works. If you want to make code cleaner, you can write a helper function:

public static void setLineHeight(BitmapFont font, float height) {
    font.getData().setScale(height * font.getScaleY() / font.getLineHeight());
}
Deepscorn
  • 822
  • 10
  • 22