Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw smooth text in libgdx?

Tags:

libgdx

I try to draw simple text in my android game on libgdx, but it's look sharp. How to make text look smooth in different resolutions? My Code:

private BitmapFont font;  font = new BitmapFont();  font.scale((ppuX*0.02f));  font.draw(spb, "Score:", width/2-ppuX*2f, height-0.5f*ppuY); 
like image 628
JustOneMan Avatar asked Feb 03 '13 17:02

JustOneMan


People also ask

How do I center text in Libgdx?

The easiest way to do this, is to use the TextButton class from libgdx. The text from a TextButton is centered by default. This still works!


2 Answers

font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear); 

This gets the texture used in a BitmapFont and changes its filtering to bilinear, allowing higher resulting image quality while both up- and downscaling it at the cost of slightly slower (the difference is usually not noticeable) GPU rendering.

like image 146
bluepuppy Avatar answered Sep 22 '22 17:09

bluepuppy


One solution is to use the FreeType extension to libgdx, as described here. This allows you to generate a bitmap font on the fly from a .ttf font. Typically you would do this at startup time once you know the target resolution.

Here's an example:

int viewportHeight; BitmapFont titleFont; BitmapFont textFont;  private void createFonts() {     FileHandle fontFile = Gdx.files.internal("data/Roboto-Bold.ttf");     FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);     FreeTypeFontParameter parameter = new FreeTypeFontParameter();     parameter.size = 12;     textFont = generator.generateFont(parameter);     parameter.size = 24;     titleFont = generator.generateFont(parameter);     generator.dispose(); } 
like image 34
Rod Hyde Avatar answered Sep 19 '22 17:09

Rod Hyde