-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneNode.cpp
More file actions
72 lines (56 loc) · 1.53 KB
/
SceneNode.cpp
File metadata and controls
72 lines (56 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "SceneNode.hpp"
#include <SFML/System/Vector2.hpp>
#include <algorithm>
#include <cassert>
SceneNode::SceneNode() : mChildren(), mParent(nullptr) {}
void SceneNode::attachChild(Ptr child)
{
child->mParent = this;
mChildren.push_back(std::move(child));
}
SceneNode::Ptr SceneNode::detachChild(const SceneNode& node)
{
auto found = std::find_if(mChildren.begin(), mChildren.end(),
[&] (Ptr& p) -> bool { return p.get() == &node; });
assert((found != mChildren.end()));
Ptr result = std::move(*found);
result->mParent = nullptr;
mChildren.erase(found);
return result;
}
void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
drawCurrent(target, states);
for (const Ptr& child : mChildren)
{
child->draw(target, states);
}
}
void SceneNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
}
void SceneNode::update(sf::Time dt)
{
updateCurrent(dt);
updateChildren(dt);
}
void SceneNode::updateCurrent(sf::Time dt)
{
}
void SceneNode::updateChildren(sf::Time dt)
{
for (Ptr& child : mChildren)
child->update(dt);
}
sf::Transform SceneNode::getWorldTransform() const
{
sf::Transform transform = sf::Transform::Identity;
for (const SceneNode* node = this; node != nullptr; node = node->mParent)
transform = node->getTransform() * transform;
return transform;
}
sf::Vector2f SceneNode::getWorldPostion() const
{
return getWorldTransform() * sf::Vector2f();
}