Kevin Bryan
Kevin Bryan

Reputation: 1858

How set Libgdx bitmap font size?

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);

Upvotes: 24

Views: 36694

Answers (5)

Netero
Netero

Reputation: 3819

Did you try setScale() method? That's 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();

Upvotes: 50

Madmenyo
Madmenyo

Reputation: 8584

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.

Upvotes: 7

Deepscorn
Deepscorn

Reputation: 832

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());
}

Upvotes: 0

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28278

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.

Upvotes: 3

Khachatur Badalyan
Khachatur Badalyan

Reputation: 29

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?

Upvotes: 1

Related Questions