hx3d  1
2D/3D Simple Game Framework
scene_graph.inl.hpp
1 /*
2  Entity Component System: Base Scene Graph.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, FiAfth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/ecs/node.hpp"
22 
23 namespace hx3d {
24 namespace ecs {
25 
26 template <class T, class... Args>
28  const Ptr<T>& ptr = createNodeChild<T>(_root, args...);
29  return ptr;
30 }
31 
32 template <class T, class... Args>
33 Ptr<T> SceneGraph::create(const std::string path, Args... args) {
34  const Ptr<Node>& container = pathExists(path);
35  if (container == nullptr) {
36  Log.Error("SceneGraph: could not create at `%s`.", path.c_str());
37  return nullptr;
38  }
39 
40  const Ptr<T>& ptr = createNodeChild<T>(container, args...);
41  return ptr;
42 }
43 
44 template <class T>
45 Ptr<T> SceneGraph::fetch(const std::string path) {
46  return std::dynamic_pointer_cast<T>(_indices[path]);
47 }
48 
49 template <class T, class... Args>
50 Ptr<T> SceneGraph::createNodeChild(const Ptr<Node>& container, Args... args) {
51  const Ptr<T>& object = Make<T>(args...);
52  auto obj_name = object->getName();
53  if (container->childNameExists(obj_name)) {
54  Log.Error("Node: a child of `%s` is already named `%s`.", container->_name.c_str(), obj_name.c_str());
55  return nullptr;
56  }
57 
58  object->_parent = container;
59  container->_children.push_back(object);
60 
61  addIndex(object);
62 
63  if (_entityEnabled) {
64  _engine.registerEntity(object);
65  }
66 
67  return object;
68 }
69 
70 } /* ecs */
71 } /* hx3d */
Ptr< T > createNodeChild(const Ptr< Node > &container, Args...args)
Create a child for a container Node.
Ptr< T > create(const std::string path, Args...args)
Create a game object at a path.
std::map< std::string, Ptr< Node > > _indices
Node indices.
hx3d framework namespace
Definition: audio.hpp:26
Ptr< Node > _root
Graph root.
void addIndex(const Ptr< Node > &object)
Add an index to the graph.
Definition: scene_graph.cpp:49
Ptr< Node > pathExists(const std::string path)
Test if the path exists and returns the node.
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
static hx3d::LogImpl Log
Current log implementation.
Definition: log.hpp:91
bool _entityEnabled
Is entity management enabled ?
Ptr< T > fetch(const std::string path)
Fetch a game object from a path.
Ptr< T > createAtRoot(Args...args)
Create a game object at the root.
Engine _engine
Engine for entity management.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
void registerEntity(const Ptr< Entity > &entity)
Affect an ID to a uninitialized entity.
Definition: engine.cpp:37