Skip to content

Commit 1d678a9

Browse files
committed
added tree root
1 parent 98c3628 commit 1d678a9

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ set(SRC_FILES
4242
${SRC_DIR}/Util/Text.cpp
4343
${SRC_DIR}/Util/TransformUtils.cpp
4444
${SRC_DIR}/Util/GameObject.cpp
45+
${SRC_DIR}/Util/Root.cpp
4546

4647
${SRC_DIR}/App.cpp
4748
${SRC_DIR}/Giraffe.cpp
@@ -71,7 +72,8 @@ set(INCLUDE_FILES
7172
${INCLUDE_DIR}/Util/Text.hpp
7273
${INCLUDE_DIR}/Util/Transform.hpp
7374
${INCLUDE_DIR}/Util/TransformUtils.hpp
74-
${INCLUDE_DIR}Util/GameObject.hpp
75+
${INCLUDE_DIR}/Util/GameObject.hpp
76+
${INCLUDE_DIR}/Util/Root.hpp
7577

7678
${INCLUDE_DIR}/App.hpp
7779
${INCLUDE_DIR}/Giraffe.hpp

include/Util/GameObject.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ class GameObject {
1313
GameObject() = default;
1414

1515
GameObject(std::unique_ptr<Core::Drawable> drawable, const float zIndex,
16+
const bool visible = true,
1617
const std::vector<std::shared_ptr<GameObject>> &children =
1718
std::vector<std::shared_ptr<GameObject>>())
1819
: m_Drawable(std::move(drawable)),
1920
m_Children(children),
20-
m_ZIndex(zIndex) {}
21+
m_ZIndex(zIndex),
22+
m_Visible(visible) {}
2123

2224
GameObject(const GameObject &other) = delete;
2325

@@ -28,17 +30,18 @@ class GameObject {
2830
virtual ~GameObject() = default;
2931

3032
float GetZIndex() const { return m_ZIndex; }
31-
32-
std::vector<std::shared_ptr<GameObject>> &GetChildren() {
33+
Transform GetTransform() const { return m_Transform; }
34+
const std::vector<std::shared_ptr<GameObject>> &GetChildren() const {
3335
return m_Children;
3436
}
3537

3638
void SetZIndex(float index) { m_ZIndex = index; }
37-
3839
void SetDrawable(std::unique_ptr<Core::Drawable> drawable) {
3940
m_Drawable = std::move(drawable);
4041
}
4142

43+
void SetVisible(bool visible) { m_Visible = visible; }
44+
4245
void AppendChild(std::shared_ptr<GameObject> child) {
4346
m_Children.push_back(std::move(child));
4447
}
@@ -57,6 +60,7 @@ class GameObject {
5760
std::vector<std::shared_ptr<GameObject>> m_Children;
5861

5962
float m_ZIndex;
63+
bool m_Visible = true;
6064
};
6165
} // namespace Util
6266
#endif

include/Util/Root.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef UTIL_ROOT_HPP
2+
#define UTIL_ROOT_HPP
3+
4+
#include <memory>
5+
#include <vector>
6+
7+
#include "Util/GameObject.hpp"
8+
9+
class App;
10+
11+
namespace Util {
12+
class Root final {
13+
public:
14+
Root(const std::vector<std::shared_ptr<GameObject>> &children = {});
15+
16+
void AddChild(std::shared_ptr<GameObject> child);
17+
void AddChildren(const std::vector<std::shared_ptr<GameObject>> &children);
18+
19+
void Update();
20+
21+
private:
22+
std::vector<std::shared_ptr<GameObject>> m_Children;
23+
};
24+
} // namespace Util
25+
26+
#endif

src/Util/GameObject.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace Util {
44

55
void GameObject::Draw() {
6+
if (!m_Visible) {
7+
return;
8+
}
9+
610
m_Drawable->Draw(m_Transform, m_ZIndex);
711
}
812

src/Util/Root.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "Util/Root.hpp"
2+
3+
#include "Util/Logger.hpp"
4+
5+
namespace Util {
6+
Root::Root(const std::vector<std::shared_ptr<GameObject>> &children)
7+
: m_Children(children) {}
8+
9+
void Root::AddChild(std::shared_ptr<GameObject> child) {
10+
m_Children.push_back(child);
11+
}
12+
13+
void Root::AddChildren(
14+
const std::vector<std::shared_ptr<GameObject>> &children) {
15+
m_Children.reserve(m_Children.size() + children.size());
16+
m_Children.insert(m_Children.end(), children.begin(), children.end());
17+
}
18+
19+
void Root::Update() {
20+
struct StackInfo {
21+
std::shared_ptr<GameObject> m_GameObject;
22+
Transform m_ParentTransform;
23+
};
24+
25+
std::vector<StackInfo> stack;
26+
stack.reserve(m_Children.size());
27+
28+
for (const auto &child : m_Children) {
29+
stack.push_back(StackInfo{child, Transform{}});
30+
}
31+
32+
while (!stack.empty()) {
33+
auto curr = stack.back();
34+
stack.pop_back();
35+
36+
curr.m_GameObject->Update(curr.m_ParentTransform);
37+
curr.m_GameObject->Draw();
38+
39+
for (const auto &child : curr.m_GameObject->GetChildren()) {
40+
stack.push_back(
41+
StackInfo{child, curr.m_GameObject->GetTransform()});
42+
}
43+
}
44+
}
45+
} // namespace Util

0 commit comments

Comments
 (0)