加载图片后,图片是以Texture2D存储在内存当中的,这个Texture2D包含了显示图片所有的数据,包含像素纹理信息等等,所以每张图片被加载到内存后都会生成一个Texture2D
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结构体内存储量,当前像素格式所有需要用到的信息
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;
};
存储所有对应像素格式的信息,在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)),
......
};
/**
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();
CC_DEPRECATED_ATTRIBUTE static void PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
virtual std::string getDescription() const;
void releaseGLTexture();
初始化的data和其他信息都是来自Image里
bool initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& contentSize);
添加数据到内存里,默认的mipmap数量是1
bool initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh);
bool updateWithData(const void *data,int offsetX,int offsetY,int width,int height);
void drawAtPoint(const Vec2& point);
void drawInRect(const Rect& rect);
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);
void setAntiAliasTexParameters();
void generateMipmap();
const char* getStringForFormat() const;
unsigned int getBitsPerPixelForFormat() const;
const Size& getContentSizeInPixels();
bool hasPremultipliedAlpha() const;
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;
void setGLProgram(GLProgram* program);
GLProgram* getGLProgram() const;
std::string getPath()const { return _filePath; }
/** 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;
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所创,推荐一下!我也在群里!
本文属于原创文章,转载请著名作者出处并置顶!!