《不靠谱2.x》006.CCSprite(上)001 CCImage

前端之家收集整理的这篇文章主要介绍了《不靠谱2.x》006.CCSprite(上)001 CCImage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、概述
CCSprite涉及到了CCTexture2D、CCSpriteFrame、CCSpriteBatchNode,因此在了解CCSprite前,肯定要先对它们进行了解。而CCTexture2D又涉及到了CCImage,所以这里先来了解CCImage,源码如下:

class CC_DLL CCImage : @H_403_5@public CCObject
{
@H_403_5@public:
    CCImage();
    ~CCImage();

    typedef @H_403_5@enum
    {
        kFmtJpg = 0,kFmtPng,kFmtTiff,kFmtWebp,kFmtRawData,kFmtUnKnown
    }EImageFormat;

    typedef @H_403_5@enum
    {
        kAlignCenter        = 0x33,///< Horizontal center and vertical center.
        kAlignTop           = 0x13,///< Horizontal center and vertical top.
        kAlignTopRight      = 0x12,///< Horizontal right and vertical top.
        kAlignRight         = 0x32,///< Horizontal right and vertical center.
        kAlignBottomRight   = 0x22,///< Horizontal right and vertical bottom.
        kAlignBottom        = 0x23,///< Horizontal center and vertical bottom.
        kAlignBottomLeft    = 0x21,///< Horizontal left and vertical bottom.
        kAlignLeft          = 0x31,///< Horizontal left and vertical center.
        kAlignTopLeft       = 0x11,///< Horizontal left and vertical top.
    }ETextAlign;

    /**  @brief Load the image from the specified path.  @param strPath the absolute file path.  @param imageType the type of image,currently only supporting two types.  @return true if loaded correctly. */
    bool initWithImageFile(@H_403_5@const @H_403_5@char * strPath,EImageFormat imageType = kFmtPng);

    /* @brief The same result as with initWithImageFile,but thread safe. It is caused by loadImage() in CCTextureCache.cpp. @param fullpath full path of the file. @param imageType the type of image,currently only supporting two types. @return true if loaded correctly. */
    bool initWithImageFileThreadSafe(@H_403_5@const @H_403_5@char *fullpath,EImageFormat imageType = kFmtPng);

    /**  @brief Load image from stream buffer.  @warning kFmtRawData only supports RGBA8888.  @param pBuffer stream buffer which holds the image data.  @param nLength data length expressed in (number of) bytes.  @param nWidth,nHeight,nBitsPerComponent are used for kFmtRawData.  @return true if loaded correctly. */
    bool initWithImageData(@H_403_5@void * pData,@H_403_5@int nDataLen,EImageFormat eFmt = kFmtUnKnown,@H_403_5@int nWidth = 0,@H_403_5@int nHeight = 0,@H_403_5@int nBitsPerComponent = 8);

    /**  @brief Create image with specified string.  @param pText the text the image will show (cannot be nil).  @param nWidth the image width,if 0,the width will match the text's width.  @param nHeight the image height,the height will match the text's height.  @param eAlignMask the test Alignment  @param pFontName the name of the font used to draw the text. If nil,use the default system font.  @param nSize the font size,use the system default size. */
    bool initWithString(
        @H_403_5@const @H_403_5@char *    pText,@H_403_5@int             nWidth = 0,@H_403_5@int             nHeight = 0,ETextAlign      eAlignMask = kAlignCenter,@H_403_5@const @H_403_5@char *    pFontName = 0,@H_403_5@int             nSize = 0);

    #@H_403_5@if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

        bool initWithStringShadowStroke(
                                            @H_403_5@const @H_403_5@char *    pText,@H_403_5@int             nWidth      = 0,@H_403_5@int             nHeight     = 0,ETextAlign      eAlignMask  = kAlignCenter,@H_403_5@const @H_403_5@char *    pFontName   = 0,@H_403_5@int             nSize       = 0,@H_403_5@float           textTintR   = 1,@H_403_5@float           textTintG   = 1,@H_403_5@float           textTintB   = 1,bool shadow                 = @H_403_5@false,@H_403_5@float shadowOffsetX         = 0.0,@H_403_5@float shadowOffsetY         = 0.0,@H_403_5@float shadowOpacity         = 0.0,@H_403_5@float shadowBlur            = 0.0,bool  stroke                =  @H_403_5@false,@H_403_5@float strokeR               = 1,@H_403_5@float strokeG               = 1,@H_403_5@float strokeB               = 1,@H_403_5@float strokeSize            = 1

                                        );

    #endif


    unsigned @H_403_5@char *   getData()               { @H_403_5@return m_pData; }
    @H_403_5@int               getDataLen()            { @H_403_5@return m_nWidth * m_nHeight; }


    bool hasAlpha()                     { @H_403_5@return m_bHasAlpha;   }
    bool isPremultipliedAlpha()         { @H_403_5@return m_bPreMulti;   }


    /**  @brief Save CCImage data to the specified file,with specified format.  @param pszFilePath the file's absolute path,including file suffix.  @param bIsToRGB whether the image is saved as RGB format. */
    bool saveToFile(@H_403_5@const @H_403_5@char *pszFilePath,bool bIsToRGB = @H_403_5@true);

    CC_SYNTHESIZE_READONLY(unsigned @H_403_5@short,m_nWidth,Width);
    CC_SYNTHESIZE_READONLY(unsigned @H_403_5@short,m_nHeight,Height);
    CC_SYNTHESIZE_READONLY(@H_403_5@int,m_nBitsPerComponent,BitsPerComponent);

@H_403_5@protected:
    bool _initWithJpgData(@H_403_5@void *pData,@H_403_5@int nDatalen);
    bool _initWithPngData(@H_403_5@void *pData,@H_403_5@int nDatalen);
    bool _initWithTiffData(@H_403_5@void *pData,@H_403_5@int nDataLen);
    bool _initWithWebpData(@H_403_5@void *pData,@H_403_5@int nDataLen);
    // @warning kFmtRawData only support RGBA8888
    bool _initWithRawData(@H_403_5@void *pData,@H_403_5@int nDatalen,@H_403_5@int nWidth,@H_403_5@int nHeight,@H_403_5@int nBitsPerComponent,bool bPreMulti);

    bool _saveImageToPNG(@H_403_5@const @H_403_5@char *pszFilePath,bool bIsToRGB = @H_403_5@true);
    bool _saveImageToJPG(@H_403_5@const @H_403_5@char *pszFilePath);

    unsigned @H_403_5@char *m_pData;
    bool m_bHasAlpha;
    bool m_bPreMulti;


@H_403_5@private:
    // noncopyable
    CCImage(@H_403_5@const CCImage&    rImg);
    CCImage & operator=(@H_403_5@const CCImage&);
};

二、分析
1、首先是两个枚举类型,其中一个标记图片的格式,作为初始化方法的参数;另一个用于标记文本的对齐方式,同样是作为初始化方法的参数。
2、初始化方法则是分别为:通过绝对路径(获得路径方法待究)、图片数据(不知道什么意思)、字符串来生成图片对象。其中通过字符串来声明的话,可以设置字体、大小、对齐方式、渐变色、阴影偏移和线宽,可以说是非常丰富。
3、还有个方法,可以将图片保存到指定路径下,应该挺有用的,比如存储截图。
4、其他的方法感觉不常用或是比较晦涩,暂不细究。

三、总结 1、知道了可使用图片、字符串生成 image 对象,并可保存到指定路径下。

猜你在找的Cocos2d-x相关文章