Czcionki TrueType w libGDX

Czy ktoś wie jak mogę użyć czcionki TTF w libGDX? Rozejrzałem się i widziałem rzeczy o StbTrueTypeFont, ale to nie wydaje się być w najnowszej wersji.

EDIT: znalazłem czcionkę StbTrueType, plik jar znajduje się w katalogu extensions. Dodałem to do mojego projektu. Teraz muszę tylko wymyślić, jak tego użyć. Jakieś przykłady?

Author: Alex_Hyzer_Kenoyer, 2012-02-28

4 answers

Tak, na pewno będziesz musiał dodać gdx-stb-truetype słoiki do swojego projektu, jak podałeś w edycji. Oto, jak go użyjesz, całkiem prosto...

Najpierw musisz zadeklarować swoje BitmapFont i znaki, których będziesz używać...

BitmapFont font;
public static final String FONT_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789][_!$%#@|\\/?-+=()*&.;,{}\"´`'<>";

Następnie należy utworzyć czcionkę...

font = TrueTypeFontFactory.createBitmapFont(Gdx.files.internal("font.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
font.setColor(1f, 0f, 0f, 1f);

Możesz grać argumentami, które podajesz createBitmapFont(), a zobaczysz, co zrobią.

Następnie, aby renderować czcionkę, zrobisz to tak, jak zwykle...

batch.begin();
font.draw(font, "This is some text", 10, 10);
batch.end();
 40
Author: DRiFTy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-28 17:41:59

Użyj rozszerzenia gdx-freetype:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
BitmapFont font15 = generator.generateData(15);
BitmapFont font22 = generator.generateData(22);
generator.dispose();

"aby użyć gdx-freetype, Pobierz najnowsze nightlies, połącz gdx-freetype.jar i gdx-freetype-natives.jar do swojego projektu na pulpicie, połącz gdx-freetype.jar do swojego projektu na Androida i skopiuj pliki armeabi/libgdx-freetype.so i armeabi-v7a/libgdx-freetype.so do folderu libs/ Twojego projektu na Androida, tak jak w przypadku plików libgdx.so."

Źródło: http://www.badlogicgames.com/wordpress/?p=2300

 26
Author: TalkLittle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-27 21:14:28

Czy badał wiele i znalazł tę metodę pracy:

FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf"));
FreeTypeFontParameter parameter = new FreeTypeFontParameter();
parameter.size = 12; // font size
BitmapFont font12 = generator.generateFont(parameter);
generator.dispose(); // avoid memory leaks, important

Użyj tego, jeśli nie udało Ci się wypróbować powyższych odpowiedzi, libGDX FreeType Wiki w celach informacyjnych.

 6
Author: Kévin Berthommier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-16 19:28:27

To działa na telefonie klonowym S4: Pinewood to pobrana czcionka w folderze zasoby. Zobacz strukturę folderów poniżej.

import android.util.Log;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;

public class Game implements ApplicationListener {
    private SpriteBatch mSpriteBatch;
    private Texture textureBackground;
    private BitmapFont mBitmapFont;

    public void create() {

        Gdx.graphics.setContinuousRendering(false);
        Gdx.graphics.requestRendering();

        mSpriteBatch = new SpriteBatch();

        Texture.setEnforcePotImages(false);
        textureBackground = new Texture(Gdx.files.internal("background.jpg"));

        FileHandle fontFile = Gdx.files.internal("Pinewood.ttf");
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);

        mBitmapFont = generator.generateFont(150);
        generator.dispose();

        mBitmapFont.setColor(0.9f, 0.5f, 0.5f, 1); 
        Log.e("Game", "mBitmapFont.getScaleX() : "+mBitmapFont.getScaleX() + ", mBitmapFont.getScaleY() "+mBitmapFont.getScaleY());

    }

    public void render() {
        Log.e("Game", "render()");

        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
        mSpriteBatch.begin();
        // Drawing goes here!
        mSpriteBatch.draw(textureBackground, 0, 0);
        mBitmapFont.draw(mSpriteBatch, "FPS:"+Gdx.graphics.getFramesPerSecond(), 110, 260);
        mSpriteBatch.end();     
    }

    public void resize(int width, int height) {
    }

    public void pause() {
    }

    public void resume() {
    }

    public void dispose() {
    }

}

Tutaj wpisz opis obrazka

 3
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-05 04:02:27