CCTileMapAtlas.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 __CCTILE_MAP_ATLAS__
  25. #define __CCTILE_MAP_ATLAS__
  26. #include "2d/CCAtlasNode.h"
  27. #include "base/CCValue.h"
  28. NS_CC_BEGIN
  29. /// @cond DO_NOT_SHOW
  30. struct sImageTGA;
  31. /** @brief TileMapAtlas is a subclass of AtlasNode.
  32. It knows how to render a map based of tiles.
  33. The tiles must be in a .PNG format while the map must be a .TGA file.
  34. For more information regarding the format, please see this post:
  35. http://www.cocos2d-iphone.org/archives/27
  36. All features from AtlasNode are valid in TileMapAtlas
  37. IMPORTANT:
  38. This class is deprecated. It is maintained for compatibility reasons only.
  39. You SHOULD not use this class.
  40. Instead, use the newer TMX file format: TMXTiledMap
  41. @js NA
  42. */
  43. class CC_DLL TileMapAtlas : public AtlasNode
  44. {
  45. public:
  46. /** creates a TileMap with a tile file (atlas) with a map file and the width and height of each tile in points.
  47. The tile file will be loaded using the TextureMgr.
  48. */
  49. static TileMapAtlas * create(const std::string& tile, const std::string& mapFile, int tileWidth, int tileHeight);
  50. /**
  51. * @js ctor
  52. */
  53. TileMapAtlas();
  54. /**
  55. * @js NA
  56. * @lua NA
  57. */
  58. virtual ~TileMapAtlas();
  59. /** initializes a TileMap with a tile file (atlas) with a map file and the width and height of each tile in points.
  60. The file will be loaded using the TextureMgr.
  61. */
  62. bool initWithTileFile(const std::string& tile, const std::string& mapFile, int tileWidth, int tileHeight);
  63. /**
  64. * Returns a tile from position x,y.
  65. *For the moment only channel R is used
  66. */
  67. Color3B getTileAt(const Vec2& position) const;
  68. /**
  69. * Returns a tile from position x,y.
  70. *For the moment only channel R is used
  71. */
  72. CC_DEPRECATED_ATTRIBUTE Color3B tileAt(const Vec2& position) const { return getTileAt(position); };
  73. /** sets a tile at position x,y.
  74. For the moment only channel R is used
  75. */
  76. void setTile(const Color3B& tile, const Vec2& position);
  77. /** dealloc the map from memory */
  78. void releaseMap();
  79. /**
  80. * Query TGA image info.
  81. *@return The TGA image info.
  82. */
  83. struct sImageTGA* getTGAInfo() const { return _TGAInfo; }
  84. /**
  85. * Set the TGA image info for TileMapAtlas
  86. *@param TGAInfo The TGA info in sImageTGA.
  87. */
  88. void setTGAInfo(struct sImageTGA* TGAInfo) { _TGAInfo = TGAInfo; }
  89. protected:
  90. void loadTGAfile(const std::string& file);
  91. void calculateItemsToRender();
  92. void updateAtlasValueAt(const Vec2& pos, const Color3B& value, int index);
  93. void updateAtlasValues();
  94. //! x,y to atlas dictionary
  95. ValueMap _posToAtlasIndex;
  96. //! numbers of tiles to render
  97. int _itemsToRender;
  98. /** TileMap info */
  99. struct sImageTGA* _TGAInfo;
  100. };
  101. /// @endcond
  102. NS_CC_END
  103. #endif //__CCTILE_MAP_ATLAS__