Scene.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Component.h"
  25. #include "Context.h"
  26. #include "CoreEvents.h"
  27. #include "File.h"
  28. #include "Log.h"
  29. #include "PackageFile.h"
  30. #include "Profiler.h"
  31. #include "Scene.h"
  32. #include "SceneEvents.h"
  33. #include "WorkQueue.h"
  34. #include "XMLFile.h"
  35. static const int ASYNC_LOAD_MIN_FPS = 50;
  36. static const int ASYNC_LOAD_MAX_MSEC = (int)(1000.0f / ASYNC_LOAD_MIN_FPS);
  37. static const float DEFAULT_SMOOTHING_CONSTANT = 50.0f;
  38. static const float DEFAULT_SNAP_THRESHOLD = 1.0f;
  39. static const String emptyVarName;
  40. OBJECTTYPESTATIC(Scene);
  41. Scene::Scene(Context* context) :
  42. Node(context),
  43. replicatedNodeID_(FIRST_REPLICATED_ID),
  44. replicatedComponentID_(FIRST_REPLICATED_ID),
  45. localNodeID_(FIRST_LOCAL_ID),
  46. localComponentID_(FIRST_LOCAL_ID),
  47. smoothingConstant_(DEFAULT_SMOOTHING_CONSTANT),
  48. snapThreshold_(DEFAULT_SNAP_THRESHOLD),
  49. checksum_(0),
  50. active_(true),
  51. asyncLoading_(false),
  52. threadedUpdate_(false)
  53. {
  54. // Assign an ID to self so that nodes can refer to this node as a parent
  55. SetID(GetFreeNodeID(REPLICATED));
  56. NodeAdded(this);
  57. SubscribeToEvent(E_UPDATE, HANDLER(Scene, HandleUpdate));
  58. }
  59. Scene::~Scene()
  60. {
  61. // Remove scene reference and owner from all nodes that still exist
  62. for (Map<unsigned, Node*>::Iterator i = allNodes_.Begin(); i != allNodes_.End(); ++i)
  63. {
  64. i->second_->SetScene(0);
  65. i->second_->SetOwner(0);
  66. }
  67. }
  68. void Scene::RegisterObject(Context* context)
  69. {
  70. context->RegisterFactory<Scene>();
  71. REF_ACCESSOR_ATTRIBUTE(Scene, VAR_STRING, "Name", GetName, SetName, String, String(), AM_DEFAULT);
  72. REF_ACCESSOR_ATTRIBUTE(Scene, VAR_VECTOR3, "Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  73. REF_ACCESSOR_ATTRIBUTE(Scene, VAR_QUATERNION, "Rotation", GetRotation, SetRotation, Quaternion, Quaternion::IDENTITY, AM_FILE);
  74. REF_ACCESSOR_ATTRIBUTE(Scene, VAR_VECTOR3, "Scale", GetScale, SetScale, Vector3, Vector3::ONE, AM_DEFAULT);
  75. ACCESSOR_ATTRIBUTE(Scene, VAR_FLOAT, "Smoothing Constant", GetSmoothingConstant, SetSmoothingConstant, float, DEFAULT_SMOOTHING_CONSTANT, AM_DEFAULT);
  76. ACCESSOR_ATTRIBUTE(Scene, VAR_FLOAT, "Snap Threshold", GetSnapThreshold, SetSnapThreshold, float, DEFAULT_SNAP_THRESHOLD, AM_DEFAULT);
  77. ATTRIBUTE(Scene, VAR_INT, "Next Replicated Node ID", replicatedNodeID_, FIRST_REPLICATED_ID, AM_FILE | AM_NOEDIT);
  78. ATTRIBUTE(Scene, VAR_INT, "Next Replicated Component ID", replicatedComponentID_, FIRST_REPLICATED_ID, AM_FILE | AM_NOEDIT);
  79. ATTRIBUTE(Scene, VAR_INT, "Next Local Node ID", localNodeID_, FIRST_LOCAL_ID, AM_FILE | AM_NOEDIT);
  80. ATTRIBUTE(Scene, VAR_INT, "Next Local Component ID", localComponentID_, FIRST_LOCAL_ID, AM_FILE | AM_NOEDIT);
  81. ATTRIBUTE(Scene, VAR_VARIANTMAP, "Variables", vars_, VariantMap(), AM_FILE); // Network replication of vars uses custom data
  82. ACCESSOR_ATTRIBUTE(Scene, VAR_STRING, "Variable Names", GetVarNamesAttr, SetVarNamesAttr, String, String(), AM_FILE | AM_NOEDIT);
  83. REF_ACCESSOR_ATTRIBUTE(Scene, VAR_BUFFER, "Network Rotation", GetNetRotationAttr, SetNetRotationAttr, PODVector<unsigned char>, PODVector<unsigned char>(), AM_NET | AM_LATESTDATA | AM_NOEDIT);
  84. }
  85. bool Scene::Load(Deserializer& source)
  86. {
  87. StopAsyncLoading();
  88. // Check ID
  89. if (source.ReadFileID() != "USCN")
  90. {
  91. LOGERROR(source.GetName() + " is not a valid scene file");
  92. return false;
  93. }
  94. LOGINFO("Loading scene from " + source.GetName());
  95. // Load the whole scene, then perform post-load if successfully loaded
  96. if (Node::Load(source))
  97. {
  98. FinishLoading(&source);
  99. return true;
  100. }
  101. else
  102. return false;
  103. }
  104. bool Scene::Save(Serializer& dest)
  105. {
  106. // Write ID first
  107. if (!dest.WriteFileID("USCN"))
  108. {
  109. LOGERROR("Could not save scene, writing to stream failed");
  110. return false;
  111. }
  112. Deserializer* ptr = dynamic_cast<Deserializer*>(&dest);
  113. if (ptr)
  114. LOGINFO("Saving scene to " + ptr->GetName());
  115. return Node::Save(dest);
  116. }
  117. bool Scene::LoadXML(const XMLElement& source)
  118. {
  119. StopAsyncLoading();
  120. // Load the whole scene, then perform post-load if successfully loaded
  121. // Note: the scene filename and checksum can not be set, as we only used an XML element
  122. if (Node::LoadXML(source))
  123. {
  124. FinishLoading(0);
  125. return true;
  126. }
  127. else
  128. return false;
  129. }
  130. bool Scene::LoadXML(Deserializer& source)
  131. {
  132. StopAsyncLoading();
  133. SharedPtr<XMLFile> xml(new XMLFile(context_));
  134. if (!xml->Load(source))
  135. return false;
  136. LOGINFO("Loading scene from " + source.GetName());
  137. // Load the whole scene, then perform post-load if successfully loaded
  138. if (Node::LoadXML(xml->GetRoot()))
  139. {
  140. FinishLoading(&source);
  141. return true;
  142. }
  143. else
  144. return false;
  145. }
  146. bool Scene::SaveXML(Serializer& dest)
  147. {
  148. SharedPtr<XMLFile> xml(new XMLFile(context_));
  149. XMLElement rootElem = xml->CreateRoot("scene");
  150. if (!SaveXML(rootElem))
  151. return false;
  152. Deserializer* ptr = dynamic_cast<Deserializer*>(&dest);
  153. if (ptr)
  154. LOGINFO("Saving scene to " + ptr->GetName());
  155. return xml->Save(dest);
  156. }
  157. bool Scene::LoadAsync(File* file)
  158. {
  159. if (!file)
  160. {
  161. LOGERROR("Null file for async loading");
  162. return false;
  163. }
  164. StopAsyncLoading();
  165. // Check ID
  166. if (file->ReadFileID() != "USCN")
  167. {
  168. LOGERROR(file->GetName() + " is not a valid scene file");
  169. return false;
  170. }
  171. LOGINFO("Loading scene from " + file->GetName());
  172. Clear();
  173. // Store own old ID for resolving possible root node references
  174. unsigned nodeID = file->ReadUInt();
  175. resolver_.AddNode(nodeID, this);
  176. // Load root level components first
  177. if (!Node::Load(*file, resolver_, false))
  178. return false;
  179. // Then prepare for loading all root level child nodes in the async update
  180. asyncLoading_ = true;
  181. asyncProgress_.file_ = file;
  182. asyncProgress_.xmlFile_.Reset();
  183. asyncProgress_.xmlElement_ = XMLElement();
  184. asyncProgress_.loadedNodes_ = 0;
  185. asyncProgress_.totalNodes_ = file->ReadVLE();
  186. return true;
  187. }
  188. bool Scene::LoadAsyncXML(File* file)
  189. {
  190. if (!file)
  191. {
  192. LOGERROR("Null file for async loading");
  193. return false;
  194. }
  195. StopAsyncLoading();
  196. SharedPtr<XMLFile> xml(new XMLFile(context_));
  197. if (!xml->Load(*file))
  198. return false;
  199. LOGINFO("Loading scene from " + file->GetName());
  200. Clear();
  201. XMLElement rootElement = xml->GetRoot();
  202. // Store own old ID for resolving possible root node references
  203. unsigned nodeID = rootElement.GetInt("id");
  204. resolver_.AddNode(nodeID, this);
  205. // Load the root level components first
  206. if (!Node::LoadXML(rootElement, resolver_, false))
  207. return false;
  208. // Then prepare for loading all root level child nodes in the async update
  209. XMLElement childNodeElement = rootElement.GetChild("node");
  210. asyncLoading_ = true;
  211. asyncProgress_.file_ = file;
  212. asyncProgress_.xmlFile_ = xml;
  213. asyncProgress_.xmlElement_ = childNodeElement;
  214. asyncProgress_.loadedNodes_ = 0;
  215. asyncProgress_.totalNodes_ = 0;
  216. // Count the amount of child nodes
  217. while (childNodeElement)
  218. {
  219. ++asyncProgress_.totalNodes_;
  220. childNodeElement = childNodeElement.GetNext("node");
  221. }
  222. return true;
  223. }
  224. void Scene::StopAsyncLoading()
  225. {
  226. asyncLoading_ = false;
  227. asyncProgress_.file_.Reset();
  228. asyncProgress_.xmlFile_.Reset();
  229. asyncProgress_.xmlElement_ = XMLElement();
  230. resolver_.Reset();
  231. }
  232. Node* Scene::Instantiate(Deserializer& source, const Vector3& position, const Quaternion& rotation, CreateMode mode)
  233. {
  234. SceneResolver resolver;
  235. unsigned nodeID = source.ReadInt();
  236. // Rewrite IDs when instantiating
  237. Node* node = CreateChild(0, mode);
  238. resolver.AddNode(nodeID, node);
  239. if (node->Load(source, resolver, true, true, mode))
  240. {
  241. resolver.Resolve();
  242. node->ApplyAttributes();
  243. node->SetTransform(position, rotation);
  244. return node;
  245. }
  246. else
  247. {
  248. node->Remove();
  249. return 0;
  250. }
  251. }
  252. Node* Scene::InstantiateXML(Deserializer& source, const Vector3& position, const Quaternion& rotation, CreateMode mode)
  253. {
  254. SharedPtr<XMLFile> xml(new XMLFile(context_));
  255. if (!xml->Load(source))
  256. return false;
  257. return InstantiateXML(xml->GetRoot(), position, rotation, mode);
  258. }
  259. Node* Scene::InstantiateXML(const XMLElement& source, const Vector3& position, const Quaternion& rotation, CreateMode mode)
  260. {
  261. SceneResolver resolver;
  262. unsigned nodeID = source.GetInt("id");
  263. // Rewrite IDs when instantiating
  264. Node* node = CreateChild(0, mode);
  265. resolver.AddNode(nodeID, node);
  266. if (node->LoadXML(source, resolver, true, true, mode))
  267. {
  268. resolver.Resolve();
  269. node->ApplyAttributes();
  270. node->SetTransform(position, rotation);
  271. return node;
  272. }
  273. else
  274. {
  275. node->Remove();
  276. return 0;
  277. }
  278. }
  279. void Scene::Clear()
  280. {
  281. StopAsyncLoading();
  282. RemoveAllChildren();
  283. RemoveAllComponents();
  284. fileName_ = String();
  285. checksum_ = 0;
  286. }
  287. void Scene::SetActive(bool enable)
  288. {
  289. active_ = enable;
  290. }
  291. void Scene::SetSmoothingConstant(float constant)
  292. {
  293. smoothingConstant_ = Max(constant, M_EPSILON);
  294. }
  295. void Scene::SetSnapThreshold(float threshold)
  296. {
  297. snapThreshold_ = Max(threshold, 0.0f);
  298. }
  299. void Scene::AddRequiredPackageFile(PackageFile* package)
  300. {
  301. // Do not add packages that failed to load
  302. if (!package || !package->GetNumFiles())
  303. return;
  304. requiredPackageFiles_.Push(SharedPtr<PackageFile>(package));
  305. }
  306. void Scene::ClearRequiredPackageFiles()
  307. {
  308. requiredPackageFiles_.Clear();
  309. }
  310. void Scene::ResetOwner(Connection* owner)
  311. {
  312. for (Map<unsigned, Node*>::Iterator i = allNodes_.Begin(); i != allNodes_.End(); ++i)
  313. {
  314. if (i->second_->GetOwner() == owner)
  315. i->second_->SetOwner(0);
  316. }
  317. }
  318. void Scene::RegisterVar(const String& name)
  319. {
  320. varNames_[ShortStringHash(name)] = name;
  321. }
  322. void Scene::UnregisterVar(const String& name)
  323. {
  324. varNames_.Erase(ShortStringHash(name));
  325. }
  326. void Scene::UnregisterAllVars()
  327. {
  328. varNames_.Clear();
  329. }
  330. Node* Scene::GetNode(unsigned id) const
  331. {
  332. Map<unsigned, Node*>::ConstIterator i = allNodes_.Find(id);
  333. if (i != allNodes_.End())
  334. return i->second_;
  335. else
  336. return 0;
  337. }
  338. Component* Scene::GetComponent(unsigned id) const
  339. {
  340. Map<unsigned, Component*>::ConstIterator i = allComponents_.Find(id);
  341. if (i != allComponents_.End())
  342. return i->second_;
  343. else
  344. return 0;
  345. }
  346. float Scene::GetAsyncProgress() const
  347. {
  348. if (!asyncLoading_ || !asyncProgress_.totalNodes_)
  349. return 1.0f;
  350. else
  351. return (float)asyncProgress_.loadedNodes_ / (float)asyncProgress_.totalNodes_;
  352. }
  353. const String& Scene::GetVarName(ShortStringHash hash) const
  354. {
  355. Map<ShortStringHash, String>::ConstIterator i = varNames_.Find(hash);
  356. return i != varNames_.End() ? i->second_ : emptyVarName;
  357. }
  358. void Scene::Update(float timeStep)
  359. {
  360. if (asyncLoading_)
  361. {
  362. UpdateAsyncLoading();
  363. return;
  364. }
  365. PROFILE(UpdateScene);
  366. using namespace SceneUpdate;
  367. VariantMap eventData;
  368. eventData[P_SCENE] = (void*)this;
  369. eventData[P_TIMESTEP] = timeStep;
  370. // Update variable timestep logic
  371. SendEvent(E_SCENEUPDATE, eventData);
  372. // Update scene subsystems. If a physics world is present, it will be updated, triggering fixed timestep logic updates
  373. SendEvent(E_SCENESUBSYSTEMUPDATE, eventData);
  374. // Update smoothing if enabled (network client scenes)
  375. if (IsSmoothed())
  376. {
  377. PROFILE(UpdateSmoothing);
  378. float constant = 1.0f - Clamp(powf(2.0f, -timeStep * smoothingConstant_), 0.0f, 1.0f);
  379. float squaredSnapThreshold = snapThreshold_ * snapThreshold_;
  380. for (Map<unsigned, Node*>::ConstIterator i = allNodes_.Begin(); i != allNodes_.End() && i->first_ < FIRST_LOCAL_ID; ++i)
  381. i->second_->UpdateSmoothing(constant, squaredSnapThreshold);
  382. }
  383. // Post-update variable timestep logic
  384. SendEvent(E_SCENEPOSTUPDATE, eventData);
  385. }
  386. void Scene::BeginThreadedUpdate()
  387. {
  388. // Check the work queue subsystem whether it actually has created worker threads. If not, do not enter threaded mode.
  389. if (GetSubsystem<WorkQueue>()->GetNumThreads())
  390. threadedUpdate_ = true;
  391. }
  392. void Scene::EndThreadedUpdate()
  393. {
  394. if (!threadedUpdate_)
  395. return;
  396. threadedUpdate_ = false;
  397. if (!delayedDirtyComponents_.Empty())
  398. {
  399. PROFILE(EndThreadedUpdate);
  400. for (PODVector<Component*>::ConstIterator i = delayedDirtyComponents_.Begin(); i != delayedDirtyComponents_.End(); ++i)
  401. (*i)->OnMarkedDirty((*i)->GetNode());
  402. delayedDirtyComponents_.Clear();
  403. }
  404. }
  405. void Scene::DelayedMarkedDirty(Component* component)
  406. {
  407. MutexLock lock(sceneMutex_);
  408. delayedDirtyComponents_.Push(component);
  409. }
  410. unsigned Scene::GetFreeNodeID(CreateMode mode)
  411. {
  412. if (mode == REPLICATED)
  413. {
  414. for (;;)
  415. {
  416. if (!allNodes_.Contains(replicatedNodeID_))
  417. return replicatedNodeID_;
  418. if (replicatedNodeID_ != LAST_REPLICATED_ID)
  419. ++replicatedNodeID_;
  420. else
  421. replicatedNodeID_ = FIRST_REPLICATED_ID;
  422. }
  423. }
  424. else
  425. {
  426. for (;;)
  427. {
  428. if (!allNodes_.Contains(localNodeID_))
  429. return localNodeID_;
  430. if (localNodeID_ != LAST_LOCAL_ID)
  431. ++localNodeID_;
  432. else
  433. localNodeID_ = FIRST_LOCAL_ID;
  434. }
  435. }
  436. }
  437. unsigned Scene::GetFreeComponentID(CreateMode mode)
  438. {
  439. if (mode == REPLICATED)
  440. {
  441. for (;;)
  442. {
  443. if (!allComponents_.Contains(replicatedComponentID_))
  444. return replicatedComponentID_;
  445. if (replicatedComponentID_ != LAST_REPLICATED_ID)
  446. ++replicatedComponentID_;
  447. else
  448. replicatedComponentID_ = FIRST_REPLICATED_ID;
  449. }
  450. }
  451. else
  452. {
  453. for (;;)
  454. {
  455. if (!allComponents_.Contains(localComponentID_))
  456. return localComponentID_;
  457. if (localComponentID_ != LAST_LOCAL_ID)
  458. ++localComponentID_;
  459. else
  460. localComponentID_ = FIRST_LOCAL_ID;
  461. }
  462. }
  463. }
  464. void Scene::NodeAdded(Node* node)
  465. {
  466. if (!node || node->GetScene())
  467. return;
  468. node->SetScene(this);
  469. // If we already have an existing node with the same ID, must remove the scene reference from it
  470. unsigned id = node->GetID();
  471. Map<unsigned, Node*>::Iterator i = allNodes_.Find(id);
  472. if (i != allNodes_.End() && i->second_ != node)
  473. {
  474. LOGWARNING("Overwriting node with ID " + String(id));
  475. i->second_->SetScene(0);
  476. i->second_->SetOwner(0);
  477. }
  478. allNodes_[id] = node;
  479. }
  480. void Scene::NodeRemoved(Node* node)
  481. {
  482. if (!node || node->GetScene() != this)
  483. return;
  484. allNodes_.Erase(node->GetID());
  485. node->SetID(0);
  486. node->SetScene(0);
  487. }
  488. void Scene::ComponentAdded(Component* component)
  489. {
  490. if (!component)
  491. return;
  492. unsigned id = component->GetID();
  493. Map<unsigned, Component*>::Iterator i = allComponents_.Find(id);
  494. if (i != allComponents_.End() && i->second_ != component)
  495. LOGWARNING("Overwriting component with ID " + String(id));
  496. allComponents_[id] = component;
  497. }
  498. void Scene::ComponentRemoved(Component* component)
  499. {
  500. if (!component)
  501. return;
  502. allComponents_.Erase(component->GetID());
  503. component->SetID(0);
  504. }
  505. void Scene::SetVarNamesAttr(String value)
  506. {
  507. Vector<String> varNames = value.Split(';');
  508. varNames_.Clear();
  509. for (Vector<String>::ConstIterator i = varNames.Begin(); i != varNames.End(); ++i)
  510. varNames_[ShortStringHash(*i)] = *i;
  511. }
  512. String Scene::GetVarNamesAttr() const
  513. {
  514. String ret;
  515. for (Map<ShortStringHash, String>::ConstIterator i = varNames_.Begin(); i != varNames_.End(); ++i)
  516. {
  517. if (i != varNames_.Begin())
  518. ret += ';';
  519. ret += i->second_;
  520. }
  521. return ret;
  522. }
  523. void Scene::HandleUpdate(StringHash eventType, VariantMap& eventData)
  524. {
  525. using namespace Update;
  526. if (active_)
  527. Update(eventData[P_TIMESTEP].GetFloat());
  528. }
  529. void Scene::UpdateAsyncLoading()
  530. {
  531. PROFILE(UpdateAsyncLoading);
  532. Timer asyncLoadTimer;
  533. for (;;)
  534. {
  535. if (asyncProgress_.loadedNodes_ >= asyncProgress_.totalNodes_)
  536. {
  537. FinishAsyncLoading();
  538. return;
  539. }
  540. // Read one child node with its full sub-hierarchy from either from binary or XML
  541. if (!asyncProgress_.xmlFile_)
  542. {
  543. unsigned nodeID = asyncProgress_.file_->ReadUInt();
  544. Node* newNode = CreateChild(nodeID, nodeID < FIRST_LOCAL_ID ? REPLICATED : LOCAL);
  545. resolver_.AddNode(nodeID, newNode);
  546. newNode->Load(*asyncProgress_.file_, resolver_);
  547. }
  548. else
  549. {
  550. unsigned nodeID = asyncProgress_.xmlElement_.GetInt("id");
  551. Node* newNode = CreateChild(nodeID, nodeID < FIRST_LOCAL_ID ? REPLICATED : LOCAL);
  552. resolver_.AddNode(nodeID, newNode);
  553. newNode->LoadXML(asyncProgress_.xmlElement_, resolver_);
  554. asyncProgress_.xmlElement_ = asyncProgress_.xmlElement_.GetNext("node");
  555. }
  556. ++asyncProgress_.loadedNodes_;
  557. // Break if time limit exceeded, so that we keep sufficient FPS
  558. if (asyncLoadTimer.GetMSec(false) >= ASYNC_LOAD_MAX_MSEC)
  559. break;
  560. }
  561. using namespace AsyncLoadProgress;
  562. VariantMap eventData;
  563. eventData[P_SCENE] = (void*)this;
  564. eventData[P_PROGRESS] = (float)asyncProgress_.loadedNodes_ / (float)asyncProgress_.totalNodes_;
  565. eventData[P_LOADEDNODES] = asyncProgress_.loadedNodes_;
  566. eventData[P_TOTALNODES] = asyncProgress_.totalNodes_;
  567. SendEvent(E_ASYNCLOADPROGRESS, eventData);
  568. }
  569. void Scene::FinishAsyncLoading()
  570. {
  571. resolver_.Resolve();
  572. ApplyAttributes();
  573. FinishLoading(asyncProgress_.file_);
  574. StopAsyncLoading();
  575. using namespace AsyncLoadFinished;
  576. VariantMap eventData;
  577. eventData[P_SCENE] = (void*)this;
  578. SendEvent(E_ASYNCLOADFINISHED, eventData);
  579. }
  580. void Scene::FinishLoading(Deserializer* source)
  581. {
  582. if (source)
  583. {
  584. fileName_ = source->GetName();
  585. checksum_ = source->GetChecksum();
  586. }
  587. }
  588. void RegisterSceneLibrary(Context* context)
  589. {
  590. Node::RegisterObject(context);
  591. Scene::RegisterObject(context);
  592. }