libgdx学习记录2——文字显示BitmapFont

时间:2021-10-29 00:10:46

libgdx对中文支持不是太好,主要通过Hireo和ttf字库两种方式实现。本文简单介绍最基本的bitmapfont的用法。

 代码如下:

 1 package com.fxb.newtest;
 2 
 3 import com.badlogic.gdx.ApplicationAdapter;
 4 import com.badlogic.gdx.Gdx;
 5 import com.badlogic.gdx.graphics.GL10;
 6 import com.badlogic.gdx.graphics.g2d.BitmapFont;
 7 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 8 
 9 public class Lib001_Font extends ApplicationAdapter{
10 
11     BitmapFont font;
12     SpriteBatch batch;
13     
14     @Override
15     public void create() {
16         // TODO Auto-generated method stub
17         batch = new SpriteBatch();
18         font = new BitmapFont( Gdx.files.internal( "font/test.fnt" ), Gdx.files.internal( "font/test.png" ), false );
19     }
20 
21     @Override
22     public void render() {
23         // TODO Auto-generated method stub
24         Gdx.gl.glClearColor( 1, 1, 1, 1 );
25         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
26         
27         batch.begin();
28         font.draw( batch, "5月20号,天空晴朗,蓝天里飘着白云", 10, 100 );
29         font.drawMultiLine( batch, "5月20号\n天空晴朗\n蓝天里飘着白云", 10, 250 );
30         batch.end();
31     }
32 
33     @Override
34     public void dispose() {
35         // TODO Auto-generated method stub
36         font.dispose();
37         batch.dispose();
38         super.dispose();
39     }
40 
41 }

运行效果如下:

libgdx学习记录2——文字显示BitmapFont