深入分析Cocos2d-x 2.0中的“纹理”
另:本章所用Cocos2d-x版本为:
cocos2d-2.0-x-2.0.2 @ Aug 30 2012
http://cn.cocos2d-x.org/download
今天我们来学习Cocos2d-x中的“纹理”。之前有几篇文章都是谈及图片方面的,也是我刻意的安排:
(1)。如何利用Cocos2d-x开发一款游戏?(2)。游戏开发之图片元素。(3)。Cocos2d-x中图字原理之深入分析。(4)。红孩儿纹理打包器。(5)。CCImage深入分析。(6)。词典类CCDictionary深入分析。
在第一篇里我首先提到了“图元素的管理工具”。这是为什么呢?因为2D游戏画面是由图片构成的。了解好图片从美术绘制到加载到游戏以及使用和释放的整个过程是非常重要的,他关系着游戏的运行效率,内存占用等重要关键问题。有一个好的方案指导工作流程对于项目的成功是一项重要的保证。所以,请各位好好管理你们的图片资源。在第2篇里对于图片拼合的用途和意义做了启蒙。第3篇里我们深入了解了写汉字时Cocos2d-x是怎么使用图片来进行绘制的,它里面CCTextureAtlas和CCSpriteBatchNode 两个类告诉我们图片拼合原理与实现。而第4篇介绍了我开发的一款图片拼合工具。随后我会更新为最新版提供给大家使用。新增了PLIST方式导出和自动进行边缘空白裁剪的功能,免费而更强大!欢迎大家到下载。而后面两篇,其实是为了引出今天的文章而做的铺垫。
本博为什么要花这么多时间在纹理上呢?因为本博一次再一次的看到很多的Cocos2d开发者在说:“为什么我的游戏占内存这么大?”。“为什么我的游戏跑起来效率这么低?”。其实这里面最根本的问题是你思想上没有对资源进行优化的一种流程在里面。在进行游戏开发的过程中,你被美术牵着鼻子跑,你没有自然而然的想到该怎么进行图片的格式,尺寸,拼合,复用方式,以及绘制时的方式。这些因素对游戏产生的具体影响在你的脑子里还没有留下完整的概念。所以,我希望经过对于这些源码的分析,让大家去了解这些因素,从而学会思考如何优化自已的游戏。好了,大家先将今天关于纹理代码的分析博文学习完,下一篇我讲对于这些优化因素做一个具体的讲解。
当一张图片被加载到内存后,它是以纹理的形式存在的。纹理是什么东西呢?纹理就是一块内存,这块内存中存放的是按照指定的像素格式填充的图片像素信息。它被最终作为三角面着色所依据的数据源。
我们来看一下cocos2d-x中的libcocos2d库,其下有许多目录,找到textures展开,可以看到有CCTexture2D,CCTextureAtlas,CCTextureCache,CCTexturePVR四个类。
这四个类的功能分别是:
CCTexture2D: 纹理,即图片加载入内存后供CPU和GPU操作的贴图对象。
CCTexturePVR:处理PVR文件生成纹理的类,提示: 大家可以用它解析愤怒的小鸟中的图片。
CCTextureCache:纹理管理器,负责加载图片并对生成的纹理进行管理。通过“字典”来进行快速的查询。
CCTextureAtlas:纹理块管理器,如果图片是由多个小图块组成的,则纹理块管理器用来存储这些小图块的相关信息,以方便绘制相应图块。
为了让大家更好的学习纹理,在讲解纹理的代码之前我已经先给大家分析了本章用到的两个功能类:
CCImage和CCDictionary。这两个类分别在纹理模块中担任加载图片和管理纹理指针的作用。希望大家先顶一下这两篇贴子之后再开始下面的代码学习,你一定会感到非常容易。
一.CCTexture2D:
好,咱们现在开始看CCTexture2D:
- #ifndef __CCTEXTURE2D_H__
- #define __CCTEXTURE2D_H__
-
- #include <string>
- #include "cocoa/CCObject.h"
- #include "cocoa/CCGeometry.h"
- #include "ccTypes.h"
-
-
- NS_CC_BEGIN
-
- class CCImage;
-
-
-
- typedef enum {
-
-
- kCCTexture2DPixelFormat_RGBA8888,
-
- kCCTexture2DPixelFormat_RGB888,
-
- kCCTexture2DPixelFormat_RGB565,
-
- kCCTexture2DPixelFormat_A8,
-
- kCCTexture2DPixelFormat_I8,
-
- kCCTexture2DPixelFormat_AI88,
-
- kCCTexture2DPixelFormat_RGBA4444,
-
- kCCTexture2DPixelFormat_RGB5A1,
-
- kCCTexture2DPixelFormat_PVRTC4,
-
- kCCTexture2DPixelFormat_PVRTC2,
-
-
- kCCTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_RGBA8888,
-
-
- kTexture2DPixelFormat_RGBA8888 = kCCTexture2DPixelFormat_RGBA8888,
- kTexture2DPixelFormat_RGB888 = kCCTexture2DPixelFormat_RGB888,
- kTexture2DPixelFormat_RGB565 = kCCTexture2DPixelFormat_RGB565,
- kTexture2DPixelFormat_A8 = kCCTexture2DPixelFormat_A8,
- kTexture2DPixelFormat_RGBA4444 = kCCTexture2DPixelFormat_RGBA4444,
- kTexture2DPixelFormat_RGB5A1 = kCCTexture2DPixelFormat_RGB5A1,
- kTexture2DPixelFormat_Default = kCCTexture2DPixelFormat_Default
-
- } CCTexture2DPixelFormat;
-
-
- class CCGLProgram;
-
-
- typedef struct _ccTexParams {
- GLuint minFilter;
- GLuint magFilter;
- GLuint wrapS;
- GLuint wrapT;
- } ccTexParams;
-
-
-
-
- class CC_DLL CCTexture2D : public CCObject
- {
- public:
-
- CCTexture2D();
-
- virtual ~CCTexture2D();
-
- const char* description(void);
-
-
- void releaseData(void *data);
-
- void* keepData(void *data, unsigned int length);
-
-
- bool initWithData(const void* data, CCTexture2DPixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, const CCSize& contentSize);
-
-
- void drawAtPoint(const CCPoint& point);
-
- void drawInRect(const CCRect& rect);
-
-
- bool initWithImage(CCImage * uiImage);
-
-
- bool initWithString(const char *text, const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize);
-
- bool initWithString(const char *text, const char *fontName, float fontSize);
-
-
- #ifdef CC_SUPPORT_PVRTC
-
- bool initWithPVRTCData(const void *data, int level, int bpp, bool hasAlpha, int length, CCTexture2DPixelFormat pixelFormat);
- #endif // CC_SUPPORT_PVRTC
-
-
- bool initWithPVRFile(const char* file);
-
-
- void setTexParameters(ccTexParams* texParams);
-
-
- void setAntiAliasTexParameters();
-
-
- void setAliasTexParameters();
-
-
-
- void generateMipmap();
-
-
- const char* stringForFormat();
-
-
- unsigned int bitsPerPixelForFormat();
-
-
- unsigned int bitsPerPixelForFormat(CCTexture2DPixelFormat format);
-
-
-
- static void setDefaultAlphaPixelFormat(CCTexture2DPixelFormat format);
-
-
- static CCTexture2DPixelFormat defaultAlphaPixelFormat();
-
-
- static void PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
-
-
- const CCSize& getContentSizeInPixels();
-
- bool hasPremultipliedAlpha();
-
- bool hasMipmaps();
- private:
-
- bool initPremultipliedATextureWithImage(CCImage * image, unsigned int pixelsWide, unsigned int pixelsHigh);
-
-
- bool m_bPVRHaveAlphaPremultiplied;
-
-
- CC_PROPERTY_READONLY(CCTexture2DPixelFormat, m_ePixelFormat, PixelFormat)
-
- CC_PROPERTY_READONLY(unsigned int, m_uPixelsWide, PixelsWide)
-
- CC_PROPERTY_READONLY(unsigned int, m_uPixelsHigh, PixelsHigh)
-
-
- CC_PROPERTY_READONLY(GLuint, m_uName, Name)
-
-
- CC_PROPERTY(GLfloat, m_fMaxS, MaxS)
-
- CC_PROPERTY(GLfloat, m_fMaxT, MaxT)
-
- CC_PROPERTY_READONLY(CCSize, m_tContentSize, ContentSize)
-
-
- bool m_bHasPremultipliedAlpha;
-
- bool m_bHasMipmaps;
-
-
- CC_PROPERTY(CCGLProgram*, m_pShaderProgram, ShaderProgram);
- };
-
- NS_CC_END
-
- #endif //__CCTEXTURE2D_H__
再来看CCTexture2D.cpp:
- #include "CCTexture2D.h"
- #include "ccConfig.h"
- #include "ccMacros.h"
- #include "CCConfiguration.h"
- #include "platform/platform.h"
- #include "platform/CCImage.h"
- #include "CCGL.h"
- #include "support/ccUtils.h"
- #include "platform/CCPlatformMacros.h"
- #include "textures/CCTexturePVR.h"
- #include "CCDirector.h"
- #include "shaders/CCGLProgram.h"
- #include "shaders/ccGLStateCache.h"
- #include "shaders/CCShaderCache.h"
-
- #if CC_ENABLE_CACHE_TEXTURE_DATA
- #include "CCTextureCache.h"
- #endif
-
- NS_CC_BEGIN
-
-
-
- static CCTexture2DPixelFormat g_defaultAlphaPixelFormat = kCCTexture2DPixelFormat_Default;
-
-
- static bool PVRHaveAlphaPremultiplied_ = false;
-
-
- CCTexture2D::CCTexture2D()
- : m_uPixelsWide(0)
- , m_uPixelsHigh(0)
- , m_uName(0)
- , m_fMaxS(0.0)
- , m_fMaxT(0.0)
- , m_bHasPremultipliedAlpha(false)
- , m_bHasMipmaps(false)
- , m_bPVRHaveAlphaPremultiplied(true)
- , m_pShaderProgram(NULL)
- {
- }
-
- CCTexture2D::~CCTexture2D()
- {
-
- #if CC_ENABLE_CACHE_TEXTURE_DATA
- VolatileTexture::removeTexture(this);
- #endif
-
- CCLOGINFO("cocos2d: deallocing CCTexture2D %u.", m_uName);
-
- CC_SAFE_RELEASE(m_pShaderProgram);
-
- if(m_uName)
- {
- ccGLDeleteTexture(m_uName);
- }
- }
-
- CCTexture2DPixelFormat CCTexture2D::getPixelFormat()
- {
- return m_ePixelFormat;
- }
-
- unsigned int CCTexture2D::getPixelsWide()
- {
- return m_uPixelsWide;
- }
-
- unsigned int CCTexture2D::getPixelsHigh()
- {
- return m_uPixelsHigh;
- }
-
- GLuint CCTexture2D::getName()
- {
- return m_uName;
- }
-
- CCSize CCTexture2D::getContentSize()
- {
-
- CCSize ret;
- ret.width = m_tContentSize.width / CC_CONTENT_SCALE_FACTOR();
- ret.height = m_tContentSize.height / CC_CONTENT_SCALE_FACTOR();
-
- return ret;
- }
-
- const CCSize& CCTexture2D::getContentSizeInPixels()
- {
- return m_tContentSize;
- }
-
- GLfloat CCTexture2D::getMaxS()
- {
- return m_fMaxS;
- }
-
- void CCTexture2D::setMaxS(GLfloat maxS)
- {
- m_fMaxS = maxS;
- }
-
- GLfloat CCTexture2D::getMaxT()
- {
- return m_fMaxT;
- }
-
- void CCTexture2D::setMaxT(GLfloat maxT)
- {
- m_fMaxT = maxT;
- }
-
- CCGLProgram* CCTexture2D::getShaderProgram(void)
- {
- return m_pShaderProgram;
- }
-
- void CCTexture2D::setShaderProgram(CCGLProgram* pShaderProgram)
- {
- CC_SAFE_RETAIN(pShaderProgram);
- CC_SAFE_RELEASE(m_pShaderProgram);
- m_pShaderProgram = pShaderProgram;
- }
-
- void CCTexture2D::releaseData(void *data)
- {
- free(data);
- }
-
- void* CCTexture2D::keepData(void *data, unsigned int length)
- {
-
- CC_UNUSED_PARAM(length);
- return data;
- }
-
- bool CCTexture2D::hasPremultipliedAlpha()
- {
- return m_bHasPremultipliedAlpha;
- }
-
- bool CCTexture2D::initWithData(const void *data, CCTexture2DPixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, const CCSize& contentSize)
- {
-
- if( pixelFormat == kCCTexture2DPixelFormat_RGBA8888 || ( ccNextPOT(pixelsWide)==pixelsWide && ccNextPOT(pixelsHigh)==pixelsHigh) )
- {
- glPixelStorei(GL_UNPACK_ALIGNMENT,4);
- }
- else
- {
- glPixelStorei(GL_UNPACK_ALIGNMENT,1);
- }
-
- glGenTextures(1, &m_uName);
-
- ccGLBindTexture2D(m_uName);
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
-
-
- switch(pixelFormat)
- {
- case kCCTexture2DPixelFormat_RGBA8888:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
- break;
- case kCCTexture2DPixelFormat_RGB888:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
- break;
- case kCCTexture2DPixelFormat_RGBA4444:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
- break;
- case kCCTexture2DPixelFormat_RGB5A1:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, data);
- break;
- case kCCTexture2DPixelFormat_RGB565:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
- break;
- case kCCTexture2DPixelFormat_AI88:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
- break;
- case kCCTexture2DPixelFormat_A8:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
- break;
- case kCCTexture2DPixelFormat_I8:
- glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, (GLsizei)pixelsWide, (GLsizei)pixelsHigh, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
- break;
- default:
- CCAssert(0, "NSInternalInconsistencyException");
-
- }
-
- m_tContentSize = contentSize;
-
- m_uPixelsWide = pixelsWide;
- m_uPixelsHigh = pixelsHigh;
-
- m_ePixelFormat = pixelFormat;
-
- m_fMaxS = contentSize.width / (float)(pixelsWide);
- m_fMaxT = contentSize.height / (float)(pixelsHigh);
-
- m_bHasPremultipliedAlpha = false;
-
- m_bHasMipmaps = false;
-
-
- setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture));
-
- return true;
- }
-
-
- const char* CCTexture2D::description(void)
- {
- return CCString::createWithFormat("<CCTexture2D | Name = %u | Dimensions = %u x %u | Coordinates = (%.2f, %.2f)>", m_uName, m_uPixelsWide, m_uPixelsHigh, m_fMaxS, m_fMaxT)->getCString();
- }
-
-
- bool CCTexture2D::initWithImage(CCImage *uiImage)
- {
-
- if (uiImage == NULL)
- {
- CCLOG("cocos2d: CCTexture2D. Can't create Texture. UIImage is nil");
- this->release();
- return false;
- }
-
- unsigned int imageWidth = uiImage->getWidth();
- unsigned int imageHeight = uiImage->getHeight();
-
- CCConfiguration *conf = CCConfiguration::sharedConfiguration();
-
- unsigned maxTextureSize = conf->getMaxTextureSize();
-
- if (imageWidth > maxTextureSize || imageHeight > maxTextureSize)
- {
- CCLOG("cocos2d: WARNING: Image (%u x %u) is bigger than the supported %u x %u", imageWidth, imageHeight, maxTextureSize, maxTextureSize);
- this->release();
- return NULL;
- }
-
-
- return initPremultipliedATextureWithImage(uiImage, imageWidth, imageHeight);
- }
-
- bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height)
- {
-
-
- unsigned char* tempData = image->getData();
-
- unsigned int* inPixel32 = NULL;
-
- unsigned char* inPixel8 = NULL;
-
- unsigned short* outPixel16 = NULL;
-
- bool hasAlpha = image->hasAlpha();
-
- CCSize imageSize = CCSizeMake((float)(image->getWidth()), (float)(image->getHeight()));
-
- CCTexture2DPixelFormat pixelFormat;
-
- size_t bpp = image->getBitsPerComponent();
-
-
- if(hasAlpha)
- {
- pixelFormat = g_defaultAlphaPixelFormat;
- }
- else
- {
-
- if (bpp >= 8)
- {
- pixelFormat = kCCTexture2DPixelFormat_RGB888;
- }
- else
- {
- pixelFormat = kCCTexture2DPixelFormat_RGB565;
- }
-
- }
-
-
- unsigned int length = width * height;
-
- if (pixelFormat == kCCTexture2DPixelFormat_RGB565)
- {
-
- if (hasAlpha)
- {
-
-
-
- tempData = new unsigned char[width * height * 2];
-
- outPixel16 = (unsigned short*)tempData;
-
- inPixel32 = (unsigned int*)image->getData();
-
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel16++ =
- ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) |
- ((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) |
- ((((*inPixel32 >> 16) & 0xFF) >> 3) << 0);
- }
- }
- else
- {
-
-
- tempData = new unsigned char[width * height * 2];
-
- outPixel16 = (unsigned short*)tempData;
-
- inPixel8 = (unsigned char*)image->getData();
-
- for(unsigned int i = 0; i < length; ++i)
- {
-
- *outPixel16++ =
- (((*inPixel8++ & 0xFF) >> 3) << 11) |
- (((*inPixel8++ & 0xFF) >> 2) << 5) |
- (((*inPixel8++ & 0xFF) >> 3) << 0);
- }
- }
- }
- else if (pixelFormat == kCCTexture2DPixelFormat_RGBA4444)
- {
-
-
- inPixel32 = (unsigned int*)image->getData();
-
- tempData = new unsigned char[width * height * 2];
-
- outPixel16 = (unsigned short*)tempData;
-
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel16++ =
- ((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) |
- ((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) |
- ((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) |
- ((((*inPixel32 >> 24) & 0xFF) >> 4) << 0);
- }
- }
- else if (pixelFormat == kCCTexture2DPixelFormat_RGB5A1)
- {
-
- inPixel32 = (unsigned int*)image->getData();
-
- tempData = new unsigned char[width * height * 2];
-
- outPixel16 = (unsigned short*)tempData;
-
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel16++ =
- ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) |
- ((((*inPixel32 >> 8) & 0xFF) >> 3) << 6) |
- ((((*inPixel32 >> 16) & 0xFF) >> 3) << 1) |
- ((((*inPixel32 >> 24) & 0xFF) >> 7) << 0);
- }
- }
- else if (pixelFormat == kCCTexture2DPixelFormat_A8)
- {
-
- inPixel32 = (unsigned int*)image->getData();
- tempData = new unsigned char[width * height];
- unsigned char *outPixel8 = tempData;
-
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel8++ = (*inPixel32 >> 24) & 0xFF;
- }
- }
-
- if (hasAlpha && pixelFormat == kCCTexture2DPixelFormat_RGB888)
- {
-
- inPixel32 = (unsigned int*)image->getData();
- tempData = new unsigned char[width * height * 3];
- unsigned char *outPixel8 = tempData;
-
- for(unsigned int i = 0; i < length; ++i, ++inPixel32)
- {
- *outPixel8++ = (*inPixel32 >> 0) & 0xFF;
- *outPixel8++ = (*inPixel32 >> 8) & 0xFF;
- *outPixel8++ = (*inPixel32 >> 16) & 0xFF;
- }
- }
-
- initWithData(tempData, pixelFormat, width, height, imageSize);
-
- if (tempData != image->getData())
- {
- delete [] tempData;
- }
-
- m_bHasPremultipliedAlpha = image->isPremultipliedAlpha();
- return true;
- }
-
-
-
-
-
- bool CCTexture2D::initWithString(const char *text, const char *fontName, float fontSize)
- {
- return initWithString(text, CCSizeMake(0,0), kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop, fontName, fontSize);
- }
-
-
-
-
-
-
-
- bool CCTexture2D::initWithString(const char *text, const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize)
- {
-
- #if CC_ENABLE_CACHE_TEXTURE_DATA
-
- VolatileTexture::addStringTexture(this, text, dimensions, hAlignment, vAlignment, fontName, fontSize);
- #endif
-
- CCImage image;
-
- CCImage::ETextAlign eAlign;
-
- if (kCCVerticalTextAlignmentTop == vAlignment)
- {
-
- eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignTop
- : (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignTopLeft : CCImage::kAlignTopRight;
- }
-
- else if (kCCVerticalTextAlignmentCenter == vAlignment)
- {
-
- eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignCenter
- : (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignLeft : CCImage::kAlignRight;
- }
-
- else if (kCCVerticalTextAlignmentBottom == vAlignment)
- {
-
- eAlign = (kCCTextAlignmentCenter == hAlignment) ? CCImage::kAlignBottom
- : (kCCTextAlignmentLeft == hAlignment) ? CCImage::kAlignBottomLeft : CCImage::kAlignBottomRight;
- }
- else
- {
-
- CCAssert(false, "Not supported alignment format!");
- }
-
- if (!image.initWithString(text, (int)dimensions.width, (int)dimensions.height, eAlign, fontName, (int)fontSize))
- {
- return false;
- }
-
- return initWithImage(&image);
- }
-
-
-
- void CCTexture2D::drawAtPoint(const CCPoint& point)
- {
- GLfloat coordinates[] = {
- 0.0f, m_fMaxT,
- m_fMaxS,m_fMaxT,
- 0.0f, 0.0f,
- m_fMaxS,0.0f };
-
- GLfloat width = (GLfloat)m_uPixelsWide * m_fMaxS,
- height = (GLfloat)m_uPixelsHigh * m_fMaxT;
-
- GLfloat vertices[] = {
- point.x, point.y,
- width + point.x, point.y,
- point.x, height + point.y,
- width + point.x, height + point.y };
-
- ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
-
- m_pShaderProgram->use();
-
- m_pShaderProgram->setUniformForModelViewProjectionMatrix();
-
- ccGLBindTexture2D( m_uName );
-
-
- glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
-
- glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
-
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- }
-
- void CCTexture2D::drawInRect(const CCRect& rect)
- {
- GLfloat coordinates[] = {
- 0.0f, m_fMaxT,
- m_fMaxS,m_fMaxT,
- 0.0f, 0.0f,
- m_fMaxS,0.0f };
-
- GLfloat vertices[] = { rect.origin.x, rect.origin.y,
- rect.origin.x + rect.size.width, rect.origin.y,
- rect.origin.x, rect.origin.y + rect.size.height,
- rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, };
-
- ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
-
- m_pShaderProgram->use();
-
- m_pShaderProgram->setUniformForModelViewProjectionMatrix();
-
- ccGLBindTexture2D( m_uName );
-
- glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
-
- glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
-
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- }
-
- #ifdef CC_SUPPORT_PVRTC
-
-
- bool CCTexture2D::initWithPVRTCData(const void *data, int level, int bpp, bool hasAlpha, int length, CCTexture2DPixelFormat pixelFormat)
- {
- if( !(CCConfiguration::sharedConfiguration()->supportsPVRTC()) )
- {
- CCLOG("cocos2d: WARNING: PVRTC images is not supported.");
- this->release();
- return false;
- }
-
- glGenTextures(1, &m_uName);
-
- glBindTexture(GL_TEXTURE_2D, m_uName);
-
- this->setAntiAliasTexParameters();
-
- GLenum format;
-
- GLsizei size = length * length * bpp / 8;
-
- if(hasAlpha) {
- format = (bpp == 4) ? GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG : GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
- } else {
- format = (bpp == 4) ? GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG : GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
- }
- if(size < 32) {
- size = 32;
- }
-
- glCompressedTexImage2D(GL_TEXTURE_2D, level, format, length, length, 0, size, data);
-
- m_tContentSize = CCSizeMake((float)(length), (float)(length));
- m_uPixelsWide = length;
- m_uPixelsHigh = length;
- m_fMaxS = 1.0f;
- m_fMaxT = 1.0f;
- m_bHasPremultipliedAlpha = PVRHaveAlphaPremultiplied_;
- m_ePixelFormat = pixelFormat;
-
- return true;
- }
- #endif // CC_SUPPORT_PVRTC
-
- bool CCTexture2D::initWithPVRFile(const char* file)
- {
- bool bRet = false;
-
-
- CCTexturePVR *pvr = new CCTexturePVR;
- bRet = pvr->initWithContentsOfFile(file);
-
- if (bRet)
- {
- pvr->setRetainName(true);
-
- m_uName = pvr->getName();
- m_fMaxS = 1.0f;
- m_fMaxT = 1.0f;
- m_uPixelsWide = pvr->getWidth();
- m_uPixelsHigh = pvr->getHeight();
- m_tContentSize = CCSizeMake((float)m_uPixelsWide, (float)m_uPixelsHigh);
- m_bHasPremultipliedAlpha = PVRHaveAlphaPremultiplied_;
- m_ePixelFormat = pvr->getFormat();
- m_bHasMipmaps = pvr->getNumberOfMipmaps() > 1;
-
- pvr->release();
- }
- else
- {
- CCLOG("cocos2d: Couldn't load PVR image %s", file);
- }
-
- return bRet;
- }
-
- void CCTexture2D::PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied)
- {
- PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied;
- }
-
-
-
- void CCTexture2D::generateMipmap()
- {
- CCAssert( m_uPixelsWide == ccNextPOT(m_uPixelsWide) && m_uPixelsHigh == ccNextPOT(m_uPixelsHigh), "Mimpap texture only works in POT textures");
- ccGLBindTexture2D( m_uName );
- glGenerateMipmap(GL_TEXTURE_2D);
- m_bHasMipmaps = true;
- }
-
- bool CCTexture2D::hasMipmaps()
- {
- return m_bHasMipmaps;
- }
-
- void CCTexture2D::setTexParameters(ccTexParams *texParams)
- {
- CCAssert( (m_uPixelsWide == ccNextPOT(m_uPixelsWide) || texParams->wrapS == GL_CLAMP_TO_EDGE) &&
- (m_uPixelsHigh == ccNextPOT(m_uPixelsHigh) || texParams->wrapT == GL_CLAMP_TO_EDGE),
- "GL_CLAMP_TO_EDGE should be used in NPOT dimensions");
-
- ccGLBindTexture2D( m_uName );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texParams->minFilter );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texParams->magFilter );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texParams->wrapS );
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texParams->wrapT );
- }
-
- void CCTexture2D::setAliasTexParameters()
- {
- ccGLBindTexture2D( m_uName );
-
- if( ! m_bHasMipmaps )
- {
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
- }
- else
- {
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST );
- }
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
- }
-
- void CCTexture2D::setAntiAliasTexParameters()
- {
- ccGLBindTexture2D( m_uName );
-
- if( ! m_bHasMipmaps )
- {
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
- }
- else
- {
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
- }
-
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- }
-
- const char* CCTexture2D::stringForFormat()
- {
- switch (m_ePixelFormat)
- {
- case kCCTexture2DPixelFormat_RGBA8888:
- return "RGBA8888";
-
- case kCCTexture2DPixelFormat_RGB888:
- return "RGB888";
-
- case kCCTexture2DPixelFormat_RGB565:
- return "RGB565";
-
- case kCCTexture2DPixelFormat_RGBA4444:
- return "RGBA4444";
-
- case kCCTexture2DPixelFormat_RGB5A1:
- return "RGB5A1";
-
- case kCCTexture2DPixelFormat_AI88:
- return "AI88";
-
- case kCCTexture2DPixelFormat_A8:
- return "A8";
-
- case kCCTexture2DPixelFormat_I8:
- return "I8";
-
- case kCCTexture2DPixelFormat_PVRTC4:
- return "PVRTC4";
-
- case kCCTexture2DPixelFormat_PVRTC2:
- return "PVRTC2";
-
- default:
- CCAssert(false , "unrecognised pixel format");
- CCLOG("stringForFormat: %ld, cannot give useful result", (long)m_ePixelFormat);
- break;
- }
-
- return NULL;
- }
-
-
-
- void CCTexture2D::setDefaultAlphaPixelFormat(CCTexture2DPixelFormat format)
- {
- g_defaultAlphaPixelFormat = format;
- }
-
-
- CCTexture2DPixelFormat CCTexture2D::defaultAlphaPixelFormat()
- {
- return g_defaultAlphaPixelFormat;
- }
-
- unsigned int CCTexture2D::bitsPerPixelForFormat(CCTexture2DPixelFormat format)
- {
- unsigned int ret=0;
-
- switch (format) {
- case kCCTexture2DPixelFormat_RGBA8888:
- ret = 32;
- break;
- case kCCTexture2DPixelFormat_RGB888:
-
- ret = 32;
- break;
- case kCCTexture2DPixelFormat_RGB565:
- ret = 16;
- break;
- case kCCTexture2DPixelFormat_RGBA4444:
- ret = 16;
- break;
- case kCCTexture2DPixelFormat_RGB5A1:
- ret = 16;
- break;
- case kCCTexture2DPixelFormat_AI88:
- ret = 16;
- break;
- case kCCTexture2DPixelFormat_A8:
- ret = 8;
- break;
- case kCCTexture2DPixelFormat_I8:
- ret = 8;
- break;
- case kCCTexture2DPixelFormat_PVRTC4:
- ret = 4;
- break;
- case kCCTexture2DPixelFormat_PVRTC2:
- ret = 2;
- break;
- default:
- ret = -1;
- CCAssert(false , "unrecognised pixel format");
- CCLOG("bitsPerPixelForFormat: %ld, cannot give useful result", (long)format);
- break;
- }
- return ret;
- }
-
- unsigned int CCTexture2D::bitsPerPixelForFormat()
- {
- return this->bitsPerPixelForFormat(m_ePixelFormat);
- }
-
-
- NS_CC_END
二.CCTexturePVR:
CCTexturePVR.h:
- #ifndef __CCPVRTEXTURE_H__
- #define __CCPVRTEXTURE_H__
-
- #include "CCStdC.h"
- #include "CCGL.h"
- #include "cocoa/CCObject.h"
- #include "cocoa/CCArray.h"
-
- NS_CC_BEGIN
-
-
- struct CCPVRMipmap {
- unsigned char *address;
- unsigned int len;
- };
-
-
- enum {
- CC_PVRMIPMAP_MAX = 16,
- };
-
-
-
- - RGBA8888
- - BGRA8888
- - RGBA4444
- - RGBA5551
- - RGB565
- - A8
- - I8
- - AI88
- - PVRTC 4BPP
- - PVRTC 2BPP
-
- class CCTexturePVR : public CCObject
- {
- public:
-
- CCTexturePVR();
-
- virtual ~CCTexturePVR();
-
- bool initWithContentsOfFile(const char* path);
-
-
- CC_DEPRECATED_ATTRIB一个UTE static CCTexturePVR* pvrTextureWithContentsOfFile(const char* path);
-
- static CCTexturePVR* create(const char* path);
-
-
-
- inline unsigned int getName() { return m_uName; }
-
- inline unsigned int getWidth() { return m_uWidth; }
- inline unsigned int getHeight() { return m_uHeight; }
-
- inline bool hasAlpha() { return m_bHasAlpha; }
-
- inline unsigned int getNumberOfMipmaps() { return m_uNumberOfMipmaps; }
-
- inline CCTexture2DPixelFormat getFormat() { return m_eFormat; }
-
- inline bool isRetainName() { return m_bRetainName; }
-
- inline void setRetainName(bool retainName) { m_bRetainName = retainName; }
-
- private:
-
- bool unpackPVRData(unsigned char* data, unsigned int len);
-
- bool createGLTexture();
-
- protected:
-
- struct CCPVRMipmap m_asMipmaps[CC_PVRMIPMAP_MAX];
-
- unsigned int m_uNumberOfMipmaps;
-
- unsigned int m_uTableFormatIndex;
- unsigned int m_uWidth, m_uHeight;
-
- GLuint m_uName;
-
- bool m_bHasAlpha;
-
- bool m_bRetainName;
-
- CCTexture2DPixelFormat m_eFormat;
- };
-
- NS_CC_END
-
-
- #endif //__CCPVRTEXTURE_H__
CCTexturePVR.cpp:
- #include "CCTexture2D.h"
- #include "CCTexturePVR.h"
- #include "ccMacros.h"
- #include "CCConfiguration.h"
- #include "support/ccUtils.h"
- #include "CCStdC.h"
- #include "platform/CCFileUtils.h"
- #include "support/zip_support/ZipUtils.h"
- #include "shaders/ccGLStateCache.h"
- #include <ctype.h>
- #include <cctype>
-
- NS_CC_BEGIN
-
- #define PVR_TEXTURE_FLAG_TYPE_MASK 0xff
-
-
- enum {
- kPVRTextureFlagMipmap = (1<<8),
- kPVRTextureFlagTwiddle = (1<<9),
- kPVRTextureFlagBumpmap = (1<<10),
- kPVRTextureFlagTiling = (1<<11),
- kPVRTextureFlagCubemap = (1<<12),
- kPVRTextureFlagFalseMipCol = (1<<13),
- kPVRTextureFlagVolume = (1<<14),
- kPVRTextureFlagAlpha = (1<<15),
- kPVRTextureFlagVerticalFlip = (1<<16),
- };
-
- static char gPVRTexIdentifier[5] = "PVR!";
-
- enum
- {
- kPVRTexturePixelTypeRGBA_4444= 0x10,
- kPVRTexturePixelTypeRGBA_5551,
- kPVRTexturePixelTypeRGBA_8888,
- kPVRTexturePixelTypeRGB_565,
- kPVRTexturePixelTypeRGB_555,
- kPVRTexturePixelTypeRGB_888,
- kPVRTexturePixelTypeI_8,
- kPVRTexturePixelTypeAI_88,
- kPVRTexturePixelTypePVRTC_2,
- kPVRTexturePixelTypePVRTC_4,
- kPVRTexturePixelTypeBGRA_8888,
- kPVRTexturePixelTypeA_8,
- };
-
- static const unsigned int tableFormats[][7] = {
-
-
-
-
-
-
-
-
- { kPVRTexturePixelTypeRGBA_4444, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 16, false, kCCTexture2DPixelFormat_RGBA4444 },
- { kPVRTexturePixelTypeRGBA_5551, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 16, false, kCCTexture2DPixelFormat_RGB5A1 },
- { kPVRTexturePixelTypeRGBA_8888, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 32, false, kCCTexture2DPixelFormat_RGBA8888 },
- { kPVRTexturePixelTypeRGB_565, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 16, false, kCCTexture2DPixelFormat_RGB565 },
- { kPVRTexturePixelTypeRGB_888, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 24, false, kCCTexture2DPixelFormat_RGB888 },
- { kPVRTexturePixelTypeA_8, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, 8, false, kCCTexture2DPixelFormat_A8 },
- { kPVRTexturePixelTypeI_8, GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 8, false, kCCTexture2DPixelFormat_I8 },
- { kPVRTexturePixelTypeAI_88, GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,16, false, kCCTexture2DPixelFormat_AI88 },
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
-
- { kPVRTexturePixelTypePVRTC_2, GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, (unsigned int)-1, (unsigned int)-1, 2, true, kCCTexture2DPixelFormat_PVRTC2 },
- { kPVRTexturePixelTypePVRTC_4, GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, (unsigned int)-1, (unsigned int)-1, 4, true, kCCTexture2DPixelFormat_PVRTC4 },
-
- { kPVRTexturePixelTypeBGRA_8888, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE, 32, false, kCCTexture2DPixelFormat_RGBA8888 },
- #endif
- };
-
-
- #define MAX_TABLE_ELEMENTS (sizeof(tableFormats) / sizeof(tableFormats[0]))
-
- enum {
- kCCInternalPVRTextureFormat,
- kCCInternalOpenGLInternalFormat,
- kCCInternalOpenGLFormat,
- kCCInternalOpenGLType,
- kCCInternalBPP,
- kCCInternalCompressedImage,
- kCCInternalCCTexture2DPixelFormat,
- };
-
- typedef struct _PVRTexHeader
- {
- unsigned int headerLength;
- unsigned int height;
- unsigned int width;
- unsigned int numMipmaps;
- unsigned int flags;
- unsigned int dataLength;
- unsigned int bpp;
- unsigned int bitmaskRed;
- unsigned int bitmaskGreen;
- unsigned int bitmaskBlue;
- unsigned int bitmaskAlpha;
- unsigned int pvrTag;
- unsigned int numSurfs;
- } PVRTexHeader;
-
- CCTexturePVR::CCTexturePVR()
- : m_uTableFormatIndex(0)
- , m_uNumberOfMipmaps(0)
- , m_uWidth(0)
- , m_uHeight(0)
- , m_bRetainName(false)
- , m_bHasAlpha(false)
- , m_uName(0)
- , m_eFormat(kCCTexture2DPixelFormat_Default)
- {
- }
-
- CCTexturePVR::~CCTexturePVR()
- {
- CCLOGINFO( "cocos2d: deallocing CCTexturePVR" );
-
- if (m_uName != 0 && ! m_bRetainName)
- {
- ccGLDeleteTexture(m_uName);
- }
- }
-
- bool CCTexturePVR::unpackPVRData(unsigned char* data, unsigned int len)
- {
- bool success = false;
- PVRTexHeader *header = NULL;
- unsigned int flags, pvrTag;
- unsigned int dataLength = 0, dataOffset = 0, dataSize = 0;
- unsigned int blockSize = 0, widthBlocks = 0, heightBlocks = 0;
- unsigned int width = 0, height = 0, bpp = 4;
- unsigned char *bytes = NULL;
- unsigned int formatFlags;
-
-
- header = (PVRTexHeader *)data;
-
-
- pvrTag = CC_SWAP_INT32_LITTLE_TO_HOST(header->pvrTag);
-
-
-
-
-
-
-
-
-
- if (gPVRTexIdentifier[0] != ((pvrTag >> 0) & 0xff) ||
- gPVRTexIdentifier[1] != ((pvrTag >> 8) & 0xff) ||
- gPVRTexIdentifier[2] != ((pvrTag >> 16) & 0xff) ||
- gPVRTexIdentifier[3] != ((pvrTag >> 24) & 0xff))
- {
- CCLOG("Unsupported PVR format. Use the Legacy format until the new format is supported");
- return false;
- }
-
- CCConfiguration *configuration = CCConfiguration::sharedConfiguration();
-
- flags = CC_SWAP_INT32_LITTLE_TO_HOST(header->flags);
- formatFlags = flags & PVR_TEXTURE_FLAG_TYPE_MASK;
- bool flipped = (flags & kPVRTextureFlagVerticalFlip) ? true : false;
- if (flipped)
- {
- CCLOG("cocos2d: WARNING: Image is flipped. Regenerate it using PVRTexTool");
- }
-
- if (! configuration->supportsNPOT() &&
- (header->width != ccNextPOT(header->width) || header->height != ccNextPOT(header->height)))
- {
- CCLOG("cocos2d: ERROR: Loding an NPOT texture (%dx%d) but is not supported on this device", header->width, header->height);
- return false;
- }
-
- for (m_uTableFormatIndex = 0; m_uTableFormatIndex < (unsigned int)MAX_TABLE_ELEMENTS; m_uTableFormatIndex++)
- {
- if (tableFormats[m_uTableFormatIndex][kCCInternalPVRTextureFormat] == formatFlags)
- {
-
- m_uNumberOfMipmaps = 0;
-
-
- m_uWidth = width = CC_SWAP_INT32_LITTLE_TO_HOST(header->width);
- m_uHeight = height = CC_SWAP_INT32_LITTLE_TO_HOST(header->height);
-
-
- if (CC_SWAP_INT32_LITTLE_TO_HOST(header->bitmaskAlpha))
- {
- m_bHasAlpha = true;
- }
- else
- {
- m_bHasAlpha = false;
- }
-
-
- dataLength = CC_SWAP_INT32_LITTLE_TO_HOST(header->dataLength);
-
-
- bytes = ((unsigned char *)data) + sizeof(PVRTexHeader);
- m_eFormat = (CCTexture2DPixelFormat)(tableFormats[m_uTableFormatIndex][kCCInternalCCTexture2DPixelFormat]);
- bpp = tableFormats[m_uTableFormatIndex][kCCInternalBPP];
-
-
- while (dataOffset < dataLength)
- {
- switch (formatFlags) {
- case kPVRTexturePixelTypePVRTC_2:
- blockSize = 8 * 4;
- widthBlocks = width / 8;
- heightBlocks = height / 4;
- break;
- case kPVRTexturePixelTypePVRTC_4:
- blockSize = 4 * 4;
- widthBlocks = width / 4;
- heightBlocks = height / 4;
- break;
- case kPVRTexturePixelTypeBGRA_8888:
- if (CCConfiguration::sharedConfiguration()->supportsBGRA8888() == false)
- {
- CCLOG("cocos2d: TexturePVR. BGRA8888 not supported on this device");
- return false;
- }
- default:
- blockSize = 1;
- widthBlocks = width;
- heightBlocks = height;
- break;
- }
-
-
- if (widthBlocks < 2)
- {
- widthBlocks = 2;
- }
- if (heightBlocks < 2)
- {
- heightBlocks = 2;
- }
-
- dataSize = widthBlocks * heightBlocks * ((blockSize * bpp) / 8);
- unsigned int packetLength = (dataLength - dataOffset);
- packetLength = packetLength > dataSize ? dataSize : packetLength;
-
-
- m_asMipmaps[m_uNumberOfMipmaps].address = bytes + dataOffset;
- m_asMipmaps[m_uNumberOfMipmaps].len = packetLength;
- m_uNumberOfMipmaps++;
-
-
- CCAssert(m_uNumberOfMipmaps < CC_PVRMIPMAP_MAX,
- "TexturePVR: Maximum number of mimpaps reached. Increate the CC_PVRMIPMAP_MAX value");
-
- dataOffset += packetLength;
-
-
- width = MAX(width >> 1, 1);
- height = MAX(height >> 1, 1);
- }
-
-
- success = true;
- break;
- }
- }
-
- if (! success)
- {
- CCLOG("cocos2d: WARNING: Unsupported PVR Pixel Format: 0x%2x. Re-encode it with a OpenGL pixel format variant", formatFlags);
- }
-
- return success;
- }
-
- bool CCTexturePVR::createGLTexture()
- {
-
- unsigned int width = m_uWidth;
- unsigned int height = m_uHeight;
- GLenum err;
-
- if (m_uNumberOfMipmaps > 0)
- {
-
- if (m_uName != 0)
- {
- ccGLDeleteTexture(m_uName);
- }
-
- glPixelStorei(GL_UNPACK_ALIGNMENT,1);
-
- glGenTextures(1, &m_uName);
- glBindTexture(GL_TEXTURE_2D, m_uName);
-
-
- if (m_uNumberOfMipmaps == 1)
- {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- }
- else
- {
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
- }
-
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
-
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- }
-
- CHECK_GL_ERROR_DEBUG();
-
- GLenum internalFormat = tableFormats[m_uTableFormatIndex][kCCInternalOpenGLInternalFormat];
- GLenum format = tableFormats[m_uTableFormatIndex][kCCInternalOpenGLFormat];
- GLenum type = tableFormats[m_uTableFormatIndex][kCCInternalOpenGLType];
- bool compressed = (0 == tableFormats[m_uTableFormatIndex][kCCInternalCompressedImage]) ? false : true;
-
-
- for (unsigned int i = 0; i < m_uNumberOfMipmaps; ++i)
- {
-
- if (compressed && ! CCConfiguration::sharedConfiguration()->supportsPVRTC())
- {
- CCLOG("cocos2d: WARNING: PVRTC images are not supported");
- return false;
- }
-
- unsigned char *data = m_asMipmaps[i].address;
- unsigned int datalen = m_asMipmaps[i].len;
-
- if (compressed)
- {
- glCompressedTexImage2D(GL_TEXTURE_2D, i, internalFormat, width, height, 0, datalen, data);
- }
- else
- {
- glTexImage2D(GL_TEXTURE_2D, i, internalFormat, width, height, 0, format, type, data);
- }
-
- if (i > 0 && (width != height || ccNextPOT(width) != width ))
- {
- CCLOG("cocos2d: TexturePVR. WARNING. Mipmap level %u is not squared. Texture won't render correctly. width=%u != height=%u", i, width, height);
- }
-
- err = glGetError();
- if (err != GL_NO_ERROR)
- {
- CCLOG("cocos2d: TexturePVR: Error uploading compressed texture level: %u . glError: 0x%04X", i, err);
- return false;
- }
-
- width = MAX(width >> 1, 1);
- height = MAX(height >> 1, 1);
- }
-
- return true;
- }
-
-
- bool CCTexturePVR::initWithContentsOfFile(const char* path)
- {
- unsigned char* pvrdata = NULL;
- int pvrlen = 0;
-
- std::string lowerCase(path);
- for (unsigned int i = 0; i < lowerCase.length(); ++i)
- {
- lowerCase[i] = tolower(lowerCase[i]);
- }
-
- if (lowerCase.find(".ccz") != std::string::npos)
- {
-
- pvrlen = ZipUtils::ccInflateCCZFile(path, &pvrdata);
- }
-
- else if (lowerCase.find(".gz") != std::string::npos)
- {
-
- pvrlen = ZipUtils::ccInflateGZipFile(path, &pvrdata);
- }
- else
- {
-
- pvrdata = CCFileUtils::sharedFileUtils()->getFileData(path, "rb", (unsigned long *)(&pvrlen));
- }
-
- if (pvrlen < 0)
- {
- this->release();
- return false;
- }
-
- m_uNumberOfMipmaps = 0;
-
- m_uName = 0;
- m_uWidth = m_uHeight = 0;
- m_bHasAlpha = false;
-
- m_bRetainName = false;
-
-
- if (!unpackPVRData(pvrdata, pvrlen) || !createGLTexture())
- {
- CC_SAFE_DELETE_ARRAY(pvrdata);
- this->release();
- return false;
- }
-
- CC_SAFE_DELETE_ARRAY(pvrdata);
-
- return true;
- }
-
- CCTexturePVR * CCTexturePVR::pvrTextureWithContentsOfFile(const char* path)
- {
- return CCTexturePVR::create(path);
- }
-
- CCTexturePVR * CCTexturePVR::create(const char* path)
- {
-
- CCTexturePVR * pTexture = new CCTexturePVR();
- if (pTexture)
- {
-
- if (pTexture->initWithContentsOfFile(path))
- {
-
- pTexture->autorelease();
- }
- else
- {
-
- delete pTexture;
- pTexture = NULL;
- }
- }
-
- return pTexture;
- }
-
- NS_CC_END
三.CCTextureCache:
打开CCTextureCache.h:
- #ifndef __CCTEXTURE_CACHE_H__
- #define __CCTEXTURE_CACHE_H__
-
- #include "cocoa/CCObject.h"
-
- #include "cocoa/CCDictionary.h"
- #include "textures/CCTexture2D.h"
- #include <string>
-
-
- #if CC_ENABLE_CACHE_TEXTURE_DATA
- #include "platform/CCImage.h"
- #include <list>
- #endif
-
-
- NS_CC_BEGIN
-
- class CCLock;
-
- class CCImage;
-
-
- class CC_DLL CCTextureCache : public CCObject
- {
- protected:
-
- CCDictionary* m_pTextures;
-
-
-
-
- private:
-
- void addImageAsyncCallBack(float dt);
-
- public:
-
- CCTextureCache();
-
- virtual ~CCTextureCache();
-
- const char* description(void);
-
- CCDictionary* snapshotTextures();
-
-
- static CCTextureCache * sharedTextureCache();
-
-
- static void purgeSharedTextureCache();
-
-
- CCTexture2D* addImage(const char* fileimage);
-
-
- void addImageAsync(const char *path, CCObject *target, SEL_CallFuncO selector);
-
-
-
- CCTexture2D* addUIImage(CCImage *image, const char *key);
-
-
- CCTexture2D* textureForKey(const char* key);
-
-
- void removeAllTextures();
-
-
- void removeUnusedTextures();
-
-
- void removeTexture(CCTexture2D* texture);
-
-
- void removeTextureForKey(const char *textureKeyName);
-
-
- void dumpCachedTextureInfo();
-
- #ifdef CC_SUPPORT_PVRTC
-
-
-
-
-
- CCTexture2D* addPVRTCImage(const char* fileimage, int bpp, bool hasAlpha, int width);
- #endif // CC_SUPPORT_PVRTC
-
-
- CCTexture2D* addPVRImage(const char* filename);
-
-
- static void reloadAllTextures();
- };
-
-
- #if CC_ENABLE_CACHE_TEXTURE_DATA
-
-
- class VolatileTexture
- {
-
- typedef enum {
- kInvalid = 0,
- kImageFile,
- kImageData,
- kString,
- kImage,
- }ccCachedImageType;
-
- public:
-
- VolatileTexture(CCTexture2D *t);
-
- ~VolatileTexture();
-
- static void addImageTexture(CCTexture2D *tt, const char* imageFileName, CCImage::EImageFormat format);
-
- static void addStringTexture(CCTexture2D *tt, const char* text, const CCSize& dimensions, CCTextAlignment alignment,
- CCVerticalTextAlignment vAlignment, const char *fontName, float fontSize);
-