Skip to content

Commit fc3c953

Browse files
authored
Merge pull request #113 from wj461/pivot
Add a pivot for GameObject
2 parents c3642c5 + 117f19f commit fc3c953

File tree

10 files changed

+39
-22
lines changed

10 files changed

+39
-22
lines changed

include/Core/Drawable.hpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33

44
#include "pch.hpp" // IWYU pragma: export
55

6-
#include <functional>
7-
8-
#include "Core/Program.hpp"
9-
#include "Core/UniformBuffer.hpp"
10-
#include "Core/VertexArray.hpp"
11-
12-
#include "Texture.hpp"
136
#include "Util/Transform.hpp"
147

158
namespace Core {
@@ -21,7 +14,7 @@ struct Matrices {
2114
class Drawable {
2215
public:
2316
virtual ~Drawable() = default;
24-
virtual void Draw(const Util::Transform &transform, const float zIndex) = 0;
17+
virtual void Draw(const Core::Matrices &data) = 0;
2518
virtual glm::vec2 GetSize() const = 0;
2619
};
2720
} // namespace Core

include/Util/Animation.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Animation : public Core::Drawable {
110110
* @param transform Transformation matrix for drawing.
111111
* @param zIndex Z-index for drawing.
112112
*/
113-
void Draw(const Util::Transform &transform, const float zIndex) override;
113+
void Draw(const Core::Matrices &data) override;
114114

115115
/**
116116
* @brief Start playing the animation.

include/Util/GameObject.hpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#ifndef UTIL_GAME_OBJECT_HPP
22
#define UTIL_GAME_OBJECT_HPP
33

4-
#include <memory>
5-
#include <vector>
4+
#include "pch.hpp" // IWYU pragma: export
65

76
#include "Core/Drawable.hpp"
7+
88
#include "Util/Transform.hpp"
99

1010
namespace Util {
@@ -36,13 +36,15 @@ class GameObject {
3636
* @param children The children of the game object.
3737
*/
3838
GameObject(const std::shared_ptr<Core::Drawable> &drawable,
39-
const float zIndex, const bool visible = true,
39+
const float zIndex, const glm::vec2 &pivot = {0, 0},
40+
const bool visible = true,
4041
const std::vector<std::shared_ptr<GameObject>> &children =
4142
std::vector<std::shared_ptr<GameObject>>())
4243
: m_Drawable(drawable),
4344
m_Children(children),
4445
m_ZIndex(zIndex),
45-
m_Visible(visible) {}
46+
m_Visible(visible),
47+
m_Pivot(pivot) {}
4648

4749
/**
4850
* @brief Copy constructor.
@@ -98,6 +100,13 @@ class GameObject {
98100
return m_Children;
99101
}
100102

103+
/**
104+
* @brief Set the pivot of the game object.
105+
*
106+
* @param pivot The pivot of the game object.
107+
*/
108+
void SetPivot(const glm::vec2 &pivot) { m_Pivot = pivot; }
109+
101110
/**
102111
* @brief Set the z-index of the game object.
103112
* z-index is used to determine the order in which game objects are drawn,
@@ -151,6 +160,7 @@ class GameObject {
151160

152161
float m_ZIndex = 0;
153162
bool m_Visible = true;
163+
glm::vec2 m_Pivot;
154164
};
155165
} // namespace Util
156166
#endif

include/Util/Image.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#include <glm/fwd.hpp>
77

88
#include "Core/Drawable.hpp"
9+
#include "Core/Program.hpp"
910
#include "Core/Texture.hpp"
11+
#include "Core/UniformBuffer.hpp"
12+
#include "Core/VertexArray.hpp"
1013

1114
#include "Util/AssetStore.hpp"
1215
#include "Util/Transform.hpp"
@@ -56,7 +59,7 @@ class Image : public Core::Drawable {
5659
* @param transform The transform to apply to the image.
5760
* @param zIndex The z-index at which to draw the image.
5861
*/
59-
void Draw(const Util::Transform &transform, const float zIndex) override;
62+
void Draw(const Core::Matrices &data) override;
6063

6164
private:
6265
void InitProgram();

include/Util/Text.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
#include <functional>
77

88
#include "Core/Drawable.hpp"
9+
#include "Core/Program.hpp"
910
#include "Core/Texture.hpp"
11+
#include "Core/UniformBuffer.hpp"
12+
#include "Core/VertexArray.hpp"
1013

1114
#include "Util/Color.hpp"
1215
#include "Util/Transform.hpp"
@@ -53,7 +56,7 @@ class Text : public Core::Drawable {
5356
* @param transform The transform to apply to the text.
5457
* @param zIndex The z-index at which to draw the text.
5558
*/
56-
void Draw(const Transform &transform, const float zIndex) override;
59+
void Draw(const Core::Matrices &data) override;
5760

5861
private:
5962
void InitProgram();

src/Util/Animation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ void Animation::SetCurrentFrame(std::size_t index) {
2424
}
2525
}
2626

27-
void Animation::Draw(const Util::Transform &transform, const float zIndex) {
28-
m_Frames[m_Index]->Draw(transform, zIndex);
27+
void Animation::Draw(const Core::Matrices &data) {
28+
m_Frames[m_Index]->Draw(data);
2929
Update();
3030
}
3131

src/Util/GameObject.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "Util/GameObject.hpp"
2+
#include "Util/Transform.hpp"
3+
#include "Util/TransformUtils.hpp"
24

35
namespace Util {
46

@@ -7,7 +9,12 @@ void GameObject::Draw() {
79
return;
810
}
911

10-
m_Drawable->Draw(m_Transform, m_ZIndex);
12+
auto data = Util::ConvertToUniformBufferData(
13+
m_Transform, m_Drawable->GetSize(), m_ZIndex);
14+
data.m_Model = glm::translate(
15+
data.m_Model, glm::vec3{m_Pivot / m_Drawable->GetSize(), 0} * -1.0F);
16+
17+
m_Drawable->Draw(data);
1118
}
1219

1320
} // namespace Util

src/Util/Image.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Util/Image.hpp"
22

3+
#include "Util/Logger.hpp"
34
#include "pch.hpp"
45

56
#include "Core/Texture.hpp"
@@ -9,6 +10,7 @@
910
#include "Util/TransformUtils.hpp"
1011

1112
#include "config.hpp"
13+
#include <glm/fwd.hpp>
1214

1315
std::shared_ptr<SDL_Surface> LoadSurface(const std::string &filepath) {
1416
auto surface = std::shared_ptr<SDL_Surface>(IMG_Load(filepath.c_str()),
@@ -52,8 +54,7 @@ void Image::SetImage(const std::string &filepath) {
5254
m_Size = {surface->w, surface->h};
5355
}
5456

55-
void Image::Draw(const Util::Transform &transform, const float zIndex) {
56-
auto data = Util::ConvertToUniformBufferData(transform, m_Size, zIndex);
57+
void Image::Draw(const Core::Matrices &data) {
5758
s_UniformBuffer->SetData(0, data);
5859

5960
m_Texture->Bind(UNIFORM_SURFACE_LOCATION);

src/Util/Text.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ Text::Text(const std::string &font, int fontSize, const std::string &text,
4444
m_Size = {surface->pitch / surface->format->BytesPerPixel, surface->h};
4545
}
4646

47-
void Text::Draw(const Util::Transform &transform, const float zIndex) {
48-
auto data = Util::ConvertToUniformBufferData(transform, m_Size, zIndex);
47+
void Text::Draw(const Core::Matrices &data) {
4948
s_UniformBuffer->SetData(0, data);
5049

5150
m_Texture->Bind(UNIFORM_SURFACE_LOCATION);

src/Util/TransformUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Util/TransformUtils.hpp"
22

33
#include "config.hpp"
4+
#include <glm/gtx/matrix_transform_2d.hpp>
45

56
namespace Util {
67
Core::Matrices ConvertToUniformBufferData(const Util::Transform &transform,

0 commit comments

Comments
 (0)