Scene.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "Profiler.h"
  30. #include "Scene.h"
  31. #include "SceneEvents.h"
  32. #include "XMLFile.h"
  33. OBJECTTYPESTATIC(Scene);
  34. Scene::Scene(Context* context) :
  35. Node(context),
  36. nodeID_(1),
  37. componentID_(1),
  38. active_(true)
  39. {
  40. // Assign an ID to self so that nodes can refer to this node as a parent
  41. NodeAdded(this);
  42. SubscribeToEvent(E_UPDATE, HANDLER(Scene, HandleUpdate));
  43. }
  44. Scene::~Scene()
  45. {
  46. // Remove scene reference from all nodes that still exist
  47. for (std::map<unsigned, Node*>::iterator i = allNodes_.begin(); i != allNodes_.end(); ++i)
  48. i->second->setScene(0);
  49. }
  50. void Scene::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<Scene>();
  53. context->CopyBaseAttributes<Node, Scene>();
  54. }
  55. bool Scene::Load(Deserializer& source)
  56. {
  57. // Check ID
  58. if (source.ReadID() != "USCN")
  59. {
  60. LOGERROR(source.GetName() + " is not a valid scene file");
  61. return false;
  62. }
  63. // Load the whole scene, then perform post-load if successfully loaded
  64. /// \todo Async loading support
  65. if (Node::Load(source))
  66. {
  67. PostLoad();
  68. return true;
  69. }
  70. else
  71. return false;
  72. }
  73. bool Scene::Save(Serializer& dest)
  74. {
  75. // Write ID first
  76. if (!dest.WriteID("USCN"))
  77. {
  78. LOGERROR("Could not save scene, writing to stream failed");
  79. return false;
  80. }
  81. return Node::Save(dest);
  82. }
  83. bool Scene::LoadXML(const XMLElement& source)
  84. {
  85. // Load the whole scene, then perform post-load if successfully loaded
  86. /// \todo Async loading support
  87. if (Node::LoadXML(source))
  88. {
  89. PostLoad();
  90. return true;
  91. }
  92. else
  93. return false;
  94. }
  95. bool Scene::LoadXML(Deserializer& source)
  96. {
  97. SharedPtr<XMLFile> xml(new XMLFile(context_));
  98. if (!xml->Load(source))
  99. return false;
  100. return LoadXML(xml->GetRootElement());
  101. }
  102. bool Scene::SaveXML(Serializer& dest)
  103. {
  104. SharedPtr<XMLFile> xml(new XMLFile(context_));
  105. XMLElement rootElem = xml->CreateRootElement("scene");
  106. if (!SaveXML(rootElem))
  107. return false;
  108. return xml->Save(dest);
  109. }
  110. void Scene::Update(float timeStep)
  111. {
  112. PROFILE(UpdateScene);
  113. using namespace SceneUpdate;
  114. VariantMap eventData;
  115. eventData[P_SCENE] = (void*)this;
  116. eventData[P_TIMESTEP] = timeStep;
  117. // Update variable timestep logic
  118. SendEvent(E_SCENEUPDATE, eventData);
  119. // Update scene subsystems. If a physics world is present, it will be updated, triggering fixed timestep logic updates
  120. SendEvent(E_SCENESUBSYSTEMUPDATE, eventData);
  121. // Post-update variable timestep logic
  122. SendEvent(E_SCENEPOSTUPDATE, eventData);
  123. }
  124. void Scene::SetActive(bool enable)
  125. {
  126. active_ = enable;
  127. }
  128. void Scene::NodeAdded(Node* node)
  129. {
  130. if ((!node) || (node->GetScene()))
  131. return;
  132. // Assign ID if not already set
  133. if (!node->GetID())
  134. {
  135. while (allNodes_.find(nodeID_) != allNodes_.end())
  136. GetNextNodeID();
  137. node->SetID(nodeID_);
  138. }
  139. node->setScene(this);
  140. allNodes_[node->GetID()] = node;
  141. GetNextNodeID();
  142. }
  143. void Scene::NodeRemoved(Node* node)
  144. {
  145. if ((!node) || (node->GetScene() != this))
  146. return;
  147. allNodes_.erase(node->GetID());
  148. node->SetID(0);
  149. node->setScene(0);
  150. }
  151. void Scene::ComponentAdded(Component* component)
  152. {
  153. if (!component)
  154. return;
  155. // Assign ID if not already set
  156. if (!component->GetID())
  157. {
  158. while (allComponents_.find(componentID_) != allComponents_.end())
  159. GetNextComponentID();
  160. component->SetID(componentID_);
  161. }
  162. allComponents_[component->GetID()] = component;
  163. GetNextComponentID();
  164. }
  165. void Scene::ComponentRemoved(Component* component)
  166. {
  167. if (!component)
  168. return;
  169. allComponents_.erase(component->GetID());
  170. component->SetID(0);
  171. }
  172. Node* Scene::GetNodeByID(unsigned id) const
  173. {
  174. std::map<unsigned, Node*>::const_iterator i = allNodes_.find(id);
  175. if (i != allNodes_.end())
  176. return i->second;
  177. else
  178. return 0;
  179. }
  180. Component* Scene::GetComponentByID(unsigned id) const
  181. {
  182. std::map<unsigned, Component*>::const_iterator i = allComponents_.find(id);
  183. if (i != allComponents_.end())
  184. return i->second;
  185. else
  186. return 0;
  187. }
  188. void Scene::HandleUpdate(StringHash eventType, VariantMap& eventData)
  189. {
  190. using namespace Update;
  191. if (active_)
  192. Update(eventData[P_TIMESTEP].GetFloat());
  193. }
  194. void RegisterSceneLibrary(Context* context)
  195. {
  196. Node::RegisterObject(context);
  197. Scene::RegisterObject(context);
  198. }