axmol/cocos/2d/CCAtlasNode.cpp

260 lines
6.5 KiB
C++
Raw Normal View History

2011-03-19 10:34:26 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
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"
#include "2d/CCTextureAtlas.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCTextureCache.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"
#include "renderer/CCRenderer.h"
#include "renderer/CCGLProgram.h"
2014-05-10 09:39:25 +08:00
#include "renderer/CCGLProgramState.h"
#include "renderer/ccGLStateCache.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
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
// implementation AtlasNode
// AtlasNode - Creation & Init
AtlasNode::AtlasNode()
: _itemsPerRow(0)
, _itemsPerColumn(0)
, _itemWidth(0)
, _itemHeight(0)
, _textureAtlas(nullptr)
, _isOpacityModifyRGB(false)
, _quadsToDraw(0)
, _uniformColor(0)
, _ignoreContentScaleFactor(false)
{
}
AtlasNode::~AtlasNode()
{
CC_SAFE_RELEASE(_textureAtlas);
}
2013-12-05 17:19:01 +08:00
AtlasNode * AtlasNode::create(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
{
AtlasNode * ret = new AtlasNode();
if (ret->initWithTileFile(tile, tileWidth, tileHeight, itemsToRender))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
2013-12-05 17:19:01 +08:00
bool AtlasNode::initWithTileFile(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
{
CCASSERT(tile.size() > 0, "file size should not be empty");
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
{
_itemWidth = tileWidth;
_itemHeight = tileHeight;
_colorUnmodified = Color3B::WHITE;
_isOpacityModifyRGB = true;
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
_textureAtlas = new TextureAtlas();
_textureAtlas->initWithTexture(texture, itemsToRender);
2012-04-04 21:58:04 +08:00
if (! _textureAtlas)
{
CCLOG("cocos2d: Could not initialize AtlasNode. Invalid Texture.");
return false;
}
this->updateBlendFunc();
this->updateOpacityModifyRGB();
this->calculateMaxItems();
_quadsToDraw = itemsToRender;
2011-07-12 11:20:41 +08:00
// shader stuff
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
return true;
}
// AtlasNode - Atlas generation
void AtlasNode::calculateMaxItems()
{
Size s = _textureAtlas->getTexture()->getContentSize();
if (_ignoreContentScaleFactor)
{
s = _textureAtlas->getTexture()->getContentSizeInPixels();
}
_itemsPerColumn = (int)(s.height / _itemHeight);
_itemsPerRow = (int)(s.width / _itemWidth);
}
void AtlasNode::updateAtlasValues()
2010-07-08 10:26:58 +08:00
{
CCASSERT(false, "CCAtlasNode:Abstract updateAtlasValue not overridden");
}
// AtlasNode - draw
2014-04-08 22:07:35 +08:00
void AtlasNode::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
{
_quadCommand.init(
_globalZOrder,
_textureAtlas->getTexture()->getName(),
getGLProgramState(),
_blendFunc,
_textureAtlas->getQuads(),
_quadsToDraw,
transform);
renderer->addCommand(&_quadCommand);
2012-04-04 21:58:04 +08:00
}
// AtlasNode - RGBA protocol
2013-07-05 17:32:50 +08:00
const Color3B& AtlasNode::getColor() const
{
if(_isOpacityModifyRGB)
{
return _colorUnmodified;
}
return Node::getColor();
2010-07-08 10:26:58 +08:00
}
void AtlasNode::setColor(const Color3B& color3)
2010-07-08 10:26:58 +08:00
{
Color3B tmp = color3;
_colorUnmodified = color3;
if( _isOpacityModifyRGB )
{
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
}
Node::setColor(tmp);
}
2010-07-08 10:26:58 +08:00
void AtlasNode::setOpacity(GLubyte opacity)
2010-07-08 10:26:58 +08:00
{
Node::setOpacity(opacity);
2010-07-08 10:26:58 +08:00
// special opacity for premultiplied textures
if( _isOpacityModifyRGB )
this->setColor(_colorUnmodified);
}
void AtlasNode::setOpacityModifyRGB(bool value)
{
Color3B oldColor = this->getColor();
_isOpacityModifyRGB = value;
this->setColor(oldColor);
}
bool AtlasNode::isOpacityModifyRGB() const
{
return _isOpacityModifyRGB;
}
void AtlasNode::updateOpacityModifyRGB()
{
_isOpacityModifyRGB = _textureAtlas->getTexture()->hasPremultipliedAlpha();
}
void AtlasNode::setIgnoreContentScaleFactor(bool ignoreContentScaleFactor)
{
_ignoreContentScaleFactor = ignoreContentScaleFactor;
}
// AtlasNode - CocosNodeTexture protocol
const BlendFunc& AtlasNode::getBlendFunc() const
{
return _blendFunc;
}
void AtlasNode::setBlendFunc(const BlendFunc &blendFunc)
{
_blendFunc = blendFunc;
}
void AtlasNode::updateBlendFunc()
{
if( ! _textureAtlas->getTexture()->hasPremultipliedAlpha() )
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
}
void AtlasNode::setTexture(Texture2D *texture)
{
_textureAtlas->setTexture(texture);
this->updateBlendFunc();
this->updateOpacityModifyRGB();
}
Texture2D * AtlasNode::getTexture() const
{
return _textureAtlas->getTexture();
2011-03-19 10:34:26 +08:00
}
void AtlasNode::setTextureAtlas(TextureAtlas* textureAtlas)
2011-03-19 10:34:26 +08:00
{
CC_SAFE_RETAIN(textureAtlas);
CC_SAFE_RELEASE(_textureAtlas);
_textureAtlas = textureAtlas;
2011-03-19 10:34:26 +08:00
}
2012-04-04 21:58:04 +08:00
TextureAtlas * AtlasNode::getTextureAtlas() const
2011-03-19 10:34:26 +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
{
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
{
_quadsToDraw = quadsToDraw;
2011-07-12 11:20:41 +08:00
}
2012-04-18 18:43:45 +08:00
NS_CC_END