2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 10:34:26 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2011-07-01 15:48:05 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2011-03-19 10:34:26 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCAtlasNode.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTextureAtlas.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/CCShaderCache.h"
|
|
|
|
#include "shaders/ccGLStateCache.h"
|
2012-04-04 21:58:04 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "support/TransformUtils.h"
|
|
|
|
|
|
|
|
// external
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "kazmath/GL/matrix.h"
|
2011-03-19 10:34:26 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2011-03-19 10:34:26 +08:00
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
// implementation CCAtlasNode
|
|
|
|
|
|
|
|
// CCAtlasNode - Creation & Init
|
|
|
|
|
|
|
|
CCAtlasNode::CCAtlasNode()
|
2011-07-01 15:48:05 +08:00
|
|
|
: m_uItemsPerRow(0)
|
|
|
|
, m_uItemsPerColumn(0)
|
|
|
|
, m_uItemWidth(0)
|
|
|
|
, m_uItemHeight(0)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_pTextureAtlas(NULL)
|
|
|
|
, m_bIsOpacityModifyRGB(false)
|
2011-04-01 16:06:53 +08:00
|
|
|
, m_cOpacity(0)
|
2011-07-12 11:20:41 +08:00
|
|
|
, m_uQuadsToDraw(0)
|
2012-03-14 14:55:17 +08:00
|
|
|
, m_nUniformColor(0)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAtlasNode::~CCAtlasNode()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_pTextureAtlas);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCAtlasNode * CCAtlasNode::atlasWithTileFile(const char *tile, unsigned int tileWidth, unsigned int tileHeight,
|
|
|
|
unsigned int itemsToRender)
|
|
|
|
{
|
|
|
|
return CCAtlasNode::create(tile, tileWidth, tileHeight, itemsToRender);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCAtlasNode * CCAtlasNode::create(const char *tile, unsigned int tileWidth, unsigned int tileHeight,
|
|
|
|
unsigned int itemsToRender)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCAtlasNode * pRet = new CCAtlasNode();
|
|
|
|
if (pRet->initWithTileFile(tile, tileWidth, tileHeight, itemsToRender))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 15:48:05 +08:00
|
|
|
bool CCAtlasNode::initWithTileFile(const char *tile, unsigned int tileWidth, unsigned int tileHeight,
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int itemsToRender)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(tile != NULL, "title should not be null");
|
|
|
|
m_uItemWidth = tileWidth;
|
|
|
|
m_uItemHeight = tileHeight;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_cOpacity = 255;
|
|
|
|
m_tColor = m_tColorUnmodified = ccWHITE;
|
|
|
|
m_bIsOpacityModifyRGB = true;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tBlendFunc.src = CC_BLEND_SRC;
|
|
|
|
m_tBlendFunc.dst = CC_BLEND_DST;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTextureAtlas* pNewAtlas= new CCTextureAtlas();
|
|
|
|
pNewAtlas->initWithFile(tile, itemsToRender);
|
2012-04-04 21:58:04 +08:00
|
|
|
setTextureAtlas(pNewAtlas);
|
|
|
|
pNewAtlas->release();
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! m_pTextureAtlas)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Could not initialize CCAtlasNode. Invalid Texture.");
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->updateBlendFunc();
|
|
|
|
this->updateOpacityModifyRGB();
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->calculateMaxItems();
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uQuadsToDraw = itemsToRender;
|
2011-07-12 11:20:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// shader stuff
|
|
|
|
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture_uColor));
|
|
|
|
m_nUniformColor = glGetUniformLocation( getShaderProgram()->getProgram(), "u_color");
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CCAtlasNode - Atlas generation
|
|
|
|
|
|
|
|
void CCAtlasNode::calculateMaxItems()
|
|
|
|
{
|
2012-11-20 16:34:55 +08:00
|
|
|
CCSize s = m_pTextureAtlas->getTexture()->getContentSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uItemsPerColumn = (int)(s.height / m_uItemHeight);
|
|
|
|
m_uItemsPerRow = (int)(s.width / m_uItemWidth);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::updateAtlasValues()
|
2010-07-08 10:26:58 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert(false, "CCAtlasNode:Abstract updateAtlasValue not overridden");
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CCAtlasNode - draw
|
2012-03-20 23:30:18 +08:00
|
|
|
void CCAtlasNode::draw(void)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_NODE_DRAW_SETUP();
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
GLfloat colors[4] = {m_tColor.r / 255.0f, m_tColor.g / 255.0f, m_tColor.b / 255.0f, m_cOpacity / 255.0f};
|
|
|
|
getShaderProgram()->setUniformLocationWith4fv(m_nUniformColor, colors, 1);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTextureAtlas->drawNumberOfQuads(m_uQuadsToDraw, 0);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CCAtlasNode - RGBA protocol
|
|
|
|
|
2012-11-20 16:34:55 +08:00
|
|
|
ccColor3B CCAtlasNode:: getColor()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if(m_bIsOpacityModifyRGB)
|
|
|
|
{
|
|
|
|
return m_tColorUnmodified;
|
|
|
|
}
|
|
|
|
return m_tColor;
|
2010-07-08 10:26:58 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCAtlasNode::setColor(const ccColor3B& color3)
|
2010-07-08 10:26:58 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tColor = m_tColorUnmodified = color3;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( m_bIsOpacityModifyRGB )
|
|
|
|
{
|
|
|
|
m_tColor.r = color3.r * m_cOpacity/255;
|
|
|
|
m_tColor.g = color3.g * m_cOpacity/255;
|
|
|
|
m_tColor.b = color3.b * m_cOpacity/255;
|
|
|
|
}
|
2010-07-08 10:26:58 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
GLubyte CCAtlasNode::getOpacity()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_cOpacity;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2010-07-08 10:26:58 +08:00
|
|
|
|
|
|
|
void CCAtlasNode::setOpacity(GLubyte opacity)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_cOpacity = opacity;
|
2010-07-08 10:26:58 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// special opacity for premultiplied textures
|
|
|
|
if( m_bIsOpacityModifyRGB )
|
|
|
|
this->setColor(m_tColorUnmodified);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
void CCAtlasNode::setOpacityModifyRGB(bool bValue)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
ccColor3B oldColor = this->m_tColor;
|
|
|
|
m_bIsOpacityModifyRGB = bValue;
|
|
|
|
this->m_tColor = oldColor;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCAtlasNode::isOpacityModifyRGB()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_bIsOpacityModifyRGB;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::updateOpacityModifyRGB()
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
m_bIsOpacityModifyRGB = m_pTextureAtlas->getTexture()->hasPremultipliedAlpha();
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CCAtlasNode - CocosNodeTexture protocol
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
ccBlendFunc CCAtlasNode::getBlendFunc()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tBlendFunc;
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setBlendFunc(ccBlendFunc blendFunc)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tBlendFunc = blendFunc;
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
void CCAtlasNode::updateBlendFunc()
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
if( ! m_pTextureAtlas->getTexture()->hasPremultipliedAlpha() ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tBlendFunc.src = GL_SRC_ALPHA;
|
|
|
|
m_tBlendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
|
|
|
}
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setTexture(CCTexture2D *texture)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTextureAtlas->setTexture(texture);
|
|
|
|
this->updateBlendFunc();
|
|
|
|
this->updateOpacityModifyRGB();
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
CCTexture2D * CCAtlasNode::getTexture()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pTextureAtlas->getTexture();
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setTextureAtlas(CCTextureAtlas* var)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RETAIN(var);
|
|
|
|
CC_SAFE_RELEASE(m_pTextureAtlas);
|
|
|
|
m_pTextureAtlas = var;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2011-03-19 10:34:26 +08:00
|
|
|
CCTextureAtlas * CCAtlasNode::getTextureAtlas()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pTextureAtlas;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
|
|
|
|
2011-07-12 11:20:41 +08:00
|
|
|
unsigned int CCAtlasNode::getQuadsToDraw()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_uQuadsToDraw;
|
2011-07-12 11:20:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setQuadsToDraw(unsigned int uQuadsToDraw)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_uQuadsToDraw = uQuadsToDraw;
|
2011-07-12 11:20:41 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|