Libgdx sprite batch font poor rendering

I ran into a problem while trying to put a text / dialog system in my game project. When I create a font and call the draw method on it, passing the updated spriteBatch camera, each font pixel has the same size of one sprite.

I get the following render:

font draw scale issue

What you can see in the picture is the upper part of the "h" "hello", with each pixel being oversized. The same camera is used to render fragments / sprites.

The effect I want to achieve is similar to this:

Elliot quest example

Here is the code:

// 15 * 12 tile size camera = new OrthographicCamera(Const.VIEWPORT_WIDTH, Const.VIEWPORT_HEIGHT); BitmapFont font = new BitmapFont(Gdx.files.internal("data/fonts/myfont.fnt")); // .... // p => player position camera.position.x = p.getX(); camera.position.y = p.getY(); camera.update(); batch.setProjectionMatrix(camera.combined); batch.begin(); font.draw(batch, "hello", p.getX(), p.getY()); batch.end(); 

I tried using font.setScale() without success.

Does anyone know how to achieve this?

+6
source share
2 answers

Create a camera with a larger viewport (for example, the perfect pixel of your test device). And use this to render the font. Also, do not create a new font each render, create it at the beginning and use it.

Edit:

To calculate the position, you must draw the text, it will be so. Suppose your world camera is called "cam" and your text camera is called "guicam". And GameObject is called an "object."

 ratew = guicam.viewportWidth/cam.viewportWidth; //<--- you should calculate these 2 only once. rateh = guicam.viewportHeight/cam.viewportHeight; x = guicam.position.x-(cam.position.x-object.position.x)*ratew; y = guicam.position.y-(cam.position.y-object.position.y)*rateh; font.draw(batch, "stuff", x, y); 
+6
source

You can try Texture.TextureFilter.Linear for the min and max filters.

Libgdx AssetManager loads raster fonts with the necessary filters:

 BitmapFontLoader.BitmapFontParameter param = new BitmapFontLoader.BitmapFontParameter(); param.maxFilter = Texture.TextureFilter.Linear; param.minFilter = Texture.TextureFilter.Linear; assetManager.load(fileName, BitmapFont.class, param); 
-1
source

All Articles