LIBGDX Game Engine Development Note (iv) text display Bitmapfont

Source: Internet
Author: User
Tags libgdx

Http://www.tuicool.com/articles/iAJbIj

Because LIBGDX is implemented using OpenGL, LIBGDX is able to support Chinese, and Chinese characters in Libgdx are displayed by way of mapping, using Bitmapfont and spritebatch combinations to complete the drawing of text. When constructing bitmapfont, you need a FNT file that describes the text, and a PNG file that provides a text image. Therefore, the display of Chinese, in the final analysis, is to read a. fnt file containing Chinese information and the corresponding. png file and show the problem.

1. How do I generate these two files?

First, we want to download the Hiero tool: Https://code.google.com/p/libgdx/downloads/list

After the download is complete, double-click the open interface is very simple, you can make special effects on text, change the background color, set the inner spacing and so on, the right side also provides us with a good font effect Click, add button can be added, see I write a few words:

Finally, click File, save, we can see two files generated!

2. How do I use these two files and show them?

A. Place these two files under the project's Asset folder, followed by calls in the code.

(Why use these two files?) Because LIBGDX is implemented with OpenGL, LIBGDX can support Chinese. But the Chinese characters in Libgdx are displayed by way of stickers, and we open them with editplus. POTATO.FNT file, it can be clearly seen that libgdx text drawing is based on the fnt file to obtain the corresponding text in PNG coordinates position, and then the corresponding portion of the picture to draw. )

B. In code call display

package com.zhf.android_libgdx; import com.badlogic.gdx.backends.android.AndroidApplication; import android.os.Bundle; public class mainactivity span class= "keyword" >extends androidapplication { @Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); //started the game, Firstgame implemented Applicationlistener class Initialize (new firstgame ( ), false); //specifying the configuration for the Glsurfaceview. //the second parameter if set to True, OpenGL 2.0 is used when OpenGL 2.0 is available. }} 

Main interface:

Package com.zhf.android_libgdx;Import javax.microedition.khronos.opengles.GL10;Import Com.badlogic.gdx.ApplicationListener;Import COM.BADLOGIC.GDX.GDX;Import Com.badlogic.gdx.graphics.g2d.BitmapFont;Import Com.badlogic.gdx.graphics.g2d.SpriteBatch;PublicClassFirstgameImplementsApplicationlistener {private SpriteBatch batch;SpriteBatch for drawingPrivate Bitmapfont Bitmapfont;Publicvoid Create () {instantiating batch =New SpriteBatch (); Bitmapfont =New Bitmapfont (Gdx.files.internal ("Photo.fnt"), Gdx.files.internal ("Photo.png"),FALSE); Bitmapfont.setcolor (0.5f,0.5f,0.5f,1);Set the color Bitmapfont.setscale (1.0f);Set font scale size}Publicvoid Render () {STUB Gdx.gl.glClear (gl10.gl_color_buffer_bit); Gdx.gl.glClearColor (0f,0f,0f,0f); Batch.begin (); Bitmapfont.draw (Batch,"Bolted snails,"200,160);Display a specific characterBitmapfont.draw (Batch, "bolted snail", 100,200,3,5); Only show snailsWe just have to add a line break (\ n) to the Chinese characters, and it's automatically recognized, very simple. Bitmapfont.drawmultiline (Batch,"Technical Road, \ n Step by step!" ", Gdx.graphics.getWidth ()/5, Gdx.graphics.getHeight () *0.8f); Batch.end ();Display specific characters: can call public textbounds draw (SpriteBatch SpriteBatch, charsequence str, float x, float y)The first parameter is the object of the SpriteBatch class, which is not explained much here.The second parameter is the string, and you want to output the Chinese characters, such as the "bolted snail" we wrote just now this is the second corresponding character.The third and fourth parameters (that is, float x, float y) are the starting coordinates of the text characters in the drawing word, following the normal coordinate system (the bottom-left corner of the starting point).The fifth and sixth parameters are set from the beginning of the first character output, to the end of the first character, for example, we want to output "snail" the 2 words will start to change to 3,end to 5, the first character of the default encoding is 0 instead of 1, remember.or public textbounds drawmultiline (spritebatch spritebatch, charsequence str, float x, float y, float alignmentwidth,hal Ignment alignment)///After two parameters: float alignmentwidth----"refers to the text alignment, there are left, right, center several ways. Where the left drawing origin is the upper right corner, the drawing origin of right is from the upper corner, the center of the drawing origin is each line of text centers, to the sides of the expansion. ///halignment Alignment----"does not work for left, only works for right and center, and right is equivalent to the width of the translation alignmentwidth of the drawing origin. For center, translates the width of the translation alignmentwidth/2 to the original point. } public void Resize (int width, int height) { //stubs} public void Pause () { //stub} 
                                                                 
                                                                  public 
                                                                  void Resume () { //stub} public void Dispose () { //stub batch.dispose (); bitmapfont.d Ispose (); }}
                                                                 

C.:

Ok! Get! Chinese display, then you can according to their own situation to make their own favorite text effect

SOURCE Download: http://down.51cto.com/data/881831

LIBGDX Game Engine Development Note (iv) text display Bitmapfont

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.