libgdx 源码 BitmapFont类本身是不支持中文的
public
BitmapFont () {
this
(Gdx.files.classpath(
"com/badlogic/gdx/utils/arial-15.fnt"
),
Gdx.files.classpath(
"com/badlogic/gdx/utils/arial-15.png"
),
false
,
true
);
}
使用下面的代码可以实现中文显示,但必须要自定义一些字体和图片,用工具:Hiero
可以实现
|
bitmapFont =
new
BitmapFont(Gdx.files.internal(
"cf.fnt"
), Gdx.files.internal(
"cf.png"
),
false
);
今天介绍的是用字库.ttf实现中文显示,代码如下:
import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Color; 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.stbtt.TrueTypeFontFactory; /** * 显示中文 * @author cui.li * */ public class FontDemo implements ApplicationListener { // SpriteBatch是libgdx提供的opengl封装,可以在其中执行一些常规的图像渲染, // 并且libgdx所提供的大多数图形功能也是围绕它建立的。 SpriteBatch spriteBatch; // BitmapFont是libgdx提供的文字显示用类,内部将图片转化为可供opengl调用的 // 文字贴图(默认不支持中文)。 private BitmapFont font; private Texture textTure; public static final String FONT_CHARACTERS = "世界人民爱好的美好中文支持再看看"; @Override public void create() { font = TrueTypeFontFactory.createBitmapFont( Gdx.files.internal("data/font/stxingka.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); textTure = new Texture(Gdx.files.internal("data/xk.jpg")); spriteBatch = new SpriteBatch(); font.setColor(Color.RED); } @Override public void resize(int width, int height) { } @Override public void render() { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); spriteBatch.begin(); spriteBatch.draw(textTure, 0, 0); font.draw(spriteBatch, "中文支持达到法定", 80, 80); spriteBatch.end(); } @Override public void pause() { } @Override public void resume() { } @Override public void dispose() { // 释放占用的资源 spriteBatch.dispose(); font.dispose(); } }
|