How to draw smooth text in libgdx?

前端 未结 10 1286
暖寄归人
暖寄归人 2020-12-01 00:01

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:



        
10条回答
  •  天涯浪人
    2020-12-01 01:00

    My Solution for smooth text with Libgdx

    I use BitmapFont and I generate 3 different size same fonts using Hiero tool example Arial 16 , Arial 32, Arial 64

    I put them in my assets file and use (load) only one of them depeding on the size of screen

    if(Gdx.graphics.getWidth() < (480*3)/2)
            {
                textGametFont = BitmapFont(Gdx.files.internal(nameFont+16+".fnt"),
                        Gdx.files.internal(nameFont+16+".png"), false);
            }else
            {
                if(Gdx.graphics.getWidth() < (3*920)/2)
                {
    
                    textGametFont = new BitmapFont(Gdx.files.internal(nameFont+32+".fnt"),
                            Gdx.files.internal(nameFont+32+".png"), false);
                }else
                {
                    textGametFont = new BitmapFont(Gdx.files.internal(nameFont+64+".fnt"),
                            Gdx.files.internal(nameFont+64+".png"), false);
                }
            }
    

    then I use this line of code to higher result quality of down and up Scaling

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

    scale the image

    to handle the size of the font for all type of resolution of device I use those two functions

    public static float xTrans(float x)
    {
        return x*Gdx.graphics.width/(YourModel.SCREEN_WIDTH);
    }
    
    public static float yTrans(float y)
    {
        return y*Gdx.graphics.height/YourModel.SCREEN_Height;
    }
    

    the model screen resolution that i use is

    SCREEN_WIDTH = 480

    SCREEN_HEIGHT = 320

    Set the scale to the font

    textGametFont.setScale((xtrans(yourScale)+ ytrans(yourScale))/2f);
    

    and finally draw your text

    textGametFont.draw(batch, "WINNER !!", xTrans(250), yTrans(236));
    

    Hope this was clear and helpful !!!

提交回复
热议问题