Skip to content

Commit e2f9115

Browse files
committed
centre sprites on GameObject's position
1 parent fed403a commit e2f9115

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

FluffyEngine/Sprite.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "GameObject.h"
44
#include "ResourceManager.h"
55
#include "Texture2D.h"
6+
#include "Rectf.h"
67

78
namespace Fluffy
89
{
@@ -13,12 +14,27 @@ namespace Fluffy
1314
m_pTexture = ResourceManager::GetInstance().LoadTexture(fileName);
1415
}
1516

17+
Sprite::Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset)
18+
: Sprite(pOwner, fileName)
19+
{
20+
m_Offset = offset;
21+
}
22+
23+
Rectf Sprite::GetRect() const
24+
{
25+
return Rectf
26+
{
27+
m_pOwner->GetWorldPosition() - (m_pTexture->GetSize() * 0.5f) + m_Offset,
28+
m_pTexture->GetSize()
29+
};
30+
}
31+
1632
void Sprite::Render() const
1733
{
1834
if (m_pTexture == nullptr)
1935
return;
2036

21-
const auto& pos = m_pOwner->GetWorldPosition();
37+
const auto& pos = m_pOwner->GetWorldPosition() - (m_pTexture->GetSize() * 0.5f) + m_Offset;
2238
Renderer::GetInstance().RenderTexture(*m_pTexture, pos.x, pos.y);
2339
}
2440

FluffyEngine/Sprite.h

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Component.h"
55
#include "IRenderable.h"
66
#include "Texture2D.h"
7+
#include "Rectf.h"
78

89
namespace Fluffy
910
{
@@ -13,8 +14,10 @@ namespace Fluffy
1314
void Render() const override;
1415
void SetTexture(const std::string& fileName);
1516
inline glm::vec2 GetTextureSize() const { return m_pTexture->GetSize(); }
17+
Rectf GetRect() const;
1618

1719
Sprite(class GameObject* pOwner, const std::string& fileName);
20+
Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset);
1821
virtual ~Sprite() = default;
1922
Sprite(const Sprite& other) = delete;
2023
Sprite(Sprite&& other) = delete;
@@ -23,5 +26,6 @@ namespace Fluffy
2326

2427
private:
2528
std::shared_ptr<class Texture2D> m_pTexture{};
29+
glm::vec2 m_Offset{ 0.0f, 0.0f };
2630
};
2731
}

FluffyEngine/Texture2D.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Texture2D::~Texture2D()
77
SDL_DestroyTexture(m_pTexture);
88
}
99

10-
glm::ivec2 Texture2D::GetSize() const
10+
glm::vec2 Texture2D::GetSize() const
1111
{
1212
SDL_Rect dst;
1313
SDL_QueryTexture(GetSDLTexture(), nullptr, nullptr, &dst.w, &dst.h);

FluffyEngine/Texture2D.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Texture2D final
1313
explicit Texture2D(SDL_Texture* texture);
1414
~Texture2D();
1515

16-
glm::ivec2 GetSize() const;
16+
glm::vec2 GetSize() const;
1717

1818
Texture2D(const Texture2D&) = delete;
1919
Texture2D(Texture2D&&) = delete;

0 commit comments

Comments
 (0)