您的位置:首页 > 其它

Libgdx 之BitmapFont 字体

2016-04-03 21:36 405 查看
教程总目录: /article/7583650.html

前面介绍了怎么将图片在屏幕上渲染出,但是一个游戏仍然需要文字来说明。在Libgdx中用BitmapFont来处理文字。

BitmapFont的简单说明

BitmapFont(位图字体)使用Batch来渲染,BitmapFont字体包含2个文件.png 和 .fnt。 .fnt文件描述了位图字体的位置,大小等等信息。还是首先看类图:



其实这几个方法是很简单的,看一下具体实现就能知道是做什么的

/** Creates a BitmapFont using the default 15pt Arial font included in the libgdx JAR file. This is convenient to easily
* display text without bothering without generating a bitmap font yourself. */
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);
}

/** Creates a BitmapFont from a BMFont file. The image file name is read from the BMFont file and the image is loaded from the
* same directory. The font data is not flipped. */
public BitmapFont (FileHandle fontFile) {
this(fontFile, false);
}

/** Draws text at the specified position.
* @see BitmapFontCache#addText(CharSequence, float, float) */
public GlyphLayout draw (Batch batch, CharSequence str, float x, float y) {
cache.clear();
GlyphLayout layout = cache.addText(str, x, y);
cache.draw(batch);
return layout;
}

/** Draws text at the specified position.
* @see BitmapFontCache#addText(CharSequence, float, float, int, int, float, int, boolean, String) */
public GlyphLayout draw (Batch batch, CharSequence str, float x, float y, float targetWidth, int halign, boolean wrap) {
cache.clear();
GlyphLayout layout = cache.addText(str, x, y, targetWidth, halign, wrap);
cache.draw(batch);
return layout;
}

/** Draws text at the specified position.
* @see BitmapFontCache#addText(CharSequence, float, float, int, int, float, int, boolean, String) */
public GlyphLayout draw (Batch batch, CharSequence str, float x, float y, int start, int end, float targetWidth, int halign,
boolean wrap) {
cache.clear();
GlyphLayout layout = cache.addText(str, x, y, start, end, targetWidth, halign, wrap);
cache.draw(batch);
return layout;
}


从第一个构造函数我们可以看出,如果不指定具体的.fnt函数,系统自动从JAR自带的包中加载一个字体

BitmapFont绘制中文

默认的字体中是不支持中文的,因此如果我们想要在Libgdx中绘制中文,需要自己创建字体。Libgdx提供了一个工具Hiero方便我们来创建自己的字体,首先看工具的介绍:



注意这里有一些字体是不支持中文的,可以下拉选择 宋体 仿宋 等字体

Libgdx只能绘制出在效果区有汉字的文字,如果效果区没有,那么不能绘制

测试代码

SpriteBatch batch;
BitmapFont font;

@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("font/hireo.fnt"));

// 防止字体模糊
font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
}

@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

batch.begin();
font.setColor(Color.WHITE);
font.draw(batch, "我爱编程", 200, 400);

font.setColor(Color.BLUE);
font.draw(batch, "WOAIBIANCHENG", 200, 340);

// 放大2倍
font.setColor(Color.RED);
font.getData().setScale(2.0f);
font.draw(batch, "Libgdx", 200, 280);
font.getData().setScale(1.0f);

// 绘制多行字体
font.draw(batch, "This is\nmultiline string", 200, 220);

batch.end();
}

@Override
public void dispose() {
batch.dispose();
font.dispose();
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: