• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 132  文章 - 51  trackbacks - 0
            <2012年7月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            常用鏈接

            留言簿(7)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            cocos2d-x

            OGRE

            OPenGL

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            在使用cocos2d-x發(fā)布Android平臺(tái)游戲時(shí),游戲中可能需要顯示中文字體, 或者想顯示漂亮的自定義字體,這怎么辦呢?
            cocos2d-x中字體標(biāo)簽提供了CCLabelAtlas, CCLabelBMFont CCLabelTTF

            1.CCLabelAtlas速度快,支持簡單有限的幾個(gè)字符或數(shù)字集合

            2.CCLabelBMFont 
            我們可以用CCLabelBMFont來加載字體編輯器生成的.plist文件,但是當(dāng)顯示的文字很多時(shí),這種做法就有點(diǎn)費(fèi)時(shí)費(fèi)力了
            如:我們想顯示游戲中劇情介紹

            3.CCLabelTTF
            支持選擇一種字體來顯示文字,但是只支持系統(tǒng)中默認(rèn)字體
            CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

            問題:我們?cè)贏ndroid游戲中想顯示游戲劇情,想用自己指定的一種字體gril.ttf(非系統(tǒng)默認(rèn)字體),怎么辦

            其實(shí)cocos2d-x已經(jīng)提供了幫我們實(shí)現(xiàn)了
            構(gòu)造CCLabelTTF中指定的字體名傳給了CCTexture2D 
            void CCLabelTTF::updateTexture()
            {
                CCTexture2D *tex;
                if (m_tDimensions.width == 0 || m_tDimensions.height == 0)
                {
                    tex = new CCTexture2D();
                    tex->initWithString(m_string.c_str(), m_pFontName->c_str(), m_fFontSize * CC_CONTENT_SCALE_FACTOR()) ;
                }
                else
                {
                    tex = new CCTexture2D();
                    tex->initWithString(m_string.c_str(),
                                        CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), 
                                        m_hAlignment,
                                        m_vAlignment,
                                        m_pFontName->c_str(),
                                        m_fFontSize * CC_CONTENT_SCALE_FACTOR());
                }

               .
            }
            CCTexture2D又將字體名傳給了CCImage
            (PS:這里調(diào)用的是android平臺(tái)下的CCImage類(./platform/android/CCImage.h)
            而不是win32平臺(tái)下的CCImage類(./platform/win32/CCImage.h) )

            android平臺(tái)下的CCImage
            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));

                    // assign the dc.m_pData to m_pData in order to save time
                    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;
            }

            然后調(diào)用了android平臺(tái)下的BitmapDC
                bool getBitmapFromJava(const char *text, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask, const char * pFontName, float fontSize)
                {
                    JniMethodInfo methodInfo;
                    if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxBitmap", "createTextBitmap", 
                        "(Ljava/lang/String;Ljava/lang/String;IIII)V"))
                    {
                        CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
                        return false;
                    }

                    
            }

            這里顯示了調(diào)用JAVA代碼Cocos2dxBitmap.java下的createTextBitmap函數(shù)

                /*
                 * @width: the width to draw, it can be 0
                 * @height: the height to draw, it can be 0
                 
            */
                public static void createTextBitmap(String content, String fontName, 
                        int fontSize, int alignment, int width, int height){
                    
                    content = refactorString(content);       
                    Paint paint = newPaint(fontName, fontSize, alignment);
                    
                    TextProperty textProperty = computeTextProperty(content, paint, width, height);          

                    int bitmapTotalHeight = (height == 0 ? textProperty.totalHeight:height);

                    // Draw text to bitmap
                    Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, 
                            bitmapTotalHeight, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    
                    // Draw string
                    FontMetricsInt fm = paint.getFontMetricsInt();
                    int x = 0;
                    int y = computeY(fm, height, textProperty.totalHeight, alignment);
                    String[] lines = textProperty.lines;
                    for (String line : lines){
                        x = computeX(paint, line, textProperty.maxWidth, alignment);
                        canvas.drawText(line, x, y, paint);
                        y += textProperty.heightPerLine;
                    }
                    
                    initNativeObject(bitmap);
                }

            newPaint函數(shù)中調(diào)用了字體加載
            private static Paint newPaint(String fontName, int fontSize, int alignment){
                    Paint paint = new Paint();
                    paint.setColor(Color.WHITE);
                    paint.setTextSize(fontSize);      
                    paint.setAntiAlias(true);    
                    
                    /*
                     * Set type face for paint, now it support .ttf file.
                     
            */
                    if (fontName.endsWith(".ttf")){      //字體需要.ttf結(jié)束
                         try {
                            //Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName);
                             Typeface typeFace = Cocos2dxTypefaces.get(context, fontName);
                              paint.setTypeface(typeFace);
                         } catch (Exception e){
                             Log.e("Cocos2dxBitmap", 
                                 "error to create ttf type face: " + fontName);
                             
                             /*
                              * The file may not find, use system font
                              
            */
                             paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
                         }
                    }
                    else {
                        paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
                    }
                    
                    .......
                    
                    return paint;
                }
            (PS:Typeface類定義字體和字體內(nèi)在的類型。這個(gè)類被用在畫筆Paint設(shè)置的時(shí)候,比如用textSize,textSkewX和textScale設(shè)置來指定text在畫的時(shí)候如何來顯示和測(cè)量。
            android中用Typeface來指定字體)

            另一點(diǎn)需要注意的是:字體需要.ttf結(jié)束

            使用方法
            1.在proj.android\assets文件夾中添加girl.ttf
            2.程序中調(diào)用對(duì)應(yīng)的字體就可以了
                CCSize size = CCDirector::sharedDirector()->getWinSize();
                CCLabelTTF* label = CCLabelTTF::create("少女HelloWorld", "girl.ttf", 24);
                label->setPosition(ccp(size.width/2, size.height/2));
                addChild(label);
            3.如果你需要顯示漢字,則需要將包含有字符串的文件(如.cpp)轉(zhuǎn)換為UTF-8格式編碼,否則會(huì)顯示亂碼

            IOS和win32平臺(tái)沒有測(cè)試,相信的差不多

            附上測(cè)試圖片:




            哈哈 可以在游戲中打包自己想要的字體啦






            posted on 2012-10-15 16:58 風(fēng)輕云淡 閱讀(14764) 評(píng)論(4)  編輯 收藏 引用 所屬分類: cocos2d

            FeedBack:
            # re: cocos2d-x android游戲使用自己的字體 2012-10-16 03:01 fzy
            # re: cocos2d-x android游戲使用自己的字體 2014-03-13 17:58 lwx5924
            差別有點(diǎn)大....  回復(fù)  更多評(píng)論
              
            # re: cocos2d-x android游戲使用自己的字體[未登錄] 2014-10-17 10:35 lee
            天差地別  回復(fù)  更多評(píng)論
              
            # re: cocos2d-x android游戲使用自己的字體 2014-11-02 18:56 叛逆神佛
            win32 和 ios不行啊,只有android可以  回復(fù)  更多評(píng)論
              
            日本免费久久久久久久网站| 精品久久久久久无码中文字幕一区| 久久久久噜噜噜亚洲熟女综合| 色婷婷综合久久久久中文字幕| 久久精品国产亚洲AV蜜臀色欲 | 国产精品久久久久久影院 | 久久www免费人成看国产片| 久久天天躁狠狠躁夜夜2020| 亚洲第一极品精品无码久久| 久久精品国产WWW456C0M| 久久精品日日躁夜夜躁欧美| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲欧洲日产国码无码久久99| 国产2021久久精品| 欧美大香线蕉线伊人久久| 精品久久久无码中文字幕| 久久婷婷激情综合色综合俺也去 | 日韩精品久久无码人妻中文字幕| 99久久免费国产精品| 无码精品久久久天天影视 | 日韩久久久久久中文人妻| 亚洲v国产v天堂a无码久久| 91精品国产色综久久| 久久国产精品无码一区二区三区| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区| 亚洲精品无码久久久久去q | 国产午夜电影久久| 久久亚洲AV成人无码电影| 漂亮人妻被中出中文字幕久久| 久久精品视屏| 久久99精品久久久久久齐齐| 91精品国产综合久久四虎久久无码一级 | 亚洲欧美成人综合久久久| 欧美成人免费观看久久| 青青草原综合久久大伊人导航 | 亚洲人成电影网站久久| 久久久久亚洲AV综合波多野结衣| 久久婷婷综合中文字幕| 91精品久久久久久无码| 国产成人精品久久| 精品久久久久久无码中文野结衣|