CCAtlasNode.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCAtlasNode.h"
  25. #include "renderer/CCTextureAtlas.h"
  26. #include "base/CCDirector.h"
  27. #include "renderer/CCTextureCache.h"
  28. #include "renderer/CCRenderer.h"
  29. #include "renderer/CCGLProgram.h"
  30. NS_CC_BEGIN
  31. // implementation AtlasNode
  32. // AtlasNode - Creation & Init
  33. AtlasNode::AtlasNode()
  34. : _itemsPerRow(0)
  35. , _itemsPerColumn(0)
  36. , _itemWidth(0)
  37. , _itemHeight(0)
  38. , _textureAtlas(nullptr)
  39. , _isOpacityModifyRGB(false)
  40. , _quadsToDraw(0)
  41. , _uniformColor(0)
  42. , _ignoreContentScaleFactor(false)
  43. {
  44. }
  45. AtlasNode::~AtlasNode()
  46. {
  47. CC_SAFE_RELEASE(_textureAtlas);
  48. }
  49. AtlasNode * AtlasNode::create(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
  50. {
  51. AtlasNode * ret = new (std::nothrow) AtlasNode();
  52. if (ret->initWithTileFile(tile, tileWidth, tileHeight, itemsToRender))
  53. {
  54. ret->autorelease();
  55. return ret;
  56. }
  57. CC_SAFE_DELETE(ret);
  58. return nullptr;
  59. }
  60. bool AtlasNode::initWithTileFile(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender)
  61. {
  62. CCASSERT(tile.size() > 0, "file size should not be empty");
  63. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(tile);
  64. return initWithTexture(texture, tileWidth, tileHeight, itemsToRender);
  65. }
  66. bool AtlasNode::initWithTexture(Texture2D* texture, int tileWidth, int tileHeight, int itemsToRender)
  67. {
  68. _itemWidth = tileWidth;
  69. _itemHeight = tileHeight;
  70. _colorUnmodified = Color3B::WHITE;
  71. _isOpacityModifyRGB = true;
  72. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  73. _textureAtlas = new (std::nothrow) TextureAtlas();
  74. if (! _textureAtlas)
  75. {
  76. CCLOG("cocos2d: Could not initialize AtlasNode. Invalid Texture.");
  77. return false;
  78. }
  79. _textureAtlas->initWithTexture(texture, itemsToRender);
  80. this->updateBlendFunc();
  81. this->updateOpacityModifyRGB();
  82. this->calculateMaxItems();
  83. _quadsToDraw = itemsToRender;
  84. // shader stuff
  85. setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP, texture));
  86. return true;
  87. }
  88. // AtlasNode - Atlas generation
  89. void AtlasNode::calculateMaxItems()
  90. {
  91. Size s = _textureAtlas->getTexture()->getContentSize();
  92. if (_ignoreContentScaleFactor)
  93. {
  94. s = _textureAtlas->getTexture()->getContentSizeInPixels();
  95. }
  96. _itemsPerColumn = (int)(s.height / _itemHeight);
  97. _itemsPerRow = (int)(s.width / _itemWidth);
  98. }
  99. void AtlasNode::updateAtlasValues()
  100. {
  101. CCASSERT(false, "CCAtlasNode:Abstract updateAtlasValue not overridden");
  102. }
  103. // AtlasNode - draw
  104. void AtlasNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
  105. {
  106. // ETC1 ALPHA supports.
  107. _quadCommand.init(_globalZOrder, _textureAtlas->getTexture(), getGLProgramState(), _blendFunc, _textureAtlas->getQuads(), _quadsToDraw, transform, flags);
  108. renderer->addCommand(&_quadCommand);
  109. }
  110. // AtlasNode - RGBA protocol
  111. const Color3B& AtlasNode::getColor() const
  112. {
  113. if(_isOpacityModifyRGB)
  114. {
  115. return _colorUnmodified;
  116. }
  117. return Node::getColor();
  118. }
  119. void AtlasNode::setColor(const Color3B& color3)
  120. {
  121. Color3B tmp = color3;
  122. _colorUnmodified = color3;
  123. if( _isOpacityModifyRGB )
  124. {
  125. tmp.r = tmp.r * _displayedOpacity/255;
  126. tmp.g = tmp.g * _displayedOpacity/255;
  127. tmp.b = tmp.b * _displayedOpacity/255;
  128. }
  129. Node::setColor(tmp);
  130. }
  131. void AtlasNode::setOpacity(GLubyte opacity)
  132. {
  133. Node::setOpacity(opacity);
  134. // special opacity for premultiplied textures
  135. if( _isOpacityModifyRGB )
  136. this->setColor(_colorUnmodified);
  137. }
  138. void AtlasNode::setOpacityModifyRGB(bool value)
  139. {
  140. Color3B oldColor = this->getColor();
  141. _isOpacityModifyRGB = value;
  142. this->setColor(oldColor);
  143. }
  144. bool AtlasNode::isOpacityModifyRGB() const
  145. {
  146. return _isOpacityModifyRGB;
  147. }
  148. void AtlasNode::updateOpacityModifyRGB()
  149. {
  150. _isOpacityModifyRGB = _textureAtlas->getTexture()->hasPremultipliedAlpha();
  151. }
  152. void AtlasNode::setIgnoreContentScaleFactor(bool ignoreContentScaleFactor)
  153. {
  154. _ignoreContentScaleFactor = ignoreContentScaleFactor;
  155. }
  156. // AtlasNode - CocosNodeTexture protocol
  157. const BlendFunc& AtlasNode::getBlendFunc() const
  158. {
  159. return _blendFunc;
  160. }
  161. void AtlasNode::setBlendFunc(const BlendFunc &blendFunc)
  162. {
  163. _blendFunc = blendFunc;
  164. }
  165. void AtlasNode::updateBlendFunc()
  166. {
  167. if( ! _textureAtlas->getTexture()->hasPremultipliedAlpha() )
  168. {
  169. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  170. setOpacityModifyRGB(false);
  171. }
  172. else
  173. {
  174. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  175. setOpacityModifyRGB(true);
  176. }
  177. }
  178. void AtlasNode::setTexture(Texture2D *texture)
  179. {
  180. _textureAtlas->setTexture(texture);
  181. this->updateBlendFunc();
  182. this->updateOpacityModifyRGB();
  183. }
  184. Texture2D * AtlasNode::getTexture() const
  185. {
  186. return _textureAtlas->getTexture();
  187. }
  188. void AtlasNode::setTextureAtlas(TextureAtlas* textureAtlas)
  189. {
  190. CC_SAFE_RETAIN(textureAtlas);
  191. CC_SAFE_RELEASE(_textureAtlas);
  192. _textureAtlas = textureAtlas;
  193. }
  194. TextureAtlas * AtlasNode::getTextureAtlas() const
  195. {
  196. return _textureAtlas;
  197. }
  198. ssize_t AtlasNode::getQuadsToDraw() const
  199. {
  200. return _quadsToDraw;
  201. }
  202. void AtlasNode::setQuadsToDraw(ssize_t quadsToDraw)
  203. {
  204. _quadsToDraw = quadsToDraw;
  205. }
  206. NS_CC_END