Tabs on Android (tab)--A fantastic character (COCOS2DX crash)

Source: Internet
Author: User
Tags addchild

Today's test found a problem with the game, System mail, assuming the tab, on Android on the open message content will be crash. And they're pretty sure it's the tab question.


With my many months of experience (indeed not for many years.)

。 View Never heard of a tab crash on Android. And assume that there is this problem. There will certainly be a lot of people, expected to be noisy, search for a bit, what information is not available.

So I wrote a test project and tested it. 4 txt documents were edited with text editing tools under Mac and under Windows. UTF has a BOM and no BOM, the content is "tab ABCD", found to be able to display normal, and will not crash.

For example, the following:

    unsigned long fsize = 0;    unsigned char *data = ccfileutils::sharedfileutils ()->getfiledata ("Tab.txt", "RB", &fsize);        Cclabelttf *label = cclabelttf::create ((const char*) data, "ABC", +);    Label->setposition (CCP (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2));    AddChild (label);

Then you start to rage with the code and find that you are using a custom control in project to implement the functionality of a rich text box.

The so-called rich text box is able to display a variety of colors, can display URLs, and some things can point to the kind. But there is one step in the realization of this thing . is to take apart a UTF8 string, split it into a single character , and make a texture out of each character. Then come to the puzzle and spell it into a text box . Then the tab must be split into a single character, and the final discovery is when the tab texture is generated. It's crash. The stack is as follows:


By the way popular Science Labelttf the principle of display, roughly. Through the font and font size, and then call the corresponding platform API. Generate a graph (image), Note that it is a picture, and then as a texture, set to a sprite. From the stack above, it was crash when the image was generated.

Then find the code and find the last step of the stack under Cctexture2d. For example, the following:

BOOL Cctexture2d::initwithstring (const char *text, const char *fontname, float fontSize, const ccsize& dimensions, CCT Extalignment halignment, Ccverticaltextalignment valignment) {       ...          ... Do        {            ccimage* pimage = new Ccimage ();            cc_break_if (NULL = = pimage);            BRet = pimage->initwithstring (text, (int) dimensions.width, (int) dimensions.height, ealign, FontName, (int) fontSize );            Cc_break_if (!bret);            BRet = Initwithimage (pimage);            Cc_safe_release (pimage);        } while (0);         ....... return bRet; }
The first step here is. New an image comes out. Then use the text to init the image, and then use this image to go to the init texture.

Let's look at the init image here. The code under Android is this:

BOOL Ccimage::initwithstring (                               const char *    ptext,                                int             nwidth/* = 0*/,                                int             nheight/* = 0*/,                               etextalign      ealignmask/* = kaligncenter*/,                               const char *    pfontname/* = nil*/,                               int             nsize/* = 0 */) {    bool BRet = false;    Do     {        cc_break_if (! ptext);                BITMAPDC &DC = SHAREDBITMAPDC ();        Cc_break_if (Dc.getbitmapfromjava (Ptext, nwidth, nheight, Ealignmask, Pfontname, nSize));        M_pdata = Dc.m_pdata;        CC_BREAK_IF (! m_pdata);        M_nwidth    = (short) dc.m_nwidth;        M_nheight   = (short) dc.m_nheight;        M_bhasalpha = true;        M_bpremulti = true;        M_nbitspercomponent = 8;        BRet = true;    } while (0);    return bRet;}
The focus is on the Getbitmapfromjava function, such as the following

    BOOL Getbitmapfromjava (const char *text, int nwidth, int nheight, ccimage::etextalign ealignmask, const char * pfontname, Float fontSize)    {    return  getbitmapfromjavashadowstroke (text, nwidth, nheight, Ealignmask, Pfontname, fontSize);    }
  I know it from here. Must be the word. Font. The font size of these things, called to the Java layer, lets them invoke system-related things and return a bitmap back. Then the next step is to initialize the returned image to texture. But there is no need for the next step. As a result of this, crash. Get for example the following log:


From this log, it probably means that the Java layer is creating bitmap. Need a picture of the width of the height. But this size equals 0.

But before the test is able to display tab, carefully think about, before the test has a problem, is that the previous test is the tab plus other characters of the mix , this place because the rich text box processing, is a single tab character , then with a single tab test. Sure enough, the same crash as above.

Back to this crash log itself, the graph to be generated, that is, the picture that wrote the word, then the width of the image should depend on the character's content and the size of the character, how can be 0. is it because tab on Android will be ignored by itself .

Write a project test now:

    unsigned long fsize = 0;    unsigned char *data = ccfileutils::sharedfileutils ()->getfiledata ("Tab.txt", "RB", &fsize);        Cclabelttf *label = cclabelttf::create ((const char*) data, "ABC", +);    Label->setposition (CCP (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2));    Cclayercolor *col = cclayercolor::create (CCC4 (123, 255,0, 255));    Col->setcontentsize (Label->getcontentsize ());    Col->ignoreanchorpointforposition (false);    Col->setanchorpoint (CCP (0.5, 0.5));    Col->setposition (CCP (Label->getposition (). X,label->getposition (). y));    AddChild (col);    AddChild (label);

The difference between this test and the previous one is that it adds a same-sized color block behind the text so that we can see the actual range of the texture (as it should have been, in fact, it started at 2.)

。 ), sure enough to find no matter how to add tab. The tab is not displayed, and the size of the texture is the same as the size of the Non-tab.


Suppose this is the case on the COCOS2DX. But he will eventually call to the Java layer of the Android system to generate this bitmap, indicating that this is not a cocos2dx problem, then on android applications. Will the tab be ignored, be decisive to open a project and then test, such as the following:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/container "    android:layout_width=" Match_parent "    Android : layout_height= "Match_parent" >        <textview        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:text= "@string/hello_world"         android:background= "#FFFF0000"/></ Linearlayout>

<resources>    <string name= "app_name" >tabTest</string>    <string name= "Hello_world" >hello world!</string>    <string name= "Action_settings" >settings</string></resources >

Sure enough, the tab is still not shown, hahaha haha, finish ~ ~


Summary:The Android system ignores tab and Cclabelttf in COCOS2DX. Assuming that the text content has only one tab character, it will be crash.

Tabs on Android (tab)--A fantastic character (COCOS2DX crash)

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.