Libgdx 之BitmapFont 字体

教程总目录: http://blog.csdn.net/zqiang_55/article/details/50878524

前面介绍了怎么将图片在屏幕上渲染出,但是一个游戏仍然需要文字来说明。在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();
    }

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值