Create a high-performance text genie

Source: Internet
Author: User
Tags gety

In game development, the most performance-consuming is to display text, because the text must be generated temporarily in the memory bitmap, and then the bitmap is bound to the OpenGL texture and rendered, the author of andengine estimates the importance of caching in Java Development. He carefully planned the text Genie and divided it into immutable text genie) and the variable Text sprite (changeabletext), and delegate the responsibility of generating a text bitmap and converting it to the texture to a class called font. The implementation of the font class is very special, the string to be displayed is segmented into a single character. bitmap is generated through the canvas and cached, and then merged when the texture is bound, the advantage of this is that the performance is very high when duplicate characters are displayed, but the disadvantage is that the implementation is complicated, resulting in lower performance if the cache pool does not have this character. In actual use, when many genie exist in a scenario, the text or changeabletext content is often not displayed or displayed as a blank area.

On the iPhone, the text genie are cclabelttf and cclabelatlas, which are equivalent to the text and changeabletext of andengine, but the implementation mechanism is completely different, the cclabelttf is the string to be displayed, which is a full bitmap and then rendered using OpenGL, while the cclabelatlas requires pre-input of prepared text sequence images for caching. Therefore, I added a label class by referring to cocos2d, which is reliable in combination with loadingscene, the disadvantage is that when you change the text of more than two labels at runtime, sometimes the text of one label cannot be displayed. The following code is used:

Ultratextsource:

Package COM. weedong. openGL; import Org. anddev. andengine. openGL. texture. source. itexturesource; import Org. anddev. andengine. util. horizontalalign; import Org. anddev. andengine. util. mathutils; import Org. anddev. andengine. util. stringutils; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. paint; import android. graphics. rect; import android. graphics. typeface; import android. graphics. bitmap. config; import android. graphics. paint. fontmetrics; import android. util. floatmath; import android. util. log;/*** high-performance font texturesource * @ author **/public class ultrafonttexturesource implements itexturesource {private int mwidth; private int mheight; private bitmap mbitmap; public partition (string text, typeface, float fontsize, final int pcolor, final Boolean pantialias, horizontalalign alignment) {initialize (text, alignment, fontsize, typeface, pantialias, pcolor );} public ultrafonttexturesource (string text, typeface, float fontsize, final int pcolor, final Boolean pantialias) {This (text, typeface, fontsize, pcolor, pantialias, horizontalalign. left);} public ultrafonttexturesource (string text, typeface, float fontsize, final int pcolor) {This (text, typeface, fontsize, pcolor, true );} private void initialize (string text, horizontalalign alignment, float fontsize, typeface, final Boolean pantialias, final int pcolor) {paint textpaint = new paint (); textpaint. settypeface (typeface); textpaint. settextsize (fontsize); textpaint. setcolor (pcolor); textpaint. setantialias (pantialias); fontmetrics = textpaint. getfontmetrics (); int lineheight = (INT) floatmath. ceil (math. ABS (fontmetrics. ascent) + math. ABS (fontmetrics. descent); int linegap = (INT) (floatmath. ceil (fontmetrics. leading); string [] arylines = stringutils. split (text, '\ n', null); int linecount = arylines. length; int nmaxlinewidth = getmaxlinewidth (textpaint, arylines); int nmaxlineheight = linecount * lineheight + (linecount-1) * linegap; int width = mathutils. nextpoweroftwo (nmaxlinewidth); int Height = mathutils. nextpoweroftwo (nmaxlineheight); mwidth = width; mheight = height; bitmap. config = bitmap. config. argb_8888; Bitmap bitmap = bitmap. createbitmap (width, height, config); canvas = new canvas (Bitmap); bitmap. erasecolor (0); log. I ("ultrafont", "font width:" + bitmap. getwidth () + ", height:" + bitmap. getheight (); mbitmap = bitmap; int centeroffsetheight = (height-nmaxlineheight)/2; int centeroffsetwidth = (width-nmaxlinewidth)/2; Switch (alignment) {case left: centeroffsetwidth = 0; break; Case center: // centeroffsetwidth = (effectivetextwidth-textwidth)/2; break; case right: centeroffsetwidth = width-nmaxlinewidth; break ;} float Originaly =-fontmetrics. ascent + centeroffsetheight; For (INT I = 0; I <arylines. length; ++ I) {canvas. drawtext (arylines [I], centeroffsetwidth, Originaly + I * (lineheight + linegap), textpaint);} private int getmaxlinewidth (paint, string [] arylines) {int maximumlinewidth = 0; rect getstringwidthtemporaryrect = new rect (); For (INT I = arylines. length-1; I> = 0; I --) {paint. gettextbounds (arylines [I], 0, arylines [I]. length (), getstringwidthtemporaryrect); maximumlinewidth = math. max (maximumlinewidth, getstringwidthtemporaryrect. width ();} return maximumlinewidth;} @ overridepublic int getheight () {return mheight;} @ overridepublic int getwidth () {return mwidth;} @ overridepublic bitmap onloadbitmap (config pbitmapconfig) {return mbitmap;} public itexturesource clone () {return NULL ;}}

Label:

Package COM. weedong. sprite; import Org. anddev. andengine. entity. entity; import Org. anddev. andengine. entity. sprite. sprite; import Org. anddev. andengine. openGL. texture. texture; import Org. anddev. andengine. openGL. texture. region. textureregion; import android. graphics. typeface; import COM. weedong. openGL. ultrafonttexturesource; import COM. weedong. scene. abstractbasescene; import COM. weedong. utils. textureutils;/*** text genie * @ author **/public class label extends entity {private sprite msprite; private typeface mface; private float mfontsize; private int mcolor; private abstractbasescene mscene; public label (final float PX, final float py, abstractbasescene scene, string text, typeface face, float fontsize, int color) {This. mface = face; this. mfontsize = fontsize; this. mcolor = color; this. mscene = scene; textureregion region = textureutils. createfonttextureregion (scene, text, face, fontsize, color); msprite = new sprite (PX, Py, region); this. attachchild (msprite);} public sprite getsprite () {return msprite;} public float getx () {return msprite. getx ();} public float Gety () {return msprite. gety ();} public float getwidth () {return msprite. getwidth ();} public float getheight () {return msprite. getheight ();} public void settext (final string text, final typeface, final float fontsize, final int pcolor) {ultrafonttexturesource font = new ultrafonttexturesource (text, typeface, fontsize, pcolor); texture = msprite. gettextureregion (). gettexture (); texture. cleartexturesources (); texture. addtexturesource (font, 0, 0);} public void settext (string text) {settext (text, mface, mfontsize, mcolor);} public void settext (string text, int pcolor) {settext (text, mface, mfontsize, pcolor );}}

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.