Codejie's C++ Space

Using C++

LIBGDX: FreeTypeFontGenerator and BitmapTrueFont

 

    在刚开始敲I;P游戏的时候, 字体使用的是BitmapFont, 只要有一套字体的PNG文件就可以显示字体数据了. 并且通过BitmapFont对象可以很好的跟Label结合在一起使用. BitmapFont中的字体从PNG文件中截取出来有很多方便的地方, 比如, 一来只需要准备使用的字符即可, 这样字体文件比较小; 二来可以定义任意的图案来代替字符, 灵活性定制方便. 但反过来, 由于基于PNG文件, 也导致了BitmapFont有个两个主要的缺陷 --

        1. 标量字体, 放大会失真;

        2. 字符集有限, 尤其是使用中文时, 改动几率大;

 

    好在LIBGDX还在其扩展库(gdx-freetype)中提供了另外一个好用的对象 --- FreeTypeFontGenerator. 通过这个对象可以加载TTF (TrueType Font), 然后产生BitmapFont对象就可以使用了.

    下面是FreeTypeFontGenerator对象的主要函数原型.

/** Generates a new {@link BitmapFont}, containing glyphs for the given characters. The size is expressed in pixels. Throws a
 * GdxRuntimeException in case the font could not be generated. Using big sizes might cause such an exception. All characters
 * need to fit onto a single texture.
 * @param size the size in pixels
 * @param characters the characters the font should contain
 * @param flip whether to flip the font horizontally, see {@link BitmapFont#BitmapFont(FileHandle, TextureRegion, boolean)} */
public BitmapFont generateFont (int size, String characters, boolean flip) {}
/** Generates a new {@link BitmapFont}. The size is expressed in pixels. Throws a GdxRuntimeException in case the font could not
 * be generated. Using big sizes might cause such an exception. All characters need to fit onto a single texture.
 * 
 * @param size the size of the font in pixels */
public BitmapFont generateFont (int size) {}

 

    在LIBGDX中, AssetManager是个很实用的对象, 通过它可以在游戏初始时就将资源加载或者初始化好, 使用时只需要传递资源名称即可获取相关资源. 比如, 下面代码加载了TextureAtlas和Sound的资源.

assetManager.load(PackConfig.SCREEN_PLAY, TextureAtlas.class);
assetManager.load(PackConfig.SCREEN_MENU, TextureAtlas.class);

assetManager.load(AudioConfig.MENU_CLICK, Sound.class);
assetManager.load(AudioConfig.TRAY_CATCH, Sound.class);

    字体也是一种资源, 因此也可以通过AssetManager来加载TTF字体. 为了能使得两者无缝链接, 可以模仿BitmapFont对象创建个BitmapTrueFont来实现.

 

package jie.android.ip.common.ttf;

import java.util.HashMap;

import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.utils.Disposable;

public class BitmapTrueFont implements Disposable {

	private final HashMap<Integer, BitmapFont> fontMap = new HashMap<Integer, BitmapFont>();
	
	private final FreeTypeFontGenerator generator;
	private final String chars;
	
	public BitmapTrueFont(final FreeTypeFontGenerator generator, final BitmapTrueFontParameter parameter) {
		this.generator = generator;
		this.chars = parameter.getChars();
	}
	
	@Override
	public void dispose() {
		for (final BitmapFont font : fontMap.values()) {
			if (font != null) {
				font.dispose();
			}
		}

		if (generator != null) {
			generator.dispose();
		}
	}
	
	public final BitmapFont getBitmapFont(int size) {
		if (generator == null) {
			return null;
		}
		
		BitmapFont font = fontMap.get(Integer.valueOf(size));
		if (font == null) {
			if (chars == null) {
				font = generator.generateFont(size);
			} else {
				font = generator.generateFont(size, chars, false);
			}
			fontMap.put(Integer.valueOf(size), font);
		}
		return font;
	}
	
	static public class BitmapTrueFontParameter extends AssetLoaderParameters<BitmapTrueFont> {
		private String chars = null;
		
		public BitmapTrueFontParameter() {			
		}
		
		public BitmapTrueFontParameter(final String chars) {
			this.chars = chars;
		}
		
		public final String getChars() {
			return chars;
		}
	}	
}

    OK, 现在可以使用下面代码加载TTF资源了.

assetManager.load("example.ttf", BitmapTrueFont.class, new BitmapTrueFont.BitmapTrueFontParameter(null));

posted on 2014-03-09 21:03 codejie 阅读(1950) 评论(6)  编辑 收藏 引用 所属分类: G7I;P

评论

# re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-22 17:55 西米的风

那get 怎么做 你这个构造貌似不能调用get 得不到值啊 因为 一直generator 是null吧!! 在构造函数里面应该要new一个generator 把  回复  更多评论   

# re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-22 21:05 codejie

@西米的风
啊..还有一个BitmapTrueFontLoader来负责这事情..  回复  更多评论   

# re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 15:34 西米的风

恩 我也是刚接触这个,我昨天看了下源代码 的确还要写一个loader 来处理,谢谢了,膜拜大神,中文游戏就是麻烦ttf资源 如果不放在assetManager里面 游戏如果转入后台资源就被释放了,哎这个问题 纠结好久 再次谢谢了,看了您的文章受益匪浅!@codejie  回复  更多评论   

# re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 15:56 codejie

@西米的风
客气了..这里想多说一句 -- LIBGDX真的还有点小众...  回复  更多评论   

# re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 16:06 西米的风

@codejie
我想先拿来学习 ,学习写法思路,我开始的想法是先把一个搞精通,搞其他的引擎也就水到渠成,libgdx现在的网上学习资源还是很多的,哈哈,天资愚钝只能慢慢来啦!  回复  更多评论   

# re: LIBGDX: FreeTypeFontGenerator and BitmapTrueFont 2014-05-23 17:26 codejie

@西米的风
哈, 如此'执迷不悟'...那很高兴这些对你有用.  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

Using C++

导航

统计

留言簿(73)

随笔分类(513)

积分与排名

最新评论

阅读排行榜

评论排行榜