cocos2dx:CCTexture2D注解

前言

什么是Texture2D

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

头文件CCTexture2D.h

基类
class CC_DLL Texture2D : public Ref
像素格式

枚举类型,标志着所有兼容的纹理格式,我们常见和默认的就是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
};
像素格式信息

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;
};
像素格式信息的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)),
    
    ......
};
纹理过滤的参数的结构体
  • 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;
默认纹理格式的设置

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

static void setDefaultAlphaPixelFormat(Texture2D::PixelFormat format);
默认纹理格式的获取
static Texture2D::PixelFormat getDefaultAlphaPixelFormat();
设置PVR图片预乘alpha的开关
CC_DEPRECATED_ATTRIBUTE static void PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
获取纹理的描述信息
virtual std::string getDescription() const;
删除纹理数据
void releaseGLTexture();
初始化一个texture2d

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

 bool initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& contentSize);
初始化mipmap

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

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

更新texture的数据
bool updateWithData(const void *data,int offsetX,int offsetY,int width,int height);
绘制一个点
void drawAtPoint(const Vec2& point);
绘制一个矩形
void drawInRect(const Rect& rect);
通过Image初始化一个Texture2D
bool initWithImage(Image * image);
bool initWithImage(Image * image, PixelFormat format);
初始化一个字体纹理

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

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);
设置过滤
void setTexParameters(const TexParams& texParams);

Sets alias texture parameters
void setAntiAliasTexParameters();
为纹理生成mipmap图像
void generateMipmap();
获取像素格式(字符串)
const char* getStringForFormat() const;
获取每像素bit深度
unsigned int getBitsPerPixelForFormat() const;
获取纹理的尺寸
const Size& getContentSizeInPixels();
判断是否预乘alpha
bool hasPremultipliedAlpha() const;
判断是否有mipmap
bool hasMipmaps() const;
获取像素格式(枚举)
Texture2D::PixelFormat getPixelFormat() const;
获取宽高、名字、过滤参数
/** 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;
设置获取shader pargram
void setGLProgram(GLProgram* program);

GLProgram* getGLProgram() const;
获取文件路径
std::string getPath()const { return _filePath; }
设置和获取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;
获取像素格式信息的map
static const PixelFormatInfoMap& getPixelFormatInfoMap();
九宫
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);
根据纹理格式去转换数据
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);

属性字段
// 像素格式
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;

推送

Github:https://github.com/KingSun5

结语

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值