当前位置:编程学习 > wap >>

cocos2d-x android游戏使用自己的字体

在使用cocos2d-x发布Android平台游戏时,游戏中可能需要显示中文字体, 或者想显示漂亮的自定义字体,这怎么办呢?
cocos2d-x中字体标签提供了CCLabelAtlas, CCLabelBMFont CCLabelTTF

1.CCLabelAtlas速度快,支持简单有限的几个字符或数字集合

2.CCLabelBMFont
我们可以用CCLabelBMFont来加载字体编辑器生成的.plist文件,但是当显示的文字很多时,这种做法就有点费时费力了
如:我们想显示游戏中剧情介绍

3.CCLabelTTF
支持选择一种字体来显示文字,但是只支持系统中默认字体
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

问题:我们在Android游戏中想显示游戏剧情,想用自己指定的一种字体gril.ttf(非系统默认字体),怎么办

其实cocos2d-x已经提供了帮我们实现了
构造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:这里调用的是android平台下的CCImage类(./platform/android/CCImage.h)
而不是win32平台下的CCImage类(./platform/win32/CCImage.h) )

android平台下的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;
}

然后调用了android平台下的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;
        }

       
}

这里显示了调用JAVA代码Cocos2dxBitmap.java下的createTextBitmap函数

    /*
     * @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

补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,