CmSceneManager.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "CmSceneManager.h"
  2. #include "CmGameObject.h"
  3. #include "CmComponent.h"
  4. #include "CmCamera.h"
  5. #include "CmRenderable.h"
  6. namespace CamelotEngine
  7. {
  8. SceneManager::SceneManager()
  9. {
  10. mRootNode = GameObject::createInternal("SceneRoot");
  11. }
  12. SceneManager::~SceneManager()
  13. {
  14. if(mRootNode != nullptr)
  15. mRootNode->destroy();
  16. }
  17. void SceneManager::update()
  18. {
  19. stack<HGameObject>::type todo;
  20. todo.push(mRootNode);
  21. while(!todo.empty())
  22. {
  23. HGameObject currentGO = todo.top();
  24. todo.pop();
  25. vector<ComponentPtr>::type components = currentGO->getComponents();
  26. for(auto iter = components.begin(); iter != components.end(); ++iter)
  27. {
  28. (*iter)->update();
  29. }
  30. for(UINT32 i = 0; i < currentGO->getNumChildren(); i++)
  31. todo.push(currentGO->getChild(i));
  32. }
  33. }
  34. vector<RenderablePtr>::type SceneManager::getVisibleRenderables(const CameraPtr camera) const
  35. {
  36. // TODO - Cull invisible objects
  37. vector<RenderablePtr>::type renderables;
  38. stack<HGameObject>::type todo;
  39. todo.push(mRootNode);
  40. while(!todo.empty())
  41. {
  42. HGameObject currentGO = todo.top();
  43. todo.pop();
  44. RenderablePtr curRenderable = currentGO->getComponent<Renderable>();
  45. if(curRenderable != nullptr)
  46. renderables.push_back(curRenderable);
  47. for(UINT32 i = 0; i < currentGO->getNumChildren(); i++)
  48. todo.push(currentGO->getChild(i));
  49. }
  50. return renderables;
  51. }
  52. void SceneManager::registerNewGO(const HGameObject& node)
  53. {
  54. if(mRootNode) // If root node is null, then this new node is the root node
  55. node->setParent(mRootNode);
  56. }
  57. void SceneManager::notifyComponentAdded(ComponentPtr component)
  58. {
  59. if(component->getTypeId() == TID_Camera)
  60. {
  61. CameraPtr camera = std::static_pointer_cast<Camera>(component);
  62. auto findIter = std::find(mCachedCameras.begin(), mCachedCameras.end(), camera);
  63. if(findIter != mCachedCameras.end())
  64. {
  65. CM_EXCEPT(InternalErrorException, "Trying to add an already existing camera!");
  66. }
  67. mCachedCameras.push_back(camera);
  68. }
  69. }
  70. void SceneManager::notifyComponentRemoved(ComponentPtr component)
  71. {
  72. if(component->getTypeId() == TID_Camera)
  73. {
  74. CameraPtr camera = std::static_pointer_cast<Camera>(component);
  75. auto findIter = std::find(mCachedCameras.begin(), mCachedCameras.end(), camera);
  76. if(findIter == mCachedCameras.end())
  77. {
  78. CM_EXCEPT(InternalErrorException, "Cannot find specified camera!");
  79. }
  80. mCachedCameras.erase(findIter);
  81. }
  82. }
  83. SceneManager& gSceneManager()
  84. {
  85. return SceneManager::instance();
  86. }
  87. }