LibGDX game engine-3-text rendering (Hiero) = use the accompanying tool Hiero to generate a font
This series of blog posts are from Sina's blog-xiaotudou. I have added my own understanding and modifications to this article!
Tool: Hiero.
: Https://code.google.com/p/libgdx/downloads/list
After the text is configured, click File-Save as BMFont Files to generate two Files fnt and png and copy them to the asserts folder.
Fnt is equivalent to a small font, While png is the drawing of this small font. It uses the configuration in fnt to capture the PNG image.
------------------------------------------------------------------------------
Source code:
Package com. qsuron;
Import com. badlogic. gdx. ApplicationListener;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. graphics. GL10;
Import com. badlogic. gdx. graphics. g2d. BitmapFont;
Import com. badlogic. gdx. graphics. g2d. SpriteBatch;
Public class MyGdxGame implements ApplicationListener {
Private SpriteBatch batch;
Private BitmapFont font;
@ Override
Public void create (){
Batch = new SpriteBatch ();
Font = new BitmapFont (Gdx. files. internal ("data/test. fnt "),
Gdx. files. internal ("data/test.png"), false );
}
@ Override
Public void dispose (){
Batch. dispose ();
}
@ Override
Public void render (){
Gdx. gl. glClearColor (0, 0, 0 );
Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT );
Batch. begin ();
Font. draw (batch, "LibGDX game engine Fin font test program", 20,280); // normal drawing
Font. draw (batch, "LibGDX game engine Fin font test program", 20,300, 0, 10); // capture and draw
Font. drawMultiLine (batch, "small tree \ n Engine Test", 330,230); // line feed drawing, can recognize the \ n line feed symbol
Batch. end ();
}
@ Override
Public void resize (int width, int height ){
}
@ Override
Public void pause (){
}
@ Override
Public void resume (){
}
}
BitmapFont class (Text container)
----------------------------------------------------------------------------------
API Introduction: displays bitmap fonts, including two files: an image file textureregion, which contains symbols in anglecode.
The bmfont text format describes each glyph as an image file. Text is drawn using a SpriteBatch.
----------------------------------------------------------------------------------
Function purpose: render static text and set the text color and size.
----------------------------------------------------------------------------------
Usage: You can manually set the text size, color, and rotation,
You can use the function settings here, but you can also set them in Hiero. You can choose which method to set it,
The third parameter in the Process of instantiating an object is management flip,
Private BitmapFont font;
Font = new BitmapFont (Gdx. files. internal ("data/test. fnt "),
Gdx. files. internal ("data/test.png"), false );
Font. setColor (0.5f, 0.4f, 0.6f, 1); // set the color
Font. setScale (1.0f); // font Ratio
Font. draw (batch, "Xiaoshu", 330,130 );
----------------------------------------------------------------------------------
(1) display a single line of text (two methods are rewritten, and five parameters, marker, can be used to draw captured characters)
Method: draw (SpriteBatch spriteBatch, java. lang. CharSequence str,
Float x, float y, int start, int end)
SpriteBatch Class Object
Str string
Parameter (x, y) Normal Coordinate System (lower left corner ).
Fifth, six parameters, string truncation, first encoding 0
----------------------------------------------------------------------------------
(2) text line feed display (use drawMultiLine to recognize \ n line breaks)
Method: drawMultiLine (SpriteBatch spriteBatch, java. lang. CharSequence str,
Float x, float y, float alignmentWidth, BitmapFont. HAlignment alignment)
Alignment: LEFT, RIGHT, and CENTER.
Here, the LEFT is the upper LEFT corner, and the RIGHT is the source of the drawing from the cursor and CENTER is the CENTER of each line of text,
Expand to both sides.
Float alignmentWidth, which does not work for LEFT. It only works for RIGHT and CENTER,
RIGHT is equivalent to translating the painting origin.
The width of alignmentWidth, which is equivalent to the width of the base point translation alignmentWidth/2 for the CENTER.
----------------------------------------------------------------------------------
Reprinted please indicate the source: blog.csdn.net/qsuron Xiaoshu blog (qsuron)