Skip to content

Commit f74285d

Browse files
Merge pull request #84 from NOOBDY/root
Add auto tree rendering root
2 parents 4674a7e + a0a606d commit f74285d

File tree

10 files changed

+103
-109
lines changed

10 files changed

+103
-109
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ 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
47-
${SRC_DIR}/Triangle.cpp
4848
${SRC_DIR}/Giraffe.cpp
4949
)
5050
set(INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
@@ -72,10 +72,10 @@ set(INCLUDE_FILES
7272
${INCLUDE_DIR}/Util/Text.hpp
7373
${INCLUDE_DIR}/Util/Transform.hpp
7474
${INCLUDE_DIR}/Util/TransformUtils.hpp
75-
${INCLUDE_DIR}Util/GameObject.hpp
75+
${INCLUDE_DIR}/Util/GameObject.hpp
76+
${INCLUDE_DIR}/Util/Root.hpp
7677

7778
${INCLUDE_DIR}/App.hpp
78-
${INCLUDE_DIR}/Triangle.hpp
7979
${INCLUDE_DIR}/Giraffe.hpp
8080
${INCLUDE_DIR}/config.hpp
8181
)

include/App.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "pch.hpp" // IWYU pragma: export
55

66
#include "Giraffe.hpp"
7-
#include "Triangle.hpp"
87

98
class App {
109
public:
@@ -23,8 +22,7 @@ class App {
2322
private:
2423
State m_CurrentState = State::START;
2524

26-
Triangle m_Triangle;
27-
std::shared_ptr<Giraffe> m_Giraffe= std::make_shared<Giraffe>();
25+
std::shared_ptr<Giraffe> m_Giraffe = std::make_shared<Giraffe>();
2826
};
2927

3028
#endif

include/Triangle.hpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

include/Util/GameObject.hpp

Lines changed: 9 additions & 5 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,18 +30,19 @@ 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

42-
void AppendChild(std::shared_ptr<GameObject> child) {
43+
void SetVisible(bool visible) { m_Visible = visible; }
44+
45+
void AddChild(std::shared_ptr<GameObject> child) {
4346
m_Children.push_back(std::move(child));
4447
}
4548

@@ -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/App.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void App::Start() {
1717
"Giraffe");
1818
gf->SetZIndex(m_Giraffe->GetZIndex() - 1);
1919
gf->Start();
20-
m_Giraffe->AppendChild(gf);
20+
m_Giraffe->AddChild(gf);
2121

2222
m_CurrentState = State::UPDATE;
2323
}

src/Giraffe.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
#include "Giraffe.hpp"
2+
3+
#include <cmath>
4+
25
#include "Util/GameObject.hpp"
36
#include "Util/Image.hpp"
7+
#include "Util/Logger.hpp"
48
#include "Util/Text.hpp"
59
#include "Util/Time.hpp"
610
#include "Util/Transform.hpp"
11+
712
#include "config.hpp"
813

914
void GiraffeText::Update(const Util::Transform &transform) {
1015
auto &pos = m_Transform.translation;
1116
auto &scale = m_Transform.scale;
1217
auto &rotation = m_Transform.rotation;
1318

14-
pos += transform.translation;
15-
rotation += transform.rotation;
19+
pos = transform.translation;
20+
// rotation = std::fmod(rotation + 50.0F, 360.0F);
1621
scale = transform.scale;
1722

23+
LOG_DEBUG("{} {}", scale.x, scale.y);
24+
1825
m_Drawable->Draw(m_Transform, m_ZIndex);
1926
}
2027

@@ -31,10 +38,12 @@ void Giraffe::Update([[maybe_unused]] const Util::Transform &transform) {
3138
auto &scale = m_Transform.scale;
3239
auto &rotation = m_Transform.rotation;
3340

34-
if (pos.y > WINDOW_HEIGHT || pos.y + WINDOW_HEIGHT < 0) {
41+
if (pos.y >= static_cast<float>(WINDOW_HEIGHT) / 2 ||
42+
pos.y + static_cast<float>(WINDOW_HEIGHT) / 2 <= 0) {
3543
dir.y *= -1;
3644
}
37-
if (pos.x > WINDOW_WIDTH || pos.x + WINDOW_WIDTH < 0) {
45+
if (pos.x >= static_cast<float>(WINDOW_WIDTH) / 2 ||
46+
pos.x + static_cast<float>(WINDOW_WIDTH) / 2 <= 0) {
3847
dir.x *= -1;
3948
}
4049

@@ -49,7 +58,7 @@ void Giraffe::Update([[maybe_unused]] const Util::Transform &transform) {
4958

5059
m_Drawable->Draw(m_Transform, m_ZIndex);
5160
for (auto &child : m_Children) {
52-
child->Update(deltaTransform);
61+
child->Update(m_Transform);
5362
}
5463

5564
// LOG_DEBUG("GIRA: x: {}, y: {}", pos.x, pos.y);

src/Triangle.cpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

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)