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"
|
2013-02-28 11:55:36 +08:00
|
|
|
#include "textures/CCTextureCache.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()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _itemsPerRow(0)
|
|
|
|
, _itemsPerColumn(0)
|
|
|
|
, _itemWidth(0)
|
|
|
|
, _itemHeight(0)
|
|
|
|
, _textureAtlas(NULL)
|
|
|
|
, _isOpacityModifyRGB(false)
|
|
|
|
, _quadsToDraw(0)
|
|
|
|
, _uniformColor(0)
|
|
|
|
, _ignoreContentScaleFactor(false)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCAtlasNode::~CCAtlasNode()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-27 09:38:30 +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
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(tile != NULL, "title should not be null");
|
2013-02-27 09:38:30 +08:00
|
|
|
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(tile);
|
|
|
|
return initWithTexture(texture, tileWidth, tileHeight, itemsToRender);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCAtlasNode::initWithTexture(CCTexture2D* texture, unsigned int tileWidth, unsigned int tileHeight,
|
|
|
|
unsigned int itemsToRender)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_itemWidth = tileWidth;
|
|
|
|
_itemHeight = tileHeight;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorUnmodified = ccWHITE;
|
|
|
|
_isOpacityModifyRGB = true;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc.src = CC_BLEND_SRC;
|
|
|
|
_blendFunc.dst = CC_BLEND_DST;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas = new CCTextureAtlas();
|
|
|
|
_textureAtlas->initWithTexture(texture, itemsToRender);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _textureAtlas)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_quadsToDraw = 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));
|
2013-06-15 14:03:30 +08:00
|
|
|
_uniformColor = 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()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CCSize s = _textureAtlas->getTexture()->getContentSize();
|
2013-05-23 16:12:04 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_ignoreContentScaleFactor)
|
2013-05-23 15:06:57 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
s = _textureAtlas->getTexture()->getContentSizeInPixels();
|
2013-05-23 15:06:57 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_itemsPerColumn = (int)(s.height / _itemHeight);
|
|
|
|
_itemsPerRow = (int)(s.width / _itemWidth);
|
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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccGLBlendFunc( _blendFunc.src, _blendFunc.dst );
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2013-02-28 11:55:36 +08:00
|
|
|
GLfloat colors[4] = {_displayedColor.r / 255.0f, _displayedColor.g / 255.0f, _displayedColor.b / 255.0f, _displayedOpacity / 255.0f};
|
2013-06-15 14:03:30 +08:00
|
|
|
getShaderProgram()->setUniformLocationWith4fv(_uniformColor, colors, 1);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->drawNumberOfQuads(_quadsToDraw, 0);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CCAtlasNode - RGBA protocol
|
|
|
|
|
2013-02-28 11:55:36 +08:00
|
|
|
const ccColor3B& CCAtlasNode::getColor()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_isOpacityModifyRGB)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _colorUnmodified;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-02-27 09:38:30 +08:00
|
|
|
return CCNodeRGBA::getColor();
|
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
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
ccColor3B tmp = color3;
|
2013-06-15 14:03:30 +08:00
|
|
|
_colorUnmodified = color3;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _isOpacityModifyRGB )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-02-28 11:55:36 +08:00
|
|
|
tmp.r = tmp.r * _displayedOpacity/255;
|
|
|
|
tmp.g = tmp.g * _displayedOpacity/255;
|
|
|
|
tmp.b = tmp.b * _displayedOpacity/255;
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
CCNodeRGBA::setColor(tmp);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2010-07-08 10:26:58 +08:00
|
|
|
|
|
|
|
void CCAtlasNode::setOpacity(GLubyte opacity)
|
|
|
|
{
|
2013-02-27 09:38:30 +08:00
|
|
|
CCNodeRGBA::setOpacity(opacity);
|
2010-07-08 10:26:58 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// special opacity for premultiplied textures
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _isOpacityModifyRGB )
|
|
|
|
this->setColor(_colorUnmodified);
|
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
|
|
|
{
|
2013-01-18 16:40:42 +08:00
|
|
|
ccColor3B oldColor = this->getColor();
|
2013-06-15 14:03:30 +08:00
|
|
|
_isOpacityModifyRGB = bValue;
|
2013-01-18 16:40:42 +08:00
|
|
|
this->setColor(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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _isOpacityModifyRGB;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::updateOpacityModifyRGB()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isOpacityModifyRGB = _textureAtlas->getTexture()->hasPremultipliedAlpha();
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
void CCAtlasNode::setIgnoreContentScaleFactor(bool bIgnoreContentScaleFactor)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ignoreContentScaleFactor = bIgnoreContentScaleFactor;
|
2013-05-23 15:06:57 +08:00
|
|
|
}
|
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
// CCAtlasNode - CocosNodeTexture protocol
|
|
|
|
|
2010-07-20 13:49:13 +08:00
|
|
|
ccBlendFunc CCAtlasNode::getBlendFunc()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setBlendFunc(ccBlendFunc blendFunc)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc = blendFunc;
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
void CCAtlasNode::updateBlendFunc()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! _textureAtlas->getTexture()->hasPremultipliedAlpha() ) {
|
|
|
|
_blendFunc.src = GL_SRC_ALPHA;
|
|
|
|
_blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setTexture(CCTexture2D *texture)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->setTexture(texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _textureAtlas->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);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
|
|
|
_textureAtlas = 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()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _textureAtlas;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
|
|
|
|
2011-07-12 11:20:41 +08:00
|
|
|
unsigned int CCAtlasNode::getQuadsToDraw()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _quadsToDraw;
|
2011-07-12 11:20:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCAtlasNode::setQuadsToDraw(unsigned int uQuadsToDraw)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_quadsToDraw = uQuadsToDraw;
|
2011-07-12 11:20:41 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|