0
\$\begingroup\$

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;
        }
    }
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I found the solution and now it works:

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class BitmapFontShader extends BitmapFont {

private final Texture.TextureFilter minFilter = Texture.TextureFilter.MipMapLinearNearest;
private final Texture.TextureFilter magFilter = Texture.TextureFilter.Linear;

private DistanceFieldShader distanceFieldShader;
private float smoothing;

public BitmapFontShader(FileHandle fontFile, TextureRegion textureRegion, Boolean flip, float shaderSmooth) {
    super(fontFile, textureRegion, flip);
    this.smoothing = shaderSmooth;
    distanceFieldShader = new DistanceFieldShader();
    getRegion().getTexture().setFilter(minFilter, magFilter);
}

@Override
public BitmapFont.TextBounds draw(Batch batch, java.lang.CharSequence str, float x, float y) {
    smoothing = this.getBounds(str).width;
    if(this.getScaleX() != 0) {
        batch.setShader(distanceFieldShader);
        distanceFieldShader.setSmoothing(smoothing / this.getScaleX());
    }
    TextBounds textBounds = super.draw(batch, str, x, y + this.getScaleY() * getBaselineShift(this));
    batch.setShader(null);
    return textBounds;
}

private float getBaselineShift(BitmapFont font) {
    if (font == this) {
        // 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;
    }
}

}

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .