Scene.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #ifndef SCENE_H_
  2. #define SCENE_H_
  3. #include "Node.h"
  4. namespace gameplay
  5. {
  6. /**
  7. * Represents the root container for a hierarchy of nodes.
  8. */
  9. class Scene : public Ref
  10. {
  11. public:
  12. /**
  13. * Creates a new empty scene.
  14. *
  15. * @return The newly created empty scene.
  16. */
  17. static Scene* createScene();
  18. /**
  19. * Loads a scene from the given '.scene' file.
  20. *
  21. * @param filePath The path to the '.scene' file to load from.
  22. * @return The loaded scene or <code>NULL</code> if the scene
  23. * could not be loaded from the given file.
  24. */
  25. static Scene* load(const char* filePath);
  26. /**
  27. * Gets the identifier for the scene.
  28. *
  29. * @return The scene identifier.
  30. */
  31. const char* getId() const;
  32. /**
  33. * Sets the identifier for the scene.
  34. *
  35. * @param id The identifier to set for the scene.
  36. */
  37. void setId(const char* id);
  38. /**
  39. * Returns the first node in the scene that matches the given ID.
  40. *
  41. * @param id The ID of the node to find.
  42. * @param recursive true if a recursive search should be performed, false otherwise.
  43. * @param exactMatch true if only nodes whose ID exactly matches the specified ID are returned,
  44. * or false if nodes that start with the given ID are returned.
  45. *
  46. * @return The first node found that matches the given ID.
  47. */
  48. Node* findNode(const char* id, bool recursive = true, bool exactMatch = true) const;
  49. /**
  50. * Returns all nodes in the scene that match the given ID.
  51. *
  52. * @param id The ID of the node to find.
  53. * @param nodes Vector of nodes to be populated with matches.
  54. * @param recursive true if a recursive search should be performed, false otherwise.
  55. * @param exactMatch true if only nodes whos ID exactly matches the specified ID are returned,
  56. * or false if nodes that start with the given ID are returned.
  57. *
  58. * @return The number of matches found.
  59. */
  60. unsigned int findNodes(const char* id, std::vector<Node*>& nodes, bool recursive = true, bool exactMatch = true) const;
  61. /**
  62. * Creates and adds a new node to the scene.
  63. *
  64. * @param id An optional node ID.
  65. *
  66. * @return The new node.
  67. */
  68. Node* addNode(const char* id = NULL);
  69. /**
  70. * Adds the specified node to the scene.
  71. *
  72. * @param node The node to be added to the scene.
  73. */
  74. void addNode(Node* node);
  75. /**
  76. * Removes the specified node from the scene.
  77. *
  78. * @param node The node to remove.
  79. */
  80. void removeNode(Node* node);
  81. /**
  82. * Removes all nodes from the scene.
  83. */
  84. void removeAllNodes();
  85. /**
  86. * Returns the number of nodes at the root level of the scene.
  87. *
  88. * @return The node count.
  89. */
  90. unsigned int getNodeCount() const;
  91. /**
  92. * Returns the first node in the scene.
  93. *
  94. * @return The first node in the scene.
  95. */
  96. Node* getFirstNode() const;
  97. /**
  98. * Gets the active camera for the scene.
  99. *
  100. * @return The active camera for the scene.
  101. */
  102. Camera* getActiveCamera() const;
  103. /**
  104. * Sets the active camera on the scene.
  105. *
  106. * @param camera The active camera to be set on the scene.
  107. */
  108. void setActiveCamera(Camera* camera);
  109. /**
  110. * Sets the audio listener to transform along with the active camera if set to true.
  111. * If you have a 2D game that doesn't require it, then set to false. This is on by default for the scene.
  112. *
  113. * @param bind true if you want to the audio listener to follow the active camera's transform.
  114. */
  115. void bindAudioListenerToCamera(bool bind);
  116. /**
  117. * Gets the viewport for the scene.
  118. *
  119. * @return The scene's viewport.
  120. */
  121. const Viewport& getViewport() const;
  122. /**
  123. * Sets the scene's viewport.
  124. *
  125. * @param viewport The viewport to be set for this scene.
  126. */
  127. void setViewport(const Viewport& viewport);
  128. /**
  129. * Returns the ambient color of the scene. Black is the default color.
  130. *
  131. * @return The ambient color of the scene.
  132. */
  133. const Vector3& getAmbientColor();
  134. /**
  135. * Sets the ambient color of the scene.
  136. *
  137. * @param red The red channel between 0.0 and 1.0.
  138. * @param green The green channel between 0.0 and 1.0.
  139. * @param blue The blue channel between 0.0 and 1.0.
  140. */
  141. void setAmbientColor(float red, float green, float blue);
  142. /**
  143. * Visits each node in the scene and calls the specified method pointer.
  144. *
  145. * Calling this method invokes the specified method pointer for each node
  146. * in the scene hierarchy, passing the Node and the specified cookie value.
  147. *
  148. * The visitMethod parameter must be a pointer to a method that has a bool
  149. * return type and accepts two parameters: a Node pointer and a void* (cookie).
  150. * The scene travesal continues while visitMethod return true. Returning false
  151. * will cause the traversal to stop.
  152. *
  153. * @param instance The pointer to an instance of the object that contains visitMethod.
  154. * @param visitMethod The pointer to the class method to call for each node in the scene.
  155. * @param cookie An optional user-defined parameter that will be passed to each invocation of visitMethod.
  156. */
  157. template <class T>
  158. void visit(T* instance, bool (T::*visitMethod)(Node*,void*), void* cookie = 0);
  159. private:
  160. /**
  161. * Constructor.
  162. */
  163. Scene();
  164. /**
  165. * Hidden copy constructor.
  166. */
  167. Scene(const Scene& copy);
  168. /**
  169. * Destructor.
  170. */
  171. virtual ~Scene();
  172. /**
  173. * Visits the given node and all of its children recursively.
  174. */
  175. template <class T>
  176. bool visitNode(Node* node, T* instance, bool (T::*visitMethod)(Node*,void*), void* cookie);
  177. std::string _id;
  178. Camera* _activeCamera;
  179. Viewport _viewport;
  180. Node* _firstNode;
  181. Node* _lastNode;
  182. unsigned int _nodeCount;
  183. Vector3 _ambientColor;
  184. bool _bindAudioListenerToCamera;
  185. };
  186. template <class T>
  187. void Scene::visit(T* instance, bool (T::*visitMethod)(Node*,void*), void* cookie)
  188. {
  189. for (Node* node = getFirstNode(); node != NULL; node = node->getNextSibling())
  190. {
  191. if (!visitNode(node, instance, visitMethod, cookie))
  192. return;
  193. }
  194. }
  195. template <class T>
  196. bool Scene::visitNode(Node* node, T* instance, bool (T::*visitMethod)(Node*,void*), void* cookie)
  197. {
  198. // Invoke the visit method for this node.
  199. if (!(instance->*visitMethod)(node, cookie))
  200. return false;
  201. // Recurse for all children.
  202. for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling())
  203. {
  204. if (!visitNode(child, instance, visitMethod, cookie))
  205. return false;
  206. }
  207. return true;
  208. }
  209. }
  210. #endif