Fuentes TrueType en libGDX


¿Alguien sabe cómo puedo usar una fuente TTF en libGDX? He mirado alrededor y he visto cosas sobre StbTrueTypeFont, pero no parece estar en la última versión.

EDIT: Encontré la fuente StbTrueType, el archivo jar se encuentra en el directorio extensions. Lo he añadido a mi proyecto. Ahora solo necesito averiguar cómo usarlo. ¿Algún ejemplo?

Author: Alex_Hyzer_Kenoyer, 2012-02-28

4 answers

Sí, definitivamente tendrá que agregar los jars gdx-stb-truetype a su proyecto como lo indicó en su edición. Así es como lo usarás, bastante recto...

Primero debes declarar tu BitmapFont y los caracteres que usarás...

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

Entonces necesitas crear la fuente...

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);

Puedes jugar con los argumentos que pases a createBitmapFont() y verás lo que hacen.

Luego, para renderizar la fuente, lo haría como lo hace normalmente...

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-28 17:41:59

Utilice la extensión gdx-freetype:

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

"Para usar gdx-freetype, toma los últimos nightlies, enlaza gdx-freetype.jar y gdx-freetype-natives.jar a tu proyecto de escritorio, enlaza gdx-freetype.jar a tu proyecto de Android y copia los archivos armeabi/libgdx-freetype.so y armeabi-v7a/libgdx-freetype.so a la carpeta libs/ de tu proyecto de Android, al igual que con los archivos libgdx.so."

Fuente: 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-27 21:14:28

Investigó mucho y encontró este método de trabajo:

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

Use esto si falló las respuestas probadas anteriores, libGDX Freetype Wiki para referencia.

 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-16 19:28:27

Esto está funcionando en un teléfono clon S4: Pinewood es una fuente descargada, en la carpeta assets. Vea la estructura de carpetas a continuación.

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() {
    }

}

introduzca la descripción de la imagen aquí

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