Scene.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Scene.cpp
  3. */
  4. #include <algorithm>
  5. #include "Base.h"
  6. #include "AudioListener.h"
  7. #include "Scene.h"
  8. namespace gameplay
  9. {
  10. Scene::Scene() : _activeCamera(NULL), _firstNode(NULL), _lastNode(NULL), _nodeCount(0), _bindAudioListenerToCamera(true)
  11. {
  12. }
  13. Scene::Scene(const Scene& copy)
  14. {
  15. }
  16. Scene::~Scene()
  17. {
  18. // Unbind our active camera from the audio listener
  19. if (_activeCamera)
  20. {
  21. AudioListener* audioListener = AudioListener::getInstance();
  22. if (audioListener && (audioListener->getCamera() == _activeCamera))
  23. {
  24. audioListener->setCamera(NULL);
  25. }
  26. SAFE_RELEASE(_activeCamera);
  27. }
  28. // Remove all nodes from the scene
  29. removeAllNodes();
  30. }
  31. Scene* Scene::createScene()
  32. {
  33. return new Scene();
  34. }
  35. const char* Scene::getId() const
  36. {
  37. return _id.c_str();
  38. }
  39. void Scene::setId(const char* id)
  40. {
  41. if (id)
  42. {
  43. _id = id;
  44. }
  45. }
  46. Node* Scene::findNode(const char* id, bool recursive, bool exactMatch) const
  47. {
  48. assert(id);
  49. // Search immediate children first.
  50. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  51. {
  52. // Does this child's ID match?
  53. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  54. {
  55. return child;
  56. }
  57. }
  58. // Recurse.
  59. if (recursive)
  60. {
  61. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  62. {
  63. Node* match = child->findNode(id, true, exactMatch);
  64. if (match)
  65. {
  66. return match;
  67. }
  68. }
  69. }
  70. return NULL;
  71. }
  72. unsigned int Scene::findNodes(const char* id, std::vector<Node*>& nodes, bool recursive, bool exactMatch) const
  73. {
  74. assert(id);
  75. unsigned int count = 0;
  76. // Search immediate children first.
  77. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  78. {
  79. // Does this child's ID match?
  80. if ((exactMatch && child->_id == id) || (!exactMatch && child->_id.find(id) == 0))
  81. {
  82. nodes.push_back(child);
  83. ++count;
  84. }
  85. }
  86. // Recurse.
  87. if (recursive)
  88. {
  89. for (Node* child = getFirstNode(); child != NULL; child = child->getNextSibling())
  90. {
  91. count += child->findNodes(id, nodes, true, exactMatch);
  92. }
  93. }
  94. return count;
  95. }
  96. Node* Scene::addNode(const char* id)
  97. {
  98. Node* node = Node::create(id);
  99. addNode(node);
  100. // Call release to decrement the ref count to 1 before returning.
  101. node->release();
  102. return node;
  103. }
  104. void Scene::addNode(Node* node)
  105. {
  106. assert(node);
  107. if (node->_scene == this)
  108. {
  109. // The node is already a member of this scene.
  110. return;
  111. }
  112. node->addRef();
  113. // If the node is part of another scene, remove it.
  114. if (node->_scene && node->_scene != this)
  115. {
  116. node->_scene->removeNode(node);
  117. }
  118. // If the node is part of another node hierarchy, remove it.
  119. if (node->getParent())
  120. {
  121. node->getParent()->removeChild(node);
  122. }
  123. // Link the new node into our list.
  124. if (_lastNode)
  125. {
  126. _lastNode->_nextSibling = node;
  127. node->_prevSibling = _lastNode;
  128. _lastNode = node;
  129. }
  130. else
  131. {
  132. _firstNode = _lastNode = node;
  133. }
  134. node->_scene = this;
  135. ++_nodeCount;
  136. // If we don't have an active camera set, then check for one and set it.
  137. if (_activeCamera == NULL)
  138. {
  139. Camera* camera = node->getCamera();
  140. if (camera)
  141. {
  142. setActiveCamera(camera);
  143. }
  144. }
  145. }
  146. void Scene::removeNode(Node* node)
  147. {
  148. assert(node);
  149. if (node->_scene != this)
  150. return;
  151. if (node == _firstNode)
  152. {
  153. _firstNode = node->_nextSibling;
  154. }
  155. if (node == _lastNode)
  156. {
  157. _lastNode = node->_prevSibling;
  158. }
  159. node->remove();
  160. node->_scene = NULL;
  161. SAFE_RELEASE(node);
  162. --_nodeCount;
  163. }
  164. void Scene::removeAllNodes()
  165. {
  166. while (_lastNode)
  167. {
  168. removeNode(_lastNode);
  169. }
  170. }
  171. unsigned int Scene::getNodeCount() const
  172. {
  173. return _nodeCount;
  174. }
  175. Node* Scene::getFirstNode() const
  176. {
  177. return _firstNode;
  178. }
  179. Camera* Scene::getActiveCamera() const
  180. {
  181. return _activeCamera;
  182. }
  183. void Scene::setActiveCamera(Camera* camera)
  184. {
  185. // Make sure we don't release the camera if the same camera is set twice.
  186. if (_activeCamera != camera)
  187. {
  188. AudioListener* audioListener = AudioListener::getInstance();
  189. if (_activeCamera)
  190. {
  191. // Unbind the active camera from the audio listener
  192. if (audioListener && (audioListener->getCamera() == _activeCamera))
  193. {
  194. AudioListener::getInstance()->setCamera(NULL);
  195. }
  196. SAFE_RELEASE(_activeCamera);
  197. }
  198. _activeCamera = camera;
  199. if (_activeCamera)
  200. {
  201. _activeCamera->addRef();
  202. if (audioListener && _bindAudioListenerToCamera)
  203. {
  204. AudioListener::getInstance()->setCamera(_activeCamera);
  205. }
  206. }
  207. }
  208. }
  209. void Scene::bindAudioListenerToCamera(bool bind)
  210. {
  211. if (_bindAudioListenerToCamera != bind)
  212. {
  213. _bindAudioListenerToCamera = bind;
  214. if (AudioListener::getInstance())
  215. {
  216. AudioListener::getInstance()->setCamera(bind ? _activeCamera : NULL);
  217. }
  218. }
  219. }
  220. const Viewport& Scene::getViewport() const
  221. {
  222. return _viewport;
  223. }
  224. void Scene::setViewport(const Viewport& viewport)
  225. {
  226. _viewport = viewport;
  227. }
  228. const Vector3& Scene::getAmbientColor()
  229. {
  230. return _ambientColor;
  231. }
  232. void Scene::setAmbientColor(float red, float green, float blue)
  233. {
  234. _ambientColor.set(red, green, blue);
  235. }
  236. }