最近开始学习libgdx这个开源的游戏框架,使用bitmapfont有很多缺陷,还要自己动手制作,通过查看代码发现libgdx带了freetype,所以应该是能通过使用中文字体来显示中文字符, 下面是具体使用的代码,有不正确的地方还请多多指正。
package com.GreenLemonMobile.achellies;
import android.opengl.GLES10;
import android.os.Bundle;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.io.VorbisDecoder;
import com.badlogic.gdx.audio.transform.SoundTouch;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Jpeg;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData;
import com.badlogic.gdx.math.Vector2;
public class HelloWorld extends AndroidApplication implements ApplicationListener {
private FreeTypeFontGenerator freetypeGenerator;
private Texture backgroundTexture;
private SpriteBatch spriteBatch;
private Vector2 textPosition = new Vector2(100, 100);
private Vector2 textDirection = new Vector2(1, 1);
private BitmapFont freetypeBitmapFont;
private BitmapFont defaultBitmapFont;
private Pixmap jpegPixmap;
private Texture jpegTexture;
/** the file to playback **/
private static final String FILE = "data/bg.ogg";
/** a VorbisDecoder to read PCM data from the ogg file **/
VorbisDecoder decoder;
/** an AudioDevice for playing back the PCM data **/
AudioDevice device;
/** SoundTouch instance to modify the PCM data **/
SoundTouch soundTouch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialize(this, false);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void create() {
freetypeGenerator = new FreeTypeFontGenerator(Gdx.files.internal("data/STXINGKA.TTF"));
FreeTypeBitmapFontData fontData = freetypeGenerator.generateData(48, FreeTypeFontGenerator.DEFAULT_CHARS + "开源游戏引擎", false);
freetypeGenerator.dispose();
freetypeBitmapFont = new BitmapFont(fontData, fontData.getTextureRegion(), false);
backgroundTexture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
spriteBatch = new SpriteBatch();
jpegPixmap = Jpeg.read(Gdx.files.internal("data/badlogic.jpg"));
jpegTexture = new Texture(jpegPixmap);
defaultBitmapFont = new BitmapFont();
freetypeBitmapFont.setColor(Color.RED);
defaultBitmapFont.setColor(Color.BLUE);
}
@Override
public void dispose() {
backgroundTexture.dispose();
spriteBatch.dispose();
jpegPixmap.dispose();
jpegTexture.dispose();
}
@Override
public void pause() {
}
@Override
public void render() {
int centerX = Gdx.graphics.getWidth() / 2;
int centerY = Gdx.graphics.getHeight() / 2;
Gdx.gl10.glClear(GLES10.GL_COLOR_BUFFER_BIT);
textPosition.x += textDirection.x * Gdx.graphics.getDeltaTime() * 60;
textPosition.y += textDirection.y * Gdx.graphics.getDeltaTime() * 60;
if (textPosition.x < 0) {
textPosition.x = 0;
textDirection.x = -textDirection.x;
}
if (textPosition.x > Gdx.graphics.getWidth()) {
textPosition.x = Gdx.graphics.getWidth();
textDirection.x = -textDirection.x;
}
if (textPosition.y < 0) {
textPosition.y = 0;
textDirection.y = -textDirection.y;
}
if (textPosition.y > Gdx.graphics.getHeight()) {
textPosition.y = Gdx.graphics.getHeight();
textDirection.y = -textDirection.y;
}
spriteBatch.begin();
spriteBatch.draw(backgroundTexture, centerX - backgroundTexture.getWidth() / 2,
centerY - backgroundTexture.getHeight() / 2, 0, 0, backgroundTexture.getWidth(), backgroundTexture.getHeight());
spriteBatch.draw(jpegTexture, 0, 0);
spriteBatch.draw(jpegTexture, Gdx.graphics.getWidth() - jpegTexture.getWidth(), Gdx.graphics.getHeight() - jpegTexture.getHeight());
spriteBatch.draw(jpegTexture, 0, Gdx.graphics.getHeight() - jpegTexture.getHeight());
spriteBatch.draw(jpegTexture, Gdx.graphics.getWidth() - jpegTexture.getWidth(), 0);
freetypeBitmapFont.draw(spriteBatch, "hello libgdx 开源游戏引擎", textPosition.x, textPosition.y);
spriteBatch.end();
}
@Override
public void resize(int arg0, int arg1) {
spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, arg0, arg1);
}
@Override
public void resume() {
}
}