2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
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"
|
|
|
|
#include "CCTextureAtlas.h"
|
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
|
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)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAtlasNode::~CCAtlasNode()
|
|
|
|
{
|
2011-04-01 16:06:53 +08:00
|
|
|
CC_SAFE_RELEASE(m_pTextureAtlas);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 15:48:05 +08:00
|
|
|
CCAtlasNode * CCAtlasNode::atlasWithTileFile(const char *tile, unsigned int tileWidth, unsigned int tileHeight,
|
|
|
|
unsigned int itemsToRender)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
CCAtlasNode * pRet = new CCAtlasNode();
|
|
|
|
if (pRet->initWithTileFile(tile, tileWidth, tileHeight, itemsToRender))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
2010-09-04 12:02:52 +08:00
|
|
|
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,
|
|
|
|
unsigned int itemsToRender)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
assert(tile != NULL);
|
2011-07-01 15:48:05 +08:00
|
|
|
m_uItemWidth = (int) (tileWidth * CC_CONTENT_SCALE_FACTOR());
|
|
|
|
m_uItemHeight = (int) (tileHeight * CC_CONTENT_SCALE_FACTOR());
|
2010-07-19 16:45:53 +08:00
|
|
|
|
|
|
|
m_cOpacity = 255;
|
|
|
|
m_tColor = m_tColorUnmodified = ccWHITE;
|
2010-07-20 13:49:13 +08:00
|
|
|
m_bIsOpacityModifyRGB = true;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
|
|
|
m_tBlendFunc.src = CC_BLEND_SRC;
|
|
|
|
m_tBlendFunc.dst = CC_BLEND_DST;
|
|
|
|
|
|
|
|
// double retain to avoid the autorelease pool
|
|
|
|
// also, using: self.textureAtlas supports re-initialization without leaking
|
|
|
|
this->m_pTextureAtlas = new CCTextureAtlas();
|
|
|
|
m_pTextureAtlas->initWithFile(tile, itemsToRender);
|
2010-12-23 11:19:56 +08:00
|
|
|
|
|
|
|
if (! m_pTextureAtlas)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: Could not initialize CCAtlasNode. Invalid Texture.");
|
|
|
|
delete this;
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-19 16:45:53 +08:00
|
|
|
|
|
|
|
this->updateBlendFunc();
|
|
|
|
this->updateOpacityModifyRGB();
|
|
|
|
|
|
|
|
this->calculateMaxItems();
|
|
|
|
|
2011-07-12 11:20:41 +08:00
|
|
|
m_uQuadsToDraw = itemsToRender;
|
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CCAtlasNode - Atlas generation
|
|
|
|
|
|
|
|
void CCAtlasNode::calculateMaxItems()
|
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCSize s = m_pTextureAtlas->getTexture()->getContentSizeInPixels();
|
2011-07-01 15:48:05 +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
|
|
|
{
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAssert(false, "CCAtlasNode:Abstract updateAtlasValue not overriden");
|
2010-07-19 16:45:53 +08:00
|
|
|
//[NSException raise:@"CCAtlasNode:Abstract" format:@"updateAtlasValue not overriden"];
|
|
|
|
}
|
|
|
|
|
|
|
|
// CCAtlasNode - draw
|
|
|
|
void CCAtlasNode::draw()
|
|
|
|
{
|
2011-07-12 11:20:41 +08:00
|
|
|
CCNode::draw();
|
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
|
|
|
|
// Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_TEXTURE_COORD_ARRAY
|
|
|
|
// Unneeded states: GL_COLOR_ARRAY
|
|
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
|
|
|
|
2011-03-25 13:50:57 +08:00
|
|
|
// glColor4ub isn't implement on some android devices
|
|
|
|
// glColor4ub( m_tColor.r, m_tColor.g, m_tColor.b, m_cOpacity);
|
|
|
|
glColor4f(((GLfloat)m_tColor.r) / 255, ((GLfloat)m_tColor.g) / 255, ((GLfloat)m_tColor.b) / 255, ((GLfloat)m_cOpacity) / 255);
|
2010-12-23 11:19:56 +08:00
|
|
|
bool newBlend = m_tBlendFunc.src != CC_BLEND_SRC || m_tBlendFunc.dst != CC_BLEND_DST;
|
|
|
|
if(newBlend)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
glBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );
|
|
|
|
}
|
|
|
|
|
2011-07-12 11:20:41 +08:00
|
|
|
m_pTextureAtlas->drawNumberOfQuads(m_uQuadsToDraw, 0);
|
2010-07-19 16:45:53 +08:00
|
|
|
|
|
|
|
if( newBlend )
|
|
|
|
glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
|
|
|
|
|
|
|
|
// is this chepear than saving/restoring color state ?
|
|
|
|
// XXX: There is no need to restore the color to (255,255,255,255). Objects should use the color
|
|
|
|
// XXX: that they need
|
|
|
|
// glColor4ub( 255, 255, 255, 255);
|
|
|
|
|
|
|
|
// restore default GL state
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// CCAtlasNode - RGBA protocol
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
ccColor3B CCAtlasNode:: getColor()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2010-07-20 13:49:13 +08:00
|
|
|
if(m_bIsOpacityModifyRGB)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
return m_tColorUnmodified;
|
|
|
|
}
|
2010-07-08 10:26:58 +08:00
|
|
|
return m_tColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setColor(ccColor3B color3)
|
|
|
|
{
|
2010-07-19 16:45:53 +08:00
|
|
|
m_tColor = m_tColorUnmodified = color3;
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
if( m_bIsOpacityModifyRGB )
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
return m_cOpacity;
|
|
|
|
}
|
2010-07-08 10:26:58 +08:00
|
|
|
|
|
|
|
void CCAtlasNode::setOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
m_cOpacity = opacity;
|
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
// special opacity for premultiplied textures
|
2010-07-20 13:49:13 +08:00
|
|
|
if( m_bIsOpacityModifyRGB )
|
2010-12-23 11:19:56 +08:00
|
|
|
this->setColor(m_tColorUnmodified);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
void CCAtlasNode::setIsOpacityModifyRGB(bool bValue)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
ccColor3B oldColor = this->m_tColor;
|
2010-07-20 13:49:13 +08:00
|
|
|
m_bIsOpacityModifyRGB = bValue;
|
2010-07-19 16:45:53 +08:00
|
|
|
this->m_tColor = oldColor;
|
|
|
|
}
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
bool CCAtlasNode::getIsOpacityModifyRGB()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2010-07-20 13:49:13 +08:00
|
|
|
return m_bIsOpacityModifyRGB;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::updateOpacityModifyRGB()
|
|
|
|
{
|
2010-07-20 13:49:13 +08:00
|
|
|
m_bIsOpacityModifyRGB = m_pTextureAtlas->getTexture()->getHasPremultipliedAlpha();
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CCAtlasNode - CocosNodeTexture protocol
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
ccBlendFunc CCAtlasNode::getBlendFunc()
|
|
|
|
{
|
|
|
|
return m_tBlendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setBlendFunc(ccBlendFunc blendFunc)
|
|
|
|
{
|
|
|
|
m_tBlendFunc = blendFunc;
|
|
|
|
}
|
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
void CCAtlasNode::updateBlendFunc()
|
|
|
|
{
|
|
|
|
if( ! m_pTextureAtlas->getTexture()->getHasPremultipliedAlpha() ) {
|
|
|
|
m_tBlendFunc.src = GL_SRC_ALPHA;
|
|
|
|
m_tBlendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setTexture(CCTexture2D *texture)
|
|
|
|
{
|
|
|
|
m_pTextureAtlas->setTexture(texture);
|
|
|
|
this->updateBlendFunc();
|
|
|
|
this->updateOpacityModifyRGB();
|
|
|
|
}
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
CCTexture2D * CCAtlasNode::getTexture()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
return m_pTextureAtlas->getTexture();
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setTextureAtlas(CCTextureAtlas* var)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(var);
|
|
|
|
CC_SAFE_RELEASE(m_pTextureAtlas);
|
|
|
|
m_pTextureAtlas = var;
|
|
|
|
}
|
|
|
|
CCTextureAtlas * CCAtlasNode::getTextureAtlas()
|
|
|
|
{
|
|
|
|
return m_pTextureAtlas;
|
|
|
|
}
|
|
|
|
|
2011-07-12 11:20:41 +08:00
|
|
|
unsigned int CCAtlasNode::getQuadsToDraw()
|
|
|
|
{
|
|
|
|
return m_uQuadsToDraw;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setQuadsToDraw(unsigned int uQuadsToDraw)
|
|
|
|
{
|
|
|
|
m_uQuadsToDraw = uQuadsToDraw;
|
|
|
|
}
|
|
|
|
|
2011-03-19 10:34:26 +08:00
|
|
|
} // namespace cocos2d
|