• cocos2dx:CCTexture2D注解


    前言

    什么是Texture2D

    加载图片后,图片是以Texture2D存储在内存当中的,这个Texture2D包含了显示图片所有的数据,包含像素纹理信息等等,所以每张图片被加载到内存后都会生成一个Texture2D

    头文件CCTexture2D.h

    基类

    class CC_DLL Texture2D : public Ref
    
    • 1

    像素格式

    枚举类型,标志着所有兼容的纹理格式,我们常见和默认的就是BGRA8888,每个像素是32-bit也就是4个字节

    enum class PixelFormat
    {
        //! auto detect the type
        AUTO,
        //! 32-bit texture: BGRA8888
        BGRA8888,
        //! 32-bit texture: RGBA8888
        RGBA8888,
        //! 24-bit texture: RGBA888
        RGB888,
        //! 16-bit texture without Alpha channel
        RGB565,
        //! 8-bit textures used as masks
        A8,
        //! 8-bit intensity texture
        I8,
        //! 16-bit textures used as masks
        AI88,
        //! 16-bit textures: RGBA4444
        RGBA4444,
        //! 16-bit textures: RGB5A1
        RGB5A1,
        //! 4-bit PVRTC-compressed texture: PVRTC4
        PVRTC4,
        //! 4-bit PVRTC-compressed texture: PVRTC4 (has alpha channel)
        PVRTC4A,
        //! 2-bit PVRTC-compressed texture: PVRTC2
        PVRTC2,
        //! 2-bit PVRTC-compressed texture: PVRTC2 (has alpha channel)
        PVRTC2A,
        //! ETC-compressed texture: ETC
        ETC,
        //! S3TC-compressed texture: S3TC_Dxt1
        S3TC_DXT1,
        //! S3TC-compressed texture: S3TC_Dxt3
        S3TC_DXT3,
        //! S3TC-compressed texture: S3TC_Dxt5
        S3TC_DXT5,
        //! ATITC-compressed texture: ATC_RGB
        ATC_RGB,
        //! ATITC-compressed texture: ATC_EXPLICIT_ALPHA
        ATC_EXPLICIT_ALPHA,
        //! ATITC-compressed texture: ATC_INTERPOLATED_ALPHA
        ATC_INTERPOLATED_ALPHA,
        //! Default texture format: AUTO
        DEFAULT = AUTO,
        
        NONE = -1
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    像素格式信息

    PixelFormatInfo结构体内存储量,当前像素格式所有需要用到的信息

    • internalFormat:图像数据的存储格式
    • format : 指定纹理数据的格式,和internalFormat一致
    • type:指定纹理数据的数据类型
    • bpp:每像素的bit数
    • compressed:是否是压缩纹理
    • alpha:是否有透明通道
    struct PixelFormatInfo {
    
        PixelFormatInfo(GLenum anInternalFormat, GLenum aFormat, GLenum aType, int aBpp, bool aCompressed, bool anAlpha)
            : internalFormat(anInternalFormat)
            , format(aFormat)
            , type(aType)
            , bpp(aBpp)
            , compressed(aCompressed)
            , alpha(anAlpha)
        {}
    
        GLenum internalFormat;
        GLenum format;
        GLenum type;
        int bpp;
        bool compressed;
        bool alpha;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    像素格式信息的map

    存储所有对应像素格式的信息,在Texture2D.cpp头部位置就全部输入了进去

    typedef std::map<Texture2D::PixelFormat, const PixelFormatInfo> PixelFormatInfoMap;
    
    typedef Texture2D::PixelFormatInfoMap::value_type PixelFormatInfoMapValue;
    static const PixelFormatInfoMapValue TexturePixelFormatInfoTablesValue[] =
    {
        PixelFormatInfoMapValue(Texture2D::PixelFormat::BGRA8888, Texture2D::PixelFormatInfo(GL_BGRA, GL_BGRA, GL_UNSIGNED_BYTE, 32, false, true)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::RGBA8888, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32, false, true)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::RGBA4444, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 16, false, true)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB5A1, Texture2D::PixelFormatInfo(GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 16, false, true)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB565, Texture2D::PixelFormatInfo(GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 16, false, false)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::RGB888, Texture2D::PixelFormatInfo(GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 24, false, false)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::A8, Texture2D::PixelFormatInfo(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, 8, false, false)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::I8, Texture2D::PixelFormatInfo(GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 8, false, false)),
        PixelFormatInfoMapValue(Texture2D::PixelFormat::AI88, Texture2D::PixelFormatInfo(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 16, false, true)),
        
        ......
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    纹理过滤的参数的结构体

    • minFilter:缩小过滤
    • magFilter:放大过滤
    • wrapS:S方向的过滤
    • wrapT:T方向的过滤
    /**
     Extension to set the Min / Mag filter
     */
    typedef struct _TexParams {
        GLuint    minFilter;
        GLuint    magFilter;
        GLuint    wrapS;
        GLuint    wrapT;
    }TexParams;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    默认纹理格式的设置

    默认带透明通道的像素格式设置,设置后,RGBA8888、RGB888、RGBA4444、RGB5A1、RGB565、A8像素格式的图片都会按照设置的默认格式去加载

    static void setDefaultAlphaPixelFormat(Texture2D::PixelFormat format);
    
    • 1

    默认纹理格式的获取

    static Texture2D::PixelFormat getDefaultAlphaPixelFormat();
    
    • 1

    设置PVR图片预乘alpha的开关

    CC_DEPRECATED_ATTRIBUTE static void PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
    
    • 1

    获取纹理的描述信息

    virtual std::string getDescription() const;
    
    • 1

    删除纹理数据

    void releaseGLTexture();
    
    • 1

    初始化一个texture2d

    初始化的data和其他信息都是来自Image里

     bool initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& contentSize);
    
    • 1

    初始化mipmap

    添加数据到内存里,默认的mipmap数量是1

    bool initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh);
    
    
    • 1
    • 2

    更新texture的数据

    bool updateWithData(const void *data,int offsetX,int offsetY,int width,int height);
    
    • 1

    绘制一个点

    void drawAtPoint(const Vec2& point);
    
    • 1

    绘制一个矩形

    void drawInRect(const Rect& rect);
    
    • 1

    通过Image初始化一个Texture2D

    bool initWithImage(Image * image);
    bool initWithImage(Image * image, PixelFormat format);
    
    • 1
    • 2

    初始化一个字体纹理

    位图字体用的应该都是这玩意,具体没细看

    bool initWithString(const char *text,  const std::string &fontName, float fontSize, const Size& dimensions = Size(0, 0), TextHAlignment hAlignment = TextHAlignment::CENTER, TextVAlignment vAlignment = TextVAlignment::TOP, bool enableWrap = true, int overflow = 0);
    
    • 1

    设置过滤

    void setTexParameters(const TexParams& texParams);
    
    
    • 1
    • 2

    Sets alias texture parameters

    void setAntiAliasTexParameters();
    
    • 1

    为纹理生成mipmap图像

    void generateMipmap();
    
    • 1

    获取像素格式(字符串)

    const char* getStringForFormat() const;
    
    • 1

    获取每像素bit深度

    unsigned int getBitsPerPixelForFormat() const;
    
    • 1

    获取纹理的尺寸

    const Size& getContentSizeInPixels();
    
    • 1

    判断是否预乘alpha

    bool hasPremultipliedAlpha() const;
    
    • 1

    判断是否有mipmap

    bool hasMipmaps() const;
    
    • 1

    获取像素格式(枚举)

    Texture2D::PixelFormat getPixelFormat() const;
    
    • 1

    获取宽高、名字、过滤参数

    /** Gets the width of the texture in pixels. */
    int getPixelsWide() const;
    
    /** Gets the height of the texture in pixels. */
    int getPixelsHigh() const;
    
    /** Gets the texture name. */
    GLuint getName() const;
    
    /** Gets max S. */
    GLfloat getMaxS() const;
    /** Sets max S. */
    void setMaxS(GLfloat maxS);
    
    /** Gets max T. */
    GLfloat getMaxT() const;
    /** Sets max T. */
    void setMaxT(GLfloat maxT);
    
    /** Get the texture content size.*/
    Size getContentSize() const;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    设置获取shader pargram

    void setGLProgram(GLProgram* program);
    
    GLProgram* getGLProgram() const;
    
    • 1
    • 2
    • 3

    获取文件路径

    std::string getPath()const { return _filePath; }
    
    • 1

    设置和获取alpha texture

    /** Get a shader program from the texture.*/
    GLProgram* getGLProgram() const;
    
    std::string getPath()const { return _filePath; }
    
    void setAlphaTexture(Texture2D* alphaTexture);
    Texture2D* getAlphaTexture() const;
    
    GLuint getAlphaTextureName() const;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    获取像素格式信息的map

    static const PixelFormatInfoMap& getPixelFormatInfoMap();
    
    • 1

    九宫

    class NinePatchInfo
    {
    public:
        Rect capInsetSize;
        std::unordered_map<SpriteFrame*, Rect> capInsetMap;
    };
    
    // 是否是九宫图
    bool isContain9PatchInfo()const;
    
    // 获取九宫信息
    const Rect& getSpriteFrameCapInset(SpriteFrame* spriteFrame)const;
    
    // 移除九宫信息
    void removeSpriteFrameCapInset(SpriteFrame* spriteFrame);
    
    // 添加九宫信息
    void addSpriteFrameCapInset(SpriteFrame* spritframe, const Rect& capInsets);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    根据纹理格式去转换数据

    static PixelFormat convertDataToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat originFormat, PixelFormat format, unsigned char** outData, ssize_t* outDataLen);
    
    static PixelFormat convertI8ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen);
    static PixelFormat convertAI88ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen);
    static PixelFormat convertRGB888ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen);
    static PixelFormat convertRGBA8888ToFormat(const unsigned char* data, ssize_t dataLen, PixelFormat format, unsigned char** outData, ssize_t* outDataLen);
    
    //I8 to XXX
    static void convertI8ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertI8ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertI8ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertI8ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertI8ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertI8ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    
    //AI88 to XXX
    static void convertAI88ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertAI88ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertAI88ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertAI88ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertAI88ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertAI88ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertAI88ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    
    //RGB888 to XXX
    static void convertRGB888ToRGBA8888(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGB888ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGB888ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGB888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGB888ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGB888ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGB888ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    
    //RGBA8888 to XXX
    static void convertRGBA8888ToRGB888(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGBA8888ToRGB565(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGBA8888ToI8(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGBA8888ToA8(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGBA8888ToAI88(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGBA8888ToRGBA4444(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    static void convertRGBA8888ToRGB5A1(const unsigned char* data, ssize_t dataLen, unsigned char* outData);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    属性字段

    // 像素格式
    Texture2D::PixelFormat _pixelFormat;
    
    // 纹理宽度
    int _pixelsWide;
    
    // 纹理高度
    int _pixelsHigh;
    
    // 纹理名字
    GLuint _name;
    
    // s方向的过滤参数
    GLfloat _maxS;
    
    // T方向的过滤参数
    GLfloat _maxT;
    
    // 尺寸
    Size _contentSize;
    
    // 移除alpha开启标签
    bool _hasPremultipliedAlpha;
    
    // 是否开启mipmap
    bool _hasMipmaps;
    
    // shader program
    GLProgram* _shaderProgram;
    
    // 纹理信息存储map
    static const PixelFormatInfoMap _pixelFormatInfoTables;
    
    // 抗锯齿开关
    bool _antialiasEnabled;
    
    // 九宫信息
    NinePatchInfo* _ninePatchInfo;
    friend class SpriteFrameCache;
    friend class TextureCache;
    friend class ui::Scale9Sprite;
    
    bool _valid;
    
    // 文件路径
    std::string _filePath;
    
    // alpha texture
    Texture2D* _alphaTexture;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    推送

    Github:https://github.com/KingSun5

    结语

    希望看到最后的同学有所收获,若是觉得博主的文章写的不错,不妨关注一下博主,点赞一下博文,另博主能力有限,若文中有出现什么错误的地方,欢迎各位评论指摘。
    QQ交流群:806091680(Chinar)
    该群为CSDN博主Chinar所创,推荐一下!我也在群里!
    本文属于原创文章,转载请著名作者出处并置顶!!

  • 相关阅读:
    如何利用无线远程通讯模块实现触摸屏与PLC间通信?
    awk文本处理-1
    高考英语语法填空满分秒杀技巧
    redis在linux下设置开机自启动、检查服务状态
    【C#】用于基于 UV DLP 的 3D 打印机的切片软件源码解析(二)思维导图
    计算机毕业设计Python+django网上求职招聘系统(源码+系统+mysql数据库+Lw文档)
    k8s部署Skywalking及java接入agent
    驱动有没有(静态和动态加载)
    Ubuntu搭建Docker&docker-compose环境
    golang中的panic 和 recover
  • 原文地址:https://blog.csdn.net/Mr_Sun88/article/details/126010093