2010-08-02 10:58:00 +08:00
|
|
|
/****************************************************************************
|
2012-06-08 14:11:48 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008 Apple Inc. All Rights Reserved.
|
2010-08-02 10:58:00 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Support for RGBA_4_4_4_4 and RGBA_5_5_5_1 was copied from:
|
|
|
|
* https://devforums.apple.com/message/37855#37855 by a1studmuffin
|
|
|
|
*/
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
#include "CCTexture2D.h"
|
2010-07-15 18:15:00 +08:00
|
|
|
#include "ccConfig.h"
|
|
|
|
#include "ccMacros.h"
|
2010-07-20 14:29:18 +08:00
|
|
|
#include "CCConfiguration.h"
|
2010-07-27 13:47:29 +08:00
|
|
|
#include "platform/platform.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "platform/CCImage.h"
|
2010-12-30 17:30:11 +08:00
|
|
|
#include "CCGL.h"
|
|
|
|
#include "support/ccUtils.h"
|
2011-01-10 17:54:44 +08:00
|
|
|
#include "platform/CCPlatformMacros.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTexturePVR.h"
|
2013-05-27 14:42:22 +08:00
|
|
|
#include "textures/CCTextureETC.h"
|
2011-11-28 17:28:43 +08:00
|
|
|
#include "CCDirector.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/ccGLStateCache.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
2010-08-31 16:53:25 +08:00
|
|
|
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-04-06 16:29:58 +08:00
|
|
|
#include "CCTextureCache.h"
|
2011-01-10 17:54:44 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
//CLASS IMPLEMENTATIONS:
|
|
|
|
|
|
|
|
// If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
|
|
|
|
// Default is: RGBA8888 (32-bit textures)
|
|
|
|
static CCTexture2DPixelFormat g_defaultAlphaPixelFormat = kCCTexture2DPixelFormat_Default;
|
|
|
|
|
2011-12-20 14:59:15 +08:00
|
|
|
// By default PVR images are treated as if they don't have the alpha channel premultiplied
|
|
|
|
static bool PVRHaveAlphaPremultiplied_ = false;
|
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
CCTexture2D::CCTexture2D()
|
2013-03-03 10:32:09 +08:00
|
|
|
: m_bPVRHaveAlphaPremultiplied(true)
|
|
|
|
, m_uPixelsWide(0)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_uPixelsHigh(0)
|
|
|
|
, m_uName(0)
|
2011-04-01 16:06:53 +08:00
|
|
|
, m_fMaxS(0.0)
|
|
|
|
, m_fMaxT(0.0)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_bHasPremultipliedAlpha(false)
|
2012-06-08 14:11:48 +08:00
|
|
|
, m_bHasMipmaps(false)
|
2012-03-14 14:55:17 +08:00
|
|
|
, m_pShaderProgram(NULL)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCTexture2D::~CCTexture2D()
|
|
|
|
{
|
2012-06-06 10:06:51 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2011-01-10 17:54:44 +08:00
|
|
|
VolatileTexture::removeTexture(this);
|
|
|
|
#endif
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOGINFO("cocos2d: deallocing CCTexture2D %u.", m_uName);
|
|
|
|
CC_SAFE_RELEASE(m_pShaderProgram);
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if(m_uName)
|
|
|
|
{
|
|
|
|
ccGLDeleteTexture(m_uName);
|
|
|
|
}
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCTexture2DPixelFormat CCTexture2D::getPixelFormat()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_ePixelFormat;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
unsigned int CCTexture2D::getPixelsWide()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_uPixelsWide;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2010-08-02 10:58:00 +08:00
|
|
|
unsigned int CCTexture2D::getPixelsHigh()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_uPixelsHigh;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint CCTexture2D::getName()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_uName;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2012-04-08 22:37:58 +08:00
|
|
|
CCSize CCTexture2D::getContentSize()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-15 15:46:01 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize ret;
|
2012-04-08 22:37:58 +08:00
|
|
|
ret.width = m_tContentSize.width / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
ret.height = m_tContentSize.height / CC_CONTENT_SCALE_FACTOR();
|
|
|
|
|
|
|
|
return ret;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
const CCSize& CCTexture2D::getContentSizeInPixels()
|
2010-12-30 17:30:11 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tContentSize;
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
GLfloat CCTexture2D::getMaxS()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fMaxS;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTexture2D::setMaxS(GLfloat maxS)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fMaxS = maxS;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
GLfloat CCTexture2D::getMaxT()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fMaxT;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTexture2D::setMaxT(GLfloat maxT)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fMaxT = maxT;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
CCGLProgram* CCTexture2D::getShaderProgram(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pShaderProgram;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTexture2D::setShaderProgram(CCGLProgram* pShaderProgram)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RETAIN(pShaderProgram);
|
|
|
|
CC_SAFE_RELEASE(m_pShaderProgram);
|
|
|
|
m_pShaderProgram = pShaderProgram;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2010-12-30 17:30:11 +08:00
|
|
|
void CCTexture2D::releaseData(void *data)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* CCTexture2D::keepData(void *data, unsigned int length)
|
|
|
|
{
|
2011-06-10 17:51:37 +08:00
|
|
|
CC_UNUSED_PARAM(length);
|
2012-09-17 15:02:24 +08:00
|
|
|
//The texture data mustn't be saved because it isn't a mutable texture.
|
2012-04-19 14:35:52 +08:00
|
|
|
return data;
|
2010-12-30 17:30:11 +08:00
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCTexture2D::hasPremultipliedAlpha()
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_bHasPremultipliedAlpha;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
bool CCTexture2D::initWithData(const void *data, CCTexture2DPixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, const CCSize& contentSize)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2013-05-19 23:53:59 +08:00
|
|
|
unsigned int bitsPerPixel;
|
|
|
|
//Hack: bitsPerPixelForFormat returns wrong number for RGB_888 textures. See function.
|
|
|
|
if(pixelFormat == kCCTexture2DPixelFormat_RGB888)
|
|
|
|
{
|
|
|
|
bitsPerPixel = 24;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bitsPerPixel = bitsPerPixelForFormat(pixelFormat);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int bytesPerRow = pixelsWide * bitsPerPixel / 8;
|
2013-05-12 23:08:33 +08:00
|
|
|
|
2013-05-19 23:53:59 +08:00
|
|
|
if(bytesPerRow % 8 == 0)
|
2013-05-19 20:12:39 +08:00
|
|
|
{
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
|
|
|
|
}
|
2013-05-19 23:53:59 +08:00
|
|
|
else if(bytesPerRow % 4 == 0)
|
2013-05-12 23:08:33 +08:00
|
|
|
{
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
|
|
|
|
}
|
2013-05-19 23:53:59 +08:00
|
|
|
else if(bytesPerRow % 2 == 0)
|
2013-05-19 20:12:39 +08:00
|
|
|
{
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
|
|
|
|
}
|
2012-06-13 16:20:58 +08:00
|
|
|
else
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2013-05-12 23:08:33 +08:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
|
|
|
|
2013-05-19 20:17:48 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glGenTextures(1, &m_uName);
|
|
|
|
ccGLBindTexture2D(m_uName);
|
|
|
|
|
2012-06-13 16:20:58 +08:00
|
|
|
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 );
|
2012-06-08 14:11:48 +08:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Specify OpenGL texture image
|
|
|
|
|
|
|
|
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;
|
2012-06-08 14:11:48 +08:00
|
|
|
m_bHasMipmaps = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture));
|
|
|
|
|
|
|
|
return true;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
const char* CCTexture2D::description(void)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-06-14 16:05:58 +08:00
|
|
|
return CCString::createWithFormat("<CCTexture2D | Name = %u | Dimensions = %u x %u | Coordinates = (%.2f, %.2f)>", m_uName, m_uPixelsWide, m_uPixelsHigh, m_fMaxS, m_fMaxT)->getCString();
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// implementation CCTexture2D (Image)
|
2012-04-08 14:16:29 +08:00
|
|
|
|
|
|
|
bool CCTexture2D::initWithImage(CCImage *uiImage)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
if (uiImage == NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCTexture2D. Can't create Texture. UIImage is nil");
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
|
|
|
|
unsigned int imageWidth = uiImage->getWidth();
|
|
|
|
unsigned int imageHeight = uiImage->getHeight();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCConfiguration *conf = CCConfiguration::sharedConfiguration();
|
2012-08-08 18:39:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned maxTextureSize = conf->getMaxTextureSize();
|
2012-04-24 15:02:18 +08:00
|
|
|
if (imageWidth > maxTextureSize || imageHeight > maxTextureSize)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
CCLOG("cocos2d: WARNING: Image (%u x %u) is bigger than the supported %u x %u", imageWidth, imageHeight, maxTextureSize, maxTextureSize);
|
2013-02-04 17:38:22 +08:00
|
|
|
return false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-08-08 18:39:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// always load premultiplied images
|
2012-04-24 15:02:18 +08:00
|
|
|
return initPremultipliedATextureWithImage(uiImage, imageWidth, imageHeight);
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
2012-08-08 18:39:33 +08:00
|
|
|
|
2012-04-26 17:36:32 +08:00
|
|
|
bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-26 17:36:32 +08:00
|
|
|
unsigned char* tempData = image->getData();
|
2013-05-01 07:36:14 +08:00
|
|
|
unsigned int* inPixel32 = NULL;
|
2012-04-26 17:36:32 +08:00
|
|
|
unsigned char* inPixel8 = NULL;
|
|
|
|
unsigned short* outPixel16 = NULL;
|
|
|
|
bool hasAlpha = image->hasAlpha();
|
|
|
|
CCSize imageSize = CCSizeMake((float)(image->getWidth()), (float)(image->getHeight()));
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTexture2DPixelFormat pixelFormat;
|
2012-04-26 17:36:32 +08:00
|
|
|
size_t bpp = image->getBitsPerComponent();
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2011-04-20 15:34:33 +08:00
|
|
|
// compute pixel format
|
2013-05-01 07:36:14 +08:00
|
|
|
if (hasAlpha)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-05-01 07:36:14 +08:00
|
|
|
pixelFormat = g_defaultAlphaPixelFormat;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (bpp >= 8)
|
|
|
|
{
|
|
|
|
pixelFormat = kCCTexture2DPixelFormat_RGB888;
|
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
else
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pixelFormat = kCCTexture2DPixelFormat_RGB565;
|
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Repack the pixel data into the right format
|
2012-04-26 17:36:32 +08:00
|
|
|
unsigned int length = width * height;
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2012-04-26 17:36:32 +08:00
|
|
|
if (pixelFormat == kCCTexture2DPixelFormat_RGB565)
|
|
|
|
{
|
2012-04-27 15:53:29 +08:00
|
|
|
if (hasAlpha)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-27 15:53:29 +08:00
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
|
|
|
|
|
|
|
|
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) | // R
|
|
|
|
((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | // G
|
|
|
|
((((*inPixel32 >> 16) & 0xFF) >> 3) << 0); // B
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-27 15:53:29 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB"
|
|
|
|
|
|
|
|
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) | // R
|
|
|
|
(((*inPixel8++ & 0xFF) >> 2) << 5) | // G
|
|
|
|
(((*inPixel8++ & 0xFF) >> 3) << 0); // B
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
else if (pixelFormat == kCCTexture2DPixelFormat_RGBA4444)
|
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA"
|
|
|
|
|
|
|
|
inPixel32 = (unsigned int*)image->getData();
|
|
|
|
tempData = new unsigned char[width * height * 2];
|
2012-04-19 14:35:52 +08:00
|
|
|
outPixel16 = (unsigned short*)tempData;
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for(unsigned int i = 0; i < length; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel16++ =
|
|
|
|
((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | // R
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) | // G
|
2012-04-19 14:35:52 +08:00
|
|
|
((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) | // B
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 24) & 0xFF) >> 4) << 0); // A
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
else if (pixelFormat == kCCTexture2DPixelFormat_RGB5A1)
|
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA"
|
|
|
|
inPixel32 = (unsigned int*)image->getData();
|
|
|
|
tempData = new unsigned char[width * height * 2];
|
2012-04-19 14:35:52 +08:00
|
|
|
outPixel16 = (unsigned short*)tempData;
|
2012-04-26 17:36:32 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for(unsigned int i = 0; i < length; ++i, ++inPixel32)
|
|
|
|
{
|
|
|
|
*outPixel16++ =
|
|
|
|
((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 8) & 0xFF) >> 3) << 6) | // G
|
2012-04-19 14:35:52 +08:00
|
|
|
((((*inPixel32 >> 16) & 0xFF) >> 3) << 1) | // B
|
2012-04-26 17:36:32 +08:00
|
|
|
((((*inPixel32 >> 24) & 0xFF) >> 7) << 0); // A
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (pixelFormat == kCCTexture2DPixelFormat_A8)
|
|
|
|
{
|
2012-04-27 15:53:29 +08:00
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "AAAAAAAA"
|
|
|
|
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; // A
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasAlpha && pixelFormat == kCCTexture2DPixelFormat_RGB888)
|
|
|
|
{
|
|
|
|
// Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRRRRGGGGGGGGBBBBBBBB"
|
|
|
|
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; // R
|
|
|
|
*outPixel8++ = (*inPixel32 >> 8) & 0xFF; // G
|
|
|
|
*outPixel8++ = (*inPixel32 >> 16) & 0xFF; // B
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
|
|
|
initWithData(tempData, pixelFormat, width, height, imageSize);
|
|
|
|
|
|
|
|
if (tempData != image->getData())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-26 17:36:32 +08:00
|
|
|
delete [] tempData;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-26 17:36:32 +08:00
|
|
|
|
|
|
|
m_bHasPremultipliedAlpha = image->isPremultipliedAlpha();
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-07-26 17:32:23 +08:00
|
|
|
}
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
// implementation CCTexture2D (Text)
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCTexture2D::initWithString(const char *text, const char *fontName, float fontSize)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
return initWithString(text, fontName, fontSize, CCSizeMake(0,0), kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop);
|
2010-07-26 17:32:23 +08:00
|
|
|
}
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
bool CCTexture2D::initWithString(const char *text, const char *fontName, float fontSize, const CCSize& dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment)
|
2013-04-26 09:22:26 +08:00
|
|
|
{
|
2013-05-02 08:11:53 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2013-05-07 05:27:54 +08:00
|
|
|
ccFontDefinition tempDef;
|
2013-05-02 08:11:53 +08:00
|
|
|
|
|
|
|
tempDef.m_shadow.m_shadowEnabled = false;
|
|
|
|
tempDef.m_stroke.m_strokeEnabled = false;
|
2013-05-07 05:27:54 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
|
|
|
|
tempDef.m_fontName = std::string(fontName);
|
|
|
|
tempDef.m_fontSize = fontSize;
|
|
|
|
tempDef.m_dimensions = dimensions;
|
|
|
|
tempDef.m_alignment = hAlignment;
|
|
|
|
tempDef.m_vertAlignment = vAlignment;
|
2013-05-07 05:27:54 +08:00
|
|
|
tempDef.m_fontFillColor = ccWHITE;
|
|
|
|
|
2013-05-14 02:35:52 +08:00
|
|
|
return initWithString(text, &tempDef);
|
2013-05-01 07:36:14 +08:00
|
|
|
|
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#else
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// cache the texture data
|
|
|
|
VolatileTexture::addStringTexture(this, text, dimensions, hAlignment, vAlignment, fontName, fontSize);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool bRet = false;
|
|
|
|
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!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CCImage* pImage = new CCImage();
|
|
|
|
CC_BREAK_IF(NULL == pImage);
|
|
|
|
bRet = pImage->initWithString(text, (int)dimensions.width, (int)dimensions.height, eAlign, fontName, (int)fontSize);
|
|
|
|
CC_BREAK_IF(!bRet);
|
|
|
|
bRet = initWithImage(pImage);
|
|
|
|
CC_SAFE_RELEASE(pImage);
|
|
|
|
} while (0);
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
return bRet;
|
2013-04-27 07:34:10 +08:00
|
|
|
|
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2013-04-27 07:34:10 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
|
2013-05-14 02:35:52 +08:00
|
|
|
bool CCTexture2D::initWithString(const char *text, ccFontDefinition *textDefinition)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
2011-01-19 14:23:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// cache the texture data
|
|
|
|
VolatileTexture::addStringTexture(this, text, dimensions, hAlignment, vAlignment, fontName, fontSize);
|
|
|
|
#endif
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
bool bRet = false;
|
|
|
|
CCImage::ETextAlign eAlign;
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-04 04:03:38 +08:00
|
|
|
if (kCCVerticalTextAlignmentTop == textDefinition->m_vertAlignment)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-05-04 04:03:38 +08:00
|
|
|
eAlign = (kCCTextAlignmentCenter == textDefinition->m_alignment) ? CCImage::kAlignTop
|
|
|
|
: (kCCTextAlignmentLeft == textDefinition->m_alignment) ? CCImage::kAlignTopLeft : CCImage::kAlignTopRight;
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
2013-05-04 04:03:38 +08:00
|
|
|
else if (kCCVerticalTextAlignmentCenter == textDefinition->m_vertAlignment)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-05-04 04:03:38 +08:00
|
|
|
eAlign = (kCCTextAlignmentCenter == textDefinition->m_alignment) ? CCImage::kAlignCenter
|
|
|
|
: (kCCTextAlignmentLeft == textDefinition->m_alignment) ? CCImage::kAlignLeft : CCImage::kAlignRight;
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
2013-05-04 04:03:38 +08:00
|
|
|
else if (kCCVerticalTextAlignmentBottom == textDefinition->m_vertAlignment)
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
2013-05-04 04:03:38 +08:00
|
|
|
eAlign = (kCCTextAlignmentCenter == textDefinition->m_alignment) ? CCImage::kAlignBottom
|
|
|
|
: (kCCTextAlignmentLeft == textDefinition->m_alignment) ? CCImage::kAlignBottomLeft : CCImage::kAlignBottomRight;
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCAssert(false, "Not supported alignment format!");
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
// handle shadow parameters
|
|
|
|
bool shadowEnabled = false;
|
|
|
|
float shadowDX = 0.0;
|
|
|
|
float shadowDY = 0.0;
|
|
|
|
float shadowBlur = 0.0;
|
|
|
|
float shadowOpacity = 0.0;
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-04 04:03:38 +08:00
|
|
|
if ( textDefinition->m_shadow.m_shadowEnabled )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
|
|
|
shadowEnabled = true;
|
2013-05-04 04:03:38 +08:00
|
|
|
shadowDX = textDefinition->m_shadow.m_shadowOffset.width;
|
|
|
|
shadowDY = textDefinition->m_shadow.m_shadowOffset.height;
|
|
|
|
shadowBlur = textDefinition->m_shadow.m_shadowBlur;
|
|
|
|
shadowOpacity = textDefinition->m_shadow.m_shadowOpacity;
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle stroke parameters
|
|
|
|
bool strokeEnabled = false;
|
|
|
|
float strokeColorR = 0.0;
|
|
|
|
float strokeColorG = 0.0;
|
|
|
|
float strokeColorB = 0.0;
|
|
|
|
float strokeSize = 0.0;
|
|
|
|
|
2013-05-04 04:03:38 +08:00
|
|
|
if ( textDefinition->m_stroke.m_strokeEnabled )
|
2013-05-02 08:11:53 +08:00
|
|
|
{
|
|
|
|
strokeEnabled = true;
|
2013-05-04 04:03:38 +08:00
|
|
|
strokeColorR = textDefinition->m_stroke.m_strokeColor.r / 255;
|
|
|
|
strokeColorG = textDefinition->m_stroke.m_strokeColor.g / 255;
|
|
|
|
strokeColorB = textDefinition->m_stroke.m_strokeColor.b / 255;
|
|
|
|
strokeSize = textDefinition->m_stroke.m_strokeSize;
|
2013-05-02 08:11:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCImage* pImage = new CCImage();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
CC_BREAK_IF(NULL == pImage);
|
|
|
|
|
|
|
|
bRet = pImage->initWithStringShadowStroke(text,
|
2013-05-04 04:03:38 +08:00
|
|
|
(int)textDefinition->m_dimensions.width,
|
|
|
|
(int)textDefinition->m_dimensions.height,
|
2013-05-02 08:11:53 +08:00
|
|
|
eAlign,
|
2013-05-04 04:03:38 +08:00
|
|
|
textDefinition->m_fontName.c_str(),
|
|
|
|
textDefinition->m_fontSize,
|
2013-05-07 05:27:54 +08:00
|
|
|
textDefinition->m_fontFillColor.r / 255,
|
|
|
|
textDefinition->m_fontFillColor.g / 255,
|
|
|
|
textDefinition->m_fontFillColor.b / 255,
|
2013-05-02 08:11:53 +08:00
|
|
|
shadowEnabled,
|
|
|
|
shadowDX,
|
|
|
|
shadowDY,
|
|
|
|
shadowOpacity,
|
|
|
|
shadowBlur,
|
|
|
|
strokeEnabled,
|
|
|
|
strokeColorR,
|
|
|
|
strokeColorG,
|
|
|
|
strokeColorB,
|
|
|
|
strokeSize);
|
|
|
|
|
|
|
|
|
|
|
|
CC_BREAK_IF(!bRet);
|
|
|
|
bRet = initWithImage(pImage);
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
CC_SAFE_RELEASE(pImage);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:32:16 +08:00
|
|
|
CCAssert(false, "Currently only supported on iOS and Android!");
|
2013-05-16 16:58:35 +08:00
|
|
|
return false;
|
2013-04-26 09:22:26 +08:00
|
|
|
|
2013-05-02 08:11:53 +08:00
|
|
|
#endif
|
2010-08-11 18:09:10 +08:00
|
|
|
}
|
2010-07-15 18:15:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
// implementation CCTexture2D (Drawing)
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCTexture2D::drawAtPoint(const CCPoint& point)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat coordinates[] = {
|
|
|
|
0.0f, m_fMaxT,
|
|
|
|
m_fMaxS,m_fMaxT,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
m_fMaxS,0.0f };
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat width = (GLfloat)m_uPixelsWide * m_fMaxS,
|
|
|
|
height = (GLfloat)m_uPixelsHigh * m_fMaxT;
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat vertices[] = {
|
|
|
|
point.x, point.y,
|
|
|
|
width + point.x, point.y,
|
|
|
|
point.x, height + point.y,
|
|
|
|
width + point.x, height + point.y };
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
|
|
|
|
m_pShaderProgram->use();
|
2012-11-09 12:08:18 +08:00
|
|
|
m_pShaderProgram->setUniformsForBuiltins();
|
2012-03-21 11:07:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLBindTexture2D( m_uName );
|
2012-03-21 11:07:31 +08:00
|
|
|
|
|
|
|
|
2013-04-09 12:08:34 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
setGLBufferData(vertices, 8 * sizeof(GLfloat), 0);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
|
|
|
|
setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
|
2013-04-09 12:08:34 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCTexture2D::drawInRect(const CCRect& rect)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat coordinates[] = {
|
|
|
|
0.0f, m_fMaxT,
|
|
|
|
m_fMaxS,m_fMaxT,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
m_fMaxS,0.0f };
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat vertices[] = { rect.origin.x, rect.origin.y, /*0.0f,*/
|
|
|
|
rect.origin.x + rect.size.width, rect.origin.y, /*0.0f,*/
|
|
|
|
rect.origin.x, rect.origin.y + rect.size.height, /*0.0f,*/
|
|
|
|
rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, /*0.0f*/ };
|
2010-07-15 18:15:00 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
|
|
|
|
m_pShaderProgram->use();
|
2012-11-09 12:08:18 +08:00
|
|
|
m_pShaderProgram->setUniformsForBuiltins();
|
2012-03-21 11:07:31 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLBindTexture2D( m_uName );
|
2012-03-21 11:07:31 +08:00
|
|
|
|
2013-04-09 12:08:34 +08:00
|
|
|
#ifdef EMSCRIPTEN
|
|
|
|
setGLBufferData(vertices, 8 * sizeof(GLfloat), 0);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
|
|
|
|
setGLBufferData(coordinates, 8 * sizeof(GLfloat), 1);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, coordinates);
|
2013-04-09 12:08:34 +08:00
|
|
|
#endif // EMSCRIPTEN
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
bool CCTexture2D::initWithPVRFile(const char* file)
|
2010-07-15 18:15:00 +08:00
|
|
|
{
|
2011-07-19 15:14:59 +08:00
|
|
|
bool bRet = false;
|
|
|
|
// nothing to do with CCObject::init
|
|
|
|
|
|
|
|
CCTexturePVR *pvr = new CCTexturePVR;
|
|
|
|
bRet = pvr->initWithContentsOfFile(file);
|
|
|
|
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
pvr->setRetainName(true); // don't dealloc texture on release
|
|
|
|
|
|
|
|
m_uName = pvr->getName();
|
|
|
|
m_fMaxS = 1.0f;
|
|
|
|
m_fMaxT = 1.0f;
|
|
|
|
m_uPixelsWide = pvr->getWidth();
|
|
|
|
m_uPixelsHigh = pvr->getHeight();
|
2011-12-20 14:59:15 +08:00
|
|
|
m_tContentSize = CCSizeMake((float)m_uPixelsWide, (float)m_uPixelsHigh);
|
|
|
|
m_bHasPremultipliedAlpha = PVRHaveAlphaPremultiplied_;
|
2011-07-19 15:14:59 +08:00
|
|
|
m_ePixelFormat = pvr->getFormat();
|
2012-06-08 14:11:48 +08:00
|
|
|
m_bHasMipmaps = pvr->getNumberOfMipmaps() > 1;
|
|
|
|
|
2011-07-20 15:35:20 +08:00
|
|
|
pvr->release();
|
2011-07-19 15:14:59 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't load PVR image %s", file);
|
|
|
|
}
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
2010-07-21 11:13:32 +08:00
|
|
|
|
2013-05-27 14:42:22 +08:00
|
|
|
bool CCTexture2D::initWithETCFile(const char* file)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
// nothing to do with CCObject::init
|
|
|
|
|
|
|
|
CCTextureETC *etc = new CCTextureETC;
|
|
|
|
bRet = etc->initWithFile(file);
|
|
|
|
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
m_uName = etc->getName();
|
|
|
|
m_fMaxS = 1.0f;
|
|
|
|
m_fMaxT = 1.0f;
|
|
|
|
m_uPixelsWide = etc->getWidth();
|
|
|
|
m_uPixelsHigh = etc->getHeight();
|
|
|
|
m_tContentSize = CCSizeMake((float)m_uPixelsWide, (float)m_uPixelsHigh);
|
|
|
|
m_bHasPremultipliedAlpha = true;
|
|
|
|
|
|
|
|
etc->release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Couldn't load ETC image %s", file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2011-12-20 14:59:15 +08:00
|
|
|
void CCTexture2D::PVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied)
|
2011-07-19 15:14:59 +08:00
|
|
|
{
|
2011-12-20 14:59:15 +08:00
|
|
|
PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2011-07-19 15:14:59 +08:00
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
//
|
|
|
|
// Use to apply MIN/MAG filter
|
|
|
|
//
|
|
|
|
// implementation CCTexture2D (GLFilter)
|
|
|
|
|
|
|
|
void CCTexture2D::generateMipmap()
|
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert( m_uPixelsWide == ccNextPOT(m_uPixelsWide) && m_uPixelsHigh == ccNextPOT(m_uPixelsHigh), "Mipmap texture only works in POT textures");
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLBindTexture2D( m_uName );
|
|
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
2012-06-08 14:11:48 +08:00
|
|
|
m_bHasMipmaps = true;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCTexture2D::hasMipmaps()
|
|
|
|
{
|
|
|
|
return m_bHasMipmaps;
|
|
|
|
}
|
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
void CCTexture2D::setTexParameters(ccTexParams *texParams)
|
|
|
|
{
|
2012-06-13 16:20:58 +08:00
|
|
|
CCAssert( (m_uPixelsWide == ccNextPOT(m_uPixelsWide) || texParams->wrapS == GL_CLAMP_TO_EDGE) &&
|
|
|
|
(m_uPixelsHigh == ccNextPOT(m_uPixelsHigh) || texParams->wrapT == GL_CLAMP_TO_EDGE),
|
2012-06-08 14:11:48 +08:00
|
|
|
"GL_CLAMP_TO_EDGE should be used in NPOT dimensions");
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
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 );
|
2012-08-31 03:02:05 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
VolatileTexture::setTexParameters(this, texParams);
|
|
|
|
#endif
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTexture2D::setAliasTexParameters()
|
|
|
|
{
|
2012-06-13 16:20:58 +08:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
|
2012-08-31 03:02:05 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
ccTexParams texParams = {m_bHasMipmaps?GL_NEAREST_MIPMAP_NEAREST:GL_NEAREST,GL_NEAREST,GL_NONE,GL_NONE};
|
|
|
|
VolatileTexture::setTexParameters(this, &texParams);
|
|
|
|
#endif
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTexture2D::setAntiAliasTexParameters()
|
|
|
|
{
|
2012-06-13 16:20:58 +08:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2012-06-08 14:11:48 +08:00
|
|
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
2012-08-31 03:02:05 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
ccTexParams texParams = {m_bHasMipmaps?GL_LINEAR_MIPMAP_NEAREST:GL_LINEAR,GL_LINEAR,GL_NONE,GL_NONE};
|
|
|
|
VolatileTexture::setTexParameters(this, &texParams);
|
|
|
|
#endif
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
2012-06-13 16:20:58 +08:00
|
|
|
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:
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert(false , "unrecognized pixel format");
|
2012-06-13 16:20:58 +08:00
|
|
|
CCLOG("stringForFormat: %ld, cannot give useful result", (long)m_ePixelFormat);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-06-11 21:58:04 +08:00
|
|
|
|
2010-07-15 18:15:00 +08:00
|
|
|
//
|
|
|
|
// Texture options for images that contains alpha
|
|
|
|
//
|
|
|
|
// implementation CCTexture2D (PixelFormat)
|
|
|
|
|
|
|
|
void CCTexture2D::setDefaultAlphaPixelFormat(CCTexture2DPixelFormat format)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
g_defaultAlphaPixelFormat = format;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCTexture2DPixelFormat CCTexture2D::defaultAlphaPixelFormat()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return g_defaultAlphaPixelFormat;
|
2010-07-15 18:15:00 +08:00
|
|
|
}
|
2010-08-02 10:58:00 +08:00
|
|
|
|
2012-06-13 16:20:58 +08:00
|
|
|
unsigned int CCTexture2D::bitsPerPixelForFormat(CCTexture2DPixelFormat format)
|
|
|
|
{
|
|
|
|
unsigned int ret=0;
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case kCCTexture2DPixelFormat_RGBA8888:
|
|
|
|
ret = 32;
|
|
|
|
break;
|
|
|
|
case kCCTexture2DPixelFormat_RGB888:
|
|
|
|
// It is 32 and not 24, since its internal representation uses 32 bits.
|
|
|
|
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;
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert(false , "unrecognized pixel format");
|
2012-06-13 16:20:58 +08:00
|
|
|
CCLOG("bitsPerPixelForFormat: %ld, cannot give useful result", (long)format);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CCTexture2D::bitsPerPixelForFormat()
|
|
|
|
{
|
|
|
|
return this->bitsPerPixelForFormat(m_ePixelFormat);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2011-07-05 10:47:25 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|