CCAtlasNode.h 5.3 KB

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