LibGDX game engine-4-text rendering (TTF-FreeType)

Source: Internet
Author: User
Tags libgdx


LibGDX game engine-4-text rendering (TTF font-FreeType)


This series of blog posts are from Sina's blog-xiaotudou. I have added my own understanding and modifications to this article!


If you create a project through step-ui, this package is not added by default and needs to be imported manually


Import method:


(1) Open the decompressed 0.9.9 package, extensions folder, gdx-freetype folder,
Locate gdx-freetype.jar and gdx-freetype-natives.jar.


(2) Import extension packages


First, the core code project, copy the gdx-freetype.jar and paste it to the lib folder of the core code project,
Add it to the Referenced Libraries library.


Secondly, android project, copy and paste the gdx-freetype.jar to-android project lib folder,
Add it to the Referenced Libraries library.


Finally, the desktop project, copy and paste the gdx-freetype-natives.jar to the lib folder of the-desktop project,
Then add it to Referenced Libraries.


(3) copy the two folders, armeabi and armeabi-v7a, In the gdx-freetype folder to the-android project,
Located in libs/, replace the original armeabi and armeabi-v7a folders.




Bytes -------------------------------------------------------------------------------------
Usage:


Package com. qsuron;


Import com. badlogic. gdx. ApplicationListener;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. graphics. Color;
Import com. badlogic. gdx. graphics. GL10;
Import com. badlogic. gdx. graphics. g2d. BitmapFont;
Import com. badlogic. gdx. graphics. g2d. SpriteBatch;
Import com. badlogic. gdx. graphics. g2d. freetype. FreeTypeFontGenerator;
Import com. badlogic. gdx. graphics. g2d. freetype. FreeTypeFontGenerator. FreeTypeBitmapFontData;


Public class MyGdxGame implements ApplicationListener {
Private SpriteBatch batch;
Private BitmapFont font;
Private FreeTypeFontGenerator generator; // font producer
Private FreeTypeBitmapFontData fontData; // font Processor

@ Override
Public void create (){
Batch = new SpriteBatch ();
Generator = new FreeTypeFontGenerator (Gdx. files. internal ("data/MyFont. ttf "));
FontData = generator. generateData (25, "bBdDfFgGiIlLnNtTxX small tree game engine font test program! ", False );
Font = new BitmapFont (fontData, fontData. getTextureRegions (), 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 Fnt font testing program", 20,280); // normal drawing
Font. draw (batch, "LibGDX game engine Fnt font testing 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 (){
}


}


Bytes -------------------------------------------------------------------------------------


PS: libgdx. so file


A: In general, the. so file is often referred to as the dynamic link library, which is compiled by C or C ++. During file loading or running,
The library is loaded by the Android operating system program. As part of the loading process, it is responsible for parsing external references (such as libraries ).
On different systems, the executable file contains a table named import directory, each of which contains the name of a database.
Load the database required by the program according to the name recorded in the table, load it into the memory, and then determine the database address based on the database address after the database is loaded.
Update the executable program. After the update, call the functions in the database or reference the data in the database. This is the principle of the. so file.


Bytes -------------------------------------------------------------------------------------









04.1 FreeTypeFontGenerator class





TTF font producer


Bytes -------------------------------------------------------------------------------------


API definition: The generator (producer) of TTF fonts is actually the same as that of FreeTypeFontGenerator and Texture,
It is only a container, responsible for containing ". ttf" files and then backup.


Bytes -------------------------------------------------------------------------------------


Function purpose: saves and parses files in the. ttf format. This is the same as that of the Texture class.


Bytes -------------------------------------------------------------------------------------


Usage: FreeTypeFontGenerator is generally used with FreeTypeBitmapFontData.


Bytes -------------------------------------------------------------------------------------


PS: specifically, the TTF file must use the English name.


Bytes -------------------------------------------------------------------------------------







04.2 FreeTypeBitmapFontData class


TTF font Processor


Bytes -------------------------------------------------------------------------------------


API definition: responsible for processing FreeTypeBitmapFontData data (processor)


Bytes -------------------------------------------------------------------------------------


Function purpose: Process FreeTypeGenerator data similar to BitmapFontData


Bytes -------------------------------------------------------------------------------------


Usage:
FreeTypeBitmapFontData fontData = freetypeGenerator. generateData (
Int Size, some Chinese String, false );


Bytes -------------------------------------------------------------------------------------


Class parameters and important member variables. The first one is the font size,
The second is a Chinese character string (Chinese character, which must be enclosed by quotation marks), and the third is in the flip state.


Bytes -------------------------------------------------------------------------------------
DEFAULT_CHARS:


FreeTypeBitmapFontData has an important member variable DEFAULT_CHARS,
It is the basic English character encapsulated by Mario. Open FreeTypeBitmapFontData.
You will see the variable assignment, so that even if there are no English characters in the ". TTF" font, we can also output English characters,
Because the author has already packaged it for us.






Reprinted please indicate the source: blog.csdn.net/qsuron Xiaoshu blog (qsuron)










Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.