テキスト描画に関するBitmapFontクラスを見てみましょう。ソースコードを見てみましょう:
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);
}
これはデフォルトのコンストラクタで、2つのファイル arial-15.fnt と arial-15.png を読み込んでいることがわかります。
arial-15.fnt ファイルの一部:
info face="Arial" size=15 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1
common lineHeight=18 base=14 scaleW=256 scaleH=256 pages=1 packed=0
page id=0 file="arial-15.png"
chars count=189
char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=14 xadvance=4 page=0 chnl=0
char id=255 x=0 y=0 width=8 height=19 xoffset=-1 yoffset=0 xadvance=7 page=0 chnl=0
char id=254 x=8 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=253 x=17 y=0 width=8 height=19 xoffset=-1 yoffset=0 xadvance=7 page=0 chnl=0
char id=252 x=25 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=251 x=34 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=250 x=43 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=249 x=52 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=248 x=61 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=247 x=70 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=246 x=79 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=245 x=88 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=244 x=97 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=243 x=106 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=242 x=115 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=241 x=124 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=240 x=133 y=0 width=9 height=19 xoffset=1 yoffset=0 xadvance=8 page=0 chnl=0
char id=239 x=142 y=0 width=5 height=19 xoffset=0 yoffset=0 xadvance=3 page=0 chnl=0
...
kernings count=374
kerning first=49 second=49 amount=-1
kerning first=121 second=44 amount=-1
kerning first=121 second=46 amount=-1
kerning first=119 second=44 amount=-1
kerning first=119 second=46 amount=-1
kerning first=118 second=44 amount=-1
kerning first=118 second=46 amount=-1
kerning first=114 second=44 amount=-1
kerning first=114 second=46 amount=-1
kerning first=89 second=44 amount=-2
kerning first=89 second=45 amount=-1
kerning first=89 second=46 amount=-2
kerning first=89 second=58 amount=-1
kerning first=89 second=59 amount=-1
kerning first=89 second=65 amount=-1
kerning first=89 second=97 amount=-1
kerning first=89 second=101 amount=-1
arial-15.pngをもう一度見てください:
libgdxがfntファイルに従ってテキストを描画し、png内の対応するテキストの座標を取得し、イメージの対応する部分をインターセプトして描画することは明らかです。
エフェクトは右側のエフェクトで設定できます:
File-Save as BMFont Filesをタップして2つのファイルを生成し、alertsフォルダにコピーします。
使う
bitmapFont = new BitmapFont(Gdx.files.internal("cf.fnt"), Gdx.files.internal("cf.png"), false);
bitmapFont.draw(spriteBatch, "FPS" + Gdx.graphics.getFramesPerSecond(), 5, Gdx.graphics.getHeight() - 10);
bitmapFont.draw(spriteBatch, "ハッピー・ハヌカ・トゥ・エブリワン", 0, Gdx.graphics.getHeight()/2-8);
プログラミング効果:
複数行のテキストについては
public TextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y)
または
public TextBounds drawMultiLine (SpriteBatch spriteBatch, CharSequence str, float x, float y, float alignmentWidth,HAlignment alignment)