TmxFile2D.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Core/Context.h"
  23. #include "../Graphics/Texture2D.h"
  24. #include "../Graphics/Graphics.h"
  25. #include "../IO/FileSystem.h"
  26. #include "../IO/Log.h"
  27. #include "../Resource/ResourceCache.h"
  28. #include "../Resource/XMLFile.h"
  29. #include "../Resource/Image.h"
  30. #include "../Atomic2D/Sprite2D.h"
  31. #include "../Atomic2D/TmxFile2D.h"
  32. #include "../Math/AreaAllocator.h"
  33. // ATOMIC BEGIN
  34. #include "../Atomic2D/Drawable2D.h"
  35. // ATOMIC END
  36. #include "../DebugNew.h"
  37. namespace Atomic
  38. {
  39. // ATOMIC BEGIN
  40. // extern const float PIXEL_SIZE;
  41. // ATOMIC END
  42. TmxLayer2D::TmxLayer2D(TmxFile2D* tmxFile, TileMapLayerType2D type) :
  43. tmxFile_(tmxFile),
  44. type_(type)
  45. {
  46. }
  47. TmxLayer2D::~TmxLayer2D()
  48. {
  49. }
  50. TmxFile2D* TmxLayer2D::GetTmxFile() const
  51. {
  52. return tmxFile_;
  53. }
  54. bool TmxLayer2D::HasProperty(const String& name) const
  55. {
  56. if (!propertySet_)
  57. return false;
  58. return propertySet_->HasProperty(name);
  59. }
  60. const String& TmxLayer2D::GetProperty(const String& name) const
  61. {
  62. if (!propertySet_)
  63. return String::EMPTY;
  64. return propertySet_->GetProperty(name);
  65. }
  66. void TmxLayer2D::LoadInfo(const XMLElement& element)
  67. {
  68. name_ = element.GetAttribute("name");
  69. width_ = element.GetInt("width");
  70. height_ = element.GetInt("height");
  71. if (element.HasAttribute("visible"))
  72. visible_ = element.GetInt("visible") != 0;
  73. else
  74. visible_ = true;
  75. }
  76. void TmxLayer2D::LoadPropertySet(const XMLElement& element)
  77. {
  78. propertySet_ = new PropertySet2D();
  79. propertySet_->Load(element);
  80. }
  81. TmxTileLayer2D::TmxTileLayer2D(TmxFile2D* tmxFile) :
  82. TmxLayer2D(tmxFile, LT_TILE_LAYER)
  83. {
  84. }
  85. enum LayerEncoding {
  86. XML,
  87. CSV,
  88. Base64,
  89. };
  90. bool TmxTileLayer2D::Load(const XMLElement& element, const TileMapInfo2D& info)
  91. {
  92. LoadInfo(element);
  93. XMLElement dataElem = element.GetChild("data");
  94. if (!dataElem)
  95. {
  96. ATOMIC_LOGERROR("Could not find data in layer");
  97. return false;
  98. }
  99. LayerEncoding encoding;
  100. if (dataElem.HasAttribute("compression"))
  101. {
  102. ATOMIC_LOGERROR("Compression not supported now");
  103. return false;
  104. }
  105. if (dataElem.HasAttribute("encoding"))
  106. {
  107. String encodingAttribute = dataElem.GetAttribute("encoding");
  108. if (encodingAttribute == "xml")
  109. encoding = XML;
  110. else if (encodingAttribute == "csv")
  111. encoding = CSV;
  112. else if (encodingAttribute == "base64")
  113. encoding = Base64;
  114. else
  115. {
  116. ATOMIC_LOGERROR("Invalid encoding: " + encodingAttribute);
  117. return false;
  118. }
  119. }
  120. else
  121. encoding = XML;
  122. tiles_.Resize((unsigned)(width_ * height_));
  123. if (encoding == XML)
  124. {
  125. XMLElement tileElem = dataElem.GetChild("tile");
  126. for (int y = 0; y < height_; ++y)
  127. {
  128. for (int x = 0; x < width_; ++x)
  129. {
  130. if (!tileElem)
  131. return false;
  132. int gid = tileElem.GetInt("gid");
  133. if (gid > 0)
  134. {
  135. SharedPtr<Tile2D> tile(new Tile2D());
  136. tile->gid_ = gid;
  137. tile->sprite_ = tmxFile_->GetTileSprite(gid);
  138. tile->propertySet_ = tmxFile_->GetTilePropertySet(gid);
  139. // ATOMIC BEGIN
  140. tile->objectGroup_ = tmxFile_->GetTileObjectGroup(gid);
  141. // ATOMIC END
  142. tiles_[y * width_ + x] = tile;
  143. }
  144. tileElem = tileElem.GetNext("tile");
  145. }
  146. }
  147. }
  148. else if (encoding == CSV)
  149. {
  150. String dataValue = dataElem.GetValue();
  151. Vector<String> gidVector = dataValue.Split(',');
  152. int currentIndex = 0;
  153. for (int y = 0; y < height_; ++y)
  154. {
  155. for (int x = 0; x < width_; ++x)
  156. {
  157. gidVector[currentIndex].Replace("\n", "");
  158. int gid = ToInt(gidVector[currentIndex]);
  159. if (gid > 0)
  160. {
  161. SharedPtr<Tile2D> tile(new Tile2D());
  162. tile->gid_ = gid;
  163. tile->sprite_ = tmxFile_->GetTileSprite(gid);
  164. tile->propertySet_ = tmxFile_->GetTilePropertySet(gid);
  165. tiles_[y * width_ + x] = tile;
  166. }
  167. ++currentIndex;
  168. }
  169. }
  170. }
  171. else if (encoding == Base64)
  172. {
  173. String dataValue = dataElem.GetValue();
  174. int startPosition = 0;
  175. while (!IsAlpha(dataValue[startPosition]) && !IsDigit(dataValue[startPosition])
  176. && dataValue[startPosition] != '+' && dataValue[startPosition] != '/') ++startPosition;
  177. dataValue = dataValue.Substring(startPosition);
  178. PODVector<unsigned char> buffer = DecodeBase64(dataValue);
  179. int currentIndex = 0;
  180. for (int y = 0; y < height_; ++y)
  181. {
  182. for (int x = 0; x < width_; ++x)
  183. {
  184. // buffer contains 32-bit integers in little-endian format
  185. int gid = (buffer[currentIndex+3] << 24) | (buffer[currentIndex+2] << 16)
  186. | (buffer[currentIndex+1] << 8) | buffer[currentIndex];
  187. if (gid > 0)
  188. {
  189. SharedPtr<Tile2D> tile(new Tile2D());
  190. tile->gid_ = gid;
  191. tile->sprite_ = tmxFile_->GetTileSprite(gid);
  192. tile->propertySet_ = tmxFile_->GetTilePropertySet(gid);
  193. tiles_[y * width_ + x] = tile;
  194. }
  195. currentIndex += 4;
  196. }
  197. }
  198. }
  199. if (element.HasChild("properties"))
  200. LoadPropertySet(element.GetChild("properties"));
  201. return true;
  202. }
  203. Tile2D* TmxTileLayer2D::GetTile(int x, int y) const
  204. {
  205. if (x < 0 || x >= width_ || y < 0 || y >= height_)
  206. return 0;
  207. return tiles_[y * width_ + x];
  208. }
  209. TmxObjectGroup2D::TmxObjectGroup2D(TmxFile2D* tmxFile) :
  210. TmxLayer2D(tmxFile, LT_OBJECT_GROUP)
  211. {
  212. }
  213. bool TmxObjectGroup2D::Load(const XMLElement& element, const TileMapInfo2D& info, bool local)
  214. {
  215. LoadInfo(element);
  216. for (XMLElement objectElem = element.GetChild("object"); objectElem; objectElem = objectElem.GetNext("object"))
  217. {
  218. SharedPtr<TileMapObject2D> object(new TileMapObject2D());
  219. StoreObject(objectElem, object, info, false, local);
  220. objects_.Push(object);
  221. }
  222. if (element.HasChild("properties"))
  223. LoadPropertySet(element.GetChild("properties"));
  224. return true;
  225. }
  226. void TmxObjectGroup2D::StoreObject(XMLElement objectElem, SharedPtr<TileMapObject2D> object, const TileMapInfo2D& info, bool isTile, bool local)
  227. {
  228. if (objectElem.HasAttribute("name"))
  229. object->name_ = objectElem.GetAttribute("name");
  230. // ATOMIC BEGIN
  231. else
  232. object->name_ = "Object";
  233. // ATOMIC END
  234. if (objectElem.HasAttribute("type"))
  235. object->type_ = objectElem.GetAttribute("type");
  236. if (objectElem.HasAttribute("gid"))
  237. object->objectType_ = OT_TILE;
  238. else if (objectElem.HasChild("polygon"))
  239. object->objectType_ = OT_POLYGON;
  240. else if (objectElem.HasChild("polyline"))
  241. object->objectType_ = OT_POLYLINE;
  242. else if (objectElem.HasChild("ellipse"))
  243. object->objectType_ = OT_ELLIPSE;
  244. else
  245. object->objectType_ = OT_RECTANGLE;
  246. const Vector2 position(objectElem.GetFloat("x"), objectElem.GetFloat("y"));
  247. const Vector2 size(objectElem.GetFloat("width"), objectElem.GetFloat("height"));
  248. switch (object->objectType_)
  249. {
  250. case OT_RECTANGLE:
  251. case OT_ELLIPSE:
  252. // ATOMIC BEGIN
  253. object->size_ = Vector2(size.x_ * PIXEL_SIZE, size.y_ * PIXEL_SIZE);
  254. // ATOMIC BEGIN
  255. if (!local)
  256. {
  257. object->position_ = info.ConvertPosition(Vector2(position.x_, position.y_ + size.y_));
  258. }
  259. else
  260. {
  261. Vector2 nposition = position;
  262. nposition.x_ *= PIXEL_SIZE;
  263. nposition.y_ *= PIXEL_SIZE;
  264. nposition.x_ = nposition.x_ + object->size_.x_ / 2.0f;
  265. nposition.y_ = nposition.y_ + object->size_.y_ / 2.0f;
  266. nposition.y_ = info.tileHeight_ - nposition.y_;
  267. object->position_ = nposition;
  268. }
  269. // ATOMIC END
  270. break;
  271. case OT_TILE:
  272. object->position_ = info.ConvertPosition(position);
  273. object->gid_ = objectElem.GetInt("gid");
  274. object->sprite_ = tmxFile_->GetTileSprite(object->gid_);
  275. if (objectElem.HasAttribute("width") || objectElem.HasAttribute("height"))
  276. {
  277. object->size_ = Vector2(size.x_ * PIXEL_SIZE, size.y_ * PIXEL_SIZE);
  278. }
  279. else if (object->sprite_)
  280. {
  281. IntVector2 spriteSize = object->sprite_->GetRectangle().Size();
  282. object->size_ = Vector2(spriteSize.x_, spriteSize.y_);
  283. }
  284. break;
  285. case OT_POLYGON:
  286. case OT_POLYLINE:
  287. {
  288. Vector<String> points;
  289. const char* name = object->objectType_ == OT_POLYGON ? "polygon" : "polyline";
  290. XMLElement polygonElem = objectElem.GetChild(name);
  291. points = polygonElem.GetAttribute("points").Split(' ');
  292. if (points.Size() <= 1)
  293. return;
  294. object->points_.Resize(points.Size());
  295. for (unsigned i = 0; i < points.Size(); ++i)
  296. {
  297. points[i].Replace(',', ' ');
  298. Vector2 point = position + ToVector2(points[i]);
  299. object->points_[i] = info.ConvertPosition(point);
  300. }
  301. }
  302. break;
  303. default: break;
  304. }
  305. if (objectElem.HasChild("properties"))
  306. {
  307. object->propertySet_ = new PropertySet2D();
  308. object->propertySet_->Load(objectElem.GetChild("properties"));
  309. }
  310. }
  311. TileMapObject2D* TmxObjectGroup2D::GetObject(unsigned index) const
  312. {
  313. if (index >= objects_.Size())
  314. return 0;
  315. return objects_[index];
  316. }
  317. TmxImageLayer2D::TmxImageLayer2D(TmxFile2D* tmxFile) :
  318. TmxLayer2D(tmxFile, LT_IMAGE_LAYER)
  319. {
  320. }
  321. bool TmxImageLayer2D::Load(const XMLElement& element, const TileMapInfo2D& info)
  322. {
  323. LoadInfo(element);
  324. XMLElement imageElem = element.GetChild("image");
  325. if (!imageElem)
  326. return false;
  327. position_ = Vector2(0.0f, info.GetMapHeight());
  328. source_ = imageElem.GetAttribute("source");
  329. String textureFilePath = GetParentPath(tmxFile_->GetName()) + source_;
  330. ResourceCache* cache = tmxFile_->GetSubsystem<ResourceCache>();
  331. SharedPtr<Texture2D> texture(cache->GetResource<Texture2D>(textureFilePath));
  332. if (!texture)
  333. {
  334. ATOMIC_LOGERROR("Could not load texture " + textureFilePath);
  335. return false;
  336. }
  337. sprite_ = new Sprite2D(tmxFile_->GetContext());
  338. sprite_->SetTexture(texture);
  339. sprite_->SetRectangle(IntRect(0, 0, texture->GetWidth(), texture->GetHeight()));
  340. // Set image hot spot at left top
  341. sprite_->SetHotSpot(Vector2(0.0f, 1.0f));
  342. if (element.HasChild("properties"))
  343. LoadPropertySet(element.GetChild("properties"));
  344. return true;
  345. }
  346. Sprite2D* TmxImageLayer2D::GetSprite() const
  347. {
  348. return sprite_;
  349. }
  350. TmxFile2D::TmxFile2D(Context* context) :
  351. Resource(context)
  352. {
  353. }
  354. TmxFile2D::~TmxFile2D()
  355. {
  356. // ATOMIC BEGIN
  357. // use shared ptr
  358. //for (unsigned i = 0; i < layers_.Size(); ++i)
  359. // delete layers_[i];
  360. // ATOMIC END
  361. }
  362. void TmxFile2D::RegisterObject(Context* context)
  363. {
  364. context->RegisterFactory<TmxFile2D>();
  365. }
  366. bool TmxFile2D::BeginLoad(Deserializer& source)
  367. {
  368. if (GetName().Empty())
  369. SetName(source.GetName());
  370. loadXMLFile_ = new XMLFile(context_);
  371. if (!loadXMLFile_->Load(source))
  372. {
  373. ATOMIC_LOGERROR("Load XML failed " + source.GetName());
  374. loadXMLFile_.Reset();
  375. return false;
  376. }
  377. XMLElement rootElem = loadXMLFile_->GetRoot("map");
  378. if (!rootElem)
  379. {
  380. ATOMIC_LOGERROR("Invalid tmx file " + source.GetName());
  381. loadXMLFile_.Reset();
  382. return false;
  383. }
  384. // If we're async loading, request the texture now. Finish during EndLoad().
  385. if (GetAsyncLoadState() == ASYNC_LOADING)
  386. {
  387. for (XMLElement tileSetElem = rootElem.GetChild("tileset"); tileSetElem; tileSetElem = tileSetElem.GetNext("tileset"))
  388. {
  389. // Tile set defined in TSX file
  390. if (tileSetElem.HasAttribute("source"))
  391. {
  392. String source = tileSetElem.GetAttribute("source");
  393. SharedPtr<XMLFile> tsxXMLFile = LoadTSXFile(source);
  394. if (!tsxXMLFile)
  395. return false;
  396. // ATOMIC BEGIN
  397. // Look for an image indicating that this is a spritesheet with multiple tiles instead
  398. // of a series of individual images which are not supported
  399. if (!tsxXMLFile->GetRoot("tileset").GetChild("image"))
  400. {
  401. ATOMIC_LOGERROR("Load TSX File failed: " + source + ". tsx files with individual images are not supported.");
  402. return false;
  403. }
  404. // ATOMIC END
  405. tsxXMLFiles_[source] = tsxXMLFile;
  406. String textureFilePath =
  407. GetParentPath(GetName()) + tsxXMLFile->GetRoot("tileset").GetChild("image").GetAttribute("source");
  408. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  409. }
  410. else
  411. {
  412. String textureFilePath = GetParentPath(GetName()) + tileSetElem.GetChild("image").GetAttribute("source");
  413. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  414. }
  415. }
  416. for (XMLElement imageLayerElem = rootElem.GetChild("imagelayer"); imageLayerElem;
  417. imageLayerElem = imageLayerElem.GetNext("imagelayer"))
  418. {
  419. String textureFilePath = GetParentPath(GetName()) + imageLayerElem.GetChild("image").GetAttribute("source");
  420. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  421. }
  422. }
  423. return true;
  424. }
  425. bool TmxFile2D::EndLoad()
  426. {
  427. if (!loadXMLFile_)
  428. return false;
  429. XMLElement rootElem = loadXMLFile_->GetRoot("map");
  430. String version = rootElem.GetAttribute("version");
  431. // ATOMIC BEGIN
  432. if (version != "1.0" && version != "1.0.0")
  433. // ATOMIC END
  434. {
  435. ATOMIC_LOGERROR("Invalid version");
  436. return false;
  437. }
  438. String orientation = rootElem.GetAttribute("orientation");
  439. if (orientation == "orthogonal")
  440. info_.orientation_ = O_ORTHOGONAL;
  441. else if (orientation == "isometric")
  442. info_.orientation_ = O_ISOMETRIC;
  443. else if (orientation == "staggered")
  444. info_.orientation_ = O_STAGGERED;
  445. else if (orientation == "hexagonal")
  446. info_.orientation_ = O_HEXAGONAL;
  447. else
  448. {
  449. ATOMIC_LOGERROR("Unsupported orientation type " + orientation);
  450. return false;
  451. }
  452. info_.width_ = rootElem.GetInt("width");
  453. info_.height_ = rootElem.GetInt("height");
  454. info_.tileWidth_ = rootElem.GetFloat("tilewidth") * PIXEL_SIZE;
  455. info_.tileHeight_ = rootElem.GetFloat("tileheight") * PIXEL_SIZE;
  456. for (unsigned i = 0; i < layers_.Size(); ++i)
  457. delete layers_[i];
  458. layers_.Clear();
  459. for (XMLElement childElement = rootElem.GetChild(); childElement; childElement = childElement.GetNext())
  460. {
  461. bool ret = true;
  462. String name = childElement.GetName();
  463. if (name == "tileset")
  464. ret = LoadTileSet(childElement);
  465. // ATOMIC BEGIN
  466. else if (name == "layer")
  467. {
  468. SharedPtr<TmxTileLayer2D> tileLayer (new TmxTileLayer2D(this));
  469. ret = tileLayer->Load(childElement, info_);
  470. layers_.Push(tileLayer);
  471. }
  472. else if (name == "objectgroup")
  473. {
  474. SharedPtr<TmxObjectGroup2D> objectGroup (new TmxObjectGroup2D(this));
  475. ret = objectGroup->Load(childElement, info_);
  476. layers_.Push(objectGroup);
  477. }
  478. else if (name == "imagelayer")
  479. {
  480. SharedPtr<TmxImageLayer2D> imageLayer (new TmxImageLayer2D(this));
  481. ret = imageLayer->Load(childElement, info_);
  482. layers_.Push(imageLayer);
  483. }
  484. // ATOMIC END
  485. if (!ret)
  486. {
  487. loadXMLFile_.Reset();
  488. tsxXMLFiles_.Clear();
  489. return false;
  490. }
  491. }
  492. loadXMLFile_.Reset();
  493. tsxXMLFiles_.Clear();
  494. return true;
  495. }
  496. bool TmxFile2D::SetInfo(Orientation2D orientation, int width, int height, float tileWidth, float tileHeight)
  497. {
  498. if (layers_.Size() > 0)
  499. return false;
  500. info_.orientation_ = orientation;
  501. info_.width_ = width;
  502. info_.height_ = height;
  503. info_.tileWidth_ = tileWidth * PIXEL_SIZE;
  504. info_.tileHeight_ = tileHeight * PIXEL_SIZE;
  505. return true;
  506. }
  507. void TmxFile2D::AddLayer(unsigned index, TmxLayer2D *layer)
  508. {
  509. // ATOMIC BEGIN
  510. if (index > layers_.Size())
  511. layers_.Push(SharedPtr<TmxLayer2D>(layer));
  512. else // index <= layers_.size()
  513. layers_.Insert(index, SharedPtr<TmxLayer2D>(layer));
  514. // ATOMIC END
  515. }
  516. void TmxFile2D::AddLayer(TmxLayer2D *layer)
  517. {
  518. // ATOMIC BEGIN
  519. layers_.Push(SharedPtr<TmxLayer2D>(layer));
  520. // ATOMIC END
  521. }
  522. Sprite2D* TmxFile2D::GetTileSprite(int gid) const
  523. {
  524. HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = gidToSpriteMapping_.Find(gid);
  525. if (i == gidToSpriteMapping_.End())
  526. return 0;
  527. return i->second_;
  528. }
  529. Vector<SharedPtr<TileMapObject2D> > TmxFile2D::GetTileCollisionShapes(int gid) const
  530. {
  531. Vector<SharedPtr<TileMapObject2D> > tileShapes;
  532. HashMap<int, Vector<SharedPtr<TileMapObject2D> > >::ConstIterator i = gidToCollisionShapeMapping_.Find(gid);
  533. if (i == gidToCollisionShapeMapping_.End())
  534. return tileShapes;
  535. return i->second_;
  536. }
  537. PropertySet2D* TmxFile2D::GetTilePropertySet(int gid) const
  538. {
  539. HashMap<int, SharedPtr<PropertySet2D> >::ConstIterator i = gidToPropertySetMapping_.Find(gid);
  540. if (i == gidToPropertySetMapping_.End())
  541. return 0;
  542. return i->second_;
  543. }
  544. const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const
  545. {
  546. if (index >= layers_.Size())
  547. return 0;
  548. return layers_[index];
  549. }
  550. SharedPtr<XMLFile> TmxFile2D::LoadTSXFile(const String& source)
  551. {
  552. String tsxFilePath = GetParentPath(GetName()) + source;
  553. SharedPtr<File> tsxFile = GetSubsystem<ResourceCache>()->GetFile(tsxFilePath);
  554. SharedPtr<XMLFile> tsxXMLFile(new XMLFile(context_));
  555. if (!tsxFile || !tsxXMLFile->Load(*tsxFile))
  556. {
  557. ATOMIC_LOGERROR("Load TSX file failed " + tsxFilePath);
  558. return SharedPtr<XMLFile>();
  559. }
  560. return tsxXMLFile;
  561. }
  562. struct TileImageInfo {
  563. Image* image;
  564. int tileGid;
  565. int imageWidth;
  566. int imageHeight;
  567. int x;
  568. int y;
  569. };
  570. bool TmxFile2D::LoadTileSet(const XMLElement& element)
  571. {
  572. int firstgid = element.GetInt("firstgid");
  573. XMLElement tileSetElem;
  574. if (element.HasAttribute("source"))
  575. {
  576. String source = element.GetAttribute("source");
  577. HashMap<String, SharedPtr<XMLFile> >::Iterator i = tsxXMLFiles_.Find(source);
  578. if (i == tsxXMLFiles_.End())
  579. {
  580. SharedPtr<XMLFile> tsxXMLFile = LoadTSXFile(source);
  581. if (!tsxXMLFile)
  582. return false;
  583. // ATOMIC BEGIN
  584. // Look for an image indicating that this is a spritesheet with multiple tiles instead
  585. // of a series of individual images which are not supported
  586. if (!tsxXMLFile->GetRoot("tileset").GetChild("image"))
  587. {
  588. ATOMIC_LOGERROR("Load TSX File failed: " + source + ". tsx files with individual images are not supported.");
  589. return false;
  590. }
  591. // ATOMIC END
  592. // Add to mapping to avoid release
  593. tsxXMLFiles_[source] = tsxXMLFile;
  594. tileSetElem = tsxXMLFile->GetRoot("tileset");
  595. }
  596. else
  597. tileSetElem = i->second_->GetRoot("tileset");
  598. }
  599. else
  600. tileSetElem = element;
  601. int tileWidth = tileSetElem.GetInt("tilewidth");
  602. int tileHeight = tileSetElem.GetInt("tileheight");
  603. int spacing = tileSetElem.GetInt("spacing");
  604. int margin = tileSetElem.GetInt("margin");
  605. int imageWidth;
  606. int imageHeight;
  607. bool isSingleTileSet = false;
  608. ResourceCache* cache = GetSubsystem<ResourceCache>();
  609. {
  610. XMLElement imageElem = tileSetElem.GetChild("image");
  611. // Tileset based on single tileset image
  612. if (imageElem.NotNull()) {
  613. isSingleTileSet = true;
  614. String textureFilePath = GetParentPath(GetName()) + imageElem.GetAttribute("source");
  615. SharedPtr<Texture2D> texture(cache->GetResource<Texture2D>(textureFilePath));
  616. if (!texture)
  617. {
  618. ATOMIC_LOGERROR("Could not load texture " + textureFilePath);
  619. return false;
  620. }
  621. // ATOMIC BEGIN
  622. // reduces border tile sample errors
  623. texture->SetFilterMode(FILTER_NEAREST);
  624. // ATOMIC END
  625. // Set hot spot at left bottom
  626. Vector2 hotSpot(0.0f, 0.0f);
  627. if (tileSetElem.HasChild("tileoffset"))
  628. {
  629. XMLElement offsetElem = tileSetElem.GetChild("tileoffset");
  630. hotSpot.x_ += offsetElem.GetFloat("x") / (float)tileWidth;
  631. hotSpot.y_ += offsetElem.GetFloat("y") / (float)tileHeight;
  632. }
  633. imageWidth = imageElem.GetInt("width");
  634. imageHeight = imageElem.GetInt("height");
  635. int gid = firstgid;
  636. for (int y = margin; y + tileHeight <= imageHeight - margin; y += tileHeight + spacing)
  637. {
  638. for (int x = margin; x + tileWidth <= imageWidth - margin; x += tileWidth + spacing)
  639. {
  640. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  641. sprite->SetTexture(texture);
  642. sprite->SetRectangle(IntRect(x, y, x + tileWidth, y + tileHeight));
  643. sprite->SetHotSpot(hotSpot);
  644. gidToSpriteMapping_[gid++] = sprite;
  645. }
  646. }
  647. }
  648. }
  649. Vector<TileImageInfo> tileImageInfos;
  650. for (XMLElement tileElem = tileSetElem.GetChild("tile"); tileElem; tileElem = tileElem.GetNext("tile"))
  651. {
  652. int gid = firstgid + tileElem.GetInt("id");
  653. // Tileset based on collection of images
  654. if (!isSingleTileSet)
  655. {
  656. XMLElement imageElem = tileElem.GetChild("image");
  657. if (imageElem.NotNull()) {
  658. String textureFilePath = GetParentPath(GetName()) + imageElem.GetAttribute("source");
  659. SharedPtr<Image> image(cache->GetResource<Image>(textureFilePath));
  660. if (!image)
  661. {
  662. ATOMIC_LOGERROR("Could not load image " + textureFilePath);
  663. return false;
  664. }
  665. tileWidth = imageWidth = imageElem.GetInt("width");
  666. tileHeight = imageHeight = imageElem.GetInt("height");
  667. TileImageInfo info = {image, gid, imageWidth, imageHeight, 0, 0};
  668. tileImageInfos.Push(info);
  669. }
  670. }
  671. // Tile collision shape(s)
  672. TmxObjectGroup2D objectGroup(this);
  673. for (XMLElement collisionElem = tileElem.GetChild("objectgroup"); collisionElem; collisionElem = collisionElem.GetNext("objectgroup"))
  674. {
  675. Vector<SharedPtr<TileMapObject2D> > objects;
  676. for (XMLElement objectElem = collisionElem.GetChild("object"); objectElem; objectElem = objectElem.GetNext("object"))
  677. {
  678. SharedPtr<TileMapObject2D> object(new TileMapObject2D());
  679. // Convert Tiled local position (left top) to Urho3D local position (left bottom)
  680. objectElem.SetAttribute("y", String(info_.GetMapHeight() / PIXEL_SIZE - (tileHeight - objectElem.GetFloat("y"))));
  681. objectGroup.StoreObject(objectElem, object, info_, true, false);
  682. objects.Push(object);
  683. }
  684. gidToCollisionShapeMapping_[gid] = objects;
  685. }
  686. if (tileElem.HasChild("properties"))
  687. {
  688. SharedPtr<PropertySet2D> propertySet(new PropertySet2D());
  689. propertySet->Load(tileElem.GetChild("properties"));
  690. gidToPropertySetMapping_[gid] = propertySet;
  691. }
  692. // ATOMIC BEGIN
  693. // collision information
  694. if (tileElem.HasChild("objectgroup"))
  695. {
  696. // ok, when you use multiple tile sets the "info"
  697. // numbers can be different, this is a bit of a hack
  698. // if something is wrong, look here... may have to need
  699. // to refactor "info" to be per set
  700. float _tileWidth = info_.tileWidth_;
  701. float _tileHeight = info_.tileHeight_;
  702. info_.tileHeight_ = (float) tileHeight * PIXEL_SIZE;
  703. info_.tileWidth_ = (float) tileWidth * PIXEL_SIZE;
  704. XMLElement groupElem = tileElem.GetChild("objectgroup");
  705. TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this);
  706. if (!objectGroup->Load(groupElem, info_, true))
  707. {
  708. ATOMIC_LOGERROR("Could not load objectgroup");
  709. objectGroup->ReleaseRef();
  710. }
  711. else
  712. {
  713. gidToObjectGroupMapping_[gid] = objectGroup;
  714. }
  715. info_.tileWidth_ = _tileWidth;
  716. info_.tileHeight_ = _tileHeight;
  717. }
  718. // ATOMIC END
  719. }
  720. if (!isSingleTileSet)
  721. {
  722. if (tileImageInfos.Empty())
  723. return false;
  724. AreaAllocator allocator(128, 128, 2048, 2048);
  725. for (int i = 0; i < tileImageInfos.Size(); ++i)
  726. {
  727. TileImageInfo& info = tileImageInfos[i];
  728. if (!allocator.Allocate(info.imageWidth + 1, info.imageHeight + 1, info.x, info.y))
  729. {
  730. ATOMIC_LOGERROR("Could not allocate area");
  731. return false;
  732. }
  733. }
  734. SharedPtr<Texture2D> texture(new Texture2D(context_));
  735. texture->SetMipsToSkip(QUALITY_LOW, 0);
  736. texture->SetNumLevels(1);
  737. texture->SetSize(allocator.GetWidth(), allocator.GetHeight(), Graphics::GetRGBAFormat());
  738. unsigned textureDataSize = allocator.GetWidth() * allocator.GetHeight() * 4;
  739. SharedArrayPtr<unsigned char> textureData(new unsigned char[textureDataSize]);
  740. memset(textureData.Get(), 0, textureDataSize);
  741. for (int i = 0; i < tileImageInfos.Size(); ++i)
  742. {
  743. TileImageInfo& info = tileImageInfos[i];
  744. Image* image = info.image;
  745. for (int y = 0; y < image->GetHeight(); ++y)
  746. {
  747. memcpy(textureData.Get() + ((info.y + y) * allocator.GetWidth() + info.x) * 4,
  748. image->GetData() + y * image->GetWidth() * 4, image->GetWidth() * 4);
  749. }
  750. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  751. sprite->SetTexture(texture);
  752. sprite->SetRectangle(IntRect(info.x, info.y, info.x + info.imageWidth, info.y + + info.imageHeight));
  753. sprite->SetHotSpot(Vector2::ZERO);
  754. gidToSpriteMapping_[info.tileGid] = sprite;
  755. }
  756. texture->SetData(0, 0, 0, allocator.GetWidth(), allocator.GetHeight(), textureData.Get());
  757. }
  758. return true;
  759. }
  760. // BEGIN ATOMIC
  761. TmxObjectGroup2D* TmxFile2D::GetTileObjectGroup(int gid) const
  762. {
  763. HashMap<int, SharedPtr<TmxObjectGroup2D> >::ConstIterator i = gidToObjectGroupMapping_.Find(gid);
  764. if (i == gidToObjectGroupMapping_.End())
  765. return 0;
  766. return i->second_;
  767. }
  768. // END ATOMIC
  769. }