Skip to content

Commit cd17945

Browse files
authored
Merge pull request #85 from nonorze-VOXX/imageSize
Changes - Add `virtual glm::vec2 Drawable::GetSize()` - Add`glm::vec2 GameObject::GetScaledSize()` - Remove `s_UniformBuffer->SetData()` in `InitUniformBuffer()` of `Text` and `Image` - Remove `Util::TransformToMat4()` (put into `ConvertToUniformBufferData()`) - Fix `Text` font squashed by fixed size Reviewed-by: noobdy <htommyf12@gmail.com> Reviewed-by: DF <dragonflybaby2.0@gmail.com> Reviewed-by: Rick <t110590016@ntut.org.tw> Tested-by: noobdy <htommyf12@gmail.com> Tested-by: DF <dragonflybaby2.0@gmail.com>
2 parents f74285d + 0b064b1 commit cd17945

File tree

8 files changed

+38
-46
lines changed

8 files changed

+38
-46
lines changed

include/Core/Drawable.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Drawable {
2222
public:
2323
virtual ~Drawable() = default;
2424
virtual void Draw(const Util::Transform &transform, const float zIndex) = 0;
25+
virtual glm::vec2 GetSize() const = 0;
2526
};
2627
} // namespace Core
2728

include/Util/GameObject.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class GameObject {
3030
virtual ~GameObject() = default;
3131

3232
float GetZIndex() const { return m_ZIndex; }
33+
34+
glm::vec2 GetScaledSize() const {
35+
return m_Drawable->GetSize() * m_Transform.scale;
36+
};
37+
3338
Transform GetTransform() const { return m_Transform; }
3439
const std::vector<std::shared_ptr<GameObject>> &GetChildren() const {
3540
return m_Children;

include/Util/Image.hpp

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

66
#include <functional>
7+
#include <glm/fwd.hpp>
78

89
#include "Core/Drawable.hpp"
910
#include "Core/Texture.hpp"
@@ -16,13 +17,14 @@ class Image : public Core::Drawable {
1617
public:
1718
Image(const std::string &filepath);
1819

20+
glm::vec2 GetSize() const override { return m_Size; };
21+
1922
void Draw(const Util::Transform &transform, const float zIndex) override;
2023

2124
private:
2225
void InitProgram();
2326
void InitVertexArray();
24-
void
25-
InitUniformBuffer(const Util::Transform &transform = Util::Transform(), const float zIndex = -1);
27+
void InitUniformBuffer();
2628

2729
static constexpr int UNIFORM_SURFACE_LOCATION = 0;
2830

@@ -34,6 +36,7 @@ class Image : public Core::Drawable {
3436
std::unique_ptr<Core::Texture> m_Texture = nullptr;
3537
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>> m_Surface =
3638
nullptr;
39+
glm::vec2 m_Size = {0, 0};
3740
};
3841
} // namespace Util
3942

include/Util/Text.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ class Text : public Core::Drawable {
1616
public:
1717
Text(const std::string &font, int size, const std::string &text);
1818

19+
glm::vec2 GetSize() const override { return m_Size; };
20+
1921
void Draw(const Transform &transform, const float zIndex) override;
2022

2123
private:
2224
void InitProgram();
2325
void InitVertexArray();
24-
void InitUniformBuffer(const Util::Transform & transform = Util::Transform(), const float zIndex = -1);
26+
void InitUniformBuffer();
2527

2628
static constexpr int UNIFORM_SURFACE_LOCATION = 0;
2729

@@ -35,6 +37,7 @@ class Text : public Core::Drawable {
3537
nullptr;
3638

3739
std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> m_Font;
40+
glm::vec2 m_Size = {0, 0};
3841
};
3942
} // namespace Util
4043

include/Util/TransformUtils.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
#include "pch.hpp"
88

99
namespace Util {
10-
11-
glm::mat4 TransformToMat4(const Transform &transform, float zIndex);
12-
1310
Core::Matrices ConvertToUniformBufferData(const Util::Transform &transform,
14-
float zIndex);
15-
11+
const glm::vec2 &size, float zIndex);
1612
} // namespace Util
1713

1814
#endif // UTIL_TRANSFORM_UTILS_HPP

src/Util/Image.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ Image::Image(const std::string &filepath) {
2929
m_Texture = std::make_unique<Core::Texture>(
3030
m_Surface->format->BytesPerPixel, m_Surface->w, m_Surface->h,
3131
m_Surface->pixels);
32+
m_Size = {m_Surface->w, m_Surface->h};
3233
}
3334

3435
void Image::Draw(const Util::Transform &transform, const float zIndex) {
35-
auto data = Util::ConvertToUniformBufferData(transform, zIndex);
36+
auto data = Util::ConvertToUniformBufferData(transform, m_Size, zIndex);
3637
s_UniformBuffer->SetData(0, data);
3738

3839
m_Texture->Bind(UNIFORM_SURFACE_LOCATION);
@@ -56,20 +57,17 @@ void Image::InitProgram() {
5657
void Image::InitVertexArray() {
5758
s_VertexArray = std::make_unique<Core::VertexArray>();
5859

59-
// hard coded value
60-
constexpr float scale = 100.0F;
61-
6260
// NOLINTBEGIN
6361
// These are vertex data for the rectangle but clang-tidy has magic
6462
// number warnings
6563

6664
// Vertex
6765
s_VertexArray->AddVertexBuffer(std::make_unique<Core::VertexBuffer>(
6866
std::vector<float>{
69-
-1.0F * scale, 1.0F * scale, //
70-
-1.0F * scale, -1.0F * scale, //
71-
1.0F * scale, -1.0F * scale, //
72-
1.0F * scale, 1.0F * scale, //
67+
-0.5F, 0.5F, //
68+
-0.5F, -0.5F, //
69+
0.5F, -0.5F, //
70+
0.5F, 0.5F, //
7371
},
7472
2));
7573

@@ -92,13 +90,9 @@ void Image::InitVertexArray() {
9290
// NOLINTEND
9391
}
9492

95-
void Image::InitUniformBuffer(const Util::Transform &transform,
96-
const float zIndex) { // YESLINT
93+
void Image::InitUniformBuffer() {
9794
s_UniformBuffer = std::make_unique<Core::UniformBuffer<Core::Matrices>>(
9895
*s_Program, "Matrices", 0);
99-
100-
auto data = Util::ConvertToUniformBufferData(transform, zIndex);
101-
s_UniformBuffer->SetData(0, data);
10296
}
10397

10498
std::unique_ptr<Core::Program> Image::s_Program = nullptr;

src/Util/Text.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ Text::Text(const std::string &font, int size, const std::string &text) {
3939
m_Surface->format->BytesPerPixel,
4040
m_Surface->pitch / m_Surface->format->BytesPerPixel, m_Surface->h,
4141
m_Surface->pixels);
42+
m_Size = {m_Surface->pitch / m_Surface->format->BytesPerPixel,
43+
m_Surface->h};
4244
}
4345

4446
void Text::Draw(const Util::Transform &transform, const float zIndex) {
45-
// FIXME: temporary fix
46-
auto data = Util::ConvertToUniformBufferData(transform, zIndex);
47+
auto data = Util::ConvertToUniformBufferData(transform, m_Size, zIndex);
4748
s_UniformBuffer->SetData(0, data);
4849

4950
m_Texture->Bind(UNIFORM_SURFACE_LOCATION);
@@ -67,20 +68,17 @@ void Text::InitProgram() {
6768
void Text::InitVertexArray() {
6869
s_VertexArray = std::make_unique<Core::VertexArray>();
6970

70-
// hard coded value
71-
constexpr float scale = 100.0F;
72-
7371
// NOLINTBEGIN
7472
// These are vertex data for the rectangle but clang-tidy has magic
7573
// number warnings
7674

7775
// Vertex
7876
s_VertexArray->AddVertexBuffer(std::make_unique<Core::VertexBuffer>(
7977
std::vector<float>{
80-
-1.0F * scale, 1.0F * scale, //
81-
-1.0F * scale, -1.0F * scale, //
82-
1.0F * scale, -1.0F * scale, //
83-
1.0F * scale, 1.0F * scale, //
78+
-0.5F, 0.5F, //
79+
-0.5F, -0.5F, //
80+
0.5F, -0.5F, //
81+
0.5F, 0.5F, //
8482
},
8583
2));
8684

@@ -103,13 +101,9 @@ void Text::InitVertexArray() {
103101
// NOLINTEND
104102
}
105103

106-
void Text::InitUniformBuffer(const Util::Transform &transform,
107-
const float zIndex) {
104+
void Text::InitUniformBuffer() {
108105
s_UniformBuffer = std::make_unique<Core::UniformBuffer<Core::Matrices>>(
109106
*s_Program, "Matrices", 0);
110-
111-
auto data = Util::ConvertToUniformBufferData(transform, zIndex);
112-
s_UniformBuffer->SetData(0, data);
113107
}
114108

115109
std::unique_ptr<Core::Program> Text::s_Program = nullptr;

src/Util/TransformUtils.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,8 @@
33
#include "config.hpp"
44

55
namespace Util {
6-
7-
glm::mat4 TransformToMat4(const Transform &transform, const float zIndex) {
8-
constexpr glm::mat4 eye(1.F);
9-
10-
// TODO: TRS comment
11-
return glm::translate(eye, {transform.translation, zIndex}) *
12-
glm::rotate(eye, transform.rotation, glm::vec3(0, 0, 1)) *
13-
glm::scale(eye, {transform.scale, 1});
14-
}
15-
166
Core::Matrices ConvertToUniformBufferData(const Util::Transform &transform,
7+
const glm::vec2 &size,
178
const float zIndex) {
189
constexpr glm::mat4 eye(1.F);
1910

@@ -26,8 +17,13 @@ Core::Matrices ConvertToUniformBufferData(const Util::Transform &transform,
2617
glm::scale(eye, {1.F / WINDOW_WIDTH, 1.F / WINDOW_HEIGHT, 1.F}) *
2718
glm::translate(eye, {WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, 0});
2819

20+
// TODO: TRS comment
21+
auto model = glm::translate(eye, {transform.translation, zIndex}) *
22+
glm::rotate(eye, transform.rotation, glm::vec3(0, 0, 1)) *
23+
glm::scale(eye, {transform.scale * size, 1});
24+
2925
Core::Matrices data = {
30-
Util::TransformToMat4(transform, zIndex),
26+
model,
3127
projection * view,
3228
};
3329

0 commit comments

Comments
 (0)