1
0

CCTileMapAtlas.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/CCTileMapAtlas.h"
  24. #include "platform/CCFileUtils.h"
  25. #include "renderer/CCTextureAtlas.h"
  26. #include "base/TGAlib.h"
  27. #include "base/CCDirector.h"
  28. #include "base/ccUTF8.h"
  29. NS_CC_BEGIN
  30. // implementation TileMapAtlas
  31. TileMapAtlas * TileMapAtlas::create(const std::string& tile, const std::string& mapFile, int tileWidth, int tileHeight)
  32. {
  33. TileMapAtlas *ret = new (std::nothrow) TileMapAtlas();
  34. if (ret->initWithTileFile(tile, mapFile, tileWidth, tileHeight))
  35. {
  36. ret->autorelease();
  37. return ret;
  38. }
  39. CC_SAFE_DELETE(ret);
  40. return nullptr;
  41. }
  42. bool TileMapAtlas::initWithTileFile(const std::string& tile, const std::string& mapFile, int tileWidth, int tileHeight)
  43. {
  44. this->loadTGAfile(mapFile);
  45. this->calculateItemsToRender();
  46. if( AtlasNode::initWithTileFile(tile, tileWidth, tileHeight, _itemsToRender) )
  47. {
  48. this->updateAtlasValues();
  49. this->setContentSize(Size((float)(_TGAInfo->width*_itemWidth),
  50. (float)(_TGAInfo->height*_itemHeight)));
  51. return true;
  52. }
  53. return false;
  54. }
  55. TileMapAtlas::TileMapAtlas()
  56. : _itemsToRender(0)
  57. , _TGAInfo(nullptr)
  58. {
  59. }
  60. TileMapAtlas::~TileMapAtlas()
  61. {
  62. if (_TGAInfo)
  63. {
  64. tgaDestroy(_TGAInfo);
  65. }
  66. }
  67. void TileMapAtlas::releaseMap()
  68. {
  69. if (_TGAInfo)
  70. {
  71. tgaDestroy(_TGAInfo);
  72. }
  73. _TGAInfo = nullptr;
  74. }
  75. void TileMapAtlas::calculateItemsToRender()
  76. {
  77. CCASSERT( _TGAInfo != nullptr, "tgaInfo must be non-nil");
  78. _itemsToRender = 0;
  79. for(int x=0;x < _TGAInfo->width; x++ )
  80. {
  81. for( int y=0; y < _TGAInfo->height; y++ )
  82. {
  83. Color3B *ptr = (Color3B*) _TGAInfo->imageData;
  84. Color3B value = ptr[x + y * _TGAInfo->width];
  85. if( value.r )
  86. {
  87. ++_itemsToRender;
  88. }
  89. }
  90. }
  91. }
  92. void TileMapAtlas::loadTGAfile(const std::string& file)
  93. {
  94. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(file);
  95. // //Find the path of the file
  96. // NSBundle *mainBndl = [Director sharedDirector].loadingBundle;
  97. // String *resourcePath = [mainBndl resourcePath];
  98. // String * path = [resourcePath stringByAppendingPathComponent:file];
  99. _TGAInfo = tgaLoad( fullPath.c_str() );
  100. #if 1
  101. if( _TGAInfo->status != TGA_OK )
  102. {
  103. CCASSERT(0, "TileMapAtlasLoadTGA : TileMapAtlas cannot load TGA file");
  104. }
  105. #endif
  106. }
  107. // TileMapAtlas - Atlas generation / updates
  108. void TileMapAtlas::setTile(const Color3B& tile, const Vec2& position)
  109. {
  110. CCASSERT(_TGAInfo != nullptr, "tgaInfo must not be nil");
  111. CCASSERT(position.x < _TGAInfo->width, "Invalid position.x");
  112. CCASSERT(position.y < _TGAInfo->height, "Invalid position.x");
  113. CCASSERT(tile.r != 0, "R component must be non 0");
  114. Color3B *ptr = (Color3B*)_TGAInfo->imageData;
  115. Color3B value = ptr[(unsigned int)(position.x + position.y * _TGAInfo->width)];
  116. if( value.r == 0 )
  117. {
  118. CCLOG("cocos2d: Value.r must be non 0.");
  119. }
  120. else
  121. {
  122. ptr[(unsigned int)(position.x + position.y * _TGAInfo->width)] = tile;
  123. // FIXME:: this method consumes a lot of memory
  124. // FIXME:: a tree of something like that shall be implemented
  125. std::string key = StringUtils::toString(position.x) + "," + StringUtils::toString(position.y);
  126. int num = _posToAtlasIndex[key].asInt();
  127. this->updateAtlasValueAt(position, tile, num);
  128. }
  129. }
  130. Color3B TileMapAtlas::getTileAt(const Vec2& position) const
  131. {
  132. CCASSERT( _TGAInfo != nullptr, "tgaInfo must not be nil");
  133. CCASSERT( position.x < _TGAInfo->width, "Invalid position.x");
  134. CCASSERT( position.y < _TGAInfo->height, "Invalid position.y");
  135. Color3B *ptr = (Color3B*)_TGAInfo->imageData;
  136. Color3B value = ptr[(unsigned int)(position.x + position.y * _TGAInfo->width)];
  137. return value;
  138. }
  139. void TileMapAtlas::updateAtlasValueAt(const Vec2& pos, const Color3B& value, int index)
  140. {
  141. CCASSERT( index >= 0 && index < _textureAtlas->getCapacity(), "updateAtlasValueAt: Invalid index");
  142. V3F_C4B_T2F_Quad* quad = &((_textureAtlas->getQuads())[index]);
  143. int x = pos.x;
  144. int y = pos.y;
  145. float row = (float) (value.r % _itemsPerRow);
  146. float col = (float) (value.r / _itemsPerRow);
  147. float textureWide = (float) (_textureAtlas->getTexture()->getPixelsWide());
  148. float textureHigh = (float) (_textureAtlas->getTexture()->getPixelsHigh());
  149. float itemWidthInPixels = _itemWidth * CC_CONTENT_SCALE_FACTOR();
  150. float itemHeightInPixels = _itemHeight * CC_CONTENT_SCALE_FACTOR();
  151. #if CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL
  152. float left = (2 * row * itemWidthInPixels + 1) / (2 * textureWide);
  153. float right = left + (itemWidthInPixels * 2 - 2) / (2 * textureWide);
  154. float top = (2 * col * itemHeightInPixels + 1) / (2 * textureHigh);
  155. float bottom = top + (itemHeightInPixels * 2 - 2) / (2 * textureHigh);
  156. #else
  157. float left = (row * itemWidthInPixels) / textureWide;
  158. float right = left + itemWidthInPixels / textureWide;
  159. float top = (col * itemHeightInPixels) / textureHigh;
  160. float bottom = top + itemHeightInPixels / textureHigh;
  161. #endif
  162. quad->tl.texCoords.u = left;
  163. quad->tl.texCoords.v = top;
  164. quad->tr.texCoords.u = right;
  165. quad->tr.texCoords.v = top;
  166. quad->bl.texCoords.u = left;
  167. quad->bl.texCoords.v = bottom;
  168. quad->br.texCoords.u = right;
  169. quad->br.texCoords.v = bottom;
  170. quad->bl.vertices.x = (float) (x * _itemWidth);
  171. quad->bl.vertices.y = (float) (y * _itemHeight);
  172. quad->bl.vertices.z = 0.0f;
  173. quad->br.vertices.x = (float)(x * _itemWidth + _itemWidth);
  174. quad->br.vertices.y = (float)(y * _itemHeight);
  175. quad->br.vertices.z = 0.0f;
  176. quad->tl.vertices.x = (float)(x * _itemWidth);
  177. quad->tl.vertices.y = (float)(y * _itemHeight + _itemHeight);
  178. quad->tl.vertices.z = 0.0f;
  179. quad->tr.vertices.x = (float)(x * _itemWidth + _itemWidth);
  180. quad->tr.vertices.y = (float)(y * _itemHeight + _itemHeight);
  181. quad->tr.vertices.z = 0.0f;
  182. Color4B color(_displayedColor.r, _displayedColor.g, _displayedColor.b, _displayedOpacity);
  183. quad->tr.colors = color;
  184. quad->tl.colors = color;
  185. quad->br.colors = color;
  186. quad->bl.colors = color;
  187. _textureAtlas->setDirty(true);
  188. ssize_t totalQuads = _textureAtlas->getTotalQuads();
  189. if (index + 1 > totalQuads) {
  190. _textureAtlas->increaseTotalQuadsWith(index + 1 - totalQuads);
  191. }
  192. }
  193. void TileMapAtlas::updateAtlasValues()
  194. {
  195. CCASSERT( _TGAInfo != nullptr, "tgaInfo must be non-nil");
  196. int total = 0;
  197. for(int x=0;x < _TGAInfo->width; x++ )
  198. {
  199. for( int y=0; y < _TGAInfo->height; y++ )
  200. {
  201. if( total < _itemsToRender )
  202. {
  203. Color3B *ptr = (Color3B*) _TGAInfo->imageData;
  204. Color3B value = ptr[x + y * _TGAInfo->width];
  205. if( value.r != 0 )
  206. {
  207. this->updateAtlasValueAt(Vec2(x,y), value, total);
  208. std::string key = StringUtils::toString(x) + "," + StringUtils::toString(y);
  209. _posToAtlasIndex[key] = total;
  210. total++;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. NS_CC_END