Use of the android game development framework libgdx (21)-use the TTF font to support Chinese Characters

Source: Internet
Author: User
Tags libgdx

This series has not been updated for a long timeArticle. This afternoon, we will discuss how libgdx supports Chinese characters in the group. Hiero is the best, but bitmapfont of libgdx does not support multiple graphs. Generally, there are 3500 Chinese characters, so it cannot be a single graph.

Libgdx was supported by TTF a long time ago, but it was a problem at that time. The new version (0.96) has been fully supported, and we will share it with you.

TTF font library

TTF (truetypefont) is a font file format jointly launched by Apple and Microsoft. With the popularity of Windows, it has become the most commonly used font file representation method, which is widely used.

For Windows operating systems, you can find many font libraries in the fonts folder. You can also download it from the Internet.

I recommend the yahei. Las las font library. It looks great. It is used by ECLIPSE anyway.

Use of GDX-setup-UI

Download version 0.96 from libgdx. The file structure is as follows:

Strongly run the gdx-setup-ui.jar, this project initialization tool.

The running interface is as follows:

Enter the project name and package name as required. In the intermediate required configuration, you can directly download the latest stable and nightly versions. However, we recommend that you use local attachments.

You do not need to decompress the package. Select a zip file to automatically extract the desired file.

For eclipse I am using, directly import it.

I only generated a desktop project. Generally, Android and Web can be generated together.

Note that the default Android project corresponds to SDK 15 and can be changed as needed. Sometimes the imported project has a red exclamation point. You can add an assets folder.

Among them, the back-libgdx project is our core, and all logic is implemented here. Back-libgdx-XXX is the corresponding startup.

Libgdx uses the TTF font library

Change the name of a TTF font to font. TTF and copy it to the assets folder. Locate the gdx-stb-truetype-natives.jar and gdx-stb-truetype.jar from the package. The gdx-stb-truetype-natives.jar is added to the desktop project, and the gdx-stb-truetype.jar is added to the core project.

Now let's take a look at the most critical method truetypefontfactory. createbitmapfont.

We use it to create a bitmapfont that supports the TTF font. It requires a filehandle and the text to be supported. Of course, there are some parameters. For more information, see the API documentation.

That is to say, the text in the TTF font must be declared. However, the problem is not enough. 3500 common Chinese characters can basically meet the requirements. There is another commonly used 500 words on the Internet, but that is not enough.

I will write a few demos here.

 
Public static final string font_characters = "Let's take a look at the wonderful Chinese that people in the world love ";

Then use Initialization

Font = truetypefontfactory. createbitmapfont (GDX. files. internal ("font. TTF "), font_characters, 12.5f, 7.5f, 1.0f, GDX. graphics. getwidth (), GDX. graphics. getheight ());

The rest of the work is the same as using Hiero.

Public class app implements applicationlistener {bitmapfont font; spritebatch batch; public static final string font_characters = ""; @ overridepublic void create () {font = truetypefontfactory. createbitmapfont (GDX. files. internal ("font. TTF "), font_characters, 12.5f, 7.5f, 1.0f, GDX. graphics. getwidth (), GDX. graphics. getheight (); font. setcolor (1f, 0f, 0f, 1f); batch = new spritebatch () ;}@ overridepublic void dispose () {font. dispose () ;}@ overridepublic void render () {GDX. GL. glclear (gl10.gl _ color_buffer_bit); batch. begin (); font. draw (batch, "supported in Chinese", 10, 10); batch. end () ;}@ overridepublic void resize (INT width, int height) {}@ overridepublic void pause () {}@ overridepublic void resume (){}}

The effect is as follows:

The principle of using the TTF font in the stage is the same. Only the bitmapfont instantiation method needs to be modified.

Conclusion

Using the TTF font to display Chinese Characters in libgdx is undoubtedly a good choice, and there is basically no text display. However, the size of the TTF font is generally relatively large. For example, the size of the font I use is 13.8m.small, And the size is 3 MB.

The size of the Android app is sometimes unacceptable.

The font library that comes with the Android system is located in/system/fonts/and can be usedAbsolute pathAccess... but it is not recommended.

Using herio is an efficient (ProgramEfficiency, not development efficiency), but libgdx does not support herio multi-graph.

Some documents mentioned the use of spritesheetpacker, but I have never found it. Google CodeCodeNo more since the migration to GitHub. I don't know whether to remove it or what it is.

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.