Libgdx 之BitmapFont 字体
2016-04-03 21:36
405 查看
教程总目录: /article/7583650.html
前面介绍了怎么将图片在屏幕上渲染出,但是一个游戏仍然需要文字来说明。在Libgdx中用BitmapFont来处理文字。
其实这几个方法是很简单的,看一下具体实现就能知道是做什么的
从第一个构造函数我们可以看出,如果不指定具体的.fnt函数,系统自动从JAR自带的包中加载一个字体。
注意这里有一些字体是不支持中文的,可以下拉选择 宋体 仿宋 等字体
Libgdx只能绘制出在效果区有汉字的文字,如果效果区没有,那么不能绘制
测试代码
前面介绍了怎么将图片在屏幕上渲染出,但是一个游戏仍然需要文字来说明。在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(); }
相关文章推荐
- 《Nginx文件类型错误解析漏洞--攻击演练》 (转)
- Xdebug+ZendStudio配置
- JFrame在去掉标题栏的情况下,如何实现拉大收缩边框的功能?
- Swift开发之初识
- DX中的绘制坐标与Window鼠标坐标不一致的可能原因
- 三种简单排序
- 多行文本输入学生信息并学号排序输出
- TCP的理解实例
- 线程与线程控制
- linux compile objective-c
- Java对象序列化
- 从简单的选择排序入手,深刻理解程序
- oracle开发人员误删表的操作及恢复
- MILLer素数测试法
- JSON解析方案
- 嵌套For循环性能优化案例
- 终端
- 腾讯笔试题-编程题3 变相求回文数
- 修改第三方jar包
- 电商平台学习笔记(八)——面向接口编程案例分析