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);
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!
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With