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é el tipo de fuente StbTrueType, el archivo jar se encuentra en el directorio de extensiones. Lo he añadido a mi proyecto. Ahora sólo tengo que averiguar cómo usarlo. ¿Algún ejemplo?

Sí, definitivamente necesitarás agregar los gdx-stb-truetype a tu proyecto como lo has dicho en tu edición. Aquí está cómo usted lo utilizará, bastante straighforward …

Primero debes declarar tu BitmapFont y los caracteres que BitmapFont

 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 pasas a createBitmapFont() y verás lo que hacen.

Entonces para hacer la fuente que usted lo haría como lo hace normalmente …

 batch.begin(); font.draw(font, "This is some text", 10, 10); batch.end(); 

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, coge los últimos nightlies, enlaza gdx-freetype.jar y gdx-freetype-natives.jar a tu proyecto de escritorio, gdx-freetype.jar a tu proyecto de Android y copia el armeabi/libgdx-freetype.so armeabi-v7a/libgdx-freetype.so y armeabi-v7a/libgdx-freetype.so en la libs/ carpeta de su proyecto Android, al igual que con los archivos libgdx.so ".

Fuente: http://www.badlogicgames.com/wordpress/?p=2300

Hizo mucha investigación 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 ha fallado las respuestas anteriores, libGDX Freetype Wiki para referencia.

Esto está trabajando en un teléfono clon S4: Pinewood es una fuente descargada, en la carpeta de activos. Consulte 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 aquí la descripción de la imagen

  • Libgdx: configurar otra pantalla, pero los botones permanentes de la pantalla antigua están activos
  • LibGDX RTL fuente de representación?
  • Hacer un juego en LibGDX con colisión y gravedad sin usar Box2D
  • Cómo girar cuatro sprites al foco de estos sprites
  • Me gustaría saber si hay una forma de poner un juego libgdx dentro de una aplicación para Android.
  • Libgdx para Android muestra negro antes de la pantalla principal
  • LibGDX 1.5 girando sprite alrededor de su centro
  • Los colores de ShapeRenderer dejan de funcionar al renderizar imágenes con SpriteBatch
  • Cómo detectar el final de la acción en Libgdx (0.9.7) de android
  • Resultados inesperados implementando desenfoque de movimiento simple en Libgdx
  • touchdown mantenido libgdx
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.