Skip to content

Commit

Permalink
centre sprites on GameObject's position
Browse files Browse the repository at this point in the history
  • Loading branch information
RiverHillbug committed Jun 5, 2024
1 parent fed403a commit e2f9115
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion FluffyEngine/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "GameObject.h"
#include "ResourceManager.h"
#include "Texture2D.h"
#include "Rectf.h"

namespace Fluffy
{
Expand All @@ -13,12 +14,27 @@ namespace Fluffy
m_pTexture = ResourceManager::GetInstance().LoadTexture(fileName);
}

Sprite::Sprite(class GameObject* pOwner, const std::string& fileName, const glm::vec2& offset)
: Sprite(pOwner, fileName)
{
m_Offset = offset;
}

Rectf Sprite::GetRect() const
{
return Rectf
{
m_pOwner->GetWorldPosition() - (m_pTexture->GetSize() * 0.5f) + m_Offset,
m_pTexture->GetSize()
};
}

void Sprite::Render() const
{
if (m_pTexture == nullptr)
return;

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

Expand Down
4 changes: 4 additions & 0 deletions FluffyEngine/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Component.h"
#include "IRenderable.h"
#include "Texture2D.h"
#include "Rectf.h"

namespace Fluffy
{
Expand All @@ -13,8 +14,10 @@ namespace Fluffy
void Render() const override;
void SetTexture(const std::string& fileName);
inline glm::vec2 GetTextureSize() const { return m_pTexture->GetSize(); }
Rectf GetRect() const;

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

private:
std::shared_ptr<class Texture2D> m_pTexture{};
glm::vec2 m_Offset{ 0.0f, 0.0f };
};
}
2 changes: 1 addition & 1 deletion FluffyEngine/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Texture2D::~Texture2D()
SDL_DestroyTexture(m_pTexture);
}

glm::ivec2 Texture2D::GetSize() const
glm::vec2 Texture2D::GetSize() const
{
SDL_Rect dst;
SDL_QueryTexture(GetSDLTexture(), nullptr, nullptr, &dst.w, &dst.h);
Expand Down
2 changes: 1 addition & 1 deletion FluffyEngine/Texture2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Texture2D final
explicit Texture2D(SDL_Texture* texture);
~Texture2D();

glm::ivec2 GetSize() const;
glm::vec2 GetSize() const;

Texture2D(const Texture2D&) = delete;
Texture2D(Texture2D&&) = delete;
Expand Down

0 comments on commit e2f9115

Please sign in to comment.