本文主要是介绍LibGdx 游戏引擎 freetype 使用指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
网上大多数的Libgdx文档,使用的版本都是0.9X的时候留下的,1.X版本的比较少。freetype在1.X时使用发生发生了变化,写一下变化。
本人使用的是Libgdx 1.6版本。
首先给两个传送门,可以去Git上看官方的wiki。
官方的freetype说明
https://github.com/libgdx/libgdx/wiki/Gdx-freetype
官方的freetype样例
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/extensions/InternationalFontsTest.java
下面是两端代码的比较
0.9X版本
package com.example.mylibgdxfont03;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.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData;public class MyGame implements ApplicationListener {private BitmapFont font;private FreeTypeFontGenerator generator;private FreeTypeBitmapFontData fontData;private SpriteBatch batch;@Overridepublic void create() {generator = new FreeTypeFontGenerator(Gdx.files.internal("testfont.ttf"));fontData = generator.generateData(25, generator.DEFAULT_CHARS+ "晚风把荣华吹散,名利是如此浅薄。—我一条寻水的鱼!", false);// 这里需要把你要输出的字,全部写上,前提是不能有重复的字。font = new BitmapFont(fontData, fontData.getTextureRegion(), false);font.setColor(Color.PINK);batch = new SpriteBatch();}@Overridepublic void dispose() {font.dispose();generator.dispose();batch.dispose();}@Overridepublic void render() {Gdx.gl.glClearColor(1, 1, 1, 1);Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);batch.begin();font.drawMultiLine(batch,"Hello Everyone: \n \n晚风把荣华吹散,\n名利是如此浅薄。",50, 220);font.draw(batch, "——我是一条寻水的鱼!", 150, 90);batch.end();}@Overridepublic void resize(int width, int height) {}@Overridepublic void pause() {}@Overridepublic void resume() {}}
摘自 http://m.blog.csdn.net/blog/yangyu20121224/9182243
1.6版本的代码
package com.mygdx.game;import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;public class MyGdxGame extends ApplicationAdapter {SpriteBatch batch;FreeTypeFontGenerator generator;FreeTypeFontParameter parameter;private BitmapFont font;@Overridepublic void create () {batch = new SpriteBatch();generator = new FreeTypeFontGenerator(Gdx.files.internal("SIMKAI.TTF"));parameter = new FreeTypeFontParameter();parameter.characters = "今天气很好,风和日丽!";font = generator.generateFont(parameter);generator.dispose();}@Overridepublic void render () {Gdx.gl.glClearColor(0, 0, 0, 0);Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);batch.begin();font.draw(batch, "今天天气很好,风和日丽!", 100, 100);batch.end();}@Overridepublic void dispose() {batch.dispose();super.dispose();}}
区别在于:
1 、生成FreeType
0.9X 版本的代码
generator.generateData(25, generator.DEFAULT_CHARS+ "晚风把荣华吹散,名利是如此浅薄。—我一条寻水的鱼!", false);// 这里需要把你要输出的字,全部写上,前提是不能有重复的字。
1.6版本 需要使用FreeTypeFontParameter对象
generator = new FreeTypeFontGenerator(Gdx.files.internal("SIMKAI.TTF"));
parameter = new FreeTypeFontParameter();
parameter.characters = "今天气很好,风和日丽!";
2、使用BitmapFont
0.9X版本代码
font = new BitmapFont(fontData, fontData.getTextureRegion(), false);
1.6版本代码
font = generator.generateFont(parameter);
1.6版本和0.9X版本的freetype差别总结就这些。
另外附上freetype 中FreeTypeFontParameter 的默认参数
/** The size in pixels */
public int size = 16;
/** Foreground color (required for non-black borders) */
public Color color = Color.WHITE;
/** Border width in pixels, 0 to disable */
public float borderWidth = 0;
/** Border color; only used if borderWidth > 0 */
public Color borderColor = Color.BLACK;
/** true for straight (mitered), false for rounded borders */
public boolean borderStraight = false;
/** Offset of text shadow on X axis in pixels, 0 to disable */
public int shadowOffsetX = 0;
/** Offset of text shadow on Y axis in pixels, 0 to disable */
public int shadowOffsetY = 0;
/** Shadow color; only used if shadowOffset > 0 */
public Color shadowColor = new Color(0, 0, 0, 0.75f);
/** The characters the font should contain */
public String characters = DEFAULT_CHARS;
/** Whether the font should include kerning */
public boolean kerning = true;
/** The optional PixmapPacker to use */
public PixmapPacker packer = null;
/** Whether to flip the font vertically */
public boolean flip = false;
/** Whether or not to generate mip maps for the resulting texture */
public boolean genMipMaps = false;
/** Minification filter */
public TextureFilter minFilter = TextureFilter.Nearest;
/** Magnification filter */
public TextureFilter magFilter = TextureFilter.Nearest;
这篇关于LibGdx 游戏引擎 freetype 使用指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!