I found that I can use DistanceFieldShader from here and found an example but I couldn't make it work. First my whole screen becomes white, all the other actors that I have on screen disappears. second, the text that supposed to be smooth becomes to be black squares. whats wrong with my code? Or maybe someone can give me an example that suits my needs.
This class represents score in the game. (Maybe this should be actor and needs to be Label)
public class Score extends Actor {
private int scoreInt;
private BitmapFont scoreBitmapFont;
private int x, y;
private DistanceFieldShader distanceFieldShader;
private Texture distanceFieldTexture;
private BitmapFont distanceFieldFont;
float scale = 1.5f;
float smoothing;
public Score(int x, int y) {
this.x = x;
this.y = y;
scoreInt = 0;
scoreBitmapFont = new BitmapFont();
scoreBitmapFont.setColor(Color.BLACK);
scoreBitmapFont.scale(scale);
distanceFieldTexture = new Texture(Gdx.files.internal("default.png"), true);
distanceFieldFont = new BitmapFont(Gdx.files.internal("default.fnt"), new TextureRegion(
distanceFieldTexture), true);
distanceFieldFont.setColor(Color.BLACK);
distanceFieldShader = new DistanceFieldShader();
// set filters for each page
Texture.TextureFilter minFilter = Texture.TextureFilter.MipMapLinearNearest;
Texture.TextureFilter magFilter = Texture.TextureFilter.Linear;
scoreBitmapFont.getRegion().getTexture().setFilter(Texture.TextureFilter.MipMapLinearNearest, Texture.TextureFilter.Linear);
}
public void inc(int inc) {
scoreInt = scoreInt + inc;
if (scoreInt < 0) scoreInt = 0;
}
@Override
public void draw(Batch batch, float alpha) {
String scoreStr = String.format("Score: %d", scoreInt);
smoothing = scoreBitmapFont.getBounds(scoreStr).width;
batch.setShader(distanceFieldShader);
scoreBitmapFont.draw(batch, scoreStr, x, y/* + scale * getBaselineShift(scoreBitmapFont)*/);
distanceFieldShader.setSmoothing(smoothing / scale);
// y += scoreBitmapFont.getLineHeight();
}
public int getScoreValue() {
return scoreInt;
}
private float getBaselineShift(BitmapFont font) {
if (font == distanceFieldFont) {
// We set -8 paddingAdvanceY in Hiero to compensate for 4 padding on each side.
// Unfortunately the padding affects the baseline inside the font description.
return -8;
} else {
return 0;
}
}
}