CCAtlasNode.cpp 6.5 KB

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