TmxFile2D.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Graphics/Texture2D.h"
  25. #include "../IO/FileSystem.h"
  26. #include "../IO/Log.h"
  27. #include "../Resource/ResourceCache.h"
  28. #include "../Resource/XMLFile.h"
  29. #include "../Atomic2D/Sprite2D.h"
  30. #include "../Atomic2D/TmxFile2D.h"
  31. // ATOMIC BEGIN
  32. #include "../Atomic2D/Drawable2D.h"
  33. // ATOMIC END
  34. #include "../DebugNew.h"
  35. namespace Atomic
  36. {
  37. // ATOMIC BEGIN
  38. // extern const float PIXEL_SIZE;
  39. // ATOMIC END
  40. TmxLayer2D::TmxLayer2D(TmxFile2D* tmxFile, TileMapLayerType2D type) :
  41. tmxFile_(tmxFile),
  42. type_(type)
  43. {
  44. }
  45. TmxLayer2D::~TmxLayer2D()
  46. {
  47. }
  48. TmxFile2D* TmxLayer2D::GetTmxFile() const
  49. {
  50. return tmxFile_;
  51. }
  52. bool TmxLayer2D::HasProperty(const String& name) const
  53. {
  54. if (!propertySet_)
  55. return false;
  56. return propertySet_->HasProperty(name);
  57. }
  58. const String& TmxLayer2D::GetProperty(const String& name) const
  59. {
  60. if (!propertySet_)
  61. return String::EMPTY;
  62. return propertySet_->GetProperty(name);
  63. }
  64. void TmxLayer2D::LoadInfo(const XMLElement& element)
  65. {
  66. name_ = element.GetAttribute("name");
  67. width_ = element.GetInt("width");
  68. height_ = element.GetInt("height");
  69. if (element.HasAttribute("visible"))
  70. visible_ = element.GetInt("visible") != 0;
  71. else
  72. visible_ = true;
  73. }
  74. void TmxLayer2D::LoadPropertySet(const XMLElement& element)
  75. {
  76. propertySet_ = new PropertySet2D();
  77. propertySet_->Load(element);
  78. }
  79. TmxTileLayer2D::TmxTileLayer2D(TmxFile2D* tmxFile) :
  80. TmxLayer2D(tmxFile, LT_TILE_LAYER)
  81. {
  82. }
  83. bool TmxTileLayer2D::Load(const XMLElement& element, const TileMapInfo2D& info)
  84. {
  85. LoadInfo(element);
  86. XMLElement dataElem = element.GetChild("data");
  87. if (!dataElem)
  88. {
  89. ATOMIC_LOGERROR("Could not find data in layer");
  90. return false;
  91. }
  92. if (dataElem.HasAttribute("encoding") && dataElem.GetAttribute("encoding") != "xml")
  93. {
  94. ATOMIC_LOGERROR("Encoding not support now");
  95. return false;
  96. }
  97. XMLElement tileElem = dataElem.GetChild("tile");
  98. tiles_.Resize((unsigned)(width_ * height_));
  99. for (int y = 0; y < height_; ++y)
  100. {
  101. for (int x = 0; x < width_; ++x)
  102. {
  103. if (!tileElem)
  104. return false;
  105. int gid = tileElem.GetInt("gid");
  106. if (gid > 0)
  107. {
  108. SharedPtr<Tile2D> tile(new Tile2D());
  109. tile->gid_ = gid;
  110. tile->sprite_ = tmxFile_->GetTileSprite(gid);
  111. tile->propertySet_ = tmxFile_->GetTilePropertySet(gid);
  112. // ATOMIC BEGIN
  113. tile->objectGroup_ = tmxFile_->GetTileObjectGroup(gid);
  114. // ATOMIC END
  115. tiles_[y * width_ + x] = tile;
  116. }
  117. tileElem = tileElem.GetNext("tile");
  118. }
  119. }
  120. if (element.HasChild("properties"))
  121. LoadPropertySet(element.GetChild("properties"));
  122. return true;
  123. }
  124. Tile2D* TmxTileLayer2D::GetTile(int x, int y) const
  125. {
  126. if (x < 0 || x >= width_ || y < 0 || y >= height_)
  127. return 0;
  128. return tiles_[y * width_ + x];
  129. }
  130. TmxObjectGroup2D::TmxObjectGroup2D(TmxFile2D* tmxFile) :
  131. TmxLayer2D(tmxFile, LT_OBJECT_GROUP)
  132. {
  133. }
  134. bool TmxObjectGroup2D::Load(const XMLElement& element, const TileMapInfo2D& info, bool local)
  135. {
  136. LoadInfo(element);
  137. for (XMLElement objectElem = element.GetChild("object"); objectElem; objectElem = objectElem.GetNext("object"))
  138. {
  139. SharedPtr<TileMapObject2D> object(new TileMapObject2D());
  140. // ATOMIC BEGIN
  141. if (objectElem.HasAttribute("name"))
  142. object->name_ = objectElem.GetAttribute("name");
  143. else
  144. object->name_ = "Object";
  145. // ATOMIC END
  146. if (objectElem.HasAttribute("type"))
  147. object->type_ = objectElem.GetAttribute("type");
  148. if (objectElem.HasAttribute("gid"))
  149. object->objectType_ = OT_TILE;
  150. else if (objectElem.HasChild("polygon"))
  151. object->objectType_ = OT_POLYGON;
  152. else if (objectElem.HasChild("polyline"))
  153. object->objectType_ = OT_POLYLINE;
  154. else if (objectElem.HasChild("ellipse"))
  155. object->objectType_ = OT_ELLIPSE;
  156. else
  157. object->objectType_ = OT_RECTANGLE;
  158. const Vector2 position(objectElem.GetFloat("x"), objectElem.GetFloat("y"));
  159. const Vector2 size(objectElem.GetFloat("width"), objectElem.GetFloat("height"));
  160. switch (object->objectType_)
  161. {
  162. case OT_RECTANGLE:
  163. case OT_ELLIPSE:
  164. // ATOMIC BEGIN
  165. object->size_ = Vector2(size.x_ * PIXEL_SIZE, size.y_ * PIXEL_SIZE);
  166. if (!local)
  167. {
  168. object->position_ = info.ConvertPosition(Vector2(position.x_, position.y_ + size.y_));
  169. }
  170. else
  171. {
  172. Vector2 nposition = position;
  173. nposition.x_ *= PIXEL_SIZE;
  174. nposition.y_ *= PIXEL_SIZE;
  175. nposition.x_ = nposition.x_ + object->size_.x_ / 2.0f;
  176. nposition.y_ = nposition.y_ + object->size_.y_ / 2.0f;
  177. nposition.y_ = info.tileHeight_ - nposition.y_;
  178. object->position_ = nposition;
  179. }
  180. // ATOMIC END
  181. break;
  182. case OT_TILE:
  183. object->position_ = info.ConvertPosition(position);
  184. object->gid_ = objectElem.GetInt("gid");
  185. object->sprite_ = tmxFile_->GetTileSprite(object->gid_);
  186. if (objectElem.HasAttribute("width") || objectElem.HasAttribute("height"))
  187. {
  188. object->size_ = Vector2(size.x_ * PIXEL_SIZE, size.y_ * PIXEL_SIZE);
  189. }
  190. else if (object->sprite_)
  191. {
  192. IntVector2 spriteSize = object->sprite_->GetRectangle().Size();
  193. object->size_ = Vector2(spriteSize.x_, spriteSize.y_);
  194. }
  195. break;
  196. case OT_POLYGON:
  197. case OT_POLYLINE:
  198. {
  199. Vector<String> points;
  200. const char* name = object->objectType_ == OT_POLYGON ? "polygon" : "polyline";
  201. XMLElement polygonElem = objectElem.GetChild(name);
  202. points = polygonElem.GetAttribute("points").Split(' ');
  203. if (points.Size() <= 1)
  204. continue;
  205. object->points_.Resize(points.Size());
  206. for (unsigned i = 0; i < points.Size(); ++i)
  207. {
  208. points[i].Replace(',', ' ');
  209. Vector2 point = position + ToVector2(points[i]);
  210. object->points_[i] = info.ConvertPosition(point);
  211. }
  212. }
  213. break;
  214. default: break;
  215. }
  216. if (objectElem.HasChild("properties"))
  217. {
  218. object->propertySet_ = new PropertySet2D();
  219. object->propertySet_->Load(objectElem.GetChild("properties"));
  220. }
  221. objects_.Push(object);
  222. }
  223. if (element.HasChild("properties"))
  224. LoadPropertySet(element.GetChild("properties"));
  225. return true;
  226. }
  227. TileMapObject2D* TmxObjectGroup2D::GetObject(unsigned index) const
  228. {
  229. if (index >= objects_.Size())
  230. return 0;
  231. return objects_[index];
  232. }
  233. TmxImageLayer2D::TmxImageLayer2D(TmxFile2D* tmxFile) :
  234. TmxLayer2D(tmxFile, LT_IMAGE_LAYER)
  235. {
  236. }
  237. bool TmxImageLayer2D::Load(const XMLElement& element, const TileMapInfo2D& info)
  238. {
  239. LoadInfo(element);
  240. XMLElement imageElem = element.GetChild("image");
  241. if (!imageElem)
  242. return false;
  243. position_ = Vector2(0.0f, info.GetMapHeight());
  244. source_ = imageElem.GetAttribute("source");
  245. String textureFilePath = GetParentPath(tmxFile_->GetName()) + source_;
  246. ResourceCache* cache = tmxFile_->GetSubsystem<ResourceCache>();
  247. SharedPtr<Texture2D> texture(cache->GetResource<Texture2D>(textureFilePath));
  248. if (!texture)
  249. {
  250. ATOMIC_LOGERROR("Could not load texture " + textureFilePath);
  251. return false;
  252. }
  253. sprite_ = new Sprite2D(tmxFile_->GetContext());
  254. sprite_->SetTexture(texture);
  255. sprite_->SetRectangle(IntRect(0, 0, texture->GetWidth(), texture->GetHeight()));
  256. // Set image hot spot at left top
  257. sprite_->SetHotSpot(Vector2(0.0f, 1.0f));
  258. if (element.HasChild("properties"))
  259. LoadPropertySet(element.GetChild("properties"));
  260. return true;
  261. }
  262. Sprite2D* TmxImageLayer2D::GetSprite() const
  263. {
  264. return sprite_;
  265. }
  266. TmxFile2D::TmxFile2D(Context* context) :
  267. Resource(context)
  268. {
  269. }
  270. TmxFile2D::~TmxFile2D()
  271. {
  272. // ATOMIC BEGIN
  273. // use shared ptr
  274. //for (unsigned i = 0; i < layers_.Size(); ++i)
  275. // delete layers_[i];
  276. // ATOMIC END
  277. }
  278. void TmxFile2D::RegisterObject(Context* context)
  279. {
  280. context->RegisterFactory<TmxFile2D>();
  281. }
  282. bool TmxFile2D::BeginLoad(Deserializer& source)
  283. {
  284. if (GetName().Empty())
  285. SetName(source.GetName());
  286. loadXMLFile_ = new XMLFile(context_);
  287. if (!loadXMLFile_->Load(source))
  288. {
  289. ATOMIC_LOGERROR("Load XML failed " + source.GetName());
  290. loadXMLFile_.Reset();
  291. return false;
  292. }
  293. XMLElement rootElem = loadXMLFile_->GetRoot("map");
  294. if (!rootElem)
  295. {
  296. ATOMIC_LOGERROR("Invalid tmx file " + source.GetName());
  297. loadXMLFile_.Reset();
  298. return false;
  299. }
  300. // If we're async loading, request the texture now. Finish during EndLoad().
  301. if (GetAsyncLoadState() == ASYNC_LOADING)
  302. {
  303. for (XMLElement tileSetElem = rootElem.GetChild("tileset"); tileSetElem; tileSetElem = tileSetElem.GetNext("tileset"))
  304. {
  305. // Tile set defined in TSX file
  306. if (tileSetElem.HasAttribute("source"))
  307. {
  308. String source = tileSetElem.GetAttribute("source");
  309. SharedPtr<XMLFile> tsxXMLFile = LoadTSXFile(source);
  310. if (!tsxXMLFile)
  311. return false;
  312. // ATOMIC BEGIN
  313. // Look for an image indicating that this is a spritesheet with multiple tiles instead
  314. // of a series of individual images which are not supported
  315. if (!tsxXMLFile->GetRoot("tileset").GetChild("image"))
  316. {
  317. ATOMIC_LOGERROR("Load TSX File failed: " + source + ". tsx files with individual images are not supported.");
  318. return false;
  319. }
  320. // ATOMIC END
  321. tsxXMLFiles_[source] = tsxXMLFile;
  322. String textureFilePath =
  323. GetParentPath(GetName()) + tsxXMLFile->GetRoot("tileset").GetChild("image").GetAttribute("source");
  324. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  325. }
  326. else
  327. {
  328. String textureFilePath = GetParentPath(GetName()) + tileSetElem.GetChild("image").GetAttribute("source");
  329. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  330. }
  331. }
  332. for (XMLElement imageLayerElem = rootElem.GetChild("imagelayer"); imageLayerElem;
  333. imageLayerElem = imageLayerElem.GetNext("imagelayer"))
  334. {
  335. String textureFilePath = GetParentPath(GetName()) + imageLayerElem.GetChild("image").GetAttribute("source");
  336. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  337. }
  338. }
  339. return true;
  340. }
  341. bool TmxFile2D::EndLoad()
  342. {
  343. if (!loadXMLFile_)
  344. return false;
  345. XMLElement rootElem = loadXMLFile_->GetRoot("map");
  346. String version = rootElem.GetAttribute("version");
  347. // ATOMIC BEGIN
  348. if (version != "1.0" && version != "1.0.0")
  349. // ATOMIC END
  350. {
  351. ATOMIC_LOGERROR("Invalid version");
  352. return false;
  353. }
  354. String orientation = rootElem.GetAttribute("orientation");
  355. if (orientation == "orthogonal")
  356. info_.orientation_ = O_ORTHOGONAL;
  357. else if (orientation == "isometric")
  358. info_.orientation_ = O_ISOMETRIC;
  359. else if (orientation == "staggered")
  360. info_.orientation_ = O_STAGGERED;
  361. else if (orientation == "hexagonal")
  362. info_.orientation_ = O_HEXAGONAL;
  363. else
  364. {
  365. ATOMIC_LOGERROR("Unsupported orientation type " + orientation);
  366. return false;
  367. }
  368. info_.width_ = rootElem.GetInt("width");
  369. info_.height_ = rootElem.GetInt("height");
  370. info_.tileWidth_ = rootElem.GetFloat("tilewidth") * PIXEL_SIZE;
  371. info_.tileHeight_ = rootElem.GetFloat("tileheight") * PIXEL_SIZE;
  372. for (unsigned i = 0; i < layers_.Size(); ++i)
  373. delete layers_[i];
  374. layers_.Clear();
  375. for (XMLElement childElement = rootElem.GetChild(); childElement; childElement = childElement.GetNext())
  376. {
  377. bool ret = true;
  378. String name = childElement.GetName();
  379. if (name == "tileset")
  380. ret = LoadTileSet(childElement);
  381. // ATOMIC BEGIN
  382. else if (name == "layer")
  383. {
  384. SharedPtr<TmxTileLayer2D> tileLayer (new TmxTileLayer2D(this));
  385. ret = tileLayer->Load(childElement, info_);
  386. layers_.Push(tileLayer);
  387. }
  388. else if (name == "objectgroup")
  389. {
  390. SharedPtr<TmxObjectGroup2D> objectGroup (new TmxObjectGroup2D(this));\
  391. ret = objectGroup->Load(childElement, info_);
  392. layers_.Push(objectGroup);
  393. }
  394. else if (name == "imagelayer")
  395. {
  396. SharedPtr<TmxImageLayer2D> imageLayer (new TmxImageLayer2D(this));
  397. ret = imageLayer->Load(childElement, info_);
  398. layers_.Push(imageLayer);
  399. }
  400. // ATOMIC END
  401. if (!ret)
  402. {
  403. loadXMLFile_.Reset();
  404. tsxXMLFiles_.Clear();
  405. return false;
  406. }
  407. }
  408. loadXMLFile_.Reset();
  409. tsxXMLFiles_.Clear();
  410. return true;
  411. }
  412. bool TmxFile2D::SetInfo(Orientation2D orientation, int width, int height, float tileWidth, float tileHeight)
  413. {
  414. if (layers_.Size() > 0)
  415. return false;
  416. info_.orientation_ = orientation;
  417. info_.width_ = width;
  418. info_.height_ = height;
  419. info_.tileWidth_ = tileWidth * PIXEL_SIZE;
  420. info_.tileHeight_ = tileHeight * PIXEL_SIZE;
  421. return true;
  422. }
  423. void TmxFile2D::AddLayer(unsigned index, TmxLayer2D *layer)
  424. {
  425. // ATOMIC BEGIN
  426. if (index > layers_.Size())
  427. layers_.Push(SharedPtr<TmxLayer2D>(layer));
  428. else // index <= layers_.size()
  429. layers_.Insert(index, SharedPtr<TmxLayer2D>(layer));
  430. // ATOMIC END
  431. }
  432. void TmxFile2D::AddLayer(TmxLayer2D *layer)
  433. {
  434. // ATOMIC BEGIN
  435. layers_.Push(SharedPtr<TmxLayer2D>(layer));
  436. // ATOMIC END
  437. }
  438. Sprite2D* TmxFile2D::GetTileSprite(int gid) const
  439. {
  440. HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = gidToSpriteMapping_.Find(gid);
  441. if (i == gidToSpriteMapping_.End())
  442. return 0;
  443. return i->second_;
  444. }
  445. PropertySet2D* TmxFile2D::GetTilePropertySet(int gid) const
  446. {
  447. HashMap<int, SharedPtr<PropertySet2D> >::ConstIterator i = gidToPropertySetMapping_.Find(gid);
  448. if (i == gidToPropertySetMapping_.End())
  449. return 0;
  450. return i->second_;
  451. }
  452. const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const
  453. {
  454. if (index >= layers_.Size())
  455. return 0;
  456. return layers_[index];
  457. }
  458. SharedPtr<XMLFile> TmxFile2D::LoadTSXFile(const String& source)
  459. {
  460. String tsxFilePath = GetParentPath(GetName()) + source;
  461. SharedPtr<File> tsxFile = GetSubsystem<ResourceCache>()->GetFile(tsxFilePath);
  462. SharedPtr<XMLFile> tsxXMLFile(new XMLFile(context_));
  463. if (!tsxFile || !tsxXMLFile->Load(*tsxFile))
  464. {
  465. ATOMIC_LOGERROR("Load TSX file failed " + tsxFilePath);
  466. return SharedPtr<XMLFile>();
  467. }
  468. return tsxXMLFile;
  469. }
  470. bool TmxFile2D::LoadTileSet(const XMLElement& element)
  471. {
  472. int firstgid = element.GetInt("firstgid");
  473. XMLElement tileSetElem;
  474. if (element.HasAttribute("source"))
  475. {
  476. String source = element.GetAttribute("source");
  477. HashMap<String, SharedPtr<XMLFile> >::Iterator i = tsxXMLFiles_.Find(source);
  478. if (i == tsxXMLFiles_.End())
  479. {
  480. SharedPtr<XMLFile> tsxXMLFile = LoadTSXFile(source);
  481. if (!tsxXMLFile)
  482. return false;
  483. // ATOMIC BEGIN
  484. // Look for an image indicating that this is a spritesheet with multiple tiles instead
  485. // of a series of individual images which are not supported
  486. if (!tsxXMLFile->GetRoot("tileset").GetChild("image"))
  487. {
  488. ATOMIC_LOGERROR("Load TSX File failed: " + source + ". tsx files with individual images are not supported.");
  489. return false;
  490. }
  491. // ATOMIC END
  492. // Add to napping to avoid release
  493. tsxXMLFiles_[source] = tsxXMLFile;
  494. tileSetElem = tsxXMLFile->GetRoot("tileset");
  495. }
  496. else
  497. tileSetElem = i->second_->GetRoot("tileset");
  498. }
  499. else
  500. tileSetElem = element;
  501. XMLElement imageElem = tileSetElem.GetChild("image");
  502. String textureFilePath = GetParentPath(GetName()) + imageElem.GetAttribute("source");
  503. ResourceCache* cache = GetSubsystem<ResourceCache>();
  504. SharedPtr<Texture2D> texture(cache->GetResource<Texture2D>(textureFilePath));
  505. if (!texture)
  506. {
  507. ATOMIC_LOGERROR("Could not load texture " + textureFilePath);
  508. return false;
  509. }
  510. // ATOMIC BEGIN
  511. // reduces border tile sample errors
  512. texture->SetFilterMode(FILTER_NEAREST);
  513. // ATOMIC END
  514. tileSetTextures_.Push(texture);
  515. int tileWidth = tileSetElem.GetInt("tilewidth");
  516. int tileHeight = tileSetElem.GetInt("tileheight");
  517. int spacing = tileSetElem.GetInt("spacing");
  518. int margin = tileSetElem.GetInt("margin");
  519. int imageWidth = imageElem.GetInt("width");
  520. int imageHeight = imageElem.GetInt("height");
  521. // Set hot spot at left bottom
  522. Vector2 hotSpot(0.0f, 0.0f);
  523. if (tileSetElem.HasChild("tileoffset"))
  524. {
  525. XMLElement offsetElem = tileSetElem.GetChild("tileoffset");
  526. hotSpot.x_ += offsetElem.GetFloat("x") / (float)tileWidth;
  527. hotSpot.y_ += offsetElem.GetFloat("y") / (float)tileHeight;
  528. }
  529. int gid = firstgid;
  530. for (int y = margin; y + tileHeight <= imageHeight - margin; y += tileHeight + spacing)
  531. {
  532. for (int x = margin; x + tileWidth <= imageWidth - margin; x += tileWidth + spacing)
  533. {
  534. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  535. sprite->SetTexture(texture);
  536. sprite->SetRectangle(IntRect(x, y, x + tileWidth, y + tileHeight));
  537. sprite->SetHotSpot(hotSpot);
  538. gidToSpriteMapping_[gid++] = sprite;
  539. }
  540. }
  541. for (XMLElement tileElem = tileSetElem.GetChild("tile"); tileElem; tileElem = tileElem.GetNext("tile"))
  542. {
  543. if (tileElem.HasChild("properties"))
  544. {
  545. SharedPtr<PropertySet2D> propertySet(new PropertySet2D());
  546. propertySet->Load(tileElem.GetChild("properties"));
  547. gidToPropertySetMapping_[firstgid + tileElem.GetInt("id")] = propertySet;
  548. }
  549. else if (tileElem.HasChild("objectgroup"))
  550. {
  551. XMLElement objectGroup = tileElem.GetChild("objectgroup");
  552. if (objectGroup.HasChild("properties"))
  553. {
  554. SharedPtr<PropertySet2D> propertySet(new PropertySet2D());
  555. propertySet->Load(objectGroup.GetChild("properties"));
  556. gidToPropertySetMapping_[firstgid + tileElem.GetInt("id")] = propertySet;
  557. }
  558. }
  559. // ATOMIC BEGIN
  560. // collision information
  561. if (tileElem.HasChild("objectgroup"))
  562. {
  563. // ok, when you use multiple tile sets the "info"
  564. // numbers can be different, this is a bit of a hack
  565. // if something is wrong, look here... may have to need
  566. // to refactor "info" to be per set
  567. float _tileWidth = info_.tileWidth_;
  568. float _tileHeight = info_.tileHeight_;
  569. info_.tileHeight_ = (float) tileHeight * PIXEL_SIZE;
  570. info_.tileWidth_ = (float) tileWidth * PIXEL_SIZE;
  571. XMLElement groupElem = tileElem.GetChild("objectgroup");
  572. TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this);
  573. if (!objectGroup->Load(groupElem, info_, true))
  574. {
  575. ATOMIC_LOGERROR("Could not load objectgroup");
  576. objectGroup->ReleaseRef();
  577. }
  578. else
  579. {
  580. gidToObjectGroupMapping_[firstgid + tileElem.GetInt("id")] = objectGroup;
  581. }
  582. info_.tileWidth_ = _tileWidth;
  583. info_.tileHeight_ = _tileHeight;
  584. }
  585. // ATOMIC END
  586. }
  587. return true;
  588. }
  589. // BEGIN ATOMIC
  590. TmxObjectGroup2D* TmxFile2D::GetTileObjectGroup(int gid) const
  591. {
  592. HashMap<int, SharedPtr<TmxObjectGroup2D> >::ConstIterator i = gidToObjectGroupMapping_.Find(gid);
  593. if (i == gidToObjectGroupMapping_.End())
  594. return 0;
  595. return i->second_;
  596. }
  597. // END ATOMIC
  598. }