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

《不靠谱2.x》006.CCSprite(上)001 CCImage,第1张

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

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

class CC_DLL CCImage : public CCObject{public:    CCImage();    ~CCImage();    typedef enum    {        kFmtJpg = 0,kFmtPng,kFmtTiff,kFmtWebp,kFmtRawData,kFmtUnKNown    }EImageFormat;    typedef 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(const 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(const 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(voID * pData,int nDataLen,EImageFormat eFmt = kFmtUnKNown,int nWIDth = 0,int nHeight = 0,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(        const char *    pText,int             nWIDth = 0,int             nHeight = 0,ETextAlign      eAlignmask = kAlignCenter,const char *    pFontname = 0,int             nSize = 0);    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)        bool initWithStringShadowstroke(                                            const char *    pText,int             nWIDth      = 0,int             nHeight     = 0,ETextAlign      eAlignmask  = kAlignCenter,const char *    pFontname   = 0,int             nSize       = 0,float           textTintR   = 1,float           textTintG   = 1,float           textTintB   = 1,bool shadow                 = false,float shadowOffsetX         = 0.0,float shadowOffsetY         = 0.0,float shadowOpacity         = 0.0,float shadowBlur            = 0.0,bool  stroke                =  false,float strokeR               = 1,float strokeG               = 1,float strokeB               = 1,float strokeSize            = 1                                        );    #endif    unsigned char *   getData()               { return m_pData; }    int               getDataLen()            { return m_nWIDth * m_nHeight; }    bool hasAlpha()                     { return m_bHasAlpha;   }    bool isPremultiplIEdAlpha()         { 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(const char *pszfilePath,bool bIsToRGB = true);    CC_SYNTHESIZE_Readonly(unsigned short,m_nWIDth,WIDth);    CC_SYNTHESIZE_Readonly(unsigned short,m_nHeight,Height);    CC_SYNTHESIZE_Readonly(int,m_nBitsPerComponent,BitsPerComponent);protected:    bool _initWithJpgData(voID *pData,int nDatalen);    bool _initWithPngData(voID *pData,int nDatalen);    bool _initWithTiffData(voID *pData,int nDataLen);    bool _initWithWebpData(voID *pData,int nDataLen);    // @warning kFmtRawData only support RGBA8888    bool _initWithRawData(voID *pData,int nDatalen,int nWIDth,int nHeight,int nBitsPerComponent,bool bPreMulti);    bool _saveImagetoPNG(const char *pszfilePath,bool bIsToRGB = true);    bool _saveImagetoJPG(const char *pszfilePath);    unsigned char *m_pData;    bool m_bHasAlpha;    bool m_bPreMulti;private:    // noncopyable    CCImage(const CCImage&    rimg);    CCImage & operator=(const CCImage&);};

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

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

总结

以上是内存溢出为你收集整理的《不靠谱2.x》006.CCSprite(上)001 CCImage全部内容,希望文章能够帮你解决《不靠谱2.x》006.CCSprite(上)001 CCImage所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存