TmxFile2D.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "FileSystem.h"
  25. #include "Log.h"
  26. #include "ResourceCache.h"
  27. #include "Sprite2D.h"
  28. #include "Texture2D.h"
  29. #include "TmxFile2D.h"
  30. #include "XMLFile.h"
  31. #include "DebugNew.h"
  32. namespace Urho3D
  33. {
  34. extern const float PIXEL_SIZE;
  35. TmxLayer2D::TmxLayer2D(TmxFile2D* tmxFile, TileMapLayerType2D type) :
  36. tmxFile_(tmxFile),
  37. type_(type)
  38. {
  39. }
  40. TmxLayer2D::~TmxLayer2D()
  41. {
  42. }
  43. TmxFile2D* TmxLayer2D::GetTmxFile() const
  44. {
  45. return tmxFile_;
  46. }
  47. bool TmxLayer2D::HasProperty(const String& name) const
  48. {
  49. if (!propertySet_)
  50. return false;
  51. return propertySet_->HasProperty(name);
  52. }
  53. const String& TmxLayer2D::GetProperty(const String& name) const
  54. {
  55. if (!propertySet_)
  56. return String::EMPTY;
  57. return propertySet_->GetProperty(name);
  58. }
  59. void TmxLayer2D::LoadInfo(const XMLElement& element)
  60. {
  61. name_ = element.GetAttribute("name");
  62. width_ = element.GetInt("width");
  63. height_ = element.GetInt("height");
  64. if (element.HasAttribute("visible"))
  65. visible_ = element.GetInt("visible") != 0;
  66. else
  67. visible_ = true;
  68. }
  69. void TmxLayer2D::LoadPropertySet(const XMLElement& element)
  70. {
  71. propertySet_ = new PropertySet2D();
  72. propertySet_->Load(element);
  73. }
  74. TmxTileLayer2D::TmxTileLayer2D(TmxFile2D* tmxFile) :
  75. TmxLayer2D(tmxFile, LT_TILE_LAYER)
  76. {
  77. }
  78. bool TmxTileLayer2D::Load(const XMLElement& element)
  79. {
  80. LoadInfo(element);
  81. XMLElement dataElem = element.GetChild("data");
  82. if (!dataElem)
  83. {
  84. LOGERROR("Could not find data in layer");
  85. return false;
  86. }
  87. if (dataElem.HasAttribute("encoding") && dataElem.GetAttribute("encoding") != "xml")
  88. {
  89. LOGERROR("Encoding not support now");
  90. return false;
  91. }
  92. XMLElement tileElem = dataElem.GetChild("tile");
  93. tiles_.Resize(width_ * height_);
  94. // Flip y
  95. for (int y = height_ - 1; y >= 0; --y)
  96. {
  97. for (int x = 0; x < width_; ++x)
  98. {
  99. if (!tileElem)
  100. return false;
  101. int gid = tileElem.GetInt("gid");
  102. if (gid > 0)
  103. {
  104. SharedPtr<Tile2D> tile(new Tile2D());
  105. tile->gid_ = gid;
  106. tile->sprite_ = tmxFile_->GetTileSprite(gid);
  107. tile->propertySet_ = tmxFile_->GetTilePropertySet(gid);
  108. tiles_[y * width_ + x] = tile;
  109. }
  110. tileElem = tileElem.GetNext("tile");
  111. }
  112. }
  113. if (element.HasChild("properties"))
  114. LoadPropertySet(element.GetChild("properties"));
  115. return true;
  116. }
  117. Tile2D* TmxTileLayer2D::GetTile(int x, int y) const
  118. {
  119. if (x < 0 || x > width_ || y < 0 || y > height_)
  120. return 0;
  121. return tiles_[y * width_ + x];
  122. }
  123. TmxObjectGroup2D::TmxObjectGroup2D(TmxFile2D* tmxFile) :
  124. TmxLayer2D(tmxFile, LT_OBJECT_GROUP)
  125. {
  126. }
  127. bool TmxObjectGroup2D::Load(const XMLElement& element)
  128. {
  129. LoadInfo(element);
  130. const float mapHeight = height_ * tmxFile_->GetTileHeight();
  131. for (XMLElement objectElem = element.GetChild("object"); objectElem; objectElem = objectElem.GetNext("object"))
  132. {
  133. SharedPtr<TileObject2D> object(new TileObject2D());
  134. object->position_ = Vector2(objectElem.GetInt("x") * PIXEL_SIZE, mapHeight - objectElem.GetInt("y") * PIXEL_SIZE);
  135. if (objectElem.HasAttribute("width") || objectElem.HasAttribute("height"))
  136. {
  137. if (!objectElem.HasChild("ellipse"))
  138. object->type_ = OT_RECTANGLE;
  139. else
  140. object->type_ = OT_ELLIPSE;
  141. object->size_ = Vector2(objectElem.GetInt("width") * PIXEL_SIZE, objectElem.GetInt("height") * PIXEL_SIZE);
  142. object->position_.y_ -= object->size_.y_;
  143. }
  144. else if (objectElem.HasAttribute("gid"))
  145. {
  146. object->type_ = OT_TILE;
  147. object->gid_ = objectElem.GetInt("gid");
  148. object->sprite_ = tmxFile_->GetTileSprite(object->gid_);
  149. }
  150. else
  151. {
  152. XMLElement childElem = objectElem.GetChild();
  153. if (childElem.GetName() == "polygon")
  154. object->type_ = OT_POLYGON;
  155. else if (childElem.GetName() == "polyline")
  156. object->type_ = OT_POLYLINE;
  157. else
  158. return false;
  159. Vector<String> points = childElem.GetAttribute("points").Split(' ');
  160. object->points_.Resize(points.Size());
  161. for (unsigned i = 0; i < points.Size(); ++i)
  162. {
  163. points[i].Replace(',', ' ');
  164. Vector2 point = ToVector2(points[i]) * PIXEL_SIZE;
  165. point.y_ = mapHeight - point.y_;
  166. object->points_[i] = point;
  167. }
  168. }
  169. if (objectElem.HasChild("properties"))
  170. {
  171. object->propertySet_ = new PropertySet2D();
  172. object->propertySet_->Load(objectElem.GetChild("properties"));
  173. }
  174. objects_.Push(object);
  175. }
  176. if (element.HasChild("properties"))
  177. LoadPropertySet(element.GetChild("properties"));
  178. return true;
  179. }
  180. TileObject2D* TmxObjectGroup2D::GetObject(unsigned index) const
  181. {
  182. if (index >= objects_.Size())
  183. return 0;
  184. return objects_[index];
  185. }
  186. TmxImageLayer2D::TmxImageLayer2D(TmxFile2D* tmxFile) :
  187. TmxLayer2D(tmxFile, LT_IMAGE_LAYER)
  188. {
  189. }
  190. bool TmxImageLayer2D::Load(const XMLElement& element)
  191. {
  192. LoadInfo(element);
  193. XMLElement imageElem = element.GetChild("image");
  194. if (!imageElem)
  195. return false;
  196. source_ = imageElem.GetAttribute("source");
  197. String textureFilePath = GetParentPath(GetName()) + source_;
  198. ResourceCache* cache = tmxFile_->GetSubsystem<ResourceCache>();
  199. SharedPtr<Texture2D> texture(cache->GetResource<Texture2D>(textureFilePath));
  200. if (!texture)
  201. {
  202. LOGERROR("Could not load texture " + textureFilePath);
  203. return false;
  204. }
  205. sprite_ = new Sprite2D(tmxFile_->GetContext());
  206. sprite_->SetTexture(texture);
  207. sprite_->SetRectangle(IntRect(0, 0, texture->GetWidth(), texture->GetHeight()));
  208. // Left top
  209. sprite_->SetHotSpot(Vector2(0.0f, 1.0f));
  210. if (element.HasChild("properties"))
  211. LoadPropertySet(element.GetChild("properties"));
  212. return true;
  213. }
  214. Sprite2D* TmxImageLayer2D::GetSprite() const
  215. {
  216. return sprite_;
  217. }
  218. TmxFile2D::TmxFile2D(Context* context) :
  219. Resource(context),
  220. width_(0),
  221. height_(0),
  222. tileWidth_(0.0f),
  223. tileHeight_(0.0f)
  224. {
  225. }
  226. TmxFile2D::~TmxFile2D()
  227. {
  228. for (unsigned i = 0; i < layers_.Size(); ++i)
  229. delete layers_[i];
  230. }
  231. void TmxFile2D::RegisterObject(Context* context)
  232. {
  233. context->RegisterFactory<TmxFile2D>();
  234. }
  235. bool TmxFile2D::BeginLoad(Deserializer& source)
  236. {
  237. loadXMLFile_ = new XMLFile(context_);
  238. if (!loadXMLFile_->Load(source))
  239. {
  240. LOGERROR("Load XML failed " + source.GetName());
  241. loadXMLFile_.Reset();
  242. return false;
  243. }
  244. XMLElement rootElem = loadXMLFile_->GetRoot("map");
  245. if (!rootElem)
  246. {
  247. LOGERROR("Invalid tmx file " + source.GetName());
  248. loadXMLFile_.Reset();
  249. return false;
  250. }
  251. // If we're async loading, request the texture now. Finish during EndLoad().
  252. if (GetAsyncLoadState() == ASYNC_LOADING)
  253. {
  254. for (XMLElement tileSetElem = rootElem.GetChild("tileset"); tileSetElem; tileSetElem = tileSetElem.GetNext("tileset"))
  255. {
  256. // Tile set defined in TSX file
  257. if (tileSetElem.HasAttribute("source"))
  258. {
  259. String source = tileSetElem.GetAttribute("source");
  260. SharedPtr<XMLFile> tsxXMLFile = LoadTSXFile(source);
  261. if (!tsxXMLFile)
  262. return false;
  263. tsxXMLFiles_[source] = tsxXMLFile;
  264. String textureFilePath = GetParentPath(GetName()) + tsxXMLFile->GetRoot("tileset").GetChild("image").GetAttribute("source");
  265. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  266. }
  267. else
  268. {
  269. String textureFilePath = GetParentPath(GetName()) + tileSetElem.GetChild("image").GetAttribute("source");
  270. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  271. }
  272. }
  273. for (XMLElement imageLayerElem = rootElem.GetChild("imagelayer"); imageLayerElem; imageLayerElem = imageLayerElem.GetNext("imagelayer"))
  274. {
  275. String textureFilePath = GetParentPath(GetName()) + imageLayerElem.GetChild("image").GetAttribute("source");
  276. GetSubsystem<ResourceCache>()->BackgroundLoadResource<Texture2D>(textureFilePath, true, this);
  277. }
  278. }
  279. return true;
  280. }
  281. bool TmxFile2D::EndLoad()
  282. {
  283. if (!loadXMLFile_)
  284. return false;
  285. XMLElement rootElem = loadXMLFile_->GetRoot("map");
  286. String version = rootElem.GetAttribute("version");
  287. if (version != "1.0")
  288. {
  289. LOGERROR("Invalid version");
  290. return false;
  291. }
  292. String orientation = rootElem.GetAttribute("orientation");
  293. if (orientation == "orthogonal")
  294. orientation_ = O_ORTHOGONAL;
  295. else if (orientation == "isometric")
  296. orientation_ = O_ISOMETRIC;
  297. else
  298. {
  299. LOGERROR("Invalid orientation type " + orientation);
  300. return false;
  301. }
  302. width_ = rootElem.GetInt("width");
  303. height_ = rootElem.GetInt("height");
  304. tileWidth_ = rootElem.GetFloat("tilewidth") * PIXEL_SIZE;
  305. tileHeight_ = rootElem.GetFloat("tileheight") * PIXEL_SIZE;
  306. for (unsigned i = 0; i < layers_.Size(); ++i)
  307. delete layers_[i];
  308. layers_.Clear();
  309. for (XMLElement childElement = rootElem.GetChild(); childElement; childElement = childElement.GetNext())
  310. {
  311. bool ret = true;
  312. String name = childElement.GetName();
  313. if (name == "tileset")
  314. ret = LoadTileSet(childElement);
  315. else if (name == "layer")
  316. {
  317. TmxTileLayer2D* tileLayer = new TmxTileLayer2D(this);
  318. ret = tileLayer->Load(childElement);
  319. layers_.Push(tileLayer);
  320. }
  321. else if (name == "objectgroup")
  322. {
  323. TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this);
  324. ret = objectGroup->Load(childElement);
  325. layers_.Push(objectGroup);
  326. }
  327. else if (name == "imagelayer")
  328. {
  329. TmxImageLayer2D* imageLayer = new TmxImageLayer2D(this);
  330. ret = imageLayer->Load(childElement);
  331. layers_.Push(imageLayer);
  332. }
  333. if (!ret)
  334. {
  335. loadXMLFile_.Reset();
  336. tsxXMLFiles_.Clear();
  337. return false;
  338. }
  339. }
  340. loadXMLFile_.Reset();
  341. tsxXMLFiles_.Clear();
  342. return true;
  343. }
  344. Sprite2D* TmxFile2D::GetTileSprite(int gid) const
  345. {
  346. HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = gidToSpriteMapping_.Find(gid);
  347. if (i == gidToSpriteMapping_.End())
  348. return 0;
  349. return i->second_;
  350. }
  351. PropertySet2D* TmxFile2D::GetTilePropertySet(int gid) const
  352. {
  353. HashMap<int, SharedPtr<PropertySet2D> >::ConstIterator i = gidToPropertySetMapping_.Find(gid);
  354. if (i == gidToPropertySetMapping_.End())
  355. return 0;
  356. return i->second_;
  357. }
  358. const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const
  359. {
  360. if (index >= layers_.Size())
  361. return 0;
  362. return layers_[index];
  363. }
  364. SharedPtr<XMLFile> TmxFile2D::LoadTSXFile(const String& source)
  365. {
  366. String tsxFilePath = GetParentPath(GetName()) + source;
  367. SharedPtr<File> tsxFile = GetSubsystem<ResourceCache>()->GetFile(tsxFilePath);
  368. SharedPtr<XMLFile> tsxXMLFile(new XMLFile(context_));
  369. if (!tsxFile || !tsxXMLFile->Load(*tsxFile))
  370. {
  371. LOGERROR("Load TSX file failed " + tsxFilePath);
  372. return SharedPtr<XMLFile>();
  373. }
  374. return tsxXMLFile;
  375. }
  376. bool TmxFile2D::LoadTileSet(const XMLElement& element)
  377. {
  378. int firstgid = element.GetInt("firstgid");
  379. XMLElement tileSetElem;
  380. if (element.HasAttribute("source"))
  381. {
  382. String source = element.GetAttribute("source");
  383. HashMap<String, SharedPtr<XMLFile> >::Iterator i = tsxXMLFiles_.Find(source);
  384. if (i == tsxXMLFiles_.End())
  385. {
  386. SharedPtr<XMLFile> tsxXMLFile = LoadTSXFile(source);
  387. if (!tsxXMLFile)
  388. return false;
  389. // Add to napping to avoid release
  390. tsxXMLFiles_[source] = tsxXMLFile;
  391. tileSetElem = tsxXMLFile->GetRoot("tileset");
  392. }
  393. else
  394. tileSetElem = i->second_->GetRoot("tileset");
  395. }
  396. else
  397. tileSetElem = element;
  398. XMLElement imageElem = tileSetElem.GetChild("image");
  399. String textureFilePath = GetParentPath(GetName()) + imageElem.GetAttribute("source");
  400. ResourceCache* cache = GetSubsystem<ResourceCache>();
  401. SharedPtr<Texture2D> texture(cache->GetResource<Texture2D>(textureFilePath));
  402. if (!texture)
  403. {
  404. LOGERROR("Could not load texture " + textureFilePath);
  405. return false;
  406. }
  407. tileSetTextures_.Push(texture);
  408. int tileWidth = tileSetElem.GetInt("tilewidth");
  409. int tileHeight = tileSetElem.GetInt("tileheight");
  410. int spacing = tileSetElem.GetInt("spacing");
  411. int margin = tileSetElem.GetInt("margin");
  412. int imageWidth = imageElem.GetInt("width");
  413. int imageHeight = imageElem.GetInt("height");
  414. int gid = firstgid;
  415. for (int y = margin; y < imageHeight - margin; y += tileHeight + spacing)
  416. {
  417. for (int x = margin; x < imageWidth - margin; x += tileWidth + spacing)
  418. {
  419. SharedPtr<Sprite2D> sprite(new Sprite2D(context_));
  420. sprite->SetTexture(texture);
  421. sprite->SetRectangle(IntRect(x, y, x + tileWidth, y + tileHeight));
  422. gidToSpriteMapping_[gid++] = sprite;
  423. }
  424. }
  425. for (XMLElement tileElem = tileSetElem.GetChild("tile"); tileElem; tileElem = tileElem.GetNext("tile"))
  426. {
  427. if (tileElem.HasChild("properties"))
  428. {
  429. SharedPtr<PropertySet2D> propertySet(new PropertySet2D());
  430. propertySet->Load(tileElem.GetChild("properties"));
  431. gidToPropertySetMapping_[firstgid + tileElem.GetInt("id")] = propertySet;
  432. }
  433. }
  434. return true;
  435. }
  436. }