1
0

CCTileMapAtlas.h 4.1 KB

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