How to draw smooth text in libgdx?

前端 未结 10 1292
暖寄归人
暖寄归人 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 00:57

    With many things deprecated after the update, this is what's working for me:

    public void regenerateFonts(OrthographicCamera cam, Game game) {
        int size = 18;
    
        if (cam != null && game != null) {
            // camera and game are provided, recalculate sizes
            float ratioX = cam.viewportWidth / game.getW();
            float ratioY = cam.viewportHeight / game.getH();
            System.out.println("Ratio: [" + ratioX + ":" + ratioY + "]");
    
            size *= ratioY;
        }
    
        // font parameters for this size
        FreeTypeFontParameter params = new FreeTypeFontParameter();
        params.flip = true; // if your cam is flipped
        params.characters = LETTERS; // your String containing all letters you need
        params.size = size;
        params.magFilter = TextureFilter.Linear; // used for resizing quality
        params.minFilter = TextureFilter.Linear; // also
    
        // Lato Light generator
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Lato-Light.ttf"));
    
        // make the font
        fontLatoLight = generator.generateFont(params);
        generator.dispose(); // dispose to avoid memory leaks
    }
    

    And when you want to render it on the screen:

    // text rendering
    fontLatoLight.setColor(Color.WHITE); // set color here (has other overloads too)
    fontLatoLight.draw(batch, "Hello World!", xCoord, yCoord);
    

提交回复
热议问题