CCAtlasNode.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef __CCATLAS_NODE_H__
  25. #define __CCATLAS_NODE_H__
  26. #include "2d/CCNode.h"
  27. #include "base/CCProtocols.h"
  28. #include "base/ccTypes.h"
  29. #include "renderer/CCQuadCommand.h"
  30. NS_CC_BEGIN
  31. /**
  32. * @addtogroup _2d
  33. * @{
  34. */
  35. class TextureAtlas;
  36. /** @brief AtlasNode is a subclass of Node that implements the RGBAProtocol and TextureProtocol protocol.
  37. * It knows how to render a TextureAtlas object.
  38. * If you are going to render a TextureAtlas consider subclassing AtlasNode (or a subclass of AtlasNode).
  39. * All features from Node are valid, plus the following features:
  40. * - opacity and RGB colors.
  41. */
  42. class CC_DLL AtlasNode : public Node, public TextureProtocol
  43. {
  44. public:
  45. /** creates a AtlasNode with an Atlas file the width and height of each item and the quantity of items to render.
  46. *
  47. * @param filename The path of Atlas file.
  48. * @param tileWidth The width of the item.
  49. * @param tileHeight The height of the item.
  50. * @param itemsToRender The quantity of items to render.
  51. */
  52. static AtlasNode * create(const std::string& filename, int tileWidth, int tileHeight, int itemsToRender);
  53. /** updates the Atlas (indexed vertex array).
  54. * Shall be overridden in subclasses.
  55. */
  56. virtual void updateAtlasValues();
  57. /** Set an buffer manager of the texture vertex. */
  58. void setTextureAtlas(TextureAtlas* textureAtlas);
  59. /** Return the buffer manager of the texture vertex.
  60. *
  61. * @return Return A TextureAtlas.
  62. */
  63. TextureAtlas* getTextureAtlas() const;
  64. void setQuadsToDraw(ssize_t quadsToDraw);
  65. ssize_t getQuadsToDraw() const;
  66. // Overrides
  67. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  68. virtual Texture2D* getTexture() const override;
  69. virtual void setTexture(Texture2D *texture) override;
  70. virtual bool isOpacityModifyRGB() const override;
  71. virtual void setOpacityModifyRGB(bool isOpacityModifyRGB) override;
  72. virtual const Color3B& getColor(void) const override;
  73. virtual void setColor(const Color3B& color) override;
  74. virtual void setOpacity(GLubyte opacity) override;
  75. /**
  76. * @code
  77. * When this function bound into js or lua,the parameter will be changed
  78. * In js: var setBlendFunc(var src, var dst)
  79. * @endcode
  80. * @lua NA
  81. */
  82. virtual void setBlendFunc(const BlendFunc& blendFunc) override;
  83. /**
  84. * @lua NA
  85. */
  86. virtual const BlendFunc& getBlendFunc() const override;
  87. CC_CONSTRUCTOR_ACCESS:
  88. AtlasNode();
  89. virtual ~AtlasNode();
  90. /** Initializes an AtlasNode with an Atlas file the width and height of each item and the quantity of items to render*/
  91. bool initWithTileFile(const std::string& tile, int tileWidth, int tileHeight, int itemsToRender);
  92. /** Initializes an AtlasNode with a texture the width and height of each item measured in points and the quantity of items to render*/
  93. bool initWithTexture(Texture2D* texture, int tileWidth, int tileHeight, int itemsToRender);
  94. protected:
  95. void calculateMaxItems();
  96. void updateBlendFunc();
  97. void updateOpacityModifyRGB();
  98. friend class Director;
  99. void setIgnoreContentScaleFactor(bool bIgnoreContentScaleFactor);
  100. /** Chars per row. */
  101. int _itemsPerRow;
  102. /** Chars per column. */
  103. int _itemsPerColumn;
  104. /** Width of each char. */
  105. int _itemWidth;
  106. /** Height of each char. */
  107. int _itemHeight;
  108. Color3B _colorUnmodified;
  109. TextureAtlas* _textureAtlas;
  110. /** Protocol variables. */
  111. bool _isOpacityModifyRGB;
  112. BlendFunc _blendFunc;
  113. /** Quads to draw. */
  114. ssize_t _quadsToDraw;
  115. /** Color uniform. */
  116. GLint _uniformColor;
  117. /** This variable is only used for LabelAtlas FPS display. So plz don't modify its value. */
  118. bool _ignoreContentScaleFactor;
  119. /** Quad command. */
  120. QuadCommand _quadCommand;
  121. private:
  122. CC_DISALLOW_COPY_AND_ASSIGN(AtlasNode);
  123. };
  124. // end of base_node group
  125. /// @}
  126. NS_CC_END
  127. #endif // __CCATLAS_NODE_H__