2011-03-19 10:34:26 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-01 15:48:05 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
CopyRight (c) 2013-2014 Chukong Technologies 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"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureAtlas.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "math/TransformUtils.h"
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2011-03-19 10:34:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation AtlasNode
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// AtlasNode - Creation & Init
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
AtlasNode::AtlasNode()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _itemsPerRow(0)
|
|
|
|
, _itemsPerColumn(0)
|
|
|
|
, _itemWidth(0)
|
|
|
|
, _itemHeight(0)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _textureAtlas(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _isOpacityModifyRGB(false)
|
|
|
|
, _quadsToDraw(0)
|
|
|
|
, _uniformColor(0)
|
|
|
|
, _ignoreContentScaleFactor(false)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
AtlasNode::~AtlasNode()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
AtlasNode * AtlasNode::create(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
AtlasNode * ret = new AtlasNode();
|
|
|
|
if (ret->initWithTileFile(tile, tileWidth, tileHeight, itemsToRender))
|
2012-06-14 15:13:16 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-06-14 15:13:16 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
bool AtlasNode::initWithTileFile(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
CCASSERT(tile.size() > 0, "file size should not be empty");
|
2013-11-07 19:11:09 +08:00
|
|
|
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(tile);
|
2013-02-27 09:38:30 +08:00
|
|
|
return initWithTexture(texture, tileWidth, tileHeight, itemsToRender);
|
|
|
|
}
|
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeight, int itemsToRender)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_itemWidth = tileWidth;
|
|
|
|
_itemHeight = tileHeight;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-07-08 18:11:32 +08:00
|
|
|
_colorUnmodified = Color3B::WHITE;
|
2013-06-15 14:03:30 +08:00
|
|
|
_isOpacityModifyRGB = true;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-07-26 08:47:42 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_textureAtlas = new TextureAtlas();
|
2013-06-15 14:03:30 +08:00
|
|
|
_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
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: Could not initialize AtlasNode. Invalid Texture.");
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
2014-05-13 10:12:56 +08:00
|
|
|
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// AtlasNode - Atlas generation
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void AtlasNode::calculateMaxItems()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Size 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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void AtlasNode::updateAtlasValues()
|
2010-07-08 10:26:58 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(false, "CCAtlasNode:Abstract updateAtlasValue not overridden");
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// AtlasNode - draw
|
2014-05-31 07:42:05 +08:00
|
|
|
void AtlasNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2014-01-18 08:08:29 +08:00
|
|
|
_quadCommand.init(
|
2014-01-19 03:35:27 +08:00
|
|
|
_globalZOrder,
|
2013-12-19 03:43:45 +08:00
|
|
|
_textureAtlas->getTexture()->getName(),
|
2014-05-13 10:12:56 +08:00
|
|
|
getGLProgramState(),
|
2013-12-19 03:43:45 +08:00
|
|
|
_blendFunc,
|
|
|
|
_textureAtlas->getQuads(),
|
2014-01-15 16:35:08 +08:00
|
|
|
_quadsToDraw,
|
2014-03-01 08:10:48 +08:00
|
|
|
transform);
|
2013-12-19 03:43:45 +08:00
|
|
|
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_quadCommand);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// AtlasNode - RGBA protocol
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-07-05 17:32:50 +08:00
|
|
|
const Color3B& AtlasNode::getColor() const
|
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-12-06 18:07:16 +08:00
|
|
|
return Node::getColor();
|
2010-07-08 10:26:58 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void AtlasNode::setColor(const Color3B& color3)
|
2010-07-08 10:26:58 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B 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
|
|
|
}
|
2013-12-06 18:07:16 +08:00
|
|
|
Node::setColor(tmp);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2010-07-08 10:26:58 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void AtlasNode::setOpacity(GLubyte opacity)
|
2010-07-08 10:26:58 +08:00
|
|
|
{
|
2013-12-06 18:07:16 +08:00
|
|
|
Node::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
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void AtlasNode::setOpacityModifyRGB(bool value)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B oldColor = this->getColor();
|
2013-12-18 17:47:20 +08:00
|
|
|
_isOpacityModifyRGB = value;
|
2013-01-18 16:40:42 +08:00
|
|
|
this->setColor(oldColor);
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 15:06:38 +08:00
|
|
|
bool AtlasNode::isOpacityModifyRGB() const
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void AtlasNode::updateOpacityModifyRGB()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isOpacityModifyRGB = _textureAtlas->getTexture()->hasPremultipliedAlpha();
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void AtlasNode::setIgnoreContentScaleFactor(bool ignoreContentScaleFactor)
|
2013-05-23 15:06:57 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_ignoreContentScaleFactor = ignoreContentScaleFactor;
|
2013-05-23 15:06:57 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// AtlasNode - CocosNodeTexture protocol
|
2010-07-19 16:45:53 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const BlendFunc& AtlasNode::getBlendFunc() const
|
2010-07-20 13:49:13 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void AtlasNode::setBlendFunc(const BlendFunc &blendFunc)
|
2010-07-20 13:49:13 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc = blendFunc;
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void AtlasNode::updateBlendFunc()
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
if( ! _textureAtlas->getTexture()->hasPremultipliedAlpha() )
|
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void AtlasNode::setTexture(Texture2D *texture)
|
2010-07-19 16:45:53 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
Texture2D * AtlasNode::getTexture() const
|
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
|
|
|
}
|
|
|
|
|
2013-07-23 15:19:49 +08:00
|
|
|
void AtlasNode::setTextureAtlas(TextureAtlas* textureAtlas)
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2013-07-23 15:19:49 +08:00
|
|
|
CC_SAFE_RETAIN(textureAtlas);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
2013-07-23 15:19:49 +08:00
|
|
|
_textureAtlas = textureAtlas;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2013-07-23 15:19:49 +08:00
|
|
|
TextureAtlas * AtlasNode::getTextureAtlas() const
|
2011-03-19 10:34:26 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _textureAtlas;
|
2011-03-19 10:34:26 +08:00
|
|
|
}
|
|
|
|
|
2013-12-27 15:06:16 +08:00
|
|
|
ssize_t AtlasNode::getQuadsToDraw() const
|
2011-07-12 11:20:41 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _quadsToDraw;
|
2011-07-12 11:20:41 +08:00
|
|
|
}
|
|
|
|
|
2013-12-27 15:06:16 +08:00
|
|
|
void AtlasNode::setQuadsToDraw(ssize_t quadsToDraw)
|
2011-07-12 11:20:41 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_quadsToDraw = quadsToDraw;
|
2011-07-12 11:20:41 +08:00
|
|
|
}
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|