TmxFile2D.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. if (tsxXMLFile->GetRoot("tileset").GetChild("tile"))
  314. {
  315. ATOMIC_LOGERROR("Load TSX File failed: " + source + ". tsx files with individual images are not supported.");
  316. return false;
  317. }
  318. // ATOMIC END
  319. tsxXMLFiles_[source] = tsxXMLFile;
  320. String textureFilePath =
  321. GetParentPath(GetName()) + tsxXMLFile->GetRoot("tileset").GetChild("image").GetAttribute("source");
  322. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  323. }
  324. else
  325. {
  326. String textureFilePath = GetParentPath(GetName()) + tileSetElem.GetChild("image").GetAttribute("source");
  327. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  328. }
  329. }
  330. for (XMLElement imageLayerElem = rootElem.GetChild("imagelayer"); imageLayerElem;
  331. imageLayerElem = imageLayerElem.GetNext("imagelayer"))
  332. {
  333. String textureFilePath = GetParentPath(GetName()) + imageLayerElem.GetChild("image").GetAttribute("source");
  334. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  335. }
  336. }
  337. return true;
  338. }
  339. bool TmxFile2D::EndLoad()
  340. {
  341. if (!loadXMLFile_)
  342. return false;
  343. XMLElement rootElem = loadXMLFile_->GetRoot("map");
  344. String version = rootElem.GetAttribute("version");
  345. // ATOMIC BEGIN
  346. if (version != "1.0" && version != "1.0.0")
  347. // ATOMIC END
  348. {
  349. ATOMIC_LOGERROR("Invalid version");
  350. return false;
  351. }
  352. String orientation = rootElem.GetAttribute("orientation");
  353. if (orientation == "orthogonal")
  354. info_.orientation_ = O_ORTHOGONAL;
  355. else if (orientation == "isometric")
  356. info_.orientation_ = O_ISOMETRIC;
  357. else if (orientation == "staggered")
  358. info_.orientation_ = O_STAGGERED;
  359. else if (orientation == "hexagonal")
  360. info_.orientation_ = O_HEXAGONAL;
  361. else
  362. {
  363. ATOMIC_LOGERROR("Unsupported orientation type " + orientation);
  364. return false;
  365. }
  366. info_.width_ = rootElem.GetInt("width");
  367. info_.height_ = rootElem.GetInt("height");
  368. info_.tileWidth_ = rootElem.GetFloat("tilewidth") * PIXEL_SIZE;
  369. info_.tileHeight_ = rootElem.GetFloat("tileheight") * PIXEL_SIZE;
  370. for (unsigned i = 0; i < layers_.Size(); ++i)
  371. delete layers_[i];
  372. layers_.Clear();
  373. for (XMLElement childElement = rootElem.GetChild(); childElement; childElement = childElement.GetNext())
  374. {
  375. bool ret = true;
  376. String name = childElement.GetName();
  377. if (name == "tileset")
  378. ret = LoadTileSet(childElement);
  379. // ATOMIC BEGIN
  380. else if (name == "layer")
  381. {
  382. SharedPtr<TmxTileLayer2D> tileLayer (new TmxTileLayer2D(this));
  383. ret = tileLayer->Load(childElement, info_);
  384. layers_.Push(tileLayer);
  385. }
  386. else if (name == "objectgroup")
  387. {
  388. SharedPtr<TmxObjectGroup2D> objectGroup (new TmxObjectGroup2D(this));\
  389. ret = objectGroup->Load(childElement, info_);
  390. layers_.Push(objectGroup);
  391. }
  392. else if (name == "imagelayer")
  393. {
  394. SharedPtr<TmxImageLayer2D> imageLayer (new TmxImageLayer2D(this));
  395. ret = imageLayer->Load(childElement, info_);
  396. layers_.Push(imageLayer);
  397. }
  398. // ATOMIC END
  399. if (!ret)
  400. {
  401. loadXMLFile_.Reset();
  402. tsxXMLFiles_.Clear();
  403. return false;
  404. }
  405. }
  406. loadXMLFile_.Reset();
  407. tsxXMLFiles_.Clear();
  408. return true;
  409. }
  410. bool TmxFile2D::SetInfo(Orientation2D orientation, int width, int height, float tileWidth, float tileHeight)
  411. {
  412. if (layers_.Size() > 0)
  413. return false;
  414. info_.orientation_ = orientation;
  415. info_.width_ = width;
  416. info_.height_ = height;
  417. info_.tileWidth_ = tileWidth * PIXEL_SIZE;
  418. info_.tileHeight_ = tileHeight * PIXEL_SIZE;
  419. return true;
  420. }
  421. void TmxFile2D::AddLayer(unsigned index, TmxLayer2D *layer)
  422. {
  423. // ATOMIC BEGIN
  424. if (index > layers_.Size())
  425. layers_.Push(SharedPtr<TmxLayer2D>(layer));
  426. else // index <= layers_.size()
  427. layers_.Insert(index, SharedPtr<TmxLayer2D>(layer));
  428. // ATOMIC END
  429. }
  430. void TmxFile2D::AddLayer(TmxLayer2D *layer)
  431. {
  432. // ATOMIC BEGIN
  433. layers_.Push(SharedPtr<TmxLayer2D>(layer));
  434. // ATOMIC END
  435. }
  436. Sprite2D* TmxFile2D::GetTileSprite(int gid) const
  437. {
  438. HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = gidToSpriteMapping_.Find(gid);
  439. if (i == gidToSpriteMapping_.End())
  440. return 0;
  441. return i->second_;
  442. }
  443. PropertySet2D* TmxFile2D::GetTilePropertySet(int gid) const
  444. {
  445. HashMap<int, SharedPtr<PropertySet2D> >::ConstIterator i = gidToPropertySetMapping_.Find(gid);
  446. if (i == gidToPropertySetMapping_.End())
  447. return 0;
  448. return i->second_;
  449. }
  450. const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const
  451. {
  452. if (index >= layers_.Size())
  453. return 0;
  454. return layers_[index];
  455. }
  456. SharedPtr<XMLFile> TmxFile2D::LoadTSXFile(const String& source)
  457. {
  458. String tsxFilePath = GetParentPath(GetName()) + source;
  459. SharedPtr<File> tsxFile = GetSubsystem<ResourceCache>()->GetFile(tsxFilePath);
  460. SharedPtr<XMLFile> tsxXMLFile(new XMLFile(context_));
  461. if (!tsxFile || !tsxXMLFile->Load(*tsxFile))
  462. {
  463. ATOMIC_LOGERROR("Load TSX file failed " + tsxFilePath);
  464. return SharedPtr<XMLFile>();
  465. }
  466. return tsxXMLFile;
  467. }
  468. bool TmxFile2D::LoadTileSet(const XMLElement& element)
  469. {
  470. int firstgid = element.GetInt("firstgid");
  471. XMLElement tileSetElem;
  472. if (element.HasAttribute("source"))
  473. {
  474. String source = element.GetAttribute("source");
  475. HashMap<String, SharedPtr<XMLFile> >::Iterator i = tsxXMLFiles_.Find(source);
  476. if (i == tsxXMLFiles_.End())
  477. {
  478. SharedPtr<XMLFile> tsxXMLFile = LoadTSXFile(source);
  479. if (!tsxXMLFile)
  480. return false;
  481. // ATOMIC BEGIN
  482. if (tsxXMLFile->GetRoot("tileset").GetChild("tile"))
  483. {
  484. ATOMIC_LOGERROR("Load TSX File failed: " + source + ". tsx files with individual images are not supported.");
  485. return false;
  486. }
  487. // ATOMIC END
  488. // Add to napping to avoid release
  489. tsxXMLFiles_[source] = tsxXMLFile;
  490. tileSetElem = tsxXMLFile->GetRoot("tileset");
  491. }
  492. else
  493. tileSetElem = i->second_->GetRoot("tileset");
  494. }
  495. else
  496. tileSetElem = element;
  497. XMLElement imageElem = tileSetElem.GetChild("image");
  498. String textureFilePath = GetParentPath(GetName()) + imageElem.GetAttribute("source");
  499. ResourceCache* cache = GetSubsystem<ResourceCache>();
  500. SharedPtr<Texture2D> texture(cache->GetResource<Texture2D>(textureFilePath));
  501. if (!texture)
  502. {
  503. ATOMIC_LOGERROR("Could not load texture " + textureFilePath);
  504. return false;
  505. }
  506. // ATOMIC BEGIN
  507. // reduces border tile sample errors
  508. texture->SetFilterMode(FILTER_NEAREST);
  509. // ATOMIC END
  510. tileSetTextures_.Push(texture);
  511. int tileWidth = tileSetElem.GetInt("tilewidth");
  512. int tileHeight = tileSetElem.GetInt("tileheight");
  513. int spacing = tileSetElem.GetInt("spacing");
  514. int margin = tileSetElem.GetInt("margin");
  515. int imageWidth = imageElem.GetInt("width");
  516. int imageHeight = imageElem.GetInt("height");
  517. // Set hot spot at left bottom
  518. Vector2 hotSpot(0.0f, 0.0f);
  519. if (tileSetElem.HasChild("tileoffset"))
  520. {
  521. XMLElement offsetElem = tileSetElem.GetChild("tileoffset");
  522. hotSpot.x_ += offsetElem.GetFloat("x") / (float)tileWidth;
  523. hotSpot.y_ += offsetElem.GetFloat("y") / (float)tileHeight;
  524. }
  525. int gid = firstgid;
  526. for (int y = margin; y + tileHeight <= imageHeight - margin; y += tileHeight + spacing)
  527. {
  528. for (int x = margin; x + tileWidth <= imageWidth - margin; x += tileWidth + spacing)
  529. {
  530. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  531. sprite->SetTexture(texture);
  532. sprite->SetRectangle(IntRect(x, y, x + tileWidth, y + tileHeight));
  533. sprite->SetHotSpot(hotSpot);
  534. gidToSpriteMapping_[gid++] = sprite;
  535. }
  536. }
  537. for (XMLElement tileElem = tileSetElem.GetChild("tile"); tileElem; tileElem = tileElem.GetNext("tile"))
  538. {
  539. if (tileElem.HasChild("properties"))
  540. {
  541. SharedPtr<PropertySet2D> propertySet(new PropertySet2D());
  542. propertySet->Load(tileElem.GetChild("properties"));
  543. gidToPropertySetMapping_[firstgid + tileElem.GetInt("id")] = propertySet;
  544. }
  545. // ATOMIC BEGIN
  546. // collision information
  547. if (tileElem.HasChild("objectgroup"))
  548. {
  549. // ok, when you use multiple tile sets the "info"
  550. // numbers can be different, this is a bit of a hack
  551. // if something is wrong, look here... may have to need
  552. // to refactor "info" to be per set
  553. float _tileWidth = info_.tileWidth_;
  554. float _tileHeight = info_.tileHeight_;
  555. info_.tileHeight_ = (float) tileHeight * PIXEL_SIZE;
  556. info_.tileWidth_ = (float) tileWidth * PIXEL_SIZE;
  557. XMLElement groupElem = tileElem.GetChild("objectgroup");
  558. TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this);
  559. if (!objectGroup->Load(groupElem, info_, true))
  560. {
  561. ATOMIC_LOGERROR("Could not load objectgroup");
  562. objectGroup->ReleaseRef();
  563. }
  564. else
  565. {
  566. gidToObjectGroupMapping_[firstgid + tileElem.GetInt("id")] = objectGroup;
  567. }
  568. info_.tileWidth_ = _tileWidth;
  569. info_.tileHeight_ = _tileHeight;
  570. }
  571. // ATOMIC END
  572. }
  573. return true;
  574. }
  575. // BEGIN ATOMIC
  576. TmxObjectGroup2D* TmxFile2D::GetTileObjectGroup(int gid) const
  577. {
  578. HashMap<int, SharedPtr<TmxObjectGroup2D> >::ConstIterator i = gidToObjectGroupMapping_.Find(gid);
  579. if (i == gidToObjectGroupMapping_.End())
  580. return 0;
  581. return i->second_;
  582. }
  583. // END ATOMIC
  584. }